Skip to content

Commit

Permalink
🚀 perf(left/right): Garbage collect empty subcollections.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed Sep 1, 2020
1 parent 6492d0f commit 945d903
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/BasePairs.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class BasePairs {
}

left() {
return filter((x) => this._map.get(x).size > 0, this._map.keys());
return this._map.keys();
}

right() {
Expand Down
3 changes: 1 addition & 2 deletions src/EfficientlyInvertiblePairs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {filter} from '@aureooms/js-itertools';
import BasePairs from './BasePairs';
import insert from './insert';
import remove from './remove';
Expand Down Expand Up @@ -45,7 +44,7 @@ export default class EfficientlyInvertiblePairs extends BasePairs {
// Custom

right() {
return filter((x) => this._imap.get(x).size > 0, this._imap.keys());
return this._imap.keys();
}

*leftOf(B) {
Expand Down
6 changes: 5 additions & 1 deletion src/remove.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export default function remove(map, A, B) {
if (map.has(A)) {
return map.get(A).delete(B);
const right = map.get(A);
if (right.delete(B)) {
if (right.size === 0) map.delete(A);
return true;
}
}

return false;
Expand Down

0 comments on commit 945d903

Please sign in to comment.