`
louisnan
  • 浏览: 27025 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JavaScript类集

阅读更多
/****************   
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;   
}
分享到:
评论
1 楼 redhatlinux10 2009-02-03  
amazing job!

相关推荐

    JavaScript全集

    12. **软件工程类**:良好的编码习惯、注释规范、测试策略以及代码审查都是软件工程实践中不可忽视的部分。了解设计模式和架构原则,如单一职责、开闭原则,有助于写出高质量的JavaScript代码。 通过"Script全集....

    javascript文档集(珍藏集)

    "JavaScript使用手册"是一本实用性极强的参考资料,它通常包含快速查找的API参考、函数库、方法和类的详细说明,帮助开发者在实际编码过程中快速解决问题。手册中可能还包括错误处理、调试技巧、性能优化等方面的...

    javascript实例代码集

    这个"javascript实例代码集"包含了一系列的JavaScript代码示例,旨在帮助开发者更好地理解和运用JavaScript。 1. **基础语法**: JavaScript的基础包括变量声明、数据类型(如字符串、数字、布尔值、对象和数组)、...

    Javascript代码集

    本压缩包文件“Javascript代码集”显然是一个包含多种JavaScript源码的资源集合,可能涵盖了各种功能和应用场景。 在JavaScript中,主要的知识点包括: 1. **基础语法**:变量声明(var、let、const)、数据类型...

    javascript合集

    这包括ES5、ES6(ECMAScript 2015)及后续版本的新特性,如箭头函数、模板字符串、let和const声明、解构赋值、类与模块等。此外,还有可能包含错误处理、类型转换、正则表达式、JSON处理以及BOM(浏览器对象模型)的...

    JavaScript精华代码2(图片集)

    这个名为"JavaScript精华代码2(图片集)"的压缩包文件,显然包含了一系列与JavaScript相关的图片,这些图片可能涵盖了JavaScript的核心概念、常用函数、编程技巧以及一些实用示例。以下是对这些潜在知识点的详细...

    李炎恢JavaScript讲义合集(全部173页pdf)

    而原型继承机制是JavaScript面向对象编程的核心,其继承方式与传统的基于类的继承方式大相径庭,影响了后来许多编程语言的设计。 在JavaScript发展过程中,尽管网景公司首先开发了JavaScript并提交给ECMA标准化,但...

    javascript源代码集

    7. **ES6新特性**:包括类(class)、模块(import/export)、解构赋值、let/const、箭头函数、模板字符串、Promise(用于处理异步操作)等,极大地提升了JavaScript的可读性和实用性。 8. **框架和库**:如React、...

    Javascript特效代码全集

    在JavaScript特效中,常见的包括以下几类: 1. 图片轮播:通过JavaScript,可以创建自动切换或手动控制的图片展示,如幻灯片效果,为网站的图片展示提供丰富的动画过渡。 2. 滚动效果:例如滚动条优化、页面滚动...

    JavaScript官方参考手册合集

    7. **ES6及后续版本的新特性**:ECMAScript(ES)每年都会发布新版本,带来新的语法和功能,如箭头函数、解构赋值、模板字符串、let和const、类和模块等。 8. **错误处理**:理解如何使用try/catch来捕获和处理运行...

    JavaScript效果集专题网

    ECMAScript 6(ES6)引入了许多新特性,如let和const声明、模板字符串、箭头函数、解构赋值、类和模块等,提高了JavaScript的可读性和开发效率。 九、性能优化 JavaScript性能优化包括减少HTTP请求、压缩代码、缓存...

    javascript基础视频80集7

    这套"javascript基础视频80集7"可能涵盖了JavaScript的基础到进阶内容,包括但不限于变量、数据类型、控制结构(如if...else、switch)、循环(for、while等)、函数、数组、对象、字符串处理、正则表达式、DOM操作...

    JavaScript源代码集

    7. **ES6及后续版本**:ECMAScript是JavaScript的标准化规范,ES6(也称为ES2015)引入了许多新特性,如let和const、模板字符串、箭头函数、类和模块等。后续的ES7、ES8等版本也不断添加了新功能。 8. **AJAX与...

    JavaScript参考手册大全合集.chm

    本合集包含了两份重要的JavaScript参考手册——“JavaScript语言中文参考手册”和“JavaScript参考手册”,它们是深入理解和掌握JavaScript的重要资源。 **JavaScript语言中文参考手册** 这份手册详细阐述了...

    javaScript源代码集

    这类工具可以帮助开发者检查代码质量、性能、错误,或者将JavaScript代码转换为其他格式,如浏览器可执行的捆绑文件。 `7880.com.txt` 文件名暗示这可能是一个文本文件,其中可能包含了网址7880.com的相关信息,...

    用JavaScript刷LeetCodeOJ解题报告合集

    4. **面向对象编程**:类的定义、继承、封装和多态在JavaScript中的应用。 5. **闭包**:理解JavaScript特有的闭包机制,以及它在函数式编程中的应用。 6. **异步编程**:Promise、async/await、回调函数等,用于...

    近1000种JavaScript网页特效集(chm)

    此外,现代JavaScript还引入了ES6(ECMAScript 6)的新特性,如箭头函数、模板字符串、类和模块,这些新特性让代码更简洁、易读。同时,随着Web组件和框架(如React、Vue、Angular)的流行,JavaScript在构建大型...

Global site tag (gtag.js) - Google Analytics