Debian Codegraph @

lib/mlrisc-lib/MLRISC/ra/raBitmatrix.sml

raw
1 (*
2 * Bit Matrix routines
3 *)
4 structure RaBitmatrix : RA_BITMATRIX = struct
5 structure W = Word
6 structure A = Array
7 structure UA = Unsafe.Array
8
9 datatype bitMatrix =
10 BM of {table:hashTable,
11 elems:int ref,
12 edges:int}
13 and hashTable =
14 SMALL of word list Array.array ref * word
15 | LARGE of bucket Array.array ref * word
16 (* | BITMATRIX of Word8Array.array *)
17
18 and bucket = NIL | B of int * int * bucket
19
20 exception Nodes
21 fun hashFun(i, j, shift, size) =
22 let val i = W.fromInt i
23 val j = W.fromInt j
24 val h = W.+(W.<<(i, shift), W.+(i, j))
25 val mask = W.-(W.fromInt size, 0w1)
26 in W.toIntX(W.andb(h, mask)) end
27
28 val empty = BM{table=SMALL(ref(A.array(2, [])), 0w0), elems=ref 0, edges=0}
29
30 (*
31 val indices = A.array(1024,0)
32
33 fun init(i,j) =
34 if i < 1024 then
35 (A.update(indices, i, j); init(i+1, i+j+1))
36 else ()
37
38 val _ = init(0, 0)
39 *)
40 fun size (BM{elems, ...}) = !elems
41
42 fun edges(BM{table=SMALL(ref table, _), ...}) = A.length table
43 | edges(BM{table=LARGE(ref table, _), ...}) = A.length table
44 (*| edges(BM{table=BITMATRIX _, edges, ...}) = edges *)
45
46 fun member(BM{table=SMALL(table, shift), ...}) =
47 (fn (i, j) =>
48 let val (i,j) = if i < j then (i, j) else (j, i)
49 val k = W.+(W.<<(W.fromInt i, 0w15), W.fromInt j)
50 fun find [] = false
51 | find(k'::b) = k = k' orelse find b
52 val tab = !table
53 in find(UA.sub(tab, hashFun(i, j, shift, A.length tab))) end
54 )
55 | member(BM{table=LARGE(table, shift), ...}) =
56 (fn (i, j) =>
57 let val (i,j) = if i < j then (i, j) else (j, i)
58 fun find NIL = false
59 | find(B(i',j',b)) = i = i' andalso j = j' orelse find b
60 val tab = !table
61 in find(UA.sub(tab, hashFun(i, j, shift, A.length tab))) end
62 )
63 (*
64 | member(BM{table=BITMATRIX table, ...}) =
65 (fn (i, j) =>
66 let val (i,j) = if i > j then (i, j) else (j, i)
67 val bit = W.fromInt(UA.sub(indices, i) + j)
68 val index = W.toIntX(W.>>(bit, 0w3))
69 val mask = W.<<(0w1, W.andb(bit, 0w7))
70 in W.andb(W.fromInt(W8.toInt(UW8A.sub(table, index))), mask) <> 0w0
71 end
72 )
73 *)
74
75 fun add (BM{table=SMALL(table, shift), elems, ...}) =
76 let fun insert(i, j) =
77 let val (i,j) = if i < j then (i, j) else (j, i)
78 val tab = !table
79 val len = A.length tab
80 in if !elems < len then
81 let val index = hashFun(i, j, shift, len)
82 val k = W.+(W.<<(W.fromInt i, 0w15), W.fromInt j)
83 fun find [] = false
84 | find(k'::b) = k = k' orelse find b
85 val b = UA.sub(tab, index)
86 in if find b then false
87 else (UA.update(tab, index, k::b);
88 elems := !elems + 1; true)
89 end
90 else (* grow table *)
91 let val oldTable = tab
92 val oldSize = A.length oldTable
93 val newSize = oldSize + oldSize
94 val newTable = A.array(newSize,[])
95 fun enter n =
96 if n < oldSize then
97 let fun loop([],a,b) =
98 (UA.update(newTable, n, a);
99 UA.update(newTable, n + oldSize, b);
100 enter(n+1))
101 | loop(k::l,a,b) =
102 let val i = W.toIntX(W.>>(k, 0w15))
103 val j = W.toIntX(W.-(k,W.<<(W.fromInt i, 0w15)))
104 in if hashFun(i, j, shift, newSize) = n
105 then loop(l, k::a, b)
106 else loop(l, a, k::b)
107 end
108 in loop(UA.sub(oldTable, n), [], []) end
109 else ()
110 in table := newTable;
111 enter 0;
112 insert(i, j)
113 end
114 end
115 in insert
116 end
117 | add (BM{table=LARGE(table, shift), elems, ...}) =
118 let fun insert(i, j) =
119 let val (i,j) = if i < j then (i, j) else (j, i)
120 val tab = !table
121 val len = A.length tab
122 in if !elems < len then
123 let val index = hashFun(i, j, shift, len)
124 fun find NIL = false
125 | find(B(i',j',b)) = i = i' andalso j = j' orelse find b
126 val b = UA.sub(tab, index)
127 in if find b then false
128 else (UA.update(tab, index, B(i,j,b));
129 elems := !elems + 1; true)
130 end
131 else (* grow table *)
132 let val oldTable = tab
133 val oldSize = A.length oldTable
134 val newSize = oldSize + oldSize
135 val newTable = A.array(newSize,NIL)
136 fun enter n =
137 if n < oldSize then
138 let fun loop(NIL,a,b) =
139 (UA.update(newTable, n, a);
140 UA.update(newTable, n + oldSize, b);
141 enter(n+1))
142 | loop(B(i,j,l),a,b) =
143 if hashFun(i, j, shift, newSize) = n
144 then loop(l, B(i,j,a), b)
145 else loop(l, a, B(i,j,b))
146 in loop(UA.sub(oldTable, n), NIL, NIL) end
147 else ()
148 in table := newTable;
149 enter 0;
150 insert(i, j)
151 end
152 end
153 in insert
154 end
155 (*
156 | add(BM{table=BITMATRIX table, ...}) =
157 (fn (i, j) =>
158 let val (i,j) = if i > j then (i, j) else (j, i)
159 val bit = W.fromInt(UA.sub(indices, i) + j)
160 val index = W.toIntX(W.>>(bit, 0w3))
161 val mask = W.<<(0w1, W.andb(bit, 0w7))
162 val value = W.fromInt(W8.toInt(UW8A.sub(table, index)))
163 in if W.andb(value, mask) <> 0w0 then false
164 else (UW8A.update(table, index,
165 W8.fromInt(W.toIntX(W.orb(value, mask)))); true)
166 end
167 )
168 *)
169
170 fun delete (BM{table=SMALL(table, shift), elems, ...}) =
171 (fn (i,j) =>
172 let val k = W.+(W.<<(W.fromInt i, 0w15), W.fromInt j)
173 fun find [] = []
174 | find(k'::b) =
175 if k = k' then (elems := !elems - 1; b) else k'::find b
176 val tab = !table
177 val index = hashFun(i, j, shift, A.length tab)
178 val n = !elems
179 in UA.update(tab, index, find(UA.sub(tab, index)));
180 !elems <> n
181 end
182 )
183 | delete (BM{table=LARGE(table, shift), elems, ...}) =
184 (fn (i,j) =>
185 let fun find NIL = NIL
186 | find(B(i', j', b)) =
187 if i = i' andalso j = j' then (elems := !elems - 1; b)
188 else B(i', j', find b)
189 val tab = !table
190 val index = hashFun(i, j, shift, A.length tab)
191 val n = !elems
192 in UA.update(tab, index, find(UA.sub(tab, index)));
193 !elems <> n
194 end
195 )
196 (*
197 | delete(BM{table=BITMATRIX table, ...}) =
198 (fn (i, j) =>
199 let val (i,j) = if i > j then (i, j) else (j, i)
200 val bit = W.fromInt(UA.sub(indices, i) + j)
201 val index = W.toIntX(W.>>(bit, 0w3))
202 val mask = W.-(W.<<(0w1, W.andb(bit, 0w7)), 0w1)
203 val value = W.fromInt(W8.toInt(UW8A.sub(table, index)))
204 in if W.andb(value, mask) = 0w0 then false
205 else (UW8A.update(table, index,
206 W8.fromInt(W.toIntX(W.andb(value,W.notb mask))));
207 true)
208 end
209 )
210 *)
211 end