@@ -138,6 +138,80 @@ val size = Map.size
138138fun fromList list = List.foldl (fn (v, s) => insert v s) empty list
139139end
140140
141+ structure UncountedRList =
142+ struct
143+ datatype 'a Tree = Leaf of 'a | Node of 'a * 'a Tree * 'a Tree
144+ type 'a Coll = (int * 'a Tree) list
145+
146+ val empty = []
147+ fun isEmpty ts = null ts
148+
149+ fun cons (x, ts as (w1, t1) :: (w2, t2) :: ts') =
150+ if w1 = w2 then
151+ (1 + w1 + w2, Node (x, t1, t2)) :: ts'
152+ else
153+ (1 , Leaf x) :: ts
154+ | cons (x, ts) = (1 , Leaf x) :: ts
155+
156+ fun head ((1 , Leaf x) :: _) = x
157+ | head ((_, Node (x, _, _)) :: _) = x
158+ | head _ = raise Empty
159+
160+ fun tail ((1 , Leaf _) :: ts) = ts
161+ | tail ((w, Node (x, t1, t2)) :: ts) = (w div 2 , t1) :: (w div 2 , t2) :: ts
162+ | tail _ = raise Empty
163+
164+ fun getTree (1 , 0 , Leaf x) = x
165+ | getTree (w, i, Leaf x) = raise Subscript
166+ | getTree (w, 0 , Node (x, t1, t2)) = x
167+ | getTree (w, i, Node (x, t1, t2)) =
168+
169+ if i <= w div 2 then
170+ getTree (w div 2 , i - 1 , t1)
171+ else
172+ getTree (w div 2 , i - 1 - w div 2 , t2)
173+
174+ fun setTree (1 , 0 , y, Leaf x) = Leaf y
175+ | setTree (w, i, y, Leaf x) = raise Subscript
176+ | setTree (w, 0 , y, Node (x, t1, t2)) = Node (y, t1, t2)
177+ | setTree (w, i, y, Node (x, t1, t2)) =
178+ if i <= w div 2 then
179+ Node (x, setTree (w div 2 , i - 1 , y, t1), t2)
180+ else
181+ Node (x, t1, setTree (w div 2 , i - 1 - w div 2 , y, t2))
182+
183+ fun get (i, []) = raise Subscript
184+ | get (i, (w, t) :: ts) =
185+ if i < w then
186+ getTree (w, i, t)
187+ else
188+ get (i - w, ts)
189+
190+ fun set (i, y, []) = raise Subscript
191+ | set (i, y, (w, t) :: ts) =
192+ if i < w then
193+ (w, setTree (w, i, y, t)) :: ts
194+ else
195+ (w, t) :: set (i - w, y, ts)
196+ end
197+
198+ structure RList =
199+ struct
200+ type 'a Coll = (int * 'a UncountedRList.Coll)
201+
202+ val empty = (0 , UncountedRList.empty)
203+ fun isEmpty (0 , _) = true
204+ | isEmpty _ = false
205+ fun size (n, _) = n
206+
207+ fun cons (x, (n, s)) = (n+1 , UncountedRList.cons (x, s))
208+ fun head (_, s) = UncountedRList.head s
209+ fun tail (n, s) = (n - 1 , UncountedRList.tail s)
210+ fun get (i, (n, s)) = UncountedRList.get (i, s)
211+ fun set (i, y, (n, s)) = (n, UncountedRList.set (i, y, s))
212+ fun fromList list = List.foldr cons empty list
213+ end
214+
141215structure CharMap = Map(
142216 struct
143217 type t = char
@@ -153,3 +227,11 @@ structure StringMap = Map(
153227 end )
154228
155229structure StringSet = Set(StringMap)
230+
231+ structure IntMap = Map(
232+ struct
233+ type t = int
234+ val cmp = Int.compare
235+ end )
236+
237+ structure IntSet = Set(IntMap)
0 commit comments