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

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