Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Sets #161

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

Merged
ashokdey merged 4 commits into master from sets
Apr 26, 2020
Merged

Sets #161

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
1 change: 1 addition & 0 deletions TOC.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
- [K Largest Elements](src/_DataStructures_/Heaps/k-largest-in-array)
- [K Smallest Elements](src/_DataStructures_/Heaps/k-smallest-in-array)
- [Hash Table](src/_DataStructures_/HashTable)
- [Set](src/_DataStructures_/Set)

### Logical Problems

Expand Down
6 changes: 3 additions & 3 deletions src/_DataStructures_/HashTable/index.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class HashTable {
this.threshold = 0.7;
// the main bucket
this.bucket = new Array(this.slot);
// fill the bucket with null
// for (let i = 0; i < this.slot; i += 1) this.bucket[i] = null;
this.bucket.fill(null);
this.allowResize = allowResize;
this.strongHash = strongHash;
if (custonHash) {
// eslint-disable-next-line no-underscore-dangle
this._hash = custonHash;
}

// fill the bucket with null
for (let i = 0; i < this.slot; i += 1) this.bucket[i] = null;
}

_hash(key) {
Expand Down
77 changes: 77 additions & 0 deletions src/_DataStructures_/Set/index.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// XSet because ES6 already has a Set class
class XSet {
constructor() {
this.data = this.getStore();
}

add(element) {
this.data.push(element);
}

remove(element) {
this.data.pop(element);
}

has(element) {
return this.data.contains(element);
}

values() {
return this.data.val();
}

union(givenSet) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If givenSet is not instanceOf XSet. Then it should throw error.

ashokdey reacted with thumbs up emoji
const result = new Set();
const firstSetValues = this.values();
const givenSetValues = givenSet.values();

// eslint-disable-next-line no-restricted-syntax
for (const e of firstSetValues) result.add(e);

// eslint-disable-next-line no-restricted-syntax
for (const e of givenSetValues) result.add(e);

return result;
}

// eslint-disable-next-line class-methods-use-this
getStore() {
const store = {};

return {
push(el) {
if (!store[el]) {
store[el] = true;
}
},
pop(el) {
if (store[el]) {
delete store[el];
}
},
contains(el) {
return !!store[el];
},
val() {
return Object.keys(store);
},
};
}
}

// const s = new XSet();

// s.add(10);
// s.add(20);
// s.add(90);

// console.log(s.has(1));
// console.log(s.has(10));
// console.log(s.has(90));

// console.log(s.values());
// s.remove(90);
// console.log(s.has(90));
// console.log(s.data);

module.exports = XSet;

AltStyle によって変換されたページ (->オリジナル) /