@gabrielrufino/cube
Version:
Data structures made in Typescript
115 lines (114 loc) • 4.56 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 HashTableLinearProbingElement_1 = __importDefault(require("./HashTableLinearProbingElement"));
var HashTableLinearProbing = /** @class */ (function () {
function HashTableLinearProbing(inputs, _a) {
if (inputs === void 0) { inputs = {}; }
var _b = _a === void 0 ? {} : _a, _c = _b.maxSize, maxSize = _c === void 0 ? 100 : _c;
this._maxSize = 0;
this._data = [];
this._maxSize = maxSize;
for (var _i = 0, _d = Object.entries(inputs); _i < _d.length; _i++) {
var _e = _d[_i], key = _e[0], value = _e[1];
this.put(key, value);
}
}
Object.defineProperty(HashTableLinearProbing.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(HashTableLinearProbing.prototype, "size", {
get: function () {
return Reflect.ownKeys(this.data).length;
},
enumerable: false,
configurable: true
});
HashTableLinearProbing.prototype.put = function (key, value) {
if (this.size === this._maxSize) {
return null;
}
var element = new HashTableLinearProbingElement_1.default(key, value);
var position = this._hashCode(key);
if (this._data[position]) {
do {
position = this._nextPositionOf(position);
} while (this._data[position]);
}
this._data[position] = element;
return value;
};
HashTableLinearProbing.prototype.get = function (key) {
var _a;
var position = this._hashCode(key);
var i = position;
do {
if (((_a = this._data[i]) === null || _a === void 0 ? void 0 : _a.key) === key) {
return this._data[i].value;
}
i = this._nextPositionOf(i);
} while (this._data[i] && i !== position);
return null;
};
HashTableLinearProbing.prototype.remove = function (key) {
if (this.get(key)) {
var position = this._hashCode(key);
while (this._data[position].key !== key) {
position = this._nextPositionOf(position);
}
var value = this._data[position].value;
Reflect.deleteProperty(this._data, position);
var i = void 0;
for (i = position; this._data[this._nextPositionOf(i)]
&& this._hashCode(this._data[this._nextPositionOf(i)].key) === this._hashCode(key)
&& this._nextPositionOf(i) !== position; i = this._nextPositionOf(i)) {
this._data[i] = this._data[this._nextPositionOf(i)];
}
Reflect.deleteProperty(this._data, i);
return value;
}
return null;
};
HashTableLinearProbing.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;
};
HashTableLinearProbing.prototype._nextPositionOf = function (position) {
return (position + 1) % this._maxSize;
};
HashTableLinearProbing.prototype[Symbol.toPrimitive] = function (type) {
var primitives = {
default: true,
number: this.size,
string: "[ ".concat(Object.values(this.data).join(', '), " ]"),
};
return primitives[type];
};
return HashTableLinearProbing;
}());
exports.default = HashTableLinearProbing;