From 94dc64bd674a30b238490828dccbd13577e76f59 Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Sat, 20 Apr 2024 22:57:39 +0900 Subject: [PATCH] fix --- src/collection/min-heap.ts | 3 ++- src/collection/multiset.spec.ts | 4 ++-- src/collection/multiset.ts | 4 +--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/collection/min-heap.ts b/src/collection/min-heap.ts index 2d9e2d4..90ecb33 100644 --- a/src/collection/min-heap.ts +++ b/src/collection/min-heap.ts @@ -32,6 +32,7 @@ export class MinHeap { this.tree[i] = value; while (i && isBefore(this.order, value /* i.e. this.tree[i] */, this.tree[j])) { + // pop up new value this.tree[i] = this.tree[j]; this.tree[j] = value; @@ -134,7 +135,7 @@ export class MinHeap { private assertInvariants() { for (let i = 1; i < this.tree.length; i++) { const p = positions.parent(i); - if (!this.order.compare(this.tree[p], this.tree[i])) { + if (!isBefore(this.order, this.tree[p], this.tree[i])) { throw new Error(`MinHeap#assertInvariants(): expected this.tree[${p} to be ordered before this.tree[${i}]`); } } diff --git a/src/collection/multiset.spec.ts b/src/collection/multiset.spec.ts index eb90a78..fd4ef2f 100644 --- a/src/collection/multiset.spec.ts +++ b/src/collection/multiset.spec.ts @@ -2,7 +2,7 @@ import { Multiset } from './multiset'; describe(Multiset, () => { it('keeps elements and count', () => { - const testee = new Multiset(false); + const testee = new Multiset(); testee.setCount('abc', 1); testee.setCount('abc', 2); @@ -17,7 +17,7 @@ describe(Multiset, () => { testee.setCount('abc', 1); testee.setCount('abc', 1); testee.setCount('abc', 0); - expect(testee.maxCount()).toEqual(1); + expect(testee.maxCount()).toBe(1); expect(testee.getCount('abc')).toEqual(0); expect(testee.getCount('abd')).toEqual(1); expect(testee.findByCount(0)).toEqual(['abc']); diff --git a/src/collection/multiset.ts b/src/collection/multiset.ts index b1b4642..9926687 100644 --- a/src/collection/multiset.ts +++ b/src/collection/multiset.ts @@ -4,8 +4,6 @@ export class Multiset { private map = new DefaultMap>((k) => new Set()); private countMap = new Map(); - constructor(readonly removeOnZeroFreq = true) {} - setCount(obj: T, count: number): void { const existedCount = this.countMap.get(obj); @@ -18,7 +16,7 @@ export class Multiset { const existedSet = this.map.get(existedCount)!; existedSet.delete(obj); - if (!existedSet.size && this.removeOnZeroFreq) { + if (!existedSet.size) { this.map.delete(existedCount); } } else {