iterable-hash-table
Advanced tools
Comparing version 1.2.0 to 2.0.0
{ | ||
"name": "iterable-hash-table", | ||
"version": "1.2.0", | ||
"description": "A simple Iterable Hash Table", | ||
"version": "2.0.0", | ||
"description": "A simple Iterable Hash Table written in TypeScript", | ||
"main": "public/HashTable.js", | ||
"scripts": { | ||
"postinstall": "tsc src/HashTable.ts -t ESNext --outDir public", | ||
"compile": "tsc src/HashTable.ts -t ESNext --outDir public -w", | ||
"postinstall": "tsc", | ||
"compile": "tsc", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
@@ -10,0 +10,0 @@ }, |
'use strict'; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __values = (this && this.__values) || function(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
/** | ||
@@ -10,4 +64,5 @@ * A dictionary/hash map data structure for storing key/value pairs. Finding | ||
*/ | ||
class HashTable { | ||
constructor(s = 11) { | ||
var HashTable = /** @class */ (function () { | ||
function HashTable(s) { | ||
if (s === void 0) { s = 11; } | ||
this._buckets = new Array(s); | ||
@@ -24,17 +79,53 @@ this._numOfBuckets = 0; | ||
*/ | ||
*[Symbol.iterator]() { | ||
// Loop over the buckets | ||
for (let i = 0; i < this._buckets.length; i++) { | ||
// If the bucket exists | ||
if (this._buckets[i] !== undefined) { | ||
// Loop over the map | ||
for (let tuple of this._buckets[i]) { | ||
HashTable.prototype[Symbol.iterator] = function () { | ||
var i, _a, _b, tuple, e_1_1; | ||
var e_1, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
i = 0; | ||
_d.label = 1; | ||
case 1: | ||
if (!(i < this._buckets.length)) return [3 /*break*/, 11]; | ||
if (!(this._buckets[i] !== undefined)) return [3 /*break*/, 9]; | ||
_d.label = 2; | ||
case 2: | ||
_d.trys.push([2, 7, 8, 9]); | ||
_a = (e_1 = void 0, __values(this._buckets[i])), _b = _a.next(); | ||
_d.label = 3; | ||
case 3: | ||
if (!!_b.done) return [3 /*break*/, 6]; | ||
tuple = _b.value; | ||
// Yield each tuple in the map | ||
yield tuple; | ||
} | ||
return [4 /*yield*/, tuple]; | ||
case 4: | ||
// Yield each tuple in the map | ||
_d.sent(); | ||
_d.label = 5; | ||
case 5: | ||
_b = _a.next(); | ||
return [3 /*break*/, 3]; | ||
case 6: return [3 /*break*/, 9]; | ||
case 7: | ||
e_1_1 = _d.sent(); | ||
e_1 = { error: e_1_1 }; | ||
return [3 /*break*/, 9]; | ||
case 8: | ||
try { | ||
if (_b && !_b.done && (_c = _a.return)) _c.call(_a); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
return [7 /*endfinally*/]; | ||
case 9: | ||
; | ||
_d.label = 10; | ||
case 10: | ||
i++; | ||
return [3 /*break*/, 1]; | ||
case 11: | ||
; | ||
return [2 /*return*/]; | ||
} | ||
; | ||
} | ||
; | ||
} | ||
}); | ||
}; | ||
; | ||
@@ -52,3 +143,3 @@ /** | ||
*/ | ||
hash(key) { | ||
HashTable.prototype.hash = function (key) { | ||
// Does not allow keys or values to be undefined or have a length of 0 | ||
@@ -59,4 +150,4 @@ if (key === undefined || key.length === 0) { | ||
; | ||
let hash = 0; | ||
for (let i = 0; i < key.length; i++) { | ||
var hash = 0; | ||
for (var i = 0; i < key.length; i++) { | ||
// hash << 5 transalets to: hash * (2 ** 5) | ||
@@ -71,3 +162,3 @@ hash = (hash << 5) - hash + key.toString().charCodeAt(i); | ||
return Math.abs(hash % this._size); | ||
} | ||
}; | ||
; | ||
@@ -84,3 +175,3 @@ /** | ||
*/ | ||
set(key, value) { | ||
HashTable.prototype.set = function (key, value) { | ||
// Does not allow keys or values to be undefined or have a length of 0 | ||
@@ -91,3 +182,3 @@ if (key === undefined || key.length === 0 || value === undefined || value.length === 0) { | ||
; | ||
let index = this.hash(key); | ||
var index = this.hash(key); | ||
// If the bucket is empty at this index | ||
@@ -113,3 +204,3 @@ if (!this._buckets[index]) { | ||
return index; | ||
} | ||
}; | ||
; | ||
@@ -126,3 +217,3 @@ /** | ||
*/ | ||
get(key) { | ||
HashTable.prototype.get = function (key) { | ||
// Does not allow keys or values to be undefined or have a length of 0 | ||
@@ -133,3 +224,3 @@ if (key === undefined || key.length === 0) { | ||
; | ||
let index = this.hash(key); | ||
var index = this.hash(key); | ||
// If there is no bucket | ||
@@ -147,3 +238,3 @@ if (!this._buckets[index]) { | ||
return this._buckets[index].get(key); | ||
} | ||
}; | ||
; | ||
@@ -159,3 +250,3 @@ /** | ||
*/ | ||
delete(key) { | ||
HashTable.prototype.delete = function (key) { | ||
// Does not allow keys or values to be undefined or have a length of 0 | ||
@@ -166,3 +257,3 @@ if (key === undefined || key.length === 0) { | ||
; | ||
let index = this.hash(key); | ||
var index = this.hash(key); | ||
// If there is no bucket at the given key | ||
@@ -197,3 +288,3 @@ if (!this._buckets[index]) { | ||
return false; | ||
} | ||
}; | ||
; | ||
@@ -208,6 +299,7 @@ /** | ||
*/ | ||
resize(sizeType) { | ||
HashTable.prototype.resize = function (sizeType) { | ||
var e_2, _a; | ||
// Helper function used to check if a number is Prime | ||
const isPrime = num => { | ||
for (let i = 2, s = Math.sqrt(num); i <= s; i++) | ||
var isPrime = function (num) { | ||
for (var i = 2, s = Math.sqrt(num); i <= s; i++) | ||
if (num % i === 0) | ||
@@ -227,3 +319,3 @@ return false; | ||
; | ||
let newSize = 0; | ||
var newSize = 0; | ||
// If the hashTable needs to increase in size | ||
@@ -254,3 +346,3 @@ if (sizeType === 'increase') { | ||
// Create a copy of the current buckets array | ||
let oldBucketsArray = this._buckets.slice(); | ||
var oldBucketsArray = this._buckets.slice(); | ||
// Replace the current buckets array with the new array and reset the length and numOfBuckets | ||
@@ -261,10 +353,20 @@ this._buckets = new Array(newSize); | ||
// Loop over the old buckets array | ||
for (let i = 0, len = oldBucketsArray.length; i < len; i++) { | ||
for (var i = 0, len = oldBucketsArray.length; i < len; i++) { | ||
// If the bucket exists | ||
if (oldBucketsArray[i] !== undefined) { | ||
// Loop over the map | ||
for (let [k, v] of oldBucketsArray[i]) { | ||
// Add all of the key/value pairs from the old buckets array to the new array | ||
this.set(k, v); | ||
try { | ||
// Loop over the map | ||
for (var _b = (e_2 = void 0, __values(oldBucketsArray[i])), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var _d = __read(_c.value, 2), k = _d[0], v = _d[1]; | ||
// Add all of the key/value pairs from the old buckets array to the new array | ||
this.set(k, v); | ||
} | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
; | ||
@@ -275,6 +377,7 @@ } | ||
; | ||
} | ||
}; | ||
; | ||
} | ||
return HashTable; | ||
}()); | ||
; | ||
module.exports = HashTable; |
@@ -11,3 +11,3 @@ 'use strict'; | ||
class HashTable { | ||
private _buckets: Array<Map<any, any>> | ||
private _buckets: Array<any> | ||
private _numOfBuckets: number | ||
@@ -14,0 +14,0 @@ private _size: number |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
25446
4
612
0