λ Hash set open-hashing - forEach

======= Algorithm ======= <syntax js> // Open hash set, enumeration that calls `callback()` once per element in // the hash set. HashSet.prototype.forEach = function(callback) { for (var i=0, c=this.array.length; i<c; i++) { if (this.array[i]) { this.array[i].forEach(callback); } } };</syntax> ======= Support ======= <syntax js> LinkedList.prototype.insertOrderedUnique = function(data) { var insertOrderedUniqueNode = function(node, data) { if (!node || data < node.data) { node = new LinkedList.Node(data, node); } else if (data !== node.data) { node.next = insertOrderedUniqueNode(node.next, data); } return node; } this.first = insertOrderedUniqueNode(this.first, data); } LinkedList.prototype.forEach = function(callback) { var n = this.first; while (n) { callback(n.data); n = n.next; } }</syntax> ======= Tests ======= <syntax js> function testA() { var hash = new HashSet(); var seen = []; for (var i=0; i<10; i++) { hash.insert(i); } hash.forEach(function(x) { assert(typeof x === "number"); seen[x] = true; }); assert(seen.length === 10); for (var i=0; i<10; i++) { assert(seen[i]===true); } } </syntax> ======= Options ======= <syntax js> { "title":"Hash set open-hashing - forEach", "height":"250px", "import": ["hash_set-open_hashing", "linked-list"] } </syntax> ======= Visualisation ======= <syntax html> <html> <head> </head> <body> </body> </html> </syntax>