UNPKG

fastiterationmap

Version:

an ES6 typescript Map that can be iterate like an Array

282 lines (275 loc) 9.61 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define([], factory); else { var a = factory(); for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i]; } })(typeof self !== 'undefined' ? self : this, function() { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports, __webpack_require__) { module.exports = __webpack_require__(1); /***/ }), /* 1 */ /***/ (function(module, exports, __webpack_require__) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var FastIterationMap = /** @class */ (function () { function FastIterationMap() { this._keys = new Map(); this._values = []; } FastIterationMap.prototype.clear = function () { this._keys.clear(); this._values = []; }; FastIterationMap.prototype.delete = function (key) { var i = this._keys.get(key); var r = this._keys.delete(key); this.offsetIndexInKeys(i, -1); var r2 = this._values.splice(i, 1); if (r2.length > 0 && r) { return true; } else { return false; } }; FastIterationMap.prototype.get = function (key) { return this._values[this._keys.get(key)]; }; /** * Return the index of an element in the value array * @param key */ FastIterationMap.prototype.getIndex = function (key) { return this._keys.get(key); }; FastIterationMap.prototype.has = function (key) { return this._keys.has(key); }; /** * Insert an item after another item * @param key the key of the item to insert * @param value the value of the item * @param keyRef the key of the item to insert after */ FastIterationMap.prototype.insertAfter = function (key, value, keyRef) { if (this._keys.get(key) !== undefined) { return false; } var i = this._keys.get(keyRef); if (i === undefined) { return false; } this.insertValue(i + 1, value); this.offsetIndexInKeys(i, 1); this._keys.set(key, i + 1); return true; }; /** * Insert 2 items around the another item * @param keyRef the key of the item insert around * @param firstK the key of the item to insert before * @param firstV the value of the item to insert before * @param secondK the key of the item to insert after * @param secondV the value of the item to insert after */ FastIterationMap.prototype.insertAround = function (keyRef, firstK, firstV, secondK, secondV) { if (this._keys.get(firstK) !== undefined || this._keys.get(secondK) !== undefined) { return false; } var index = this._keys.get(keyRef); if (index === undefined) { return false; } // insert the 2 items after the item of reference // offset index by 2 in the keys map of all element after the index of reference // in the keys map set index of the 2 new items // finally swap the item of reference with the first of the 2 items inserted this.insertValue(index + 1, firstV, secondV); this.offsetIndexInKeys(index, 2); this._keys.set(firstK, index + 1); this._keys.set(secondK, index + 2); return this.swap(keyRef, firstK); }; /** * Insert an item before another item * @param key the key of the item to insert * @param value the value of the item * @param keyRef the key of the item to insert before */ FastIterationMap.prototype.insertBefore = function (key, value, keyRef) { if (this._keys.get(key) !== undefined) { return false; } var i = this._keys.get(keyRef); if (i === undefined) { return false; } this.insertValue(i, value); this.offsetIndexInKeys(i - 1, 1); this._keys.set(key, i); return true; }; Object.defineProperty(FastIterationMap.prototype, "keys", { get: function () { return this._keys; }, enumerable: true, configurable: true }); Object.defineProperty(FastIterationMap.prototype, "length", { get: function () { return this._values.length; }, enumerable: true, configurable: true }); Object.defineProperty(FastIterationMap.prototype, "size", { get: function () { return this._values.length; }, enumerable: true, configurable: true }); Object.defineProperty(FastIterationMap.prototype, "values", { get: function () { return this._values; }, enumerable: true, configurable: true }); FastIterationMap.prototype.push = function (key, value) { var e = this._keys.get(key); // if the key doesn't exist add the element if (e === undefined) { var l = this._values.push(value); this._keys.set(key, l - 1); } else { // if the key is already there, update the value this._values[e] = value; } return this._values.length; }; FastIterationMap.prototype.set = function (key, value) { return this.push(key, value); }; /** * Swap position of 2 items in the values array and set the correct index in the keys Map * @param key1 * @param key2 */ FastIterationMap.prototype.swap = function (key1, key2) { var index1 = this._keys.get(key1); var index2 = this._keys.get(key2); if (index1 === undefined || index2 === undefined) { return false; } var tmp = this._values[index1]; this._values[index1] = this._values[index2]; this._values[index2] = tmp; this._keys.set(key1, index2); this._keys.set(key2, index1); return true; }; FastIterationMap.prototype.insertValue = function (index) { var values = []; for (var _i = 1; _i < arguments.length; _i++) { values[_i - 1] = arguments[_i]; } return (_a = this._values).splice.apply(_a, [index, 0].concat(values)); var _a; }; /** * Offset indices in the keys Map from a position ([from] and [to] not included) * @param from offset after this key * @param offsetVal the amount to offset indices * @param to if specidied offset until this key, otherwise offset to end of the collection */ FastIterationMap.prototype.offsetIndexInKeys = function (from, offsetVal, to) { var mapIter = this._keys.entries(); var l = this._keys.size; to = to || Number.MAX_VALUE; for (var i = 0; i < l; ++i) { var e = mapIter.next().value; if (e[1] > from && e[1] < to) { this._keys.set(e[0], e[1] += offsetVal); } } }; return FastIterationMap; }()); exports.FastIterationMap = FastIterationMap; /***/ }) /******/ ]); });