-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs-set.js
More file actions
157 lines (140 loc) · 3.7 KB
/
js-set.js
File metadata and controls
157 lines (140 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
*
* Copyright (c) 2014 Alejandro Perez Martin (AlePerez92)
*
* @name JavaScript Set (JS-Set)
* @description Data structure to store non-repeated values
* @version 1.0.2
* @build Jun 6, 2014
* @url https://github.com/AlejandroPerezMartin/javascript-set
*
* @author Alejandro Perez Martin
* @authorUrl https://www.linkedin.com/in/aleperez92
*
*/
(function (window, undefined) {
'use strict';
/**
* @constructor
*/
var Set = function Set() {
this.set = [];
};
/**
* Returns the set of objects as an array
*/
Set.prototype.getSet = function () {
return this.set;
};
/**
* Returns the number of items in the set
*/
Set.prototype.size = function () {
return this.set.length;
};
/**
* Returns the n-th object in set
*
* @param {Number} index
*/
Set.prototype.get = function (index) {
return this.set[index];
};
/**
* Returns true if the set is empty, otherwise false
*/
Set.prototype.isEmpty = function () {
return (this.set.length <= 0);
};
/**
* Empties set
*/
Set.prototype.clear = function () {
this.set = [];
return this;
};
/**
* Returns true if an element is in the set, otherwise false
*
* @param {Object} key
*/
Set.prototype.contains = function (key) {
return (key && this.set.indexOf(key) !== -1);
};
/**
* Returns true if all the elements of the array are contained in the set, otherwise false
*
* @param {Array} values
*/
Set.prototype.containsAll = function (values) {
if (values && values instanceof Array) {
for (var i = 0, len = values.length; i < len; i += 1) {
if (!this.contains(values[i])) {
return false;
}
}
return true;
}
return false;
};
/**
* Adds an element to the set
*
* @param {Object} key
*/
Set.prototype.add = function (key) {
if (key) {
if (!this.contains(key)) {
this.set.push(key);
}
}
return this;
};
/**
* Adds an array of objects to the set
*
* @param {Array} values
*/
Set.prototype.addAll = function (values) {
if (values && values instanceof Array) {
for (var i = 0, len = values.length; i < len; i += 1) {
if (!this.contains(values[i])) {
this.add(values[i]);
}
}
}
return this;
};
/**
* Removes an element from the set
*
* @param {Object} key
*/
Set.prototype.remove = function (key) {
if (key && this.contains(key)) {
var arr = [],
index = this.set.indexOf(key);
for (var i = 0, len = this.size(); i < len; i += 1) {
if (i !== index) {
arr.push(this.set[i]);
}
}
this.set = arr;
}
return this;
};
/**
* Removes all the elements of the set that are contained in the specified array
*
* @param {Array} values
*/
Set.prototype.removeAll = function (values) {
if (values && values instanceof Array) {
for (var i = 0, len = values.length; i < len; i += 1) {
this.remove(values[i]);
}
}
return this;
};
window.Set = Set;
})(window);