1 line
5.6 KiB
JSON
1 line
5.6 KiB
JSON
|
{"ast":null,"code":"\"use strict\";\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.IdAlloc = void 0;\nconst errors_js_1 = require(\"./errors.js\");\n// An allocator of non-negative integer ids.\n//\n// This clever data structure has these \"ideal\" properties:\n// - It consumes memory proportional to the number of used ids (which is optimal).\n// - All operations are O(1) time.\n// - The allocated ids are small (with a slight modification, we could always provide the smallest possible\n// id).\nclass IdAlloc {\n // Set of all allocated ids\n #usedIds;\n // Set of all free ids lower than `#usedIds.size`\n #freeIds;\n constructor() {\n this.#usedIds = new Set();\n this.#freeIds = new Set();\n }\n // Returns an id that was free, and marks it as used.\n alloc() {\n // this \"loop\" is just a way to pick an arbitrary element from the `#freeIds` set\n for (const freeId of this.#freeIds) {\n this.#freeIds.delete(freeId);\n this.#usedIds.add(freeId);\n // maintain the invariant of `#freeIds`\n if (!this.#usedIds.has(this.#usedIds.size - 1)) {\n this.#freeIds.add(this.#usedIds.size - 1);\n }\n return freeId;\n }\n // the `#freeIds` set is empty, so there are no free ids lower than `#usedIds.size`\n // this means that `#usedIds` is a set that contains all numbers from 0 to `#usedIds.size - 1`,\n // so `#usedIds.size` is free\n const freeId = this.#usedIds.size;\n this.#usedIds.add(freeId);\n return freeId;\n }\n free(id) {\n if (!this.#usedIds.delete(id)) {\n throw new errors_js_1.InternalError(\"Freeing an id that is not allocated\");\n }\n // maintain the invariant of `#freeIds`\n this.#freeIds.delete(this.#usedIds.size);\n if (id < this.#usedIds.size) {\n this.#freeIds.add(id);\n }\n }\n}\nexports.IdAlloc = IdAlloc;","map":{"version":3,"names":["Object","defineProperty","exports","value","IdAlloc","errors_js_1","require","usedIds","freeIds","constructor","Set","alloc","freeId","delete","add","has","size","free","id","InternalError"],"sources":["/Users/shoofle/Projects/the-forest/node_modules/@libsql/hrana-client/lib-cjs/id_alloc.js"],"sourcesContent":["\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.IdAlloc = void 0;\nconst errors_js_1 = require(\"./errors.js\");\n// An allocator of non-negative integer ids.\n//\n// This clever data structure has these \"ideal\" properties:\n// - It consumes memory proportional to the number of used ids (which is optimal).\n// - All operations are O(1) time.\n// - The allocated ids are small (with a slight modification, we could always provide the smallest possible\n// id).\nclass IdAlloc {\n // Set of all allocated ids\n #usedIds;\n // Set of all free ids lower than `#usedIds.size`\n #freeIds;\n constructor() {\n this.#usedIds = new Set();\n this.#freeIds = new Set();\n }\n // Returns an id that was free, and marks it as used.\n alloc() {\n // this \"loop\" is just a way to pick an arbitrary element from the `#freeIds` set\n for (const freeId of this.#freeIds) {\n this.#freeIds.delete(freeId);\n this.#usedIds.add(freeId);\n // maintain the invariant of `#freeIds`\n if (!this.#usedIds.has(this.#usedIds.size - 1)) {\n this.#freeIds.add(this.#usedIds.size - 1);\n }\n return freeId;\n }\n // the `#freeIds` set is empty, so there are no free ids lower than `#usedIds.size`\n // this means that `#usedIds` is a set that contains all numbers from 0 to `#usedIds.size - 1`,\n // so `#usedIds.size` is free\n const freeId = this.#usedIds.size;\n this.#usedIds.add(freeId);\n return freeId;\n }\n free(id) {\n if (!this.#usedIds.delete(id)) {\n throw new errors_js_1.InternalError(\"Freeing an id that is not allocated\");\n }\n // maintain the invariant of `#freeIds`\n this.#freeIds.delete(this.#usedIds.
|