记录Javascript集合操作

  1   function Set() {
  2         var items = {};
  3         /**
  4          * 添加元素
  5          * @param {[type]} value [description]
  6          */
  7         this.add = function(value) {
  8             if (!this.has(value)) {
  9                 items[value] = value;
 10                 return true;
 11             }
 12             return false;
 13         };
 14         /**
 15          * 删除元素
 16          * @param  {[type]} value [description]
 17          * @return {[type]}       [description]
 18          */
 19         this.remove = function(value) {
 20             if (this.has(value)) {
 21                 delete items[value];
 22                 return true;
 23             }
 24             return false;
 25         };
 26         /**
 27          * 判断元素是否存在集合里
 28          * @param  {[type]}  value [description]
 29          * @return {Boolean}       [description]
 30          */
 31         this.has = function(value) {
 32             return items.hasOwnProperty(value);
 33         };
 34         /**
 35          * 清空集合
 36          * @return {[type]} [description]
 37          */
 38         this.clear = function() {
 39             items = {};
 40         };
 41         /**
 42          * 获取集合的长度
 43          * @return {[type]} [description]
 44          */
 45         this.size = function() {
 46             return Object.keys(items).length;
 47         };
 48         /**
 49          * 获取集合的长度(兼容IE8)
 50          * @return {[type]} [description]
 51          */
 52         this.sizeLegacy = function() {
 53             var count = 0;
 54             for (var prop in items) {
 55                 if (items.hasOwnProperty(prop))
 56                     ++count;
 57             }
 58             return count;
 59         };
 60         /**
 61          * 获取集合
 62          * @return {[type]} [description]
 63          */
 64         this.values = function() {
 65             return Object.keys(items);
 66         };
 67         /**
 68          * 获取集合(兼容IE8)
 69          * @return {[type]} [description]
 70          */
 71         this.valuesLegacy = function() {
 72             var keys = [];
 73             for (var key in items) {
 74                 keys.push(key);
 75             }
 76             return keys;
 77         };
 78         /**
 79          * 并集
 80          * @param  {[type]} otherSet [description]
 81          * @return {[type]}          [description]
 82          */
 83         this.union = function(otherSet) {
 84             var unionSet = new Set();
 85 
 86             var values = this.valuesLegacy();
 87             for (var i = 0; i < values.length; i++) {
 88                 unionSet.add(values[i]);
 89             }
 90 
 91             values = otherSet.valuesLegacy();
 92             for (var i = 0; i < values.length; i++) {
 93                 unionSet.add(values[i]);
 94             }
 95             return unionSet;
 96         };
 97         /**
 98          * 交集
 99          * @param  {[type]} otherSet [description]
100          * @return {[type]}          [description]
101          */
102         this.intersection = function(otherSet) {
103             var intersectionSet = new Set();
104 
105             var values = this.valuesLegacy();
106             for (var i = 0; i < values.length; i++) {
107                 if (otherSet.has(values[i])) {
108                     intersectionSet.add(values[i]);
109                 }
110             }
111 
112             return intersectionSet;
113         };
114         /**
115          * 差集
116          * @param  {[type]} otherSet [description]
117          * @return {[type]}          [description]
118          */
119         this.difference = function(otherSet) {
120             var differenceSet = new Set();
121 
122             var values = this.valuesLegacy();
123             for (var i = 0; i < values.length; i++) {
124                 if (!otherSet.has(values[i]))
125                     differenceSet.add(values[i]);
126             }
127             return differenceSet;
128         };
129         /**
130          * 子集
131          * @param  {[type]} otherSet [description]
132          * @return {[type]}          [description]
133          */
134         this.subset = function(otherSet) {
135             if (this.sizeLegacy() > otherSet.sizeLegacy()) {
136                 return false;
137             } else {
138                 var values = this.valuesLegacy();
139                 for (var i = 0; i < values.length; i++) {
140                     if (!otherSet.has(values[i])) {
141                         return false;
142                     }
143                 }
144                 return true;
145             }
146         }
147     }
148 
149     var set = new Set();
150     set.add(1);
151     set.add(2);
152     set.add(3);
153 
154     var set1 = new Set();
155     set1.add(3);
156     set1.add(4);
157     set1.add(5);
158 
159     var set2 = new Set();
160     set2.add(3);
161 
162     var unionSet = set.union(set1);
163     console.log(unionSet.valuesLegacy());
164 
165     var intersectionSet = set.intersection(set1);
166     console.log(intersectionSet.valuesLegacy());
167 
168     var differenceSet = set.difference(set1);
169     console.log(differenceSet.valuesLegacy());
170 
171     console.log(set.subset(set1));
172     console.log(set2.subset(set));

 

posted @ 2019-04-28 22:40  a3309548  阅读(288)  评论(0编辑  收藏  举报