javascript函数库:HashSet.js

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 treeroot 的 blog

/**
* use as java.util.hashset
*/

function testhashset(){
 alert("hashset test begin:");
 try{
 }
 catch(e){
  alert(e);
 }
 alert("hashset test end");
}

function hashset(){ 

    private:
 this.map=new hashmap();
 this.zero=new integer(0);


 
 function hashiterator(it){
        this.it=it;
  
  this.hasnext=hasnext;
  function hasnext() {
   return this.it.hasnext();
        }

        this.next=next;
  function next() {
   return this.it.next().getkey();
        }
    }
 
 public:
 this.size=size;
 function size(){
  return this.map.size();
    }

    this.isempty=isempty;
 function isempty() {
  return this.map.isempty();
    }

 this.contains=contains;
 function contains(o) {
  return this.map.containskey(o);
    }
 
 this.add=add;
 function add(o) {
  return this.map.put(o,this.zero)==null;
    }

 this.addall=addall;
 function addall(set){
  var mod=false;
  for(var it=set.iterator();it.hasnext();){
   if(this.add(it.next())) mod=true;
  }
  return mod;
 }

 
 this.remove=remove;
    function remove(o) {
  return this.map.remove(o).equals(this.zero);
    }
 
 
    this.clear=clear;
 function clear() {
  this.map.clear();
    }
 
 
 this.iterator=iterator;
 function iterator(){
  return new hashiterator(this.map.iterator());
 }

 
 this.equals=equals;
 function equals(o) {
  if (o.size() != this.size())
   return false;
        for(var it=this.iterator();it.hasnext();){
   if(!o.contains(it.next())) return false;
  }
  return true;
 }
 
 this.hashcode=hashcode;
 function hashcode() {
  var h=0;
  for(var it=this.iterator();it.hasnext();){
   h+=it.next().hashcode();
  }
  return h;
 }
 
 this.toarray=toarray;
 function toarray(){
  var arr=new array();
  var i=0;
  for(var it=this.iterator();it.hasnext();){
   arr[i++]=it.next();
  }
  return arr;
 }
 
 
}

本文关键:javascript函数库:HashSet.js
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top