Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map for data structures #88

Merged
merged 14 commits into from Aug 15, 2014
14 changes: 14 additions & 0 deletions data_structures/hash_table.js
Expand Up @@ -102,4 +102,18 @@ HashTable.prototype._increaseCapacity = function () {
}
};

HashTable.prototype.forEach = function (fn) {
var applyFunction = function (linkedList) {
linkedList.forEach(function (elem) {
fn(elem.k);
});
};

for (var i = 0; i < this._table.length; i++) {
if (this._table[i]) {
applyFunction(this._table[i]);
}
}
};

module.exports = HashTable;
12 changes: 12 additions & 0 deletions data_structures/heap.js
Expand Up @@ -89,6 +89,18 @@ MinHeap.prototype.heapify = function (a) {
}
};

MinHeap.prototype.forEach = function (fn) {
var elementsCopy = JSON.parse(JSON.stringify(this._elements));

var element = this.extract();
while (typeof element !== 'undefined') {
fn(element);
element = this.extract();
}

this._elements = elementsCopy;
};

/**
* Max Heap, keeps the highest element always on top
*
Expand Down
2 changes: 1 addition & 1 deletion data_structures/linked_list.js
Expand Up @@ -140,7 +140,7 @@ LinkedList.prototype.delNode = function (node) {
/**
* Performs the fn function with each element in the list
*/
LinkedList.prototype.map = function (fn) {
LinkedList.prototype.forEach = function (fn) {
var node = this.head;
while (node) {
fn(node.value);
Expand Down
4 changes: 4 additions & 0 deletions data_structures/queue.js
Expand Up @@ -46,4 +46,8 @@ Queue.prototype.peek = function () {
return this._elements.get(0);
};

Queue.prototype.forEach = function (fn) {
this._elements.forEach(fn);
};

module.exports = Queue;
4 changes: 4 additions & 0 deletions data_structures/set.js
Expand Up @@ -36,4 +36,8 @@ HashSet.prototype.contains = function (e) {
return this._elements.get(e) !== undefined;
};

HashSet.prototype.forEach = function (fn) {
this._elements.forEach(fn);
};

module.exports = HashSet;
14 changes: 14 additions & 0 deletions test/data_structures/hash_table.js
Expand Up @@ -123,5 +123,19 @@ describe('Hash Table', function () {
h.put(o, 'foo');
assert.equal(h.get(o), 'foo');
});

it('should perform a function to all keys with forEach', function () {
var h = new HashTable();
h.put(1, true);
h.put(2, true);
h.put(3, true);

var total = 0;
h.forEach(function (elem) {
total += elem;
});

assert.equal(total, 6);
});
});

26 changes: 26 additions & 0 deletions test/data_structures/heap.js
Expand Up @@ -53,6 +53,19 @@ describe('Min Heap', function () {

assert(h.isEmpty());
});

it('should perform a function to all elements from smallest to largest' +
' with forEach', function () {
var h = new heap.MinHeap();
h.heapify([3, 10, 1000, 0, 2, 1]);

var output = [];
h.forEach(function (n) {
output.push(n);
});

assert.deepEqual(output, [0, 1, 2, 3, 10, 1000]);
});
});

describe('Max Heap', function () {
Expand Down Expand Up @@ -105,4 +118,17 @@ describe('Max Heap', function () {

assert(h.isEmpty());
});

it('should perform a function to all elements from largest to smallest' +
' with forEach', function () {
var h = new heap.MaxHeap();
h.heapify([3, 10, 1000, 0, 2, 1]);

var output = [];
h.forEach(function (n) {
output.push(n);
});

assert.deepEqual(output, [1000, 10, 3, 2, 1, 0]);
});
});
4 changes: 2 additions & 2 deletions test/data_structures/linked_list.js
Expand Up @@ -133,7 +133,7 @@ describe('LinkedList', function () {
assert.equal(l.length, 0);
});

it('should perform a function to all elements with map', function () {
it('should perform a function to all elements with forEach', function () {
var l = new LinkedList();
l.add(5);
l.add(1);
Expand All @@ -142,7 +142,7 @@ describe('LinkedList', function () {
l.add(1000);

var a = [];
l.map(function (e) {
l.forEach(function (e) {
a.push(e);
});

Expand Down
14 changes: 14 additions & 0 deletions test/data_structures/queue.js
Expand Up @@ -35,6 +35,20 @@ describe('Queue', function () {
q.pop();
assert.equal(q.peek(), 2);
});

it('should perform a function to all elements with forEach', function () {
var q = new Queue();
q.push(1);
q.push(2);
q.push(3);

var total = 0;
q.forEach(function (elem) {
total += elem;
});

assert.equal(total, 6);
});
});


12 changes: 12 additions & 0 deletions test/data_structures/set.js
Expand Up @@ -57,6 +57,18 @@ describe('HashSet', function () {
assert(s.contains(1));
assert(!s.contains(4));
});

it('should perform a function to all elements with forEach', function () {
var s = new HashSet();
s.add(1, 2, 3);

var total = 0;
s.forEach(function (elem) {
total += elem;
});

assert.equal(total, 6);
});
});