zoukankan      html  css  js  c++  java
  • 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
    	}
    }
    

      

  • 相关阅读:
    Eclipse的常见使用错误及编译错误
    Android学习笔记之Bundle
    Android牟利之道(二)广告平台的介绍
    Perl dbmopen()函数
    Perl子例程(函数)
    Perl内置操作符
    Perl正则表达式
    Linux之间配置SSH互信(SSH免密码登录)
    思科路由器NAT配置详解(转)
    Windows下查看端口被程序占用的方法
  • 原文地址:https://www.cnblogs.com/shidengyun/p/5122595.html
Copyright © 2011-2022 走看看