zoukankan      html  css  js  c++  java
  • 模拟实现ES6的set类

    function Set() {
        var items = {};
        // this.has = function(value){
        //     return value in items;
        // }
        this.has = function(value){
            return items.hasOwnProperty(value);
        },
        this.add = function(value){
            if (!this.has(value)) {
                items[value] = value;
                return true;
            }
            return false;
        },
        this.remove = function(value){
            if (this.has(value)) {
                delete value;
                return true;
            }
            return false;
        },
        this.clear = function(){
            items={};
        },
        this.size = function(){
            var count = 0;
            for(var prop in items){
                if (items.hasOwnProperty(prop)) {
                    ++count;
                }
            }
            return count;
        },
        this.values= function(){
            var values = [];
            for(var value in items){
                if (items.hasOwnProperty(value)) {
                    values.push(value);
                }
            }
            return values;
        },
        //并集
        this.union = function(otherSet){
            var unionSet = new Set();
            var values = this.values();
            for (var i = 0; i < values.length; i++) {
                unionSet.add(values[i]);
            }
    
            values = otherSet.values();
            for (var i = 0; i < values.length; i++) {
                unionSet.add(values[i]);
            }
    
            return unionSet;
        },
        //交集
        this.intersection = function(otherSet){
            var intersection = new Set();
            var values = this.values();
            for (var i = 0; i < values.length; i++) {
                if (otherSet.has(values[i])) {
                    intersection.add(values[i]);
                }
            }
            return intersection;
        },
        //差集
        this.difference = function(otherSet){
            var difference = new Set();
            var values = this.values();
            for (var i = 0; i < values.length; i++) {
                if (!otherSet.has(values[i])) {
                    difference.add(values[i]);
                }
            }
            return difference;
        },
        //子集
        this.subset = function(otherSet){
            var values = this.values();
            if (this.size() > otherSet.size()) {
                return false;
            }
            else{
                for (var i = 0; i < values.length; i++) {
                    if (!otherSet.has(values[i])) {
                        return false;
                    }
                }
                return true;
            }
        }
    }
    

      

  • 相关阅读:
    [NOIP提高&洛谷P1024]一元三次方程求解 题解(二分答案)
    浅谈二分答案的原理和相关应用
    Python 30分钟入门指南
    LG P1721 [NOI2016]国王饮水记
    LG P5238 整数校验器
    亡灵序曲
    线性代数三部曲(三)&#183;矩阵
    线性代数三部曲(二)·Gauss消元
    关于$color{darkblue}{Anverking}$的介绍
    线性代数三部曲(一)&#183;行列式
  • 原文地址:https://www.cnblogs.com/ckAng/p/11118780.html
Copyright © 2011-2022 走看看