"use strict"; var tab = window.open(); myMain(); // // hashing with probing demo // allows table resizing to prime sizes // function myMain() { var TABMAX = 29;; var ht = makeHashTab(TABMAX); var inp, val, num; while(true) { inp = prompt("comm (n,i,r,f,s,l,m,x,w,p,q) ?"); switch(inp) { case "q": return; break; case "n": ht = makeHashTab( Number(prompt("table size?")) ); break; case "i": val = prompt("val?"); alert(ht.insert(val)); break; case "r": alert("no remove implemented"); break; case "f": val = prompt("val?"); alert(ht.find(val)); break; case "s": alert(ht.size()); break; case "m": alert(ht.tabMax()); break; case "l": alert(ht.lambda()); break; case "p": ht.print(); break; case "w": num = Number(prompt("add how many keys?")); for (var i=0; i= this.max) { pv = pv % this.max; } if (tries > this.max) { return false; } // table is full } this.table[pv] = v; this.nelts++; return true; } }, probe: function ( i ) { //return i; // skip by ones return i*i; // skip by square }, remove: function( v ) { // not really working... has no markers in table var hv = this.hash(v,this.max); var pv = hv; var tries = 1; while (this.table[pv] !== v) { if (this.table[pv]===null) { return false; } pv = hv + this.probe(tries++); if (pv >= this.max) { pv = pv % this.max; } if (tries > this.max) { return false; } // its not there } this.table[pv] = null; this.nelts--; return true; }, find: function( v ) { var hv = this.hash(v,this.max); var pv = hv; var tries = 1; if (this.table[pv]===null) { return false; } if (this.table[pv]===v) { return true; } while (this.table[pv] !== v) { pv = hv + this.probe(tries++); if (pv >= this.max) { pv = pv % this.max; } if (tries > this.max) { return false; } // its not there } return true; }, extend: function() { // initial: double table size // better is to find a prime beyond double if (this.resizes===(this.primes.length-1)) return false; var oldMax = this.max; var oldTab = this.table; this.resizes++; this.max = this.primes[this.resizes]; // make and null out new table this.table = []; this.nelts = 0; for (var i=0; i