ArrayList.prototype.subList=function(begin,end){ if(begin<0) begin=0; if(end>this.length) end=this.length; var newsize=end-begin; var newbuffer=new Array(); for(var i=0;i<newsize;i++){ newbuffer[i]=this.buffer[begin+i]; } return new ArrayList(newbuffer); } ArrayList.prototype.set=function(index,obj){ if(index>=0 && index<this.length){ temp=this.buffer[index]; this.buffer[index]=obj; return temp; } }
ArrayList.prototype.iterator=function iterator(){ return new ListIterator(this.buffer,this.length); }
/***************************** SortedList extends ArrayList *****************************/ function SortedList(){ this.com=null; } SortedList.prototype=new ArrayList(); SortedList.prototype.setComparator=function(comp){ if(this.length!=0) throw "Only can be set when list is empty"; this.com=comp; }
this.equals=function(o){ var e = o; var k1 = this.getKey(); var k2 = e.getKey(); var v1 = this.getValue(); var v2 = e.getValue(); return (k1.equals(k2) && v1.equals(v2)); }
var e = this.ne; if (e == null) throw "No such Element";
var n = e.next; var t = this.table; var i = this.index; while (n == null && i > 0) n = t[--i]; this.index = i; this.ne = n; this.current=e;
return this.current; } }
function HashMap() { this.len=8; this.table=new Array(); this.length=0; } // refer to java.util.HashMap HashMap.hash=function(x){ var h = x.hashCode(); h += ~(h << 9); h ^= (h >>> 14); h += (h << 4); h ^= (h >>> 10); return h; }
HashMap.prototype.rehash=function(){ var oldTable = this.table; this.table=new Array();
//transfer for (var i = 0; i< oldTable.length; i++) { var e = oldTable[i]; if (e != null) { oldTable[i] = null; do { var next = e.next; var j = this.indexFor(e.hash); e.next = this.table[j]; this.table[j] = e; e = next; } while (e != null); } } }
HashMap.prototype.indexFor=function(h) { var index= h & (this.len-1); return index; }
HashMap.prototype.get=function(key) { var hash =HashMap.hash(key); var i = this.indexFor(hash);
var e = this.table[i];
while (true) { if (e ==null) return null; if (e.hash == hash && key.equals(e.key)) return e.value; e = e.next; } }
HashMap.prototype.containsKey=function(key) { var hash =HashMap.hash(key); var i = this.indexFor(hash); var e = this.table[i];
while (e != null) { if (e.hash == hash && key.equals(e.key)) return true; e = e.next; } return false; }
HashMap.prototype.put=function(key,value) { var hash = HashMap.hash(key); var i = this.indexFor(hash);
for (var e = this.table[i]; e != null; e = e.next) { if (e.hash == hash && key.equals(e.key)) { var oldValue = e.value; e.value = value; return oldValue; } }
HashMap.prototype.putAll=function (map){ var mod=false; for(var it=map.iterator();it.hasNext();){ var e=it.next(); if(this.put(e.getKey(),e.getValue())) mod=true; } }
HashMap.prototype.remove=function(key) { var e = this.removeEntryForKey(key); return (e ==null ? null : e.value); }
HashMap.prototype.removeEntryForKey=function(key) { var hash = HashMap.hash(key); var i = this.indexFor(hash);
var prev = this.table[i]; var e = prev;
while (e != null) { var next = e.next; if (e.hash == hash && key.equals(e.key)) { this.length--; if (prev.equals(e)) this.table[i] = next; else prev.next = next; return e; } prev = e; e = next; } return e; }
HashMap.prototype.clear=function() { for (var i = 0; i < this.table.length; i++) this.table[i] = null; this.length = 0; }
HashMap.prototype.containsValue=function(value) { if (value == null) return false;
var tab = this.table; for (var i = 0; i < tab.length ; i++) for (var e = tab[i] ; e != null ; e = e.next) if (value.equals(e.value)) return true; return false; }