@gabrielrufino/cube
Version:
Data structures made in Typescript
104 lines (103 loc) • 4.22 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var LinkedList_1 = __importDefault(require("../LinkedList"));
var HashTableSeparateChainingElement_1 = __importDefault(require("./HashTableSeparateChainingElement"));
var HashTableSeparateChaining = /** @class */ (function () {
function HashTableSeparateChaining(inputs, _a) {
if (inputs === void 0) { inputs = {}; }
var _b = _a === void 0 ? { maxSize: 100 } : _a, maxSize = _b.maxSize;
this._data = [];
this.maxSize = maxSize;
for (var _i = 0, _c = Object.entries(inputs); _i < _c.length; _i++) {
var _d = _c[_i], key = _d[0], value = _d[1];
this.put(key, value);
}
}
Object.defineProperty(HashTableSeparateChaining.prototype, "data", {
get: function () {
return this._data
.map(function (value, index) { return ({ index: index, value: value }); })
.reduce(function (accumulator, current) {
var _a;
return (__assign(__assign({}, accumulator), (_a = {}, _a[current.index] = current.value, _a)));
}, {});
},
enumerable: false,
configurable: true
});
Object.defineProperty(HashTableSeparateChaining.prototype, "size", {
get: function () {
return Reflect.ownKeys(this.data).length;
},
enumerable: false,
configurable: true
});
HashTableSeparateChaining.prototype.put = function (key, value) {
var linkedList = this._getLinkedListByKey(key);
var element = new HashTableSeparateChainingElement_1.default(key, value);
if (linkedList) {
linkedList.push(element);
}
else {
var position = this._hashCode(key);
this._data[position] = new LinkedList_1.default(element);
}
return value;
};
HashTableSeparateChaining.prototype.get = function (key) {
var linkedList = this._getLinkedListByKey(key);
if (linkedList) {
var element = linkedList.data.find(function (element) { return element.value.key === key; });
return element.value;
}
return null;
};
HashTableSeparateChaining.prototype.remove = function (key) {
var linkedList = this._getLinkedListByKey(key);
if (linkedList) {
var index = linkedList.data.findIndex(function (element) { return element.value.key === key; });
var element = linkedList.removeFromPosition(index);
if (linkedList.isEmpty) {
var position = this._hashCode(key);
Reflect.deleteProperty(this._data, position);
}
return element || null;
}
return null;
};
HashTableSeparateChaining.prototype._getLinkedListByKey = function (key) {
var position = this._hashCode(key);
return Reflect.get(this.data, position) || null;
};
HashTableSeparateChaining.prototype._hashCode = function (key) {
var code = key
.split('')
.map(function (character) { return character.charCodeAt(0); })
.reduce(function (previous, current) { return previous + current; }, 0);
return code % this.maxSize;
};
HashTableSeparateChaining.prototype[Symbol.toPrimitive] = function (type) {
var primitives = {
default: true,
number: this.size,
string: "[ ".concat(Object.values(this.data).map(String).join(', '), " ]"),
};
return primitives[type];
};
return HashTableSeparateChaining;
}());
exports.default = HashTableSeparateChaining;