From 3cc813d016c70adb0615bc89e29cc6991993782d Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sat, 7 Dec 2019 11:53:22 +0530 Subject: [PATCH 1/4] update: used fill() to fill array with null --- src/_DataStructures_/HashTable/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/_DataStructures_/HashTable/index.js b/src/_DataStructures_/HashTable/index.js index 0b1f543d..4cd2efd7 100644 --- a/src/_DataStructures_/HashTable/index.js +++ b/src/_DataStructures_/HashTable/index.js @@ -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) { From c5e89e8aba986947e3f904b7feea46ea615e5706 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sat, 7 Dec 2019 12:08:37 +0530 Subject: [PATCH 2/4] update: initial Set implementation --- src/_DataStructures_/Set/index.js | 57 +++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/_DataStructures_/Set/index.js diff --git a/src/_DataStructures_/Set/index.js b/src/_DataStructures_/Set/index.js new file mode 100644 index 00000000..1bd6843d --- /dev/null +++ b/src/_DataStructures_/Set/index.js @@ -0,0 +1,57 @@ +// XSet because ES6 already has a Set class +class XSet { + constructor() { + this.data = {}; + } + + add(element) { + if (!this.data[element]) { + this.data[element] = true; + } + } + + remove(element) { + if (this.data[element]) { + delete this.data[element]; + } + } + + has(element) { + return !!this.data[element]; + } + + values() { + return Object.keys(this.data); + } + + union(givenSet) { + 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; + } +} + +// 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; From 92be475f3d67b0b22a225c11ce499bbdbdd7221b Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sat, 7 Dec 2019 12:09:21 +0530 Subject: [PATCH 3/4] update: entry in TOC --- TOC.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TOC.md b/TOC.md index 09a67ec7..65487787 100644 --- a/TOC.md +++ b/TOC.md @@ -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 From ec38f2e109dcfccb9c67fc4de9c29045ccf8ec43 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sat, 7 Dec 2019 14:45:37 +0530 Subject: [PATCH 4/4] update: hidden storage --- src/_DataStructures_/Set/index.js | 38 +++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/_DataStructures_/Set/index.js b/src/_DataStructures_/Set/index.js index 1bd6843d..3812c4e5 100644 --- a/src/_DataStructures_/Set/index.js +++ b/src/_DataStructures_/Set/index.js @@ -1,27 +1,23 @@ // XSet because ES6 already has a Set class class XSet { constructor() { - this.data = {}; + this.data = this.getStore(); } add(element) { - if (!this.data[element]) { - this.data[element] = true; - } + this.data.push(element); } remove(element) { - if (this.data[element]) { - delete this.data[element]; - } + this.data.pop(element); } has(element) { - return !!this.data[element]; + return this.data.contains(element); } values() { - return Object.keys(this.data); + return this.data.val(); } union(givenSet) { @@ -37,6 +33,30 @@ class XSet { 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();

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