1 line
5.0 KiB
JSON
1 line
5.0 KiB
JSON
{"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,IAAI,CAAC,CAACF,OAAO,GAAG,IAAIE,GAAG,CAAC,CAAC;EAC7B;EACA;EACAC,KAAKA,CAAA,EAAG;IACJ;IACA,KAAK,MAAMC,MAAM,IAAI,IAAI,CAAC,CAACJ,OAAO,EAAE;MAChC,IAAI,CAAC,CAACA,OAAO,CAACK,MAAM,CAACD,MAAM,CAAC;MAC5B,IAAI,CAAC,CAACL,OAAO,CAACO,GAAG,CAACF,MAAM,CAAC;MACzB;MACA,IAAI,CAAC,IAAI,CAAC,CAACL,OAAO,CAACQ,GAAG,CAAC,IAAI,CAAC,CAACR,OAAO,CAACS,IAAI,GAAG,CAAC,CAAC,EAAE;QAC5C,IAAI,CAAC,CAACR,OAAO,CAACM,GAAG,CAAC,IAAI,CAAC,CAACP,OAAO,CAACS,IAAI,GAAG,CAAC,CAAC;MAC7C;MACA,OAAOJ,MAAM;IACjB;IACA;IACA;IACA;IACA,MAAMA,MAAM,GAAG,IAAI,CAAC,CAACL,OAAO,CAACS,IAAI;IACjC,IAAI,CAAC,CAACT,OAAO,CAACO,GAAG,CAACF,MAAM,CAAC;IACzB,OAAOA,MAAM;EACjB;EACAK,IAAIA,CAACC,EAAE,EAAE;IACL,IAAI,CAAC,IAAI,CAAC,CAACX,OAAO,CAACM,MAAM,CAACK,EAAE,CAAC,EAAE;MAC3B,MAAM,IAAIb,aAAa,CAAC,qCAAqC,CAAC;IAClE;IACA;IACA,IAAI,CAAC,CAACG,OAAO,CAACK,MAAM,CAAC,IAAI,CAAC,CAACN,OAAO,CAACS,IAAI,CAAC;IACxC,IAAIE,EAAE,GAAG,IAAI,CAAC,CAACX,OAAO,CAACS,IAAI,EAAE;MACzB,IAAI,CAAC,CAACR,OAAO,CAACM,GAAG,CAACI,EAAE,CAAC;IACzB;EACJ;AACJ","ignoreList":[]},"metadata":{},"sourceType":"module","externalDependencies":[]} |