Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/branches/FLINT/src/comp-lib/intmapf.sml
ViewVC logotype

Annotation of /sml/branches/FLINT/src/comp-lib/intmapf.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 207 - (view) (download)

1 : monnier 89 (* intmapf.sml
2 :     *
3 :     * COPYRIGHT (c) 1996 Bell Laboratories.
4 :     *
5 :     *)
6 :    
7 :     signature INTMAPF =
8 :     sig
9 :     type 'a intmap
10 :     val empty : 'a intmap
11 :     val singleton : int * 'a -> 'a intmap
12 :     val overlay : 'a intmap * 'a intmap -> 'a intmap
13 :     val add : 'a intmap * int * 'a -> 'a intmap
14 :     exception IntmapF
15 :     val lookup : 'a intmap -> int -> 'a
16 :     val members : 'a intmap -> (int * 'a) list
17 :     val cardinality : 'a intmap -> int
18 :     val difference : 'a intmap * 'b intmap -> 'a intmap
19 :     val delete : int * 'a intmap -> 'a intmap
20 : monnier 207 val fold : (int * 'a * 'b -> 'b) -> 'b -> 'a intmap -> 'b
21 : monnier 89 end
22 :    
23 :     (*
24 :     Copyright 1992 Stephen Adams.
25 :    
26 :     ALTERED FROM THE ORIGINAL by Andrew Appel
27 :    
28 :     This software may be used freely provided that:
29 :     1. This copyright notice is attached to any copy, derived work,
30 :     or work including all or part of this software.
31 :     2. Any derived work must contain a prominent notice stating that
32 :     it has been altered from the original.
33 :    
34 :     *)
35 :    
36 :     (* Name(s): Stephen Adams.
37 :     Department, Institution: Electronics & Computer Science,
38 :     University of Southampton
39 :     Address: Electronics & Computer Science
40 :     University of Southampton
41 :     Southampton SO9 5NH
42 :     Great Britian
43 :     E-mail: sra@ecs.soton.ac.uk
44 :    
45 :     Comments:
46 :    
47 :     1. The implementation is based on Binary search trees of Bounded
48 :     Balance, similar to Nievergelt & Reingold, SIAM J. Computing
49 :     2(1), March 1973. The main advantage of these trees is that
50 :     they keep the size of the tree in the node, giving a constant
51 :     time size operation.
52 :    
53 :     2. The bounded balance criterion is simpler than N&R's alpha.
54 :     Simply, one subtree must not have more than `weight' times as
55 :     many elements as the opposite subtree. Rebalancing is
56 :     guaranteed to reinstate the criterion for weight>2.23, but
57 :     the occasional incorrect behaviour for weight=2 is not
58 :     detrimental to performance.
59 :    
60 :     *)
61 :    
62 :     structure IntmapF :> INTMAPF =
63 :     struct
64 :    
65 :     local
66 :    
67 :     val weight = 3
68 :    
69 :     datatype 'a Map = E | T of int * 'a * int * 'a Map * 'a Map
70 :    
71 :     fun size E = 0
72 :     | size (T(_,_,n,_,_)) = n
73 :    
74 :     (*fun N(v,a,l,r) = T(v,a,1+size(l)+size(r),l,r)*)
75 :     fun N(v,a,E, E) = T(v,a,1,E,E)
76 :     | N(v,a,E, r as T(_,_,n,_,_)) = T(v,a,n+1,E,r)
77 :     | N(v,a,l as T(_,_,n,_,_),E) = T(v,a,n+1,l,E)
78 :     | N(v,a,l as T(_,_,n,_,_),r as T(_,_,m,_,_)) = T(v,a,n+m+1,l,r)
79 :    
80 :     fun single_L (a,a',x,T(b,b',_,y,z)) = N(b,b',N(a,a',x,y),z)
81 :     | single_L _ = raise Match
82 :     fun single_R (b,b',T(a,a',_,x,y),z) = N(a,a',x,N(b,b',y,z))
83 :     | single_R _ = raise Match
84 :     fun double_L (a,a',w,T(c,c',_,T(b,b',_,x,y),z)) = N(b,b',N(a,a',w,x),N(c,c',y,z))
85 :     | double_L _ = raise Match
86 :     fun double_R (c,c',T(a,a',_,w,T(b,b',_,x,y)),z) = N(b,b',N(a,a',w,x),N(c,c',y,z))
87 :     | double_R _ = raise Match
88 :    
89 :     fun T' (v,v',E,E) = T(v,v',1,E,E)
90 :     | T' (v,v',E,r as T(_,_,_,E,E)) = T(v,v',2,E,r)
91 :     | T' (v,v',l as T(_,_,_,E,E),E) = T(v,v',2,l,E)
92 :    
93 :     | T' (p as (_,_,E,T(_,_,_,T(_,_,_,_,_),E))) = double_L p
94 :     | T' (p as (_,_,T(_,_,_,E,T(_,_,_,_,_)),E)) = double_R p
95 :    
96 :     (* these cases almost never happen with small weight*)
97 :     | T' (p as (_,_,E,T(_,_,_,T(_,_,ln,_,_),T(_,_,rn,_,_)))) =
98 :     if ln<rn then single_L p else double_L p
99 :     | T' (p as (_,_,T(_,_,_,T(_,_,ln,_,_),T(_,_,rn,_,_)),E)) =
100 :     if ln>rn then single_R p else double_R p
101 :    
102 :     | T' (p as (_,_,E,T(_,_,_,E,_))) = single_L p
103 :     | T' (p as (_,_,T(_,_,_,_,E),E)) = single_R p
104 :    
105 :     | T' (p as (v,v',l as T(lv,lv',ln,ll,lr),r as T(rv,rv',rn,rl,rr))) =
106 :     if rn>=weight*ln then (*right is too big*)
107 :     let val rln = size rl
108 :     val rrn = size rr
109 :     in
110 :     if rln < rrn then single_L p else double_L p
111 :     end
112 :    
113 :     else if ln>=weight*rn then (*left is too big*)
114 :     let val lln = size ll
115 :     val lrn = size lr
116 :     in
117 :     if lrn < lln then single_R p else double_R p
118 :     end
119 :    
120 :     else
121 :     T(v,v',ln+rn+1,l,r)
122 :    
123 :     fun add (E,x,x') = T(x,x',1,E,E)
124 :     | add (T(v,v',w,l,r),x,x') =
125 :     if x<v then T'(v,v',add(l,x,x'),r)
126 :     else if x>v then T'(v,v',l,add(r,x,x'))
127 :     (* replace v,v' with x,x'! (blume/4/96) *)
128 :     else T(x,x',w,l,r)
129 :    
130 :     fun concat3 (E,v,v',r) = add(r,v,v')
131 :     | concat3 (l,v,v',E) = add(l,v,v')
132 :     | concat3 (l as T(v1,v1',n1,l1,r1), v, v', r as T(v2,v2',n2,l2,r2)) =
133 :     if weight*n1 < n2 then T'(v2,v2',concat3(l,v,v',l2),r2)
134 :     else if weight*n2 < n1 then T'(v1,v1',l1,concat3(r1,v,v',r))
135 :     else N(v,v',l,r)
136 :    
137 :     fun split_lt (E,x) = E
138 :     | split_lt (t as T(v,v',_,l,r),x) =
139 :     if v>x then split_lt(l,x)
140 :     else if v<x then concat3(l,v,v',split_lt(r,x))
141 :     else l
142 :    
143 :     fun split_gt (E,x) = E
144 :     | split_gt (t as T(v,v',_,l,r),x) =
145 :     if v<x then split_gt(r,x)
146 :     else if v>x then concat3(split_gt(l,x),v,v',r)
147 :     else r
148 :    
149 :     and delmin (T(v,v',_,E,r)) = (v,v',r)
150 :     | delmin (T(v,v',_,l,r)) = let val (x,x',l') = delmin l
151 :     in (x,x',T'(v,v',l',r))
152 :     end
153 :     | delmin _ = raise Match
154 :    
155 :     and cat2 (E,r) = r
156 :     | cat2 (l,E) = l
157 :     | cat2 (l,r) = let val (x,x',r') = delmin r
158 :     in T'(x,x',l,r')
159 :     end
160 :    
161 :     fun concat (E, s2) = s2
162 :     | concat (s1, E) = s1
163 :     | concat (t1 as T(v1,v1',n1,l1,r1), t2 as T(v2,v2',n2,l2,r2)) =
164 :     if weight*n1 < n2 then T'(v2,v2',concat(t1,l2),r2)
165 :     else if weight*n2 < n1 then T'(v1,v1',l1,concat(r1,t2))
166 :     else cat2(t1,t2)
167 :    
168 :     in
169 :    
170 :     type 'a intmap = 'a Map
171 :    
172 :     val empty = E
173 :    
174 :     fun singleton (x,x') = T(x,x',1,E,E)
175 :    
176 :     fun overlay (E,s2) = s2
177 :     | overlay (s1,E) = s1
178 :     | overlay (s1 as T(v,v',_,l,r),s2) =
179 :     let val l2 = split_lt(s2,v)
180 :     val r2 = split_gt(s2,v)
181 :     in
182 :     concat3(overlay(l,l2),v,v',overlay(r,r2))
183 :     end
184 :    
185 :     val add = add
186 :    
187 :     fun difference (E,s) = E
188 :     | difference (s,E) = s
189 :     | difference (s, T(v,_,_,l,r)) =
190 :     let val l2 = split_lt(s,v)
191 :     val r2 = split_gt(s,v)
192 :     in
193 :     concat(difference(l2,l),difference(r2,r))
194 :     end
195 :    
196 :     exception IntmapF
197 :    
198 :     fun lookup set x =
199 :     let fun mem E = raise IntmapF
200 :     | mem (T(v,v',_,l,r)) =
201 :     if x<v then mem l else if x>v then mem r else v'
202 :     in mem set end
203 :    
204 : monnier 207 fun fold f base map =
205 :     let fun fold' (base,E) = base
206 :     | fold' (base,T(v,v',_,l,r)) =
207 :     fold'(fold'(f(v, v', base), l), r)
208 :     in
209 :     fold'(base, map)
210 :     end
211 : monnier 89
212 : monnier 207 fun members m = fold (fn (i,v,res) => (i,v)::res) [] m
213 :    
214 : monnier 89 fun cardinality E = 0
215 :     | cardinality (T(_,_,n,_,_)) = n
216 :    
217 :     fun delete (x,E) = E
218 :     | delete (x,set as T(v,v',_,l,r)) =
219 :     if x<v then T'(v,v',delete(x,l),r)
220 :     else if x>v then T'(v,v',l,delete(x,r))
221 :     else cat2(l,r)
222 :    
223 :     end
224 :     end
225 :    
226 :     (*
227 : monnier 113 * $Log$
228 : monnier 89 *)

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0