• JavaScript Set


    function Set() {
    	var items = {};
    	this.has = function(value) {
    		return value in items
    	}
    	this.add = function(value) {
    		if (!this.has(value)) {
    			items[value] = value;
    			return true
    		}
    		return false
    	}
    	this.remove = function() {
    		if (this.has(value)) {
    			delete items[value];
    			return true
    		}
    		return false
    	}
    	this.size = function() {
    		return Object.keys(items).length
    	}
    	this.values = function() {
    		return Object.keys(items)
    	}
    	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 differece = new Set();
    		var values = this.values();
    		for (var i = 0; i < values.length; i++) {
    			if (!otherSet.has(values[i])) {
    				differece.add(values[i])
    			}
    		}
    		return differece
    	}
    	this.subSet = function(otherSet) {
    		var subSet = new Set();
    		if (this.size() > otherSet.size()) {
    			return false
    		}
    		var values = this.values();
    		for (var i = 0; values.length; i++) {
    			if (!otherSet.has(values[i])) {
    				return false
    			}
    		}
    		return true
    	}
    }
    

      

  • 相关阅读:
    UNIGUI如何禁止关闭新窗口
    【转】华为编码规范
    awk中 使用shell的环境变量
    【转】SDL与MFC的混合
    MSSQL学习笔记
    转 在.NET环境下为网站增加IP过滤功能
    欢迎加入asp.net交流群
    配置SQL Server2005以允许远程访问
    实用的文件操作类
    VS2005 + VSS2005 实现团队开发、源代码管理、版本
  • 原文地址:https://www.cnblogs.com/shidengyun/p/5122595.html
Copyright © 2020-2023  润新知