继续咱们的绿坝行动,嘿嘿

来源:互联网 发布:淘宝营销推广策略 编辑:程序博客网 时间:2024/04/26 07:01

function List() {
    this.list = new Array();
}
List.prototype.add = function(e) {
    this.list.push(e);
    return true;
};
List.prototype.clear = function() {
    this.list = new Array();
};
List.prototype.contains = function(e) {
    return this.indexOf(e) >= 0;
};
List.prototype.isEmpty = function() {
    return this.size() == 0;
};
List.prototype.size = function() {
    return this.list.length;
};
List.prototype.toArray = function() {
    return this.list;
};
List.prototype.remove = function(index) {
    assert(index >=0 && index < this.size(), "param is error!");
    var tmp = this.get(index);
    this.list.splice(index, 1);
    return tmp;
};
List.prototype.compareTo = function(obj) {
    assert(obj != null && obj instanceof Collection, "param type is error!");
    var index = 0, tmp = null;
    while(index < this.size() && index < obj.size()) {
        tmp = this.get(index) > obj.get(index) ? 1 : this.get(index) < obj.get(index) ? -1 : 0;
        if(tmp != 0) {
            return tmp;
        }
        index++;
    }
    return tmp >= this.size() && tmp >= obj.size() ? 0 : tmp >= this.size() ? -1 : 1;
};
List.prototype.equals = function(obj) {
    return this.compareTo(obj) == 0;
};
List.prototype.insert = function(index, e) {
    assert(index >= 0 && index < this.size(), "param is error!");
    this.list.splice(index, 0, e);
};
List.prototype.get = function(index) {
    assert(index >= 0 && index < this.size(), "param is error!");
    return this.list[index];
};
List.prototype.indexOf = function(e, fromIndex) {
    return this.list.indexOf(e, fromIndex);
};
List.prototype.lastIndexOf = function(e, fromIndex) {
    return this.list.lastIndexOf(e, fromIndex)
};
List.prototype.set = function(index, e) {
    assert(index >= 0 && index < this.size(), "param is error!");
    this.list[index] = e;
};
List.prototype.iterator = function() {
    return new Iterator(this);
};
List.prototype.listIterator = function() {
    return new ListIterator(this);
};

 


function Iterator(list) {
    assert(list instanceof List, "param type is error!");
    this.list = list;
    this.curIdx = -1;
}
Iterator.prototype.hasNext = function() {
    return this.curIdx + 1 < this.list.size();
};
Iterator.prototype.next = function() {
    return this.list.get(++this.curIdx);
};
Iterator.prototype.remove = function() {
    this.list.remove(this.curIdx--);
};

 


function ListIterator(list) {
    Iterator.call(this, list);
}
ListIterator.prototype = new Iterator();
ListIterator.prototype.add = function(e) {
    this.list.insert(this.curIdx + 1, e);
};
ListIterator.prototype.hasPrevious = function() {
    return this.curIdx - 1 >= 0;
};
ListIterator.prototype.nextIndex = function() {
    return this.curIdx + 1;
};
ListIterator.prototype.previous = function() {
    return this.list.get(--this.curIdx);
};
ListIterator.prototype.previousIndex = function() {
    return this.curIdx - 1;
};
ListIterator.prototype.set = function(e) {
    this.list.set(this.curIdx, e);
};

 


function Set() {
    List.call(this);
}
Set.prototype = new List();
Set.prototype.add = function(e) {
    if(this.contains(e)) {
    return false;
    }
    return this.add(e);
};

 


function Queue() {
    this.list = new List();
}
Queue.prototype.add = function(e) {
    return this.list.add(e);
};
Queue.prototype.remove = function() {
    return this.list.shift();
};
Queue.prototype.element = function() {
    return this.list.get[0];
};
Queue.prototype.isEmpty = function() {
    return this.list.isEmpty();
};
Queue.prototype.clear = function() {
    this.list.clear();
};
 


function Stack() {
    this.list = new List();
}
Stack.prototype.push = function(e) {
    return this.list.add(e);
};
Stack.prototype.pop = function() {
    return this.list.pop();
};
Stack.prototype.peek = function() {
    return this.list.get(this.list.size() - 1);
};
Stack.prototype.isEmpty = function() {
    return this.list.isEmpty();
};
Stack.prototype.clear = function() {
    this.list.clear();
};

 


function Entry(key, value) {
    this.key = key;
    this.value = value;
}

 


function Map() {
    this.list = new Set();
}
Map.prototype.clear = function() {
    this.list.clear();
};
Map.prototype.containsKey = function(key) {
    var tmp = this.keySet();
    return tmp.indexOf(key) >= 0;
};
Map.prototype.containsValue = function(value) {
    var tmp = this.values();
    return tmp.indexOf(value) >= 0;
};
Map.prototype.entrySet = function() {
    return this.list;
};
Map.prototype.equals = function(map) {
    assert(map instanceof Map, "param type is error!");
    return this.list.equals(map.list);
};
Map.prototype.get = function(key) {
    var tmp = this.keySet();
    var index = tmp.indexOf(key);
    return this.list.get(index);
};
Map.prototype.isEmpty = function() {
    return this.list.isEmpty();
};
Map.prototype.keySet = function() {
    var ite = this.entrySet().iterator();
    var tmp = new Set();
    while(ite.hasNext()) {
    tmp.add(ite.next().key);
    }
    return tmp;
};
Map.prototype.put = function(key, value) {
    var tmp = this.keySet();
    if(tmp.indexOf(key) >= 0) {
    return false;
    }
    return this.list.add(new Entry(key, value));
};
Map.prototype.remvoe = function(key) {
    var index = this.list.indexOf(key);
    return index < 0 ? null : this.list.remove(index);
};
Map.prototype.size = function() {
    return this.list.size();
};
Map.prototype.values = function() {
    var ite = this.entrySet().iterator();
    var tmp = new List();
    while(ite.hasNext()) {
    tmp.add(ite.next().value);
    }
    return tmp;
};

原创粉丝点击