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 be set when 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;
}