]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.runtime/scl/ArrayList.scl
Merge "List the unsatisfied dependencies in CanvasContext"
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / ArrayList.scl
1 import "Prelude" hiding (length, contains, iter, for)
2 import "Prelude" as Prelude
3 import "JavaBuiltin" as Java
4
5 importJava "java.util.ArrayList" where
6     "Type of lists."
7     data T a
8     
9     "Constructs a new list."
10     @JavaName "<init>"
11     new :: () -> <Proc> (T a)
12     
13     "Constructs a new list with initial capacity."
14     @JavaName "<init>"
15     newC :: Integer -> <Proc> (T a)
16     
17     "Adds an element to the list."
18     add :: T a -> a -> <Proc> ()
19     
20     addAll :: T a -> [a] -> <Proc> ()
21     
22     "Gets the i:th element of the list."
23     get :: T a -> Integer -> <Proc> a
24     
25     "Sets the i:th element of the list."
26     set :: T a -> Integer -> a -> <Proc> a
27     
28     "Removes the i:th element of the list"
29     remove :: T a -> Integer -> <Proc> a
30     
31     "The current length of the list."
32     @JavaName size
33     length :: T a -> <Proc> Integer
34     
35 contains :: T a -> a -> <Proc> Boolean
36 contains l a = loop 0
37   where
38     len = length l
39     loop i = if i >= len 
40              then False
41              else if get l i == a
42              then True
43              else loop (i+1)
44
45 "Converts the mutable list into immutable list. The original list must not be modified anymore."
46 freeze :: T a -> <Proc> [a]
47 freeze = Java.unsafeCoerce
48
49 "Iterates thru the list. The elements added during the iteration are also iterated."
50 iter :: (a -> <e> ()) -> T a -> <Proc,e> ()
51 iter f l = loop 0
52   where
53     loop i = if i >= length l 
54              then ()
55              else do f (get l i) ; loop (i+1)
56
57 "'iter' with swapped parameter."
58 for :: T a -> (a -> <e> ()) -> <Proc,e> ()
59 for l f = iter f l
60
61 "Replaces every element of the list by the result of applying the element to the given function."
62 mapInPlace :: (a -> <e> a) -> T a -> <Proc,e> T a
63 mapInPlace f l = loop 0
64   where
65     len = length l
66     loop i = if i >= len 
67              then l
68              else do set l i (f (get l i))
69                      loop (i+1)
70
71 "Pops the last element of the list until list becomes empty."
72 popUntilEmpty :: T a -> (a -> <e> ()) -> <Proc,e> ()
73 popUntilEmpty l f = do
74     len = length l
75     if len == 0
76     then ()
77     else do f (remove l (len-1))
78             popUntilEmpty l f
79
80 fromList :: [a] -> <Proc> T a
81 fromList l = do
82     result = newC (Prelude.length l)
83     addAll result l
84     result