Skip to content

Commit

Permalink
Merge pull request #88 from joshuacurl/map
Browse files Browse the repository at this point in the history
Map for data structures
  • Loading branch information
felipernb committed Aug 15, 2014
2 parents a4b8d5b + eed6f49 commit e794d88
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 0 deletions.
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
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]);
});
});
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);
});
});


0 comments on commit e794d88

Please sign in to comment.