/* * å®ä¹éå */ module.exports = 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; } return false; } // å 餿°æ® this.remove = function (value) { if (this.has(value)) { delete items[value]; return true; } return false; } // æ¸ ç©ºæ°æ® this.clear = function () { items = []; } // æå°éå this.print = function () { console.log(items); } // è¿åéåå¤§å° this.size = function () { var count = 0; for (var prop in items) { if (items.hasOwnProperty(prop)) { ++count; } return count; } } // è¿åæææ°æ® 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.difference = function (otherSet) { // å建æ°çéå var differenceSet = new Set(); // è·å¾å½åéåçæææ°æ® var values = this.values(); for (var i = 0; i < values.length; i++) { // 夿å½åç弿¯å¦æå¨otherSetä¸ï¼æ²¡æåå å ¥å°æ°çéåä¸ if (!otherSet.has(values[i])) { differenceSet.add(values[i]) } } // è¿åéå return differenceSet; } // åé this.subset = function (otherSet) { // 妿å½åéåæ°é大äºotherSetï¼è¿åfalse if (this.size() > otherSet.size()) { return false; } else { var values = this.values(); for (var i = 0; i < values.length; i++) { // 妿å½åéåçå¼otherSetä¸å å«ï¼å䏿¯åé if (!otherSet.has(values[i])) { return false } } return true; } } }