1
+ /**
2
+ * ECMSCRIPT 6 already have a Set class implementation:
3
+ * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
4
+ * We will try to copy the same functionalities
5
+ * @constructor
6
+ */
7
+ function Set ( ) {
8
+
9
+ var items = { } ;
10
+
11
+ this . add = function ( value ) {
12
+ if ( ! this . has ( value ) ) {
13
+ items [ value ] = value ;
14
+ return true ;
15
+ }
16
+ return false ;
17
+ } ;
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
+ this . has = function ( value ) {
28
+ return value in items ;
29
+ } ;
30
+
31
+ this . clear = function ( ) {
32
+ items = { } ;
33
+ } ;
34
+
35
+ /**
36
+ * Modern browsers function
37
+ * IE9+, FF4+, Chrome5+, Opera12+, Safari5+
38
+ * @returns {Number }
39
+ */
40
+ this . size = function ( ) {
41
+ return Object . keys ( items ) . length ;
42
+ } ;
43
+
44
+ /**
45
+ * cross browser compatibility - legacy browsers
46
+ * for modern browsers use size function
47
+ * @returns {number }
48
+ */
49
+ this . sizeLegacy = function ( ) {
50
+ var count = 0 ;
51
+ for ( var prop in items ) {
52
+ if ( items . hasOwnProperty ( prop ) )
53
+ ++ count ;
54
+ }
55
+ return count ;
56
+ } ;
57
+
58
+ /**
59
+ * Modern browsers function
60
+ * IE9+, FF4+, Chrome5+, Opera12+, Safari5+
61
+ * @returns {Array }
62
+ */
63
+ this . values = function ( ) {
64
+ return Object . keys ( items ) ;
65
+ } ;
66
+
67
+ this . valuesLegacy = function ( ) {
68
+ var keys = [ ] ;
69
+ for ( var key in items ) {
70
+ keys . push ( key ) ;
71
+ }
72
+ return keys ;
73
+ } ;
74
+
75
+ this . getItems = function ( ) {
76
+ return items ;
77
+ } ;
78
+
79
+ this . union = function ( otherSet ) {
80
+ var unionSet = new Set ( ) ; //{1}
81
+
82
+ var values = this . values ( ) ; //{2}
83
+ for ( var i = 0 ; i < values . length ; i ++ ) {
84
+ unionSet . add ( values [ i ] ) ;
85
+ }
86
+
87
+ values = otherSet . values ( ) ; //{3}
88
+ for ( var i = 0 ; i < values . length ; i ++ ) {
89
+ unionSet . add ( values [ i ] ) ;
90
+ }
91
+
92
+ return unionSet ;
93
+ } ;
94
+
95
+ this . intersection = function ( otherSet ) {
96
+ var insertectionSet = new Set ( ) ; //{1}
97
+
98
+ var values = this . values ( ) ;
99
+ for ( var i = 0 ; i < values . length ; i ++ ) { //{2}
100
+ if ( otherSet . has ( values [ i ] ) ) { //{3}
101
+ insertectionSet . add ( values [ i ] ) ; //{4}
102
+ }
103
+ }
104
+
105
+ return insertectionSet ;
106
+ } ;
107
+
108
+ this . difference = function ( otherSet ) {
109
+ var differenceSet = new Set ( ) ; //{1}
110
+
111
+ var values = this . values ( ) ;
112
+ for ( var i = 0 ; i < values . length ; i ++ ) { //{2}
113
+ if ( ! otherSet . has ( values [ i ] ) ) { //{3}
114
+ differenceSet . add ( values [ i ] ) ; //{4}
115
+ }
116
+ }
117
+
118
+ return differenceSet ;
119
+ } ;
120
+
121
+ this . subset = function ( otherSet ) {
122
+
123
+ if ( this . size ( ) > otherSet . size ( ) ) { //{1}
124
+ return false ;
125
+ } else {
126
+ var values = this . values ( ) ;
127
+ for ( var i = 0 ; i < values . length ; i ++ ) { //{2}
128
+ if ( ! otherSet . has ( values [ i ] ) ) { //{3}
129
+ return false ; //{4}
130
+ }
131
+ }
132
+ return true ;
133
+ }
134
+ } ;
135
+ }
0 commit comments