Skip to content

Commit 2744775

Browse files
Added HashedObject.js and updated README.md.
1 parent ffb3d6e commit 2744775

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,56 @@ const myList2 = new ArrayList([1, 2, 3]); // creates an ArrayList containing the
167167
<tr>
168168
<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>
169169
</tr>
170+
</table>
171+
172+
## Equals and HashCode for Objects You Cannot Modify
173+
174+
[HashedObject.js](src/HashedObject.js) provides a wrapper for an object that allows specifying an <code>equals</code> and <code>hashCode</code> function. This wrapped object can then be used as a key in a <code>HashMap</code> or stored
175+
in a <code>HashSet</code>.<br>
176+
177+
### Constructor
178+
The constructor takes the object to wrap, the hash function, and the equals function as arguments. <br>
179+
The <code>equals</code> function must take two objects as parameters and return a boolean indicating whether they are equal. If not equals function is specified, the default is strict equality comparison.<br>
180+
The <code>hashCode</code> function must take one object as input and return its hash code.
181+
182+
```
183+
let wrapped = new HashedObject(myObj, hashFn, equalsFn);
184+
```
185+
186+
### Instance Methods
187+
188+
<table>
189+
<tr><th>Method Signature</th><th>Description</th></tr>
190+
<tr>
191+
<td><code>hashCode()</code></td><td>Returns the hash code of the wrapped object obtained with the hashCode function passed in through the constructor.</td>
192+
</tr>
193+
<tr>
194+
<td><code>equals(obj)</code></td><td>Returns the result of calling the equals function provided in the constructor, passing the underlying object and <code>obj</code> as arguments.</td>
195+
</tr>
196+
<tr>
197+
<td><code>getValue()</code></td><td>Returns the underlying object that was wrapped.</td>
198+
</tr>
199+
</table>
200+
201+
### Static Methods and Properties
202+
203+
There are some static methods and properties provided for convenience that implement common equals or hash code functions.
204+
205+
<table>
206+
<tr><th>Name</th><th>Description</th></tr>
207+
<tr>
208+
<td><code>DEFAULT_EQUALS</code></td><td>A function implementing an equals function using strict equality.</td>
209+
</tr>
210+
<tr>
211+
<td><code>ITERABLE_HASHCODE</code></td><td>A function implementing a hash code function for an iterable object.</td>
212+
</tr>
213+
<tr>
214+
<td><code>STRING_HASHCODE</code></td><td>A function implementing a hash code function for a string.</td>
215+
</tr>
216+
<tr>
217+
<td><code>ITERABLE_EQUALS</code></td><td>A function implementing an equals function for iterable objects by comparing elements at corresponding indexes.</td>
218+
</tr>
219+
<tr>
220+
<td><code>factory(hashCodeFn, equalsFn)</code></td><td>Returns a factory function from the given hash code and equals functions. The factory function accepts a single object parameter and returns a <code>HashedObject</code> from the parameter and the hash code and equals functions (provided when creating the factory).</td>
221+
</tr>
170222
</table>

src/HashedObject.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright (c) 2020 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+
/*
11+
Wrapper for an object that allows specifying an equals and hashCode function.
12+
The equals function must take two objects as parameters and return a boolean indicating whether they are equal.
13+
The hashCode function must take one object as input and return its hash code.
14+
*/
15+
class HashedObject {
16+
#value
17+
#equalsFn
18+
#hashCode
19+
static DEFAULT_EQUALS = (a,b)=>a===b;
20+
static ITERABLE_HASHCODE = val => {
21+
let hashCode = 1;
22+
for (const element of val)
23+
hashCode = 31 * hashCode +
24+
(typeof element.hashCode === 'function' ? element.hashCode() :
25+
!isNaN(+element) ? +element : 0);
26+
return hashCode;
27+
}
28+
static STRING_HASHCODE = val => {
29+
return [...val].reduce((hash,char)=>hash*31 + char.codePointAt(0), 0);
30+
}
31+
static ITERABLE_EQUALS = (a,b)=>{
32+
if(a.constructor !== b.constructor) return false;
33+
const arr1 = [...a];
34+
const arr2 = [...b];
35+
if(arr1.length != arr2.length) return false;
36+
for(let i = 0, len = arr1.length; i < len; i++){
37+
if(arr1[i] !== arr2[i] &&
38+
(typeof arr1[i]?.equals !== 'function' || !arr1[i].equals(arr2[i]))){
39+
return false;
40+
}
41+
}
42+
return true;
43+
}
44+
45+
constructor(value, hashCodeFn, equalsFn){
46+
this.#value = value;
47+
this.#hashCode = hashCodeFn(value);
48+
this.#equalsFn = equalsFn || HashedObject.DEFAULT_EQUALS;
49+
}
50+
51+
static factory(hashCodeFn, equalsFn){
52+
return obj => new HashedObject(obj, hashCodeFn, equalsFn);
53+
}
54+
55+
hashCode(){
56+
return this.#hashCode;
57+
}
58+
59+
equals(obj){
60+
return this.#equalsFn(this.#value, obj instanceof HashedObject ? obj.#value : obj);
61+
}
62+
63+
getValue(){
64+
return this.#value;
65+
}
66+
}

0 commit comments

Comments
 (0)