]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/MList.scl
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / MList.scl
diff --git a/bundles/org.simantics.scl.runtime/scl/MList.scl b/bundles/org.simantics.scl.runtime/scl/MList.scl
new file mode 100644 (file)
index 0000000..6cafe6b
--- /dev/null
@@ -0,0 +1,58 @@
+import "Prelude" as Prelude
+import "JavaBuiltin" as Java
+import "Iterator" as Iterator
+
+importJava "java.util.List" where
+    data T a
+    
+    contains :: T a -> a -> <Proc> Boolean
+    size :: T a -> <Proc> Integer
+    isEmpty :: T a -> <Proc> Boolean
+    get :: T a -> Integer -> <Proc> a
+    
+    add :: T a -> a -> <Proc> ()
+    addAll :: T a -> [a] -> <Proc> ()
+    remove :: T a -> Integer -> <Proc> a
+    clear :: T a -> <Proc> ()
+    
+    @private
+    iterator :: T a -> <Proc> Iterator.T a
+
+removeLast :: T a -> <Proc> Maybe a
+removeLast l = if isEmpty l
+               then Nothing
+               else Just (remove l (Java.isub (size l) 1))
+
+@inline
+iter :: (a -> <e> ()) -> 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)
+
+@inline
+fold :: (a -> b  -> <e> a) -> a -> T b -> <Proc,e> a
+fold f init s = Iterator.fold f init (iterator s)
+
+importJava "java.util.ArrayList" 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
+
+singleton :: a -> <Proc> T a
+singleton v = do
+    result = createC 2
+    add result v
+    result
+
+@inline
+freeze :: T a -> <Proc> [a]
+freeze = Java.unsafeCoerce
\ No newline at end of file