]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/MSet.scl
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / MSet.scl
diff --git a/bundles/org.simantics.scl.runtime/scl/MSet.scl b/bundles/org.simantics.scl.runtime/scl/MSet.scl
new file mode 100644 (file)
index 0000000..9f54310
--- /dev/null
@@ -0,0 +1,82 @@
+import "Prelude" as Prelude
+import "JavaBuiltin" as Java
+import "Iterator" as Iterator
+import "Set" as Set
+import "MList" as MList
+
+importJava "java.util.Set" where
+    data T a
+    
+    contains :: T a -> a -> <Proc> Boolean
+    size :: T a -> <Proc> Integer
+    isEmpty :: T a -> <Proc> Boolean
+    
+    add :: T a -> a -> <Proc> Boolean
+    addAll :: T a -> [a] -> <Proc> Boolean
+    @JavaName addAll
+    addAllS :: T a -> T a -> <Proc> Boolean
+    remove :: T a -> a -> <Proc> Boolean
+    removeAll :: T a -> [a] -> <Proc> Boolean
+    clear :: T a -> <Proc> ()
+    
+    @private
+    iterator :: T a -> <Proc> Iterator.T a
+
+@inline
+iter :: (a -> <e> b) -> T a -> <e,Proc> ()
+iter f s = Iterator.iter f (iterator s)
+
+@inline
+iterB :: (a -> <e> Boolean) -> T a -> <e,Proc> Boolean
+iterB f s = Iterator.iterB f (iterator s)
+
+map :: (a -> <e> b) -> T a -> <e,Proc> T b
+map f set = result
+  where
+    result = createC (size set)
+    iter (\x -> add result (f x)) set
+
+@inline
+mapFirst :: (a -> <e> Maybe b) -> T a -> <e,Proc> Maybe b
+mapFirst f s = Iterator.mapFirst f (iterator s)
+
+@inline
+fold :: (a -> b  -> <e> a) -> a -> T b -> <Proc,e> a
+fold f init s = Iterator.fold f init (iterator s)
+
+importJava "gnu.trove.set.hash.THashSet" where
+    @JavaName "<init>"
+    create :: () -> <Proc> T a
+    @JavaName "<init>"
+    createC :: Integer -> <Proc> T a
+
+fromList :: [a] -> <Proc> T a
+fromList l = do
+    result = createC (Prelude.length l)
+    Prelude.iter (\x -> Prelude.ignore (add result x)) l
+    result
+    
+toList :: T a -> <Proc> [a]
+toList s = do
+    result = MList.createC (size s)
+    iter (MList.add result) s
+    MList.freeze result
+
+singleton :: a -> <Proc> T a
+singleton v = do
+    result = createC 1
+    add result v
+    result
+
+@inline
+freeze :: T a -> <Proc> Set.T a
+freeze = Java.unsafeCoerce
+
+concatMap :: (a -> <e> T b) -> T a -> <Proc,e> T b
+concatMap f s = result
+  where
+    result = create ()
+    iter (\el -> addAllS result (f el)) s
+
+all :: (a -> <e> Boolean) -> T a -> <e,Proc> Boolean
+all f s = Iterator.iterB f (iterator s)