the-forest/client/node_modules/.cache/babel-loader/bec9e31001c52b78c5dd970a34248668b9f4ef97b3288f7357a787da7c0fb951.json

1 line
5.0 KiB
JSON
Raw Normal View History

2024-09-17 20:35:18 -04:00
{"ast":null,"code":"import { InternalError } from \"./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).\nexport class 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 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}","map":{"version":3,"names":["InternalError","IdAlloc","usedIds","freeIds","constructor","Set","alloc","freeId","delete","add","has","size","free","id"],"sources":["/Users/shoofle/Projects/the-forest/node_modules/@libsql/hrana-client/lib-esm/id_alloc.js"],"sourcesContent":["import { InternalError } from \"./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).\nexport class 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 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}\n"],"mappings":"AAAA,SAASA,aAAa,QAAQ,aAAa;AAC3C;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,OAAO,CAAC;EACjB;EACA,CAACC,OAAO;EACR;EACA,CAACC,OAAO;EACRC,WAAWA,CAAA,EAAG;IACV,IAAI,CAAC,CAACF,OAAO,GAAG,IAAIG,GAAG,CAAC,CAAC;IACzB