Comparing version 0.26.0 to 0.27.0
# Changelog | ||
## 0.27.0 | ||
* Adding `FixedDeque`. | ||
* Adding `CircularBuffer.unshift`. | ||
* Changing `CircularBuffer` semantics to now overwrite values when wrapping around. | ||
## 0.26.0 | ||
@@ -79,3 +85,3 @@ | ||
* Fixing `.from` static methods not taking byte arrays into account. | ||
* Fixing bugs related to `Stack.pop` edge cases. | ||
* Fixing bugs related to `Stack.pop` edge cases. | ||
* Optimizing `Stack` performance. | ||
@@ -82,0 +88,0 @@ |
@@ -21,2 +21,3 @@ /** | ||
shift(): T | undefined; | ||
unshift(): T | undefined; | ||
peekFirst(): T | undefined; | ||
@@ -23,0 +24,0 @@ peekLast(): T | undefined; |
@@ -8,3 +8,3 @@ /** | ||
var iterables = require('./utils/iterables.js'), | ||
Iterator = require('obliterator/iterator'); | ||
FixedDeque = require('./fixed-deque'); | ||
@@ -31,13 +31,13 @@ /** | ||
/** | ||
* Method used to clear the structure. | ||
* | ||
* @return {undefined} | ||
* Pasting most of the prototype from FixedDeque. | ||
*/ | ||
CircularBuffer.prototype.clear = function() { | ||
function paste(name) { | ||
CircularBuffer.prototype[name] = FixedDeque.prototype[name]; | ||
} | ||
// Properties | ||
this.start = 0; | ||
this.size = 0; | ||
}; | ||
Object.keys(FixedDeque.prototype).forEach(paste); | ||
if (typeof Symbol !== 'undefined') | ||
Object.getOwnPropertySymbols(FixedDeque.prototype).forEach(paste); | ||
/** | ||
@@ -50,5 +50,2 @@ * Method used to append a value to the buffer. | ||
CircularBuffer.prototype.push = function(item) { | ||
if (this.size === this.capacity) | ||
throw new Error('mnemonist/circular-buffer: buffer capacity (' + this.capacity + ') exceeded!'); | ||
var index = (this.start + this.size) % this.capacity; | ||
@@ -58,228 +55,42 @@ | ||
return ++this.size; | ||
}; | ||
// Overwriting? | ||
if (this.size === this.capacity) { | ||
/** | ||
* Method used to pop the buffer. | ||
* | ||
* @return {any} - Returns the popped item. | ||
*/ | ||
CircularBuffer.prototype.pop = function() { | ||
if (this.size === 0) | ||
return; | ||
// If start is at the end, we wrap around the buffer | ||
this.start = (index + 1) % this.capacity; | ||
return this.items[--this.size]; | ||
}; | ||
return this.size; | ||
} | ||
/** | ||
* Method used to shift the buffer. | ||
* | ||
* @return {any} - Returns the shifted item. | ||
*/ | ||
CircularBuffer.prototype.shift = function() { | ||
if (this.size === 0) | ||
return; | ||
var index = this.start; | ||
this.size--; | ||
this.start++; | ||
if (this.start === this.capacity) | ||
this.start = 0; | ||
return this.items[index]; | ||
return ++this.size; | ||
}; | ||
/** | ||
* Method used to peek the first value of the buffer. | ||
* Method used to prepend a value to the buffer. | ||
* | ||
* @return {any} | ||
* @param {any} item - Item to prepend. | ||
* @return {number} - Returns the new size of the buffer. | ||
*/ | ||
CircularBuffer.prototype.peekFirst = function() { | ||
if (this.size === 0) | ||
return; | ||
CircularBuffer.prototype.unshift = function(item) { | ||
var index = this.start - 1; | ||
return this.items[this.start]; | ||
}; | ||
if (this.start === 0) | ||
index = this.capacity - 1; | ||
/** | ||
* Method used to peek the last value of the buffer. | ||
* | ||
* @return {any} | ||
*/ | ||
CircularBuffer.prototype.peekLast = function() { | ||
if (this.size === 0) | ||
return; | ||
this.items[index] = item; | ||
var index = this.start + this.size - 1; | ||
// Overwriting | ||
if (this.size === this.capacity) { | ||
if (index > this.capacity) | ||
index -= this.capacity; | ||
this.start = index; | ||
return this.items[index]; | ||
}; | ||
/** | ||
* Method used to get the desired value of the buffer. | ||
* | ||
* @param {number} index | ||
* @return {any} | ||
*/ | ||
CircularBuffer.prototype.get = function(index) { | ||
if (this.size === 0) | ||
return; | ||
index = this.start + index; | ||
if (index > this.capacity) | ||
index -= this.capacity; | ||
return this.items[index]; | ||
}; | ||
/** | ||
* Method used to iterate over the buffer. | ||
* | ||
* @param {function} callback - Function to call for each item. | ||
* @param {object} scope - Optional scope. | ||
* @return {undefined} | ||
*/ | ||
CircularBuffer.prototype.forEach = function(callback, scope) { | ||
scope = arguments.length > 1 ? scope : this; | ||
var c = this.capacity, | ||
l = this.size, | ||
i = this.start, | ||
j = 0; | ||
while (j < l) { | ||
callback.call(scope, this.items[i], j, this); | ||
i++; | ||
j++; | ||
if (i === c) | ||
i = 0; | ||
return this.size; | ||
} | ||
}; | ||
/** | ||
* Method used to convert the buffer to a JavaScript array. | ||
* | ||
* @return {array} | ||
*/ | ||
// TODO: optional array class as argument? | ||
CircularBuffer.prototype.toArray = function() { | ||
var array = new this.ArrayClass(this.size), | ||
c = this.capacity, | ||
l = this.size, | ||
i = this.start, | ||
j = 0; | ||
this.start = index; | ||
while (j < l) { | ||
array[j] = this.items[i]; | ||
i++; | ||
j++; | ||
if (i === c) | ||
i = 0; | ||
} | ||
return array; | ||
return ++this.size; | ||
}; | ||
/** | ||
* Method used to create an iterator over the buffer's values. | ||
* | ||
* @return {Iterator} | ||
*/ | ||
CircularBuffer.prototype.values = function() { | ||
var items = this.items, | ||
c = this.capacity, | ||
l = this.size, | ||
i = this.start, | ||
j = 0; | ||
return new Iterator(function() { | ||
if (j >= l) | ||
return { | ||
done: true | ||
}; | ||
var value = items[i]; | ||
i++; | ||
j++; | ||
if (i === c) | ||
i = 0; | ||
return { | ||
value: value, | ||
done: false | ||
}; | ||
}); | ||
}; | ||
/** | ||
* Method used to create an iterator over the buffer's entries. | ||
* | ||
* @return {Iterator} | ||
*/ | ||
CircularBuffer.prototype.entries = function() { | ||
var items = this.items, | ||
c = this.capacity, | ||
l = this.size, | ||
i = this.start, | ||
j = 0; | ||
return new Iterator(function() { | ||
if (j >= l) | ||
return { | ||
done: true | ||
}; | ||
var value = items[i]; | ||
i++; | ||
if (i === c) | ||
i = 0; | ||
return { | ||
value: [j++, value], | ||
done: false | ||
}; | ||
}); | ||
}; | ||
/** | ||
* Attaching the #.values method to Symbol.iterator if possible. | ||
*/ | ||
if (typeof Symbol !== 'undefined') | ||
CircularBuffer.prototype[Symbol.iterator] = CircularBuffer.prototype.values; | ||
/** | ||
* Convenience known methods. | ||
*/ | ||
CircularBuffer.prototype.inspect = function() { | ||
var array = this.toArray(); | ||
array.type = this.ArrayClass.name; | ||
array.capacity = this.capacity; | ||
// Trick so that node displays the name of the constructor | ||
Object.defineProperty(array, 'constructor', { | ||
value: CircularBuffer, | ||
enumerable: false | ||
}); | ||
return array; | ||
}; | ||
if (typeof Symbol !== 'undefined') | ||
CircularBuffer.prototype[Symbol.for('nodejs.util.inspect.custom')] = CircularBuffer.prototype.inspect; | ||
/** | ||
* Static @.from function taking an abitrary iterable & converting it into | ||
@@ -294,3 +105,2 @@ * a circular buffer. | ||
CircularBuffer.from = function(iterable, ArrayClass, capacity) { | ||
if (arguments.length < 3) { | ||
@@ -297,0 +107,0 @@ capacity = iterables.guessLength(iterable); |
@@ -57,3 +57,3 @@ /** | ||
if (this.size === this.capacity) | ||
throw new Error('mnemonist/fixed-stack: stack capacity (' + this.capacity + ') exceeded!'); | ||
throw new Error('mnemonist/fixed-stack.push: stack capacity (' + this.capacity + ') exceeded!'); | ||
@@ -60,0 +60,0 @@ this.items[this.size++] = item; |
@@ -16,2 +16,3 @@ /** | ||
export {default as DefaultMap} from './default-map'; | ||
export {default as FixedDeque} from './fixed-deque'; | ||
export {default as FibonacciHeap, MinFibonacciHeap, MaxFibonacciHeap} from './fibonacci-heap'; | ||
@@ -18,0 +19,0 @@ export {default as FixedReverseHeap} from './fixed-reverse-heap'; |
@@ -20,2 +20,3 @@ /** | ||
DefaultMap: require('./default-map.js'), | ||
FixedDeque: require('./fixed-deque.js'), | ||
StaticDisjointSet: require('./static-disjoint-set.js'), | ||
@@ -22,0 +23,0 @@ FibonacciHeap: FibonacciHeap, |
{ | ||
"name": "mnemonist", | ||
"version": "0.26.0", | ||
"version": "0.27.0", | ||
"description": "Curated collection of data structures for the JavaScript language.", | ||
@@ -34,2 +34,3 @@ "scripts": { | ||
"default map", | ||
"deque", | ||
"disjoint set", | ||
@@ -36,0 +37,0 @@ "fibonacci heap", |
@@ -42,2 +42,3 @@ [![Build Status](https://travis-ci.org/Yomguithereal/mnemonist.svg)](https://travis-ci.org/Yomguithereal/mnemonist) | ||
* [Circular Buffer](https://yomguithereal.github.io/mnemonist/circular-buffer) | ||
* [Fixed Deque](https://yomguithereal.github.io/mnemonist/fixed-deque) | ||
* [Fibonacci Heap](https://yomguithereal.github.io/mnemonist/fibonacci-heap) | ||
@@ -44,0 +45,0 @@ * [Fixed Reverse Heap](https://yomguithereal.github.io/mnemonist/fixed-reverse-heap) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
290795
83
10440
108