Skip to content

Commit 380d4c3

Browse files
committed
Switch underlying set implementation to ES6
1 parent d083b1e commit 380d4c3

File tree

2 files changed

+172
-1
lines changed

2 files changed

+172
-1
lines changed

src/numset.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ if (typeof define !== 'function') {
1818
}
1919

2020
define(function (require, exports) {
21-
var impl = require('./olist');
21+
// var impl = require('./olist');
22+
var impl = require('./set');
2223

2324
for (var p in impl)
2425
exports[p] = impl[p];

src/set.js

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2013 Max Schaefer
3+
* Copyright (c) 2018 Persper Foundation
4+
*
5+
* All rights reserved. This program and the accompanying materials
6+
* are made available under the terms of the Eclipse Public License v2.0
7+
* which accompanies this distribution, and is available at
8+
* http://www.eclipse.org/legal/epl-2.0.
9+
*******************************************************************************/
10+
11+
/*
12+
* Keep the interface of ./olist.js, but use ES6 Set under the hood
13+
*/
14+
15+
function size(s) {
16+
if (typeof s === 'undefined')
17+
return 0;
18+
19+
if (typeof s === 'number')
20+
return 1;
21+
22+
return s.size;
23+
}
24+
25+
/* Check whether set s contains number n */
26+
function contains(s, n) {
27+
if (typeof s === 'undefined')
28+
return false;
29+
30+
if (typeof s === 'number')
31+
return s === n;
32+
33+
return s.has(n);
34+
}
35+
36+
/* Add number n to set s, and return the possibly modified s */
37+
function add(s, n) {
38+
if (typeof s === 'undefined')
39+
return n;
40+
41+
if (typeof s === 'number')
42+
return new Set([s, n]);
43+
44+
s.add(n);
45+
return s;
46+
}
47+
48+
/*
49+
* Add all elements in set s2 to set s1, return the resulting set
50+
* While set s1 may be modified, set s2 never is.
51+
*/
52+
function addAll(s1, s2) {
53+
if (typeof s1 === 'undefined')
54+
return copy(s2);
55+
56+
if (typeof s2 === 'undefined')
57+
return s1;
58+
59+
if (typeof s1 === 'number' && typeof s2 === 'number') {
60+
return new Set([s1, s2]);
61+
}
62+
else if (typeof s1 === 'number' && typeof s2 === 'object') {
63+
return new Set([s1, ...s2]);
64+
}
65+
else if (typeof s1 === 'object' && typeof s2 === 'number') {
66+
s1.add(s2);
67+
return s1;
68+
}
69+
else {
70+
for (let n of s2)
71+
s1.add(n);
72+
return s1
73+
}
74+
}
75+
76+
/*
77+
* Remove number n from set s and return the modified set
78+
* Pitfall: this remove is inplace for array, not inplace for number
79+
*/
80+
function remove(s, n) {
81+
if (typeof s === 'undefined')
82+
return s;
83+
84+
if (typeof s === 'number')
85+
return s === n ? void(0) : s;
86+
87+
s.delete(n)
88+
return s;
89+
}
90+
91+
/*
92+
* Remove all elements in set s2 from set s1, return the resulting set
93+
*/
94+
function removeAll(s1, s2) {
95+
if (typeof s1 === 'undefined' || typeof s2 === 'undefined')
96+
return s1;
97+
98+
if (typeof s1 === 'number')
99+
return contains(s2, s1) ? void(0) : s1;
100+
101+
if (typeof s2 === 'number') {
102+
s1.delete(s2);
103+
return s1;
104+
}
105+
106+
for (let n of s2)
107+
s1.delete(n);
108+
109+
if (s1.size === 0)
110+
return void(0);
111+
else
112+
return s1;
113+
}
114+
115+
function copy(s) {
116+
if (typeof s === 'undefined' || typeof s === 'number')
117+
return s;
118+
119+
// Return a shallow clone of the original set s
120+
return new Set(s);
121+
}
122+
123+
function iter(s, cb) {
124+
if (typeof s !== 'undefined') {
125+
if (typeof s === 'number')
126+
cb(s);
127+
else
128+
s.forEach(cb);
129+
}
130+
}
131+
132+
function map(s, f) {
133+
if (typeof s !== 'undefined') {
134+
if (typeof s === 'number')
135+
return [f(s)];
136+
else {
137+
return Array.from(s).map(f);
138+
}
139+
}
140+
else {
141+
return [];
142+
}
143+
}
144+
145+
function fromArray(ary) {
146+
return new Set(ary);
147+
}
148+
149+
function toArray(s) {
150+
if (typeof s === 'undefined')
151+
return []
152+
153+
if (typeof s === 'number')
154+
return [s]
155+
156+
return Array.from(s);
157+
}
158+
159+
module.exports.size = size;
160+
module.exports.copy = copy;
161+
module.exports.contains = contains;
162+
module.exports.add = add;
163+
module.exports.addAll = addAll;
164+
module.exports.remove = remove;
165+
module.exports.removeAll = removeAll;
166+
module.exports.iter = iter;
167+
module.exports.map = map;
168+
module.exports.fromArray = fromArray;
169+
module.exports.toArray = toArray;
170+

0 commit comments

Comments
 (0)