Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions dist/fuse.basic.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -703,21 +703,22 @@ function computeScore(results, { ignoreFieldNorm = Config.ignoreFieldNorm }) {
//#endregion
//#region src/tools/MaxHeap.ts
var MaxHeap = class {
constructor(limit) {
constructor(limit, cmp) {
this.limit = limit;
this.heap = [];
this.cmp = cmp;
}
get size() {
return this.heap.length;
}
shouldInsert(score) {
return this.size < this.limit || score < this.heap[0].score;
shouldInsert(item) {
return this.size < this.limit || this.cmp(item, this.heap[0]) < 0;
}
insert(item) {
if (this.size < this.limit) {
this.heap.push(item);
this._bubbleUp(this.size - 1);
} else if (item.score < this.heap[0].score) {
} else if (this.cmp(item, this.heap[0]) < 0) {
this.heap[0] = item;
this._sinkDown(0);
}
Expand All @@ -729,7 +730,7 @@ var MaxHeap = class {
const heap = this.heap;
while (i > 0) {
const parent = i - 1 >> 1;
if (heap[i].score <= heap[parent].score) break;
if (this.cmp(heap[i], heap[parent]) <= 0) break;
const tmp = heap[i];
heap[i] = heap[parent];
heap[parent] = tmp;
Expand All @@ -744,8 +745,8 @@ var MaxHeap = class {
i = largest;
const left = 2 * i + 1;
const right = 2 * i + 2;
if (left < len && heap[left].score > heap[largest].score) largest = left;
if (right < len && heap[right].score > heap[largest].score) largest = right;
if (left < len && this.cmp(heap[left], heap[largest]) > 0) largest = left;
if (right < len && this.cmp(heap[right], heap[largest]) > 0) largest = right;
if (largest !== i) {
const tmp = heap[i];
heap[i] = heap[largest];
Expand Down Expand Up @@ -1013,7 +1014,7 @@ var Fuse = class {
const useHeap = isNumber(limit) && limit > 0 && isString(query);
let results;
if (useHeap) {
const heap = new MaxHeap(limit);
const heap = new MaxHeap(limit, sortFn);
if (isString(this._docs[0])) this._searchStringList(query, {
heap,
ignoreFieldNorm
Expand Down Expand Up @@ -1063,7 +1064,7 @@ var Fuse = class {
};
if (heap) {
result.score = computeScoreSingle(result.matches, { ignoreFieldNorm });
if (heap.shouldInsert(result.score)) heap.insert(result);
if (heap.shouldInsert(result)) heap.insert(result);
} else results.push(result);
}
}
Expand Down Expand Up @@ -1104,7 +1105,7 @@ var Fuse = class {
};
if (heap) {
result.score = computeScoreSingle(result.matches, { ignoreFieldNorm });
if (heap.shouldInsert(result.score)) heap.insert(result);
if (heap.shouldInsert(result)) heap.insert(result);
} else results.push(result);
}
});
Expand Down
2 changes: 1 addition & 1 deletion dist/fuse.basic.min.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/fuse.basic.min.mjs

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions dist/fuse.basic.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -702,21 +702,22 @@ function computeScore(results, { ignoreFieldNorm = Config.ignoreFieldNorm }) {
//#endregion
//#region src/tools/MaxHeap.ts
var MaxHeap = class {
constructor(limit) {
constructor(limit, cmp) {
this.limit = limit;
this.heap = [];
this.cmp = cmp;
}
get size() {
return this.heap.length;
}
shouldInsert(score) {
return this.size < this.limit || score < this.heap[0].score;
shouldInsert(item) {
return this.size < this.limit || this.cmp(item, this.heap[0]) < 0;
}
insert(item) {
if (this.size < this.limit) {
this.heap.push(item);
this._bubbleUp(this.size - 1);
} else if (item.score < this.heap[0].score) {
} else if (this.cmp(item, this.heap[0]) < 0) {
this.heap[0] = item;
this._sinkDown(0);
}
Expand All @@ -728,7 +729,7 @@ var MaxHeap = class {
const heap = this.heap;
while (i > 0) {
const parent = i - 1 >> 1;
if (heap[i].score <= heap[parent].score) break;
if (this.cmp(heap[i], heap[parent]) <= 0) break;
const tmp = heap[i];
heap[i] = heap[parent];
heap[parent] = tmp;
Expand All @@ -743,8 +744,8 @@ var MaxHeap = class {
i = largest;
const left = 2 * i + 1;
const right = 2 * i + 2;
if (left < len && heap[left].score > heap[largest].score) largest = left;
if (right < len && heap[right].score > heap[largest].score) largest = right;
if (left < len && this.cmp(heap[left], heap[largest]) > 0) largest = left;
if (right < len && this.cmp(heap[right], heap[largest]) > 0) largest = right;
if (largest !== i) {
const tmp = heap[i];
heap[i] = heap[largest];
Expand Down Expand Up @@ -1012,7 +1013,7 @@ var Fuse = class {
const useHeap = isNumber(limit) && limit > 0 && isString(query);
let results;
if (useHeap) {
const heap = new MaxHeap(limit);
const heap = new MaxHeap(limit, sortFn);
if (isString(this._docs[0])) this._searchStringList(query, {
heap,
ignoreFieldNorm
Expand Down Expand Up @@ -1062,7 +1063,7 @@ var Fuse = class {
};
if (heap) {
result.score = computeScoreSingle(result.matches, { ignoreFieldNorm });
if (heap.shouldInsert(result.score)) heap.insert(result);
if (heap.shouldInsert(result)) heap.insert(result);
} else results.push(result);
}
}
Expand Down Expand Up @@ -1103,7 +1104,7 @@ var Fuse = class {
};
if (heap) {
result.score = computeScoreSingle(result.matches, { ignoreFieldNorm });
if (heap.shouldInsert(result.score)) heap.insert(result);
if (heap.shouldInsert(result)) heap.insert(result);
} else results.push(result);
}
});
Expand Down
21 changes: 11 additions & 10 deletions dist/fuse.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,21 +1004,22 @@ function computeScore(results, { ignoreFieldNorm = Config.ignoreFieldNorm }) {
//#endregion
//#region src/tools/MaxHeap.ts
var MaxHeap = class {
constructor(limit) {
constructor(limit, cmp) {
this.limit = limit;
this.heap = [];
this.cmp = cmp;
}
get size() {
return this.heap.length;
}
shouldInsert(score) {
return this.size < this.limit || score < this.heap[0].score;
shouldInsert(item) {
return this.size < this.limit || this.cmp(item, this.heap[0]) < 0;
}
insert(item) {
if (this.size < this.limit) {
this.heap.push(item);
this._bubbleUp(this.size - 1);
} else if (item.score < this.heap[0].score) {
} else if (this.cmp(item, this.heap[0]) < 0) {
this.heap[0] = item;
this._sinkDown(0);
}
Expand All @@ -1030,7 +1031,7 @@ var MaxHeap = class {
const heap = this.heap;
while (i > 0) {
const parent = i - 1 >> 1;
if (heap[i].score <= heap[parent].score) break;
if (this.cmp(heap[i], heap[parent]) <= 0) break;
const tmp = heap[i];
heap[i] = heap[parent];
heap[parent] = tmp;
Expand All @@ -1045,8 +1046,8 @@ var MaxHeap = class {
i = largest;
const left = 2 * i + 1;
const right = 2 * i + 2;
if (left < len && heap[left].score > heap[largest].score) largest = left;
if (right < len && heap[right].score > heap[largest].score) largest = right;
if (left < len && this.cmp(heap[left], heap[largest]) > 0) largest = left;
if (right < len && this.cmp(heap[right], heap[largest]) > 0) largest = right;
if (largest !== i) {
const tmp = heap[i];
heap[i] = heap[largest];
Expand Down Expand Up @@ -1394,7 +1395,7 @@ var Fuse = class {
const useHeap = isNumber(limit) && limit > 0 && isString(query);
let results;
if (useHeap) {
const heap = new MaxHeap(limit);
const heap = new MaxHeap(limit, sortFn);
if (isString(this._docs[0])) this._searchStringList(query, {
heap,
ignoreFieldNorm
Expand Down Expand Up @@ -1444,7 +1445,7 @@ var Fuse = class {
};
if (heap) {
result.score = computeScoreSingle(result.matches, { ignoreFieldNorm });
if (heap.shouldInsert(result.score)) heap.insert(result);
if (heap.shouldInsert(result)) heap.insert(result);
} else results.push(result);
}
}
Expand Down Expand Up @@ -1543,7 +1544,7 @@ var Fuse = class {
};
if (heap) {
result.score = computeScoreSingle(result.matches, { ignoreFieldNorm });
if (heap.shouldInsert(result.score)) heap.insert(result);
if (heap.shouldInsert(result)) heap.insert(result);
} else results.push(result);
}
});
Expand Down
6 changes: 4 additions & 2 deletions dist/fuse.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,14 @@ declare function parse(query: Expression, options: any, {
declare const Config: Required<IFuseOptions<any>>;
//#endregion
//#region src/tools/MaxHeap.d.ts
type Comparator = (a: InternalResult, b: InternalResult) => number;
declare class MaxHeap {
limit: number;
heap: InternalResult[];
constructor(limit: number);
cmp: Comparator;
constructor(limit: number, cmp: Comparator);
get size(): number;
shouldInsert(score: number): boolean;
shouldInsert(item: InternalResult): boolean;
insert(item: InternalResult): void;
extractSorted(sortFn: (a: InternalResult, b: InternalResult) => number): InternalResult[];
_bubbleUp(i: number): void;
Expand Down
6 changes: 4 additions & 2 deletions dist/fuse.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,14 @@ declare function parse(query: Expression, options: any, {
declare const Config: Required<IFuseOptions<any>>;
//#endregion
//#region src/tools/MaxHeap.d.ts
type Comparator = (a: InternalResult, b: InternalResult) => number;
declare class MaxHeap {
limit: number;
heap: InternalResult[];
constructor(limit: number);
cmp: Comparator;
constructor(limit: number, cmp: Comparator);
get size(): number;
shouldInsert(score: number): boolean;
shouldInsert(item: InternalResult): boolean;
insert(item: InternalResult): void;
extractSorted(sortFn: (a: InternalResult, b: InternalResult) => number): InternalResult[];
_bubbleUp(i: number): void;
Expand Down
2 changes: 1 addition & 1 deletion dist/fuse.min.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/fuse.min.mjs

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions dist/fuse.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,21 +1003,22 @@ function computeScore(results, { ignoreFieldNorm = Config.ignoreFieldNorm }) {
//#endregion
//#region src/tools/MaxHeap.ts
var MaxHeap = class {
constructor(limit) {
constructor(limit, cmp) {
this.limit = limit;
this.heap = [];
this.cmp = cmp;
}
get size() {
return this.heap.length;
}
shouldInsert(score) {
return this.size < this.limit || score < this.heap[0].score;
shouldInsert(item) {
return this.size < this.limit || this.cmp(item, this.heap[0]) < 0;
}
insert(item) {
if (this.size < this.limit) {
this.heap.push(item);
this._bubbleUp(this.size - 1);
} else if (item.score < this.heap[0].score) {
} else if (this.cmp(item, this.heap[0]) < 0) {
this.heap[0] = item;
this._sinkDown(0);
}
Expand All @@ -1029,7 +1030,7 @@ var MaxHeap = class {
const heap = this.heap;
while (i > 0) {
const parent = i - 1 >> 1;
if (heap[i].score <= heap[parent].score) break;
if (this.cmp(heap[i], heap[parent]) <= 0) break;
const tmp = heap[i];
heap[i] = heap[parent];
heap[parent] = tmp;
Expand All @@ -1044,8 +1045,8 @@ var MaxHeap = class {
i = largest;
const left = 2 * i + 1;
const right = 2 * i + 2;
if (left < len && heap[left].score > heap[largest].score) largest = left;
if (right < len && heap[right].score > heap[largest].score) largest = right;
if (left < len && this.cmp(heap[left], heap[largest]) > 0) largest = left;
if (right < len && this.cmp(heap[right], heap[largest]) > 0) largest = right;
if (largest !== i) {
const tmp = heap[i];
heap[i] = heap[largest];
Expand Down Expand Up @@ -1393,7 +1394,7 @@ var Fuse = class {
const useHeap = isNumber(limit) && limit > 0 && isString(query);
let results;
if (useHeap) {
const heap = new MaxHeap(limit);
const heap = new MaxHeap(limit, sortFn);
if (isString(this._docs[0])) this._searchStringList(query, {
heap,
ignoreFieldNorm
Expand Down Expand Up @@ -1443,7 +1444,7 @@ var Fuse = class {
};
if (heap) {
result.score = computeScoreSingle(result.matches, { ignoreFieldNorm });
if (heap.shouldInsert(result.score)) heap.insert(result);
if (heap.shouldInsert(result)) heap.insert(result);
} else results.push(result);
}
}
Expand Down Expand Up @@ -1542,7 +1543,7 @@ var Fuse = class {
};
if (heap) {
result.score = computeScoreSingle(result.matches, { ignoreFieldNorm });
if (heap.shouldInsert(result.score)) heap.insert(result);
if (heap.shouldInsert(result)) heap.insert(result);
} else results.push(result);
}
});
Expand Down
2 changes: 1 addition & 1 deletion dist/fuse.worker.min.mjs

Large diffs are not rendered by default.

Loading