Skip to content

Commit

Permalink
add Set data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
knewbie committed Apr 17, 2017
1 parent 6045725 commit 7dbf64a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ export class Set<T> {
return tempSet;
}

public difference(s: Set<T>): Set<T> {
let tempSet = new Set<T>();
for(let e of this.dataStore) {
if(!s.contains(e)) {
tempSet.add(deepcopy(e));
}
}

return tempSet;
}

public subset(s: Set<T>): boolean {
if (this.size() > s.size()) {
return false;
Expand Down
9 changes: 9 additions & 0 deletions test_Set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@ print(s.size(), s.getSet())
let s1 = new Set<any>();
s1.init([{ a: 1, b: [2, 4] }, { d: 3 }, "test", 342, true]);

print("union op: s U s1")
print(s.union(s1).getSet());

print("intersect op: s & s1");
print(s.intersect(s1).getSet());


print("difference op: s D s1");
print(s.difference(s1).getSet());

let s2 = new Set<any>();
s2.init(["Tim", { d: 3 }]);

print("subset op: s2 S s");
print(s2.subset(s));

0 comments on commit 7dbf64a

Please sign in to comment.