diff --git a/bit-set.js b/bit-set.js index f1d92463..4c17166c 100644 --- a/bit-set.js +++ b/bit-set.js @@ -8,7 +8,8 @@ * - (i >> 5) is the same as ((i / 32) | 0) * - (i & 0x0000001f) is the sames as (i % 32) */ -var bitwise = require('./utils/bitwise.js'); +var Iterator = require('obliterator/iterator'), + bitwise = require('./utils/bitwise.js'); /** * BitSet. @@ -227,13 +228,6 @@ BitSet.prototype.forEach = function(callback, scope) { } }; -/** - * Bit Set Iterator class. - */ -function BitSetIterator(next) { - this.next = next; -} - /** * Method used to create an iterator over a set's values. * @@ -250,7 +244,7 @@ BitSet.prototype.values = function() { j = -1, b; - return new BitSetIterator(function next() { + return new Iterator(function next() { if (!inner) { if (i >= l) @@ -296,7 +290,7 @@ BitSet.prototype.entries = function() { j = -1, b; - return new BitSetIterator(function next() { + return new Iterator(function next() { if (!inner) { if (i >= l) diff --git a/index.js b/index.js index 8e2c3784..7f5acc32 100644 --- a/index.js +++ b/index.js @@ -113,28 +113,13 @@ Index.prototype.forEach = function(callback, scope) { }); }; - -/** - * Index Iterator class. - */ -function IndexIterator(next) { - this.next = next; -} - /** * Method returning an iterator over the index's values. * * @return {IndexIterator} */ Index.prototype.values = function() { - var iterator = this.items.values(); - - Object.defineProperty(iterator, 'constructor', { - value: IndexIterator, - enumerable: false - }); - - return iterator; + return this.items.values(); }; /** diff --git a/inverted-index.js b/inverted-index.js index 0f210b20..58ad944a 100644 --- a/inverted-index.js +++ b/inverted-index.js @@ -4,7 +4,8 @@ * * JavaScript implementation of an inverted index. */ -var iterateOver = require('./utils/iterate.js'), +var Iterator = require('obliterator/iterator'), + iterateOver = require('./utils/iterate.js'), helpers = require('./set.js'); function identity(x) { @@ -215,24 +216,17 @@ InvertedIndex.prototype.forEach = function(callback, scope) { callback.call(scope, this.documents[i], i, this); }; -/** - * InvertedIndex Iterator class. - */ -function InvertedIndexIterator(next) { - this.next = next; -} - /** * Method returning an iterator over the index's documents. * - * @return {InvertedIndexIterator} + * @return {Iterator} */ InvertedIndex.prototype.documents = function() { var documents = this.items, l = documents.length, i = 0; - return new InvertedIndexIterator(function() { + return new Iterator(function() { if (i >= l) return { done: true @@ -250,17 +244,10 @@ InvertedIndex.prototype.documents = function() { /** * Method returning an iterator over the index's tokens. * - * @return {InvertedIndexIterator} + * @return {Iterator} */ InvertedIndex.prototype.tokens = function() { - var iterator = this.mapping.keys(); - - Object.defineProperty(iterator, 'constructor', { - value: InvertedIndexIterator, - enumerable: false - }); - - return iterator; + return this.mapping.keys(); }; /** diff --git a/linked-list.js b/linked-list.js index 7973524f..a9eed72c 100644 --- a/linked-list.js +++ b/linked-list.js @@ -5,7 +5,8 @@ * Singly linked list implementation. Uses raw JavaScript objects as nodes * as benchmarks proved it was the fastest thing to do. */ -var iterateOver = require('./utils/iterate.js'); +var Iterator = require('obliterator/iterator'), + iterateOver = require('./utils/iterate.js'); /** * Linked List. @@ -155,13 +156,6 @@ LinkedList.prototype.toArray = function() { return array; }; -/** - * Linked List Iterator class. - */ -function LinkedListIterator(next) { - this.next = next; -} - /** * Method used to create an iterator over a list's values. * @@ -170,7 +164,7 @@ function LinkedListIterator(next) { LinkedList.prototype.values = function() { var n = this.head; - return new LinkedListIterator(function() { + return new Iterator(function() { if (!n) return { done: true @@ -195,7 +189,7 @@ LinkedList.prototype.entries = function() { var n = this.head, i = 0; - return new LinkedListIterator(function() { + return new Iterator(function() { if (!n) return { done: true diff --git a/multi-index.js b/multi-index.js index 6f8df2c6..96fa54a0 100644 --- a/multi-index.js +++ b/multi-index.js @@ -113,27 +113,13 @@ MultiIndex.prototype.forEach = function(callback, scope) { }); }; -/** - * MultiIndex Iterator class. - */ -function MultiIndexIterator(next) { - this.next = next; -} - /** * Method returning an iterator over the index's values. * * @return {MultiIndexIterator} */ MultiIndex.prototype.values = function() { - var iterator = this.items.values(); - - Object.defineProperty(iterator, 'constructor', { - value: MultiIndexIterator, - enumerable: false - }); - - return iterator; + return this.items.values(); }; /** diff --git a/multi-map.js b/multi-map.js index b38cc9f7..420e727e 100644 --- a/multi-map.js +++ b/multi-map.js @@ -4,7 +4,8 @@ * * Implementation of a MultiMap with custom container. */ -var iterateOver = require('./utils/iterate.js'); +var Iterator = require('obliterator/iterator'), + iterateOver = require('./utils/iterate.js'); /** * MultiMap. @@ -128,33 +129,19 @@ MultiMap.prototype.forEach = function(callback, scope) { }); }; -/** - * MultiMap Iterator class. - */ -function MultiMapIterator(next) { - this.next = next; -} - /** * Method returning an iterator over the map's keys. * - * @return {MultiMapIterator} + * @return {Iterator} */ MultiMap.prototype.keys = function() { - var iterator = this.items.keys(); - - Object.defineProperty(iterator, 'constructor', { - value: MultiMapIterator, - enumerable: false - }); - - return iterator; + return this.items.keys(); }; /** * Method returning an iterator over the map's keys. * - * @return {MultiMapIterator} + * @return {Iterator} */ MultiMap.prototype.values = function() { var iterator = this.items.values(), @@ -165,7 +152,7 @@ MultiMap.prototype.values = function() { l; if (this.Container === Set) - return new MultiMapIterator(function next() { + return new Iterator(function next() { if (!inContainer) { step = iterator.next(); @@ -189,7 +176,7 @@ MultiMap.prototype.values = function() { }; }); - return new MultiMapIterator(function next() { + return new Iterator(function next() { if (!inContainer) { step = iterator.next(); @@ -217,7 +204,7 @@ MultiMap.prototype.values = function() { /** * Method returning an iterator over the map's entries. * - * @return {MultiMapIterator} + * @return {Iterator} */ MultiMap.prototype.entries = function() { var iterator = this.items.entries(), @@ -229,7 +216,7 @@ MultiMap.prototype.entries = function() { l; if (this.Container === Set) - return new MultiMapIterator(function next() { + return new Iterator(function next() { if (!inContainer) { step = iterator.next(); @@ -254,7 +241,7 @@ MultiMap.prototype.entries = function() { }; }); - return new MultiMapIterator(function next() { + return new Iterator(function next() { if (!inContainer) { step = iterator.next(); @@ -283,33 +270,19 @@ MultiMap.prototype.entries = function() { /** * Method returning an iterator over the map's containers. * - * @return {MultiMapIterator} + * @return {Iterator} */ MultiMap.prototype.containers = function() { - var iterator = this.items.values(); - - Object.defineProperty(iterator, 'constructor', { - value: MultiMapIterator, - enumerable: false - }); - - return iterator; + return this.items.values(); }; /** * Method returning an iterator over the map's associations. * - * @return {MultiMapIterator} + * @return {Iterator} */ MultiMap.prototype.associations = function() { - var iterator = this.items.entries(); - - Object.defineProperty(iterator, 'constructor', { - value: MultiMapIterator, - enumerable: false - }); - - return iterator; + return this.items.entries(); }; /** diff --git a/multi-set.js b/multi-set.js index bee98113..0f2295a7 100644 --- a/multi-set.js +++ b/multi-set.js @@ -4,7 +4,8 @@ * * JavaScript implementation of a MultiSet. */ -var iterateOver = require('./utils/iterate.js'); +var Iterator = require('obliterator/iterator'), + iterateOver = require('./utils/iterate.js'); /** * MultiSet. @@ -138,17 +139,10 @@ MultiSet.prototype.forEach = function(callback, scope) { }); }; -/** - * MultiSet Iterator class. - */ -function MultiSetIterator(next) { - this.next = next; -} - /** * Method returning an iterator over the set's values. * - * @return {MultiSetIterator} + * @return {Iterator} */ MultiSet.prototype.values = function() { var iterator = this.items.entries(), @@ -158,7 +152,7 @@ MultiSet.prototype.values = function() { multiplicty, i; - return new MultiSetIterator(function next() { + return new Iterator(function next() { if (!inContainer) { step = iterator.next(); @@ -188,17 +182,10 @@ MultiSet.prototype.values = function() { /** * Method returning an iterator over the set's multiplicities. * - * @return {MultiSetIterator} + * @return {Iterator} */ MultiSet.prototype.multiplicities = function() { - var iterator = this.items.entries(); - - Object.defineProperty(iterator, 'constructor', { - value: MultiSet, - enumerable: false - }); - - return iterator; + return this.items.entries(); }; /** diff --git a/package.json b/package.json index e903f54e..6586572f 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,9 @@ "url": "https://github.com/yomguithereal/mnemonist/issues" }, "homepage": "https://github.com/yomguithereal/mnemonist#readme", + "dependencies": { + "obliterator": "^1.2.1" + }, "devDependencies": { "@yomguithereal/eslint-config": "^4.0.0", "damerau-levenshtein": "^1.0.3", @@ -59,8 +62,7 @@ "leven": "^2.0.0", "lodash": "^4.17.4", "matcha": "^0.7.0", - "mocha": "^3.1.2", - "obliterator": "^1.2.0" + "mocha": "^3.1.2" }, "eslintConfig": { "extends": "@yomguithereal/eslint-config", diff --git a/queue.js b/queue.js index eb45ac0b..d12eed90 100644 --- a/queue.js +++ b/queue.js @@ -5,7 +5,8 @@ * Queue implementation based on the ideas of Queue.js that seems to beat * a LinkedList one in performance. */ -var iterateOver = require('./utils/iterate.js'); +var Iterator = require('obliterator/iterator'), + iterateOver = require('./utils/iterate.js'); /** * Queue @@ -97,13 +98,6 @@ Queue.prototype.toArray = function() { return this.items.slice(this.offset); }; -/** - * Queue Iterator class. - */ -function QueueIterator(next) { - this.next = next; -} - /** * Method used to create an iterator over a queue's values. * @@ -113,7 +107,7 @@ Queue.prototype.values = function() { var items = this.items, i = this.offset; - return new QueueIterator(function() { + return new Iterator(function() { if (i >= items.length) return { done: true @@ -139,7 +133,7 @@ Queue.prototype.entries = function() { i = this.offset, j = 0; - return new QueueIterator(function() { + return new Iterator(function() { if (i >= items.length) return { done: true diff --git a/sparse-set.js b/sparse-set.js index 1d1958e8..8fa4f00d 100644 --- a/sparse-set.js +++ b/sparse-set.js @@ -6,6 +6,7 @@ * * [Reference]: https://research.swtch.com/sparse */ +var Iterator = require('obliterator/iterator'); /** * SparseSet. @@ -99,13 +100,6 @@ SparseSet.prototype.forEach = function(callback, scope) { } }; -/** - * Sparse Set Iterator class. - */ -function SparseSetIterator(next) { - this.next = next; -} - /** * Method used to create an iterator over a set's values. * @@ -116,7 +110,7 @@ SparseSet.prototype.values = function() { dense = this.dense, i = 0; - return new SparseSetIterator(function() { + return new Iterator(function() { if (i < size) { var item = dense[i]; i++; diff --git a/stack.js b/stack.js index 5c45dd25..b36217dc 100644 --- a/stack.js +++ b/stack.js @@ -5,7 +5,8 @@ * Stack implementation relying on JavaScript arrays, which are fast enough & * correctly optimized for this kind of work. */ -var iterateOver = require('./utils/iterate.js'); +var Iterator = require('obliterator/iterator'), + iterateOver = require('./utils/iterate.js'); /** * Stack @@ -81,13 +82,6 @@ Stack.prototype.toArray = function() { return this.items.slice(0).reverse(); }; -/** - * Stack Iterator class. - */ -function StackIterator(next) { - this.next = next; -} - /** * Method used to create an iterator over a stack's values. * @@ -98,7 +92,7 @@ Stack.prototype.values = function() { l = items.length, i = 0; - return new StackIterator(function() { + return new Iterator(function() { if (i >= l) return { done: true @@ -124,7 +118,7 @@ Stack.prototype.entries = function() { l = items.length, i = 0; - return new StackIterator(function() { + return new Iterator(function() { if (i >= l) return { done: true