您的位置:首页 > 理论基础 > 数据结构算法

JavaScript数据结构与算法(八) 集合(ECMAScript 6中定义的类似的Set类)

2017-06-09 16:30 274 查看
TypeScript方式实现源码

// 特性:
// 1. 集合是由一组无序且唯一(即不能重复)的项组成的。这个数据结构使用了与有限集合相同的数学概念,但应用在计算机科学的数据结构中。
// 2. 也可以把集合想象成一个既没有重复元素,也没有顺序概念的数组
// 3. 在数学中,集合也有并集、交集、差集等基本操作。在这一章中我们也会介绍这些操作

// 集合操作
// 并集:对于给定的两个集合,返回一个包含两个集合中所有元素的新集合
// 并集的数学概念,集合A和B的并集,表示为A∪B,定义如下:
// A∪B = { x | x ∈ A∨x ∈ B }
// 意思是x(元素)存在于A中,或x存在于B中。下图展示了并集操作:
// 并集.png

// 交集:对于给定的两个集合,返回一个包含两个集合中共有元素的新集合
// 交集的数学概念,集合A和B的交集,表示为A∩B,定义如下:
// A∩B = { x | x ∈ A∧x ∈ B }
// 意思是x(元素)存在于A中,且x存在于B中。下图展示了交集操作:
// 交集.png

// 差集:对于给定的两个集合,返回一个包含所有存在与第一个集合且不存在与第二个集合的元素的新集合
// 差集的数学概念,集合A和B的差集,表示为AB,定义如下:
// AB = { x | x ∈ A ∧ x B }
// 意思是x(元素)存在于A中,且x不存在于B中。下图展示了集合A和B的差集操作:
// 差集.png

// 子集:验证一个给定集合是否是另一个集合的子集
// 最后一个集合操作是子集。子集的数学概念,集合A是B的子集(或集合B包含
// 了A) ,表示为A⊆B,定义如下:
// ∀x { x ∈ A → x ∈ B }
// 意思是集合A中的每一个x(元素) ,也需要存在于B中。下图展示了集合A是集合B的子集:
// 子集.png


1 var Set = (function () {
2     function Set() {
3         this.items = {};
4     }
5     /**
6      * 向集合添加一个新的项
7      * @param value
8      */
9     Set.prototype.add = function (value) {
10         if (!this.has(value)) {
11             this.items[value] = value;
12             return true;
13         }
14     };
15     /**
16      * 从集合移除一个值
17      * @param value
18      */
19     Set.prototype.remove = function (value) {
20         if (this.has(value)) {
21             delete this.items[value];
22             return true;
23         }
24         return false;
25     };
26     /**
27      * 如果值在集合中,返回true,否则返回false
28      * @param value
29      */
30     Set.prototype.has = function (value) {
31         return this.items.hasOwnProperty(value);
32     };
33     /**
34      * 移除集合中的所有项
35      */
36     Set.prototype.clear = function () {
37         this.items = {};
38     };
39     /**
40      * 返回集合所包含元素的数量。与数组的length属性类似
41      */
42     Set.prototype.size = function () {
43         var count = 0;
44         for (var prop in this.items) {
45             if (this.items.hasOwnProperty(prop))
46                 ++count; //{7}
47         }
48         return count;
49     };
50     /**
51      * 返回一个包含集合中所有值的数组
52      */
53     Set.prototype.values = function () {
54         var keys = [];
55         for (var key in this.items) {
56             keys.push(key); //{8}
57         }
58         return keys;
59     };
60     Set.prototype.union = function (otherSet) {
61         var unionSet = new Set();
62         var values = this.values();
63         for (var i_1 = 0; i_1 < values.length; i_1++) {
64             unionSet.add(values[i_1]);
65         }
66         values = otherSet.values();
67         for (var i_2 = 0; i_2 < values.length; i_2++) {
68             unionSet.add(values[i_2]);
69         }
70         return unionSet;
71     };
72     Set.prototype.intersection = function (otherSet) {
73         var intersectionSet = new Set();
74         var values = this.values();
75         for (var i_3 = 0; i_3 < values.length; i_3++) {
76             if (otherSet.has(values[i_3])) {
77                 intersectionSet.add(values[i_3]);
78             }
79         }
80         return intersectionSet;
81     };
82     Set.prototype.difference = function (otherSet) {
83         var differenceSet = new Set();
84         var values = this.values();
85         for (var i_4 = 0; i_4 < values.length; i_4++) {
86             if (!otherSet.has(values[i_4])) {
87                 differenceSet.add(values[i_4]);
88             }
89         }
90         return differenceSet;
91     };
92     Set.prototype.subset = function (otherSet) {
93         if (this.size() > otherSet.size()) {
94             return false;
95         }
96         else {
97             var values = this.values();
98             for (var i_5 = 0; i_5 < values.length; i_5++) {
99                 if (!otherSet.has(values[i_5])) {
100                     return false;
101                 }
102             }
103             return true;
104         }
105     };
106     return Set;
107 }());


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐