• 模拟实现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;
            }
        }
    }
    

      

  • 相关阅读:
    k8s与监控--解读prometheus监控kubernetes的配置文件
    一天学习k8s
    Kubernetes入门:Pod、节点、容器和集群
    skywalking的核心概念
    skywalking的插件管理agent管理
    skywalking7 源码解析 (3) :agent启动服务分析以及性能影响
    HyperLedger Fabric 多机部署(一)
    Hyperledger Fabric 替换couchDB
    Hyperledger Fabric (1.0)环境部署 chaincode【转】
    Hyperledger Fabric 第一次安装
  • 原文地址:https://www.cnblogs.com/ckAng/p/11118780.html
Copyright © 2020-2023  润新知