Skip to content

Commit

Permalink
refactor(associative): update ArraySet.get()
Browse files Browse the repository at this point in the history
- add explicit thi.ng/array dep (already was a transient dep)
- update destructuring
  • Loading branch information
postspectacular committed Aug 16, 2021
1 parent e847a27 commit 40383fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/associative/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
},
"dependencies": {
"@thi.ng/api": "^7.1.9",
"@thi.ng/arrays": "^1.0.1",
"@thi.ng/binary": "^2.2.9",
"@thi.ng/checks": "^2.9.10",
"@thi.ng/compare": "^1.3.33",
Expand Down
25 changes: 9 additions & 16 deletions packages/associative/src/array-set.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Fn3, Pair, Predicate2, SEMAPHORE } from "@thi.ng/api";
import { findIndex } from "@thi.ng/arrays";
import { equiv } from "@thi.ng/equiv";
import type { EquivSetOpts, IEquivSet } from "./api";
import { dissoc } from "./dissoc";
Expand Down Expand Up @@ -54,9 +55,9 @@ export class ArraySet<T> extends Set<T> implements IEquivSet<T> {
}

copy(): ArraySet<T> {
const $this = __private.get(this)!;
const s = new ArraySet<T>(null, { equiv: $this.equiv });
__private.get(s)!.vals = $this.vals.slice();
const { equiv, vals } = __private.get(this)!;
const s = new ArraySet<T>(null, { equiv });
__private.get(s)!.vals = vals.slice();
return s;
}

Expand Down Expand Up @@ -95,23 +96,15 @@ export class ArraySet<T> extends Set<T> implements IEquivSet<T> {
* @param notFound - default value
*/
get(key: T, notFound?: T): T | undefined {
const $this = __private.get(this)!;
const eq = $this.equiv;
const vals = $this.vals;
for (let i = vals.length; --i >= 0; ) {
if (eq(vals[i], key)) {
return vals[i];
}
}
return notFound;
const { equiv, vals } = __private.get(this)!;
const i = findIndex(vals, key, equiv);
return i >= 0 ? vals[i] : notFound;
}

delete(key: T) {
const $this = __private.get(this)!;
const eq = $this.equiv;
const vals = $this.vals;
const { equiv, vals } = __private.get(this)!;
for (let i = vals.length; --i >= 0; ) {
if (eq(vals[i], key)) {
if (equiv(vals[i], key)) {
vals.splice(i, 1);
return true;
}
Expand Down

0 comments on commit 40383fa

Please sign in to comment.