JavaScript function Library-Collection Framework _javascript techniques
Last Update:2017-01-19
Source: Internet
Author: User
Classes:
Collections
Arrays
ArrayList
SortedList extends ArrayList
HashMap
HashSet
*/
/****************
Collections
Note:sort () return a new List
****************/
function collections () {}
Collections.sort=function () {
if (arguments.length==1) {
var s=new sortedlist ();
S.addall (Arguments[0]);
return s;
}
else if (arguments.length==2) {
var s=new sortedlist ();
S.setcomparator (Arguments[1]);
S.addall (Arguments[0]);
return s;
}
Else
Throw "Illegalargument";
}
/***************
Arrays
****************/
function Arrays () {}
Arrays.aslist=function (arr) {
return new ArrayList (arr);
}
Listiterator
function Listiterator (Table,len) {
this.table=table;
This.len=len;
this.index=0;
This.hasnext=function () {
return this.index< This.len;
}
This.next=function () {
if (!this.hasnext ())
Throw "No such element!";
return this.table[this.index++];
}
}
/********************
ArrayList
********************/
function ArrayList () {
This.buffer=new Array ();
if (arguments.length>0) this.buffer=arguments[0];
This.length=this.buffer.length;
}
Arraylist.prototype.hashcode=function () {
var h=0;
for (Var i=0;i<this.lengh;i++)
H+=this.buffer[i].hashcode ();
return h;
}
Arraylist.prototype.size=function () {
return this.length;
}
Arraylist.prototype.clear=function () {
for (var i=0;i<this.length;i++) This.buffer[i]=null;
this.buffer.length=0;
this.length=0;
}
Arraylist.prototype.isempty=function () {
return this.length==0;
}
Arraylist.prototype.toarray=function () {
var copy=new Array ();
for (Var i=0;i<this.length;i++) {
Copy[i]=this.buffer[i];
}
return copy;
}
Arraylist.prototype.get=function (index) {
if (index>=0 && index<this.length)
return This.buffer[index];
return null;
}
Arraylist.prototype.remove=function (param) {
var index=0;
if (isNaN (param)) {
Index=this.indexof (param);
}
else Index=param;
if (index>=0 && index<this.length) {
for (Var i=index;i<this.length-1;i++)
THIS.BUFFER[I]=THIS.BUFFER[I+1];
This.length-=1;
return true;
}
else return false;
}
Arraylist.prototype.add=function () {
var args=arguments;
if (args.length==1) {
This.buffer[this.length++]=args[0];
return true;
}
else if (args.length==2) {
var index=args[0];
var obj=args[1];
if (index>=0 && index<=this.length) {
for (Var i=this.length;i>index;i--)
THIS.BUFFER[I]=THIS.BUFFER[I-1];
This.buffer[i]=obj;
This.length+=1;
return true;
}
}
return false;
}
Arraylist.prototype.indexof=function (obj) {
for (Var i=0;i<this.length;i++) {
if (this.buffer[i].equals (obj)) return i;
}
return-1;
}
Arraylist.prototype.lastindexof=function (obj) {
for (Var i=this.length-1;i>=0;i--) {
if (this.buffer[i].equals (obj)) return i;
}
return-1;
}
Arraylist.prototype.contains=function (obj) {
return This.indexof (obj)!=-1;
}
Arraylist.prototype.equals=function (obj) {
if (This.size ()!=obj.size ()) return false;
for (Var i=0;i<this.length;i++) {
if (!obj.get (i). Equals (This.buffer[i]) return false;
}
return true;
}
Arraylist.prototype.addall=function (list) {
var Mod=false;
For (var it=list.iterator (); It.hasnext ();) {
var v=it.next ();
if (This.add (v)) Mod=true;
}
return mod;
}
Arraylist.prototype.containsall=function (list) {
For (var i=0;i<list.size (); i++) {
if (!this.contains (List.get (i)) return false;
}
return true;
}
Arraylist.prototype.removeall=function (list) {
For (var i=0;i<list.size (); i++) {
This.remove (This.indexof (List.get (i)));
}
}
Arraylist.prototype.retainall=function (list) {
for (Var i=this.length-1;i>=0;i--) {
if (!list.contains (This.buffer[i])) {
This.remove (i);
}
}
}
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 being set when the list is empty";
This.com=comp;
}
Sortedlist.prototype.getcomparator=function () {
return this.com;
}
Override
Sortedlist.prototype.add=function (obj) {
var index = this.indexof (obj);
for (Var i=this.length;i>index;) {
This.buffer[i]=this.buffer[--i];
}
This.buffer[index]=obj;
this.length++;
}
Override
Sortedlist.prototype.indexof=function (obj) {
if (this.length==0) return 0;
var min=0,max=this.length-1;
var mid=0;
while (Min<=max) {
Mid = (Min+max) >> 1;
var c=0;
if (this.com==null) C=obj.compareto (This.buffer[mid]);
else C=this.com.compare (Obj,this.buffer[mid]);
if (c==0) {
return mid;
}
else if (c<0) {
Max=mid-1;
}
else{
min=mid+1;
}
}
Mid = (Min+max) >>1;
return mid+1;
}
Override
Sortedlist.prototype.contains=function (obj) {
if (this.length==0) return false;
var min=0,max=this.length-1;
var mid=0;
while (Min<=max) {
Mid = (Min+max) >> 1;
var c=0;
if (this.com==null) C=obj.compareto (This.buffer[mid]);
else C=this.com.compare (Obj,this.buffer[mid]);
if (c==0) {
return true;
}
else if (c<0) {
Max=mid-1;
}
else{
min=mid+1;
}
}
return false;
}
Override
Sortedlist.prototype.sublist=function (begin,end) {
var sl=new sortedlist ();
S1.setcomparator (this.com);
var sub=arraylist.prototype.sublist (begin.end);
Sl.addall (sub);
return SL;
}
/****************************
HashMap
****************************/
function Entry (h,k,v,n) {
This.value = v;
This.next = n;
This.key = k;
This.hash = h;
This.getkey=function () {
return this.key;
}
This.getvalue=function () {
return this.value;
}
This.setvalue=function (NewValue) {
var oldValue = This.value;
This.value = newvalue;
return oldValue;
}
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));
}
This.hashcode=function () {
return This.key.hashCode () ^ This.value.hashCode ();
}
This.tostring=function () {
return This.getkey () + "=" + This.getvalue ();
}
}
function Hashiterator (table,index,ne) {
this.table=table;
This.ne=ne;
This.index=index;
This.current=null;
This.hasnext=function () {
return this.ne!= null;
}
This.next=function () {
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.size=function () {
return this.length;
}
Hashmap.prototype.isempty=function () {
return this.length = = 0;
}
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;
}
}
This.addentry (hash, key, value, I);
var R=math.ceil (This.length * 1.5);
if (R > This.len) {
this.len= This.len << 1;
This.rehash ();
}
return null;
}
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;
}
Hashmap.prototype.addentry=function (hash, key, value, Bucketindex) {
This.table[bucketindex] = new Entry (hash, key, value, This.table[bucketindex]);
this.length++;
}
Hashmap.prototype.iterator=function () {
var i=this.table.length;
var next=null;
while (i>0 && next==null) {
Next=this.table[--i];
}
return new Hashiterator (This.table,i,next);
}
Hashmap.prototype.hashcode=function () {
var h=0;
For (var it=this.iterator (); It.hasnext ();) {
H+=it.next (). Hashcode ();
}
return h;
}
Hashmap.prototype.equals=function (map) {
if (!this.typematches (map)) return false;
if (Map.size ()!=this.size ()) return false;
For (var it=this.iterator (); It.hasnext ();) {
var e=it.next ();
var key=e.getkey ();
var value=e.getvalue ();
if (!value.equals (Map.get (key)) return False
}
return true;
}
/*************************
HashSet
**************************/
function Hashsetiterator (ITE) {
This.it=ite;
This.hasnext=function () {
return This.it.hasNext ();
}
This.next=function () {
Return This.it.next (). Getkey ();
}
}
function HashSet () {
This.map=new HashMap ();
}
Hashset.null=new Number ("! This is null! ");
Hashset.prototype.size=function () {
return This.map.size ();
}
Hashset.prototype.isempty=function () {
return This.map.isEmpty ();
}
Hashset.prototype.contains=function (o) {
return This.map.containsKey (o);
}
Hashset.prototype.add=function (o) {
Return This.map.put (o,hashset.null) ==null;
}
Hashset.prototype.addall=function (SET) {
var Mod=false;
For (var it=set.iterator (); It.hasnext ();) {
if (This.add (It.next ())) Mod=true;
}
return mod;
}
Hashset.prototype.remove=function (o) {
return This.map.remove (O). Equals (Hashset.null);
}
Hashset.prototype.clear=function () {
This.map.clear ();
}
Hashset.prototype.iterator=function () {
return new Hashsetiterator (This.map.iterator ());
}
Hashset.prototype.equals=function (o) {
if (!this.typematches (O)) return false;
if (O.size ()!= this.size ()) return false;
For (var it=this.iterator (); It.hasnext ();) {
if (!o.contains (It.next ())) return false;
}
return true;
}
Hashset.prototype.hashcode=function () {
var h=0;
For (var it=this.iterator (); It.hasnext ();) {
H+=it.next (). Hashcode ();
}
return h;
}
Hashset.prototype.toarray=function () {
var arr=new Array ();
var i=0;
For (var it=this.iterator (); It.hasnext ();) {
Arr[i++]=it.next ();
}
return arr;
}