Skip to content

Commit c6635de

Browse files
Added HashSet.js and updated README.md.
1 parent 861ef69 commit c6635de

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,46 @@ const myMap2 = new HashMap(myMap); // create HashMap from other HashMap
6060
<tr>
6161
<td><code>equals(obj)</code></td><td>Returns <code>true</code> if the value of <code>obj</code> is equal to the value of this instance.</td>
6262
</tr>
63-
</table>
63+
</table>
64+
65+
## HashSet
66+
67+
[HashSet.js](src/HashSet.js) implements an unordered collection with unique elements. Similar to <code>HashMap</code>, each element (except primitives) stored in a <code>HashSet</code> must implement a <code>hashCode</code> and <code>equals</code> method. To use <code>HashSet</code>, <code>HashMap</code> must be included first. <br>
68+
Note that <code>HashSet</code> instances are iterable, so <code>[...myHashSet]</code> will return an array containing the elements in the <code>HashSet</code>.
69+
70+
### Constructor
71+
The constructor accepts an optional iterable object, all elements of which will be initially added to the <code>HashSet</code>. If the first parameter is not provided, it constructs an empty <code>HashSet</code>.
72+
73+
```
74+
const mySet = new HashSet(); // creates empty HashSet
75+
const mySet2 = new HashSet([1, 2, 3]); // creates a HashSet containing the elements of the array
76+
```
77+
78+
### Instance methods
79+
80+
<table>
81+
<tr><th>Method Signature</th><th>Description</th></tr>
82+
<tr><td><code>add(obj)</code></td><td>Adds <code>obj</code> to the <code>HashSet</code>, if it is not already present. Returns <code>true</code> if the object was added, i.e. it was not already present.</td></tr>
83+
<tr>
84+
<td><code>contains(obj)</code></td>
85+
<td>Returns <code>true</code> if the <code>HashSet</code> contains <code>obj</code>.</td>
86+
</tr>
87+
<tr>
88+
<td><code>remove(obj)</code></td><td>Removes <code>obj</code> from the <code>HashSet</code>.</td>
89+
</tr>
90+
<tr>
91+
<td><code>clear()</code></td><td>Removes all elements from the <code>HashSet</code>.</td>
92+
</tr>
93+
<tr>
94+
<td><code>size()</code></td><td>Returns the number of elements in the <code>HashSet</code>.</td>
95+
</tr>
96+
<tr>
97+
<td><code>isEmpty()</code></td><td>Returns <code>true</code> if there are no elements in the <code>HashSet</code></td>
98+
</tr>
99+
<tr>
100+
<td><code>hashCode()</code></td><td>Returns an integer hash code for this instance.</td>
101+
</tr>
102+
<tr>
103+
<td><code>equals(obj)</code></td><td>Returns <code>true</code> if the value of <code>obj</code> is equal to the value of this instance.</td>
104+
</tr>
105+
</table>

src/HashSet.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright (c) 2019 LieutenantPeacock
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5+
6+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
8+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9+
*/
10+
if(typeof HashMap !== 'function') throw Error("HashSet requires HashMap");
11+
class HashSet {
12+
#map
13+
static #PRESENT = true
14+
15+
constructor(arg){
16+
let arr = arg && [...arg];
17+
this.#map = new HashMap;
18+
if(Array.isArray(arr)){
19+
arr.forEach(o => this.add(o));
20+
}
21+
}
22+
23+
add(obj){
24+
return this.#map.put(obj, HashSet.#PRESENT) === undefined;
25+
}
26+
27+
contains(obj){
28+
return this.#map.containsKey(obj);
29+
}
30+
31+
remove(obj){
32+
return this.#map.remove(obj) === HashSet.#PRESENT;
33+
}
34+
35+
clear(){
36+
this.#map.clear();
37+
}
38+
39+
size(){
40+
return this.#map.size();
41+
}
42+
43+
isEmpty(){
44+
return this.#map.isEmpty();
45+
}
46+
47+
hashCode(){
48+
let h = 0;
49+
for(const x of this) h += HashSet.#hash(x);
50+
return h;
51+
}
52+
53+
equals(obj){
54+
if(!(obj instanceof HashSet) || this.size() != obj.size()) return false;
55+
for(const x of this) if(!obj.contains(x)) return false;
56+
return false;
57+
}
58+
59+
[Symbol.iterator](){
60+
return [...this.#map].map(arr => arr[0]).values();
61+
}
62+
63+
static #hash(key){
64+
return typeof key?.hashCode !== 'function' ? key: key.hashCode();
65+
}
66+
}

0 commit comments

Comments
 (0)