javascript函数库-集合框架


Posted in Javascript onApril 27, 2007

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;
}

Javascript 相关文章推荐
IE6弹出“已终止操作”的解决办法
Nov 27 Javascript
js设置function参数默认值(适合没有传参情况)
Feb 24 Javascript
使用jQuery的attr方法来修改onclick值
Jul 07 Javascript
jQuery 遍历函数详解
Jul 05 Javascript
javascript函数式编程程序员的工具集
Oct 11 Javascript
属于你的jQuery提示框(Tip)插件
Jan 20 Javascript
深入理解jQuery之防止冒泡事件
May 24 Javascript
JS实现页面载入时随机显示图片效果
Sep 07 Javascript
javascript 判断是否是微信浏览器的方法
Oct 09 Javascript
vue父组件向子组件动态传值的两种方法
Nov 11 Javascript
bootstrap table.js动态填充单元格数据的多种方法
Jul 18 Javascript
转换layUI的数据表格中的日期格式方法
Sep 19 Javascript
仿服务器端脚本方式的JS模板实现方法
Apr 27 #Javascript
改版了网上的一个js操作userdata
Apr 27 #Javascript
用 JSON 处理缓存
Apr 27 #Javascript
转一个日期输入控件,支持FF
Apr 27 #Javascript
学习jquery之一
Apr 27 #Javascript
JavaScript与函数式编程解释
Apr 27 #Javascript
漂亮的widgets,支持换肤和后期开发新皮肤(2007-4-27已更新1.7alpha)
Apr 27 #Javascript
You might like
使用sockets:从新闻组中获取文章(二)
2006/10/09 PHP
PHPMailer使用教程(PHPMailer发送邮件实例分析)
2012/12/06 PHP
带密匙的php加密解密示例分享
2014/01/29 PHP
PHP面向对象程序设计组合模式与装饰模式详解
2016/12/02 PHP
跟随鼠标旋转的文字
2006/11/30 Javascript
用javascript实现的仿Flash广告图片轮换效果
2007/04/24 Javascript
js中onload与onunload的使用示例
2013/08/25 Javascript
JQuery EasyUI 数字格式化处理示例
2014/05/05 Javascript
JS实现从表格中动态删除指定行的方法
2015/03/31 Javascript
在JavaScript中正确引用bind方法的应用
2015/05/11 Javascript
AngularJS学习笔记之ng-options指令
2015/06/16 Javascript
javascript实现的简单计时器
2015/07/19 Javascript
用Move.js配合创建CSS3动画的入门指引
2015/07/22 Javascript
JS获取当前脚本文件的绝对路径
2016/03/02 Javascript
JavaScript中return用法示例
2016/11/29 Javascript
Vue + Webpack + Vue-loader学习教程之功能介绍篇
2017/03/14 Javascript
vue.js的双向数据绑定Object.defineProperty方法的神奇之处
2019/01/18 Javascript
详解如何提升JSON.stringify()的性能
2019/06/12 Javascript
《javascript设计模式》学习笔记三:Javascript面向对象程序设计单例模式原理与实现方法分析
2020/04/07 Javascript
微信小程序接入vant Weapp组件的详细步骤
2020/10/28 Javascript
Python提取Linux内核源代码的目录结构实现方法
2016/06/24 Python
Python实现识别手写数字 简易图片存储管理系统
2018/01/29 Python
解决pyinstaller打包运行程序时出现缺少plotly库问题
2020/06/02 Python
图解Python中深浅copy(通俗易懂)
2020/09/03 Python
如何用Python徒手写线性回归
2021/01/25 Python
python中numpy.empty()函数实例讲解
2021/02/05 Python
详解移动端HTML5页面端去掉input输入框的白色背景和边框(兼容Android和ios)
2016/12/15 HTML / CSS
Belvilla法国:休闲度假房屋出租
2020/10/03 全球购物
全国道德模范事迹
2014/02/01 职场文书
政工例会汇报材料
2014/08/26 职场文书
初中学生操行评语
2014/12/26 职场文书
结婚保证书(三从四德)
2015/02/26 职场文书
独生子女证明范本
2015/06/19 职场文书
退休职工欢送会致辞
2015/08/01 职场文书
清明节主题班会
2015/08/14 职场文书
基于angular实现树形二级表格
2021/10/16 Javascript