1
+ <!DOCTYPE html>
2
+ < html >
3
+ < head >
4
+ < meta charset ="utf-8 ">
5
+ < title > Set</ title >
6
+ < script type ="text/javascript ">
7
+ function SetCollection ( ) {
8
+ var items = { } ;
9
+
10
+ //判断集合中是否有该值,有返回true,反之返回false
11
+ this . has = function ( value ) {
12
+ return items . hasOwnProperty ( value ) ; //return value in items;
13
+ } ;
14
+
15
+ //向集合添加新值
16
+ this . add = function ( value ) {
17
+ if ( ! this . has ( value ) ) {
18
+ items [ value ] = value ;
19
+ return true ;
20
+ }
21
+ return false ;
22
+ } ;
23
+
24
+ //从集合移除一个值
25
+ this . remove = function ( value ) {
26
+ if ( this . has ( value ) ) {
27
+ delete items [ value ] ;
28
+ return true ;
29
+ }
30
+ return false ;
31
+ } ;
32
+
33
+ //移除集合中所有的值
34
+ this . clear = function ( ) {
35
+ items = { } ;
36
+ } ;
37
+
38
+ //返回集合包含的元素数量
39
+ this . size = function ( ) {
40
+ var count = 0 ;
41
+ for ( var prop in items ) {
42
+ if ( items . hasOwnProperty ( prop ) ) {
43
+ ++ count ;
44
+ }
45
+ }
46
+ return count ;
47
+ //return Object.keys(items).length;简洁的方法返回集合长度
48
+ } ;
49
+
50
+ //返回集合中包含所有值的数组
51
+ this . values = function ( ) {
52
+ var keys = [ ] ;
53
+ for ( var key in items ) {
54
+ keys . push ( key ) ;
55
+ }
56
+ return keys ;
57
+ //return Object.keys(items);简洁方法返回集合中所有数值
58
+ } ;
59
+
60
+ //集合并集
61
+ this . union = function ( otherSet ) {
62
+ var unionSet = new SetCollection ( ) ;
63
+
64
+ var values = this . values ( ) ;
65
+ for ( var i = 0 ; i < values . length ; i ++ ) {
66
+ unionSet . add ( values [ i ] ) ;
67
+ }
68
+
69
+ var values = otherSet . values ( ) ;
70
+ for ( var i = 0 ; i < values . length ; i ++ ) {
71
+ unionSet . add ( values [ i ] ) ;
72
+ }
73
+
74
+ return unionSet ;
75
+ } ;
76
+
77
+ //集合交集
78
+ this . intersection = function ( otherSet ) {
79
+ var intersectionSet = new SetCollection ( ) ;
80
+
81
+ var values = this . values ( ) ;
82
+ for ( var i = 0 ; i < values . length ; i ++ ) {
83
+ if ( otherSet . has ( values [ i ] ) ) {
84
+ intersectionSet . add ( values [ i ] ) ;
85
+ }
86
+ }
87
+
88
+ return intersectionSet ;
89
+ } ;
90
+
91
+ //集合差集
92
+ this . difference = function ( otherSet ) {
93
+ var differenceSet = new SetCollection ( ) ;
94
+
95
+ var values = this . values ( ) ;
96
+ for ( var i = 0 ; i < values . length ; i ++ ) {
97
+ if ( ! otherSet . has ( values [ i ] ) ) {
98
+ differenceSet . add ( values [ i ] ) ;
99
+ }
100
+ }
101
+
102
+ return differenceSet ;
103
+ } ;
104
+
105
+ //集合的子集
106
+ this . subset = function ( otherSet ) {
107
+ if ( this . size ( ) > otherSet . size ( ) ) {
108
+ return false ;
109
+ } else {
110
+ var values = this . values ( ) ;
111
+ for ( var i = 0 ; i < values . length ; i ++ ) {
112
+ if ( ! otherSet . has ( values [ i ] ) ) {
113
+ return false ;
114
+ }
115
+ }
116
+ return true ;
117
+ }
118
+ } ;
119
+ }
120
+
121
+ var setA = new SetCollection ( ) ;
122
+ setA . add ( 1 ) ;
123
+ setA . add ( 2 ) ;
124
+ setA . add ( 3 ) ;
125
+ var setB = new SetCollection ( ) ;
126
+ setB . add ( 1 ) ;
127
+ setB . add ( 2 ) ;
128
+
129
+ console . log ( setB . subset ( setA ) )
130
+ </ script >
131
+ </ head >
132
+ < body >
133
+
134
+ </ body >
135
+ </ html >
0 commit comments