Skip to content

Commit

Permalink
Fix duplicates in has operator suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Oct 24, 2023
1 parent a1b06a2 commit 4561cf9
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 189 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
- Added suggestions listing for keyword literals as property name in an object literal
- Fixed suggestions listing inside empty parentheses
- Fixed suggestions listing for `is` operator in tolerant mode when right part is empty
- Removed missed experimental keywords `if`, `then` and `else`
- Fixed duplicates in `has` operator suggestions
- Removed missed experimental `if`, `then` and `else` keyword tokens in parser

## 1.0.0-beta.8 (September 21, 2023)

Expand Down
3 changes: 2 additions & 1 deletion src/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ export default (source, { value, stats, assertions }) => ({
storage = new MaxHeap(
limit,
sort,
filterFactory && filterFactory(normalizeFilterPattern(text))
filterFactory && filterFactory(normalizeFilterPattern(text)),
true
);
break;

Expand Down
25 changes: 22 additions & 3 deletions src/utils/heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,37 @@ const defaultMinCompare = (a, b) => a - b;
const defaultMaxCompare = (a, b) => b - a;

export class Heap {
constructor(maxSize, compare, accept) {
constructor(maxSize, compare, accept, disallowDuplicates = false) {
this.maxSize = maxSize || Infinity;
this.compare = compare || defaultMaxCompare;
this.accept = accept || null;

this.values = [];
this.valuesSet = disallowDuplicates ? new Set() : null;
}

add(value) {
if (this.accept !== null && !this.accept(value)) {
return;
}

if (this.valuesSet !== null && this.valuesSet.has(value)) {
return;
}

if (this.values.length < this.maxSize) {
if (this.valuesSet !== null) {
this.valuesSet.add(value);
}

this.values.push(value);
this.heapifyUp(this.values.length - 1);
} else if (this.compare(this.values[0], value) > 0) {
if (this.valuesSet !== null) {
this.valuesSet.delete(this.values[0]);
this.valuesSet.add(value);
}

this.values[0] = value;
this.heapifyDown();
}
Expand All @@ -39,6 +53,10 @@ export class Heap {
this.heapifyDown();
}

if (this.valuesSet !== null) {
this.valuesSet.delete(topValue);
}

return topValue;
}

Expand Down Expand Up @@ -116,11 +134,12 @@ export class Heap {

export class MaxHeap extends Heap {};
export class MinHeap extends Heap {
constructor(maxSize, compare, accept) {
constructor(maxSize, compare, accept, allowDuplicates) {
super(
maxSize,
compare ? (a, b) => -compare(a, b) : defaultMinCompare,
accept
accept,
allowDuplicates
);
}
};
Loading

0 comments on commit 4561cf9

Please sign in to comment.