aboutsummaryrefslogtreecommitdiff
path: root/src/js/fiveui/js/set.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/fiveui/js/set.js')
-rw-r--r--src/js/fiveui/js/set.js58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/js/fiveui/js/set.js b/src/js/fiveui/js/set.js
new file mode 100644
index 0000000..7015a3e
--- /dev/null
+++ b/src/js/fiveui/js/set.js
@@ -0,0 +1,58 @@
+
+
+(function() {
+
+Set = function() {
+ this.elems = {};
+};
+
+_.extend(Set.prototype, {
+
+ add: function(obj) {
+ var hash = this._getHash(obj);
+ if(this.elems[hash] == undefined) {
+ this.elems[hash] = obj;
+ }
+ },
+
+ remove: function(obj) {
+ var hash = this._getHash(obj);
+ if(this.elems[hash]) {
+ delete this.elems[hash];
+ }
+ },
+
+ member: function(obj) {
+ var hash = this._getHash(obj);
+ return !!this.elems[hash];
+ },
+
+ contains: function(obj) {
+ return this.member(obj);
+ },
+
+ size: function () {
+ return _.size(this.elems);
+ },
+
+ isEmpty: function() {
+ return this.size() == 0;
+ },
+
+ each: function(k, cxt) {
+ _.each(this.elems, k, cxt);
+ },
+
+ _getHash: function(obj) {
+ var str = obj.toString();
+
+ // the same hash function that java uses for String.hashCode
+ return _.reduce(str, function(hash, c) {
+ hash = ((hash << 5) - hash) + c.charCodeAt();
+ return hash & hash;
+ }, 0);
+ }
+
+});
+
+})();