This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbitset.js
236 lines (199 loc) · 4.78 KB
/
bitset.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*******************************************************************************
* Copyright (c) 2013 Max Schaefer.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Max Schaefer - initial API and implementation
*******************************************************************************/
/**
* Implementation of sets of non-negative integers as bitsets.
*/
if(typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(function(require, exports) {
// Wegner's algorithm
function countBitsInWord(w) {
var cnt = 0;
while(w) {
++cnt;
w &= w-1;
}
return cnt;
}
function countBits(a) {
var cnt = 0;
a.forEach(function(w) {
cnt += countBitsInWord(w);
});
return cnt;
}
function size(a) {
if(typeof a === 'undefined')
return 0;
if(typeof a === 'number')
return 1;
return countBits(a);
}
/**
* Check whether set a contains number x.
*/
function contains(a, x) {
if(typeof a === 'undefined')
return false;
if(typeof a === 'number')
return a === x;
var word_off = x >> 5,
word_idx = x % 32;
if(word_off >= a.length)
return false;
return !!(a[word_off] & (1 << word_idx));
}
function createSingletonBitset(x) {
var word_off = x >> 5,
word_idx = x % 32;
var a = new Array(word_off+1);
a[word_off] = (1 << word_idx);
return a;
}
/**
* Add number x to set a, and return the possibly modified a.
*/
function add(a, x) {
if(typeof a === 'undefined')
return x;
if(typeof a === 'number')
a = createSingletonBitset(a);
var word_off = x >> 5,
word_idx = x % 32;
a[word_off] = (a[word_off] || 0) | (1 << word_idx);
return a;
}
/**
* Add all elements in set b to set a, returning the resulting set.
* While set a may be modified, set b never is.
*/
function addAll(a, b) {
if(typeof a === 'undefined')
return copy(b);
if(typeof b === 'undefined')
return a;
if(typeof b === 'number')
return add(a, b);
if(typeof a === 'number')
return add(copy(b), a);
// both a and b must be bitsets
for(var i=0;i<b.length;++i)
if(b[i])
a[i] = (a[i] || 0) | b[i];
return a;
}
function remove(a, x) {
if(typeof a === 'undefined')
return a;
if(typeof a === 'number')
return a === x ? void(0) : a;
var word_off = x >> 5,
word_idx = x % 32;
a[word_off] = (a[word_off] || 0) & ~(1 << word_idx);
return a;
}
function removeAll(a, b) {
if(typeof a === 'undefined' || typeof b === 'undefined')
return a;
if(typeof a === 'number')
return contains(b, a) ? void(0) : a;
if(typeof b === 'number')
return remove(a, b);
a.forEach(function(w, i) {
if(b[i])
a[i] = a[i] & ~b[i];
});
return a;
}
function copy(a) {
if(typeof a === 'undefined' || typeof a === 'number')
return a;
return a.slice(0);
}
function iter(a, cb) {
if(a) {
if(typeof a === 'number')
cb(a);
else
a.forEach(function(w, i) {
for(var j=0;j<32;++j)
if(w & (1 << j))
cb(32*i + j);
});
}
}
function map(a, f) {
if(a) {
if(typeof a === 'number')
return [f(a)];
else {
var res = [];
iter(a, function(x) {
res[res.length] = f(x);
})
return res;
}
} else {
return [];
}
}
function some(a, f) {
if(a) {
if(typeof a === 'number')
return f(a);
else
for(var i=0;i<a.length;++i)
if(a[i])
for(var j=0;j<32;++j)
if(a[i] & (1 << j))
if(f(32*i + j))
return true;
}
return false;
}
function all(a, f) {
if(a) {
if(typeof a === 'number')
return f(a);
else
for(var i=0;i<a.length;++i)
if(a[i])
for(var j=0;j<32;++j)
if(a[i] & (1 << j))
if(!f(32*i + j))
return false;
}
return true;
}
function fromArray(ary) {
var a;
ary.forEach(function(x) { a = add(a, x); });
return a;
}
function toArray(a) {
return map(a, function f(x) { return x; });
}
exports.copy = copy;
exports.size = size;
exports.contains = contains;
exports.add = add;
exports.addAll = addAll;
exports.remove = remove;
exports.removeAll = removeAll;
exports.iter = iter;
exports.map = map;
exports.some = some;
exports.all = all;
exports.fromArray = fromArray;
exports.toArray = toArray;
return exports;
});