美文网首页
集合的js实现

集合的js实现

作者: infi_ | 来源:发表于2017-11-23 18:14 被阅读0次
function set(){
    var items={}
    this.has=function(value){
        return items.hasOwnProperty(value)
    }
    this.add=function(value){
        if(!this.has(value)){
            items[value]=value
        
            return true
        }else{
            return false
        }
    }
    this.remove=function(value){
       if(this.has(value)){
        delete items[value]
        return true
       }else{
        return false
       }

    }
    this.clear=function(){
        items={}
    }
    this.size=function(){
          var count=0
          for(var prop in items){ 
            if(this.has(prop)){   //因为对象圆形还包含别的自带继承属性 用has保险 
                ++count
            }
          }
         return count
    }
    this.values=function(){
        var keys=[]
        for(var prop in items){
            if(this.has(prop)){
                keys.push(prop)
            }
        }
        return keys
    }
    this.union=function(otherSet){  //合集
        var unionSet=new set()
        var values=this.values();
        for(var i=0;i<values.length;i++){
            unionSet.add(values[i])
        }

        var values=otherSet.values();
        for(var i=0;i<values.length;i++){
            unionSet.add(values[i])
        }
        return unionSet
     }
     this.intersection=function(otherSet){  //交集
           var intersectionSet=new set()
           var values=this.values()
           for(var i=0;i<values.length;i++){
                if(otherSet.has(values[i])){
                    intersectionSet.add(values[i])
                }

           }
           return intersectionSet
     }
    this.unintersection=function(otherSet){  //差集
           var intersectionSet=new set()
           var values=this.values()
           for(var i=0;i<values.length;i++){
                if(!otherSet.has(values[i])){
                    intersectionSet.add(values[i])
                }

           }
           return intersectionSet
     }
     this.son=function(otherSet){    //子集
        if(this.size()>otherSet.size()){
            return false
        }else{
             var values=this.values()
             for(var i=0;i<values.length;i++){
                if(!otherSet.has(values[i])){
                    return false
                }
             }
             return true

        }

     }
}

 var see=new set()
 see.add(1)
 see.add(2)


var see2=new set()
see2.add(1)
see2.add(5)
see2.add(7)


var see3=new set()
see3.add(5)

var ab=see.union(see2)
console.log(ab.values())
var cb=see.intersection(see2)
console.log(cb.values())

var cj=see.unintersection(see2)
console.log(cj.values())


console.log(see3.son(see))
console.log(see3.son(see2))

相关文章

  • 集合的js实现

  • javascript 面向对象技术

    javascript类的实现: 1.集合类 set.js 送人玫瑰,手留余香。

  • JS实现集合及常见的集合操作

      我们已经学习了数组(列表)、栈、队列和链表(及其变种)等顺序数据结构,接下来我们再学习集合这一数据结构。  集...

  • 2.js-集合、字典和散列表

    集合和字典基本相同,唯一的区别,就是集合没有键和值的配对,是一系列无序的、唯一的元素组合(js中集合的实现Set、...

  • js实现Hashmap功能

    js并没有类似Java的键值对存储集合类,但是可以自己定义实现: 使用方法:

  • 8 【集合】js集合

    闹鬼的很,昨天本来是写了这个集合的,结果检测死活报错,说我constructor用法不对,不用class写的时候则...

  • 60s倒计时

    JS实现 html js css vue实现 html js css

  • Java集合Collection和泛型

    集合的原理是什么呢 ? 集合有多种实现,其实就是集合类去描述集合对象,赋予了该类对象存储对象的能力。集合实现有数组...

  • 一份头条前端面试准备[整理稿]

    JS打乱数组 JS ajax JS bind 实现 懒加载 JS实现promise JS发布订阅模式 JSONP ...

  • Java 总结

    自己实现集合框架 (三): 单链表的实现 自己实现集合框架 (三): 单链表的实现基于 POI 封装 ExcelU...

网友评论

      本文标题:集合的js实现

      本文链接:https://www.haomeiwen.com/subject/msftbxtx.html