Skip to content

Commit

Permalink
Merge pull request #657 from ignlg/next
Browse files Browse the repository at this point in the history
Next release 2.5.0
  • Loading branch information
ignlg committed Mar 1, 2024
2 parents 5dc4812 + 8e6896f commit a3bb403
Show file tree
Hide file tree
Showing 19 changed files with 2,709 additions and 2,122 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ heap vs array: push + top(50) of 100

## Changelog

### 2.5

- Improves the `limit` property to support negative, Infinity, and NaN values. They will be set as `0` and the heap will not be limited.
- Adds the `setLimit` method to set the limit of the heap. It returns `NaN` if the limit is negative, or NaN. This method is useful to check if the limit was set as expected.
- Improves tests and documentation.

### 2.4

- Adds the `indexOf` method to find the first index of an element in the heap.
Expand Down
61 changes: 45 additions & 16 deletions dist/heap-js.es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -1384,22 +1384,22 @@ var Heap = /** @class */ (function () {
this.heapArray = [];
this._limit = 0;
/**
* Alias of add
* Alias of {@link add}
* @see add
*/
this.offer = this.add;
/**
* Alias of peek
* Alias of {@link peek}
* @see peek
*/
this.element = this.peek;
/**
* Alias of pop
* Alias of {@link pop}
* @see pop
*/
this.poll = this.pop;
/**
* Alias of clear
* Alias of {@link clear}
* @see clear
*/
this.removeAll = this.clear;
Expand Down Expand Up @@ -1674,8 +1674,8 @@ var Heap = /** @class */ (function () {
Instance methods
*/
/**
* Adds an element to the heap. Aliases: `offer`.
* Same as: push(element)
* Adds an element to the heap. Aliases: {@link offer}.
* Same as: {@link push}(element).
* @param {any} element Element to be added
* @return {Boolean} true
*/
Expand All @@ -1686,7 +1686,7 @@ var Heap = /** @class */ (function () {
};
/**
* Adds an array of elements to the heap.
* Similar as: push(element, element, ...).
* Similar as: {@link push}(element, element, ...).
* @param {Array} elements Elements to be added
* @return {Boolean} true
*/
Expand Down Expand Up @@ -1839,7 +1839,8 @@ var Heap = /** @class */ (function () {
return foundIndexes;
};
/**
* Get the leafs of the tree (no children nodes)
* Get the leafs of the tree (no children nodes).
* See also: {@link getChildrenOf} and {@link bottom}.
* @return {Array}
* @see getChildrenOf
* @see bottom
Expand All @@ -1853,7 +1854,7 @@ var Heap = /** @class */ (function () {
};
Object.defineProperty(Heap.prototype, "length", {
/**
* Length of the heap.
* Length of the heap. Aliases: {@link size}.
* @return {Number}
* @see size
*/
Expand All @@ -1866,33 +1867,60 @@ var Heap = /** @class */ (function () {
Object.defineProperty(Heap.prototype, "limit", {
/**
* Get length limit of the heap.
* Use {@link setLimit} or {@link limit} to set the limit.
* @return {Number}
* @see setLimit
*/
get: function () {
return this._limit;
},
/**
* Set length limit of the heap.
* @return {Number}
* Set length limit of the heap. Same as using {@link setLimit}.
* @description If the heap is longer than the limit, the needed amount of leafs are removed.
* @param {Number} _l Limit, defaults to 0 (no limit). Negative, Infinity, or NaN values set the limit to 0.
* @see setLimit
*/
set: function (_l) {
this._limit = ~~_l;
if (_l < 0 || isNaN(_l)) {
// NaN, negative, and Infinity are treated as 0
this._limit = 0;
}
else {
// truncating a floating-point number to an integer
this._limit = ~~_l;
}
this._applyLimit();
},
enumerable: false,
configurable: true
});
/**
* Top node. Aliases: `element`.
* Same as: `top(1)[0]`
* Set length limit of the heap.
* Same as assigning to {@link limit} but returns NaN if the value was invalid.
* @param {Number} _l Limit. Negative, Infinity, or NaN values set the limit to 0.
* @return {Number} The limit or NaN if the value was negative, or NaN.
* @see limit
*/
Heap.prototype.setLimit = function (_l) {
this.limit = _l;
if (_l < 0 || isNaN(_l)) {
return NaN;
}
else {
return this._limit;
}
};
/**
* Top node. Aliases: {@link element}.
* Same as: {@link top}(1)[0].
* @return {any} Top node
* @see top
*/
Heap.prototype.peek = function () {
return this.heapArray[0];
};
/**
* Extract the top node (root). Aliases: `poll`.
* Extract the top node (root). Aliases: {@link poll}.
* @return {any} Extracted top node, undefined if empty
*/
Heap.prototype.pop = function () {
Expand All @@ -1904,6 +1932,7 @@ var Heap = /** @class */ (function () {
};
/**
* Pushes element(s) to the heap.
* See also: {@link add} and {@link addAll}.
* @param {...any} elements Elements to insert
* @return {Boolean} True if elements are present
*/
Expand Down Expand Up @@ -2079,7 +2108,7 @@ var Heap = /** @class */ (function () {
* Limit heap size if needed
*/
Heap.prototype._applyLimit = function () {
if (this._limit && this._limit < this.heapArray.length) {
if (this._limit > 0 && this._limit < this.heapArray.length) {
var rm = this.heapArray.length - this._limit;
// It's much faster than splice
while (rm) {
Expand Down
61 changes: 45 additions & 16 deletions dist/heap-js.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -1390,22 +1390,22 @@
this.heapArray = [];
this._limit = 0;
/**
* Alias of add
* Alias of {@link add}
* @see add
*/
this.offer = this.add;
/**
* Alias of peek
* Alias of {@link peek}
* @see peek
*/
this.element = this.peek;
/**
* Alias of pop
* Alias of {@link pop}
* @see pop
*/
this.poll = this.pop;
/**
* Alias of clear
* Alias of {@link clear}
* @see clear
*/
this.removeAll = this.clear;
Expand Down Expand Up @@ -1680,8 +1680,8 @@
Instance methods
*/
/**
* Adds an element to the heap. Aliases: `offer`.
* Same as: push(element)
* Adds an element to the heap. Aliases: {@link offer}.
* Same as: {@link push}(element).
* @param {any} element Element to be added
* @return {Boolean} true
*/
Expand All @@ -1692,7 +1692,7 @@
};
/**
* Adds an array of elements to the heap.
* Similar as: push(element, element, ...).
* Similar as: {@link push}(element, element, ...).
* @param {Array} elements Elements to be added
* @return {Boolean} true
*/
Expand Down Expand Up @@ -1845,7 +1845,8 @@
return foundIndexes;
};
/**
* Get the leafs of the tree (no children nodes)
* Get the leafs of the tree (no children nodes).
* See also: {@link getChildrenOf} and {@link bottom}.
* @return {Array}
* @see getChildrenOf
* @see bottom
Expand All @@ -1859,7 +1860,7 @@
};
Object.defineProperty(Heap.prototype, "length", {
/**
* Length of the heap.
* Length of the heap. Aliases: {@link size}.
* @return {Number}
* @see size
*/
Expand All @@ -1872,33 +1873,60 @@
Object.defineProperty(Heap.prototype, "limit", {
/**
* Get length limit of the heap.
* Use {@link setLimit} or {@link limit} to set the limit.
* @return {Number}
* @see setLimit
*/
get: function () {
return this._limit;
},
/**
* Set length limit of the heap.
* @return {Number}
* Set length limit of the heap. Same as using {@link setLimit}.
* @description If the heap is longer than the limit, the needed amount of leafs are removed.
* @param {Number} _l Limit, defaults to 0 (no limit). Negative, Infinity, or NaN values set the limit to 0.
* @see setLimit
*/
set: function (_l) {
this._limit = ~~_l;
if (_l < 0 || isNaN(_l)) {
// NaN, negative, and Infinity are treated as 0
this._limit = 0;
}
else {
// truncating a floating-point number to an integer
this._limit = ~~_l;
}
this._applyLimit();
},
enumerable: false,
configurable: true
});
/**
* Top node. Aliases: `element`.
* Same as: `top(1)[0]`
* Set length limit of the heap.
* Same as assigning to {@link limit} but returns NaN if the value was invalid.
* @param {Number} _l Limit. Negative, Infinity, or NaN values set the limit to 0.
* @return {Number} The limit or NaN if the value was negative, or NaN.
* @see limit
*/
Heap.prototype.setLimit = function (_l) {
this.limit = _l;
if (_l < 0 || isNaN(_l)) {
return NaN;
}
else {
return this._limit;
}
};
/**
* Top node. Aliases: {@link element}.
* Same as: {@link top}(1)[0].
* @return {any} Top node
* @see top
*/
Heap.prototype.peek = function () {
return this.heapArray[0];
};
/**
* Extract the top node (root). Aliases: `poll`.
* Extract the top node (root). Aliases: {@link poll}.
* @return {any} Extracted top node, undefined if empty
*/
Heap.prototype.pop = function () {
Expand All @@ -1910,6 +1938,7 @@
};
/**
* Pushes element(s) to the heap.
* See also: {@link add} and {@link addAll}.
* @param {...any} elements Elements to insert
* @return {Boolean} True if elements are present
*/
Expand Down Expand Up @@ -2085,7 +2114,7 @@
* Limit heap size if needed
*/
Heap.prototype._applyLimit = function () {
if (this._limit && this._limit < this.heapArray.length) {
if (this._limit > 0 && this._limit < this.heapArray.length) {
var rm = this.heapArray.length - this._limit;
// It's much faster than splice
while (rm) {
Expand Down

0 comments on commit a3bb403

Please sign in to comment.