Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement umd #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 99 additions & 87 deletions hashset.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
* HashSet
*
* This is a JavaScript implementation of HashSet, similar in concept to those found in Java or C#'s standard libraries.
* It is distributed as part of jshashtable and depends on jshashtable.js. It creates a single constructor function
* called HashSet in the global scope.
* It is distributed as part of jshashtable and depends on jshashtable.js. It exports a single constructor function
* called HashSet.
*
* Depends on: jshashtable.js
* Author: Tim Down <[email protected]>
Expand All @@ -28,92 +28,104 @@
* Website: http://www.timdown.co.uk/jshashtable/
*/

function HashSet(param1, param2) {
var hashTable = new Hashtable(param1, param2);

this.add = function(o) {
hashTable.put(o, true);
};

this.addAll = function(arr) {
for (var i = 0, len = arr.length; i < len; ++i) {
hashTable.put(arr[i], true);
}
};

this.values = function() {
return hashTable.keys();
};

this.remove = function(o) {
return hashTable.remove(o) ? o : null;
};

this.contains = function(o) {
return hashTable.containsKey(o);
};

this.clear = function() {
hashTable.clear();
};

this.size = function() {
return hashTable.size();
};

this.isEmpty = function() {
return hashTable.isEmpty();
};

this.clone = function() {
var h = new HashSet(param1, param2);
h.addAll(hashTable.keys());
return h;
};

this.intersection = function(hashSet) {
var intersection = new HashSet(param1, param2);
var values = hashSet.values(), i = values.length, val;
while (i--) {
val = values[i];
if (hashTable.containsKey(val)) {
intersection.add(val);
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['./Hashtable'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(require('./Hashtable'));
} else {
root.HashSet = factory(root.Hashtable);
}
}(this, function(Hashtable) {
function HashSet(param1, param2) {
var hashTable = new Hashtable(param1, param2);

this.add = function(o) {
hashTable.put(o, true);
};

this.addAll = function(arr) {
for (var i = 0, len = arr.length; i < len; ++i) {
hashTable.put(arr[i], true);
}
}
return intersection;
};

this.union = function(hashSet) {
var union = this.clone();
var values = hashSet.values(), i = values.length, val;
while (i--) {
val = values[i];
if (!hashTable.containsKey(val)) {
union.add(val);
};

this.values = function() {
return hashTable.keys();
};

this.remove = function(o) {
return hashTable.remove(o) ? o : null;
};

this.contains = function(o) {
return hashTable.containsKey(o);
};

this.clear = function() {
hashTable.clear();
};

this.size = function() {
return hashTable.size();
};

this.isEmpty = function() {
return hashTable.isEmpty();
};

this.clone = function() {
var h = new HashSet(param1, param2);
h.addAll(hashTable.keys());
return h;
};

this.intersection = function(hashSet) {
var intersection = new HashSet(param1, param2);
var values = hashSet.values(), i = values.length, val;
while (i--) {
val = values[i];
if (hashTable.containsKey(val)) {
intersection.add(val);
}
}
}
return union;
};

this.isSubsetOf = function(hashSet) {
var values = hashTable.keys(), i = values.length;
while (i--) {
if (!hashSet.contains(values[i])) {
return false;
return intersection;
};

this.union = function(hashSet) {
var union = this.clone();
var values = hashSet.values(), i = values.length, val;
while (i--) {
val = values[i];
if (!hashTable.containsKey(val)) {
union.add(val);
}
}
}
return true;
};

this.complement = function(hashSet) {
var complement = new HashSet(param1, param2);
var values = this.values(), i = values.length, val;
while (i--) {
val = values[i];
if (!hashSet.contains(val)) {
complement.add(val);
return union;
};

this.isSubsetOf = function(hashSet) {
var values = hashTable.keys(), i = values.length;
while (i--) {
if (!hashSet.contains(values[i])) {
return false;
}
}
}
return complement;
};
}
return true;
};

this.complement = function(hashSet) {
var complement = new HashSet(param1, param2);
var values = this.values(), i = values.length, val;
while (i--) {
val = values[i];
if (!hashSet.contains(val)) {
complement.add(val);
}
}
return complement;
};
}

return HashSet;
} ) );
Loading