{"id":21612456,"url":"https://github.com/altanis/generational_indices","last_synced_at":"2026-01-07T09:16:36.089Z","repository":{"id":251161237,"uuid":"836569190","full_name":"Altanis/generational_indices","owner":"Altanis","description":null,"archived":false,"fork":false,"pushed_at":"2024-08-01T06:29:13.000Z","size":2,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-19T13:53:04.675Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Altanis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-01T06:04:20.000Z","updated_at":"2024-08-01T06:41:56.000Z","dependencies_parsed_at":"2024-08-01T08:14:18.483Z","dependency_job_id":null,"html_url":"https://github.com/Altanis/generational_indices","commit_stats":null,"previous_names":["altanis/generational_indices"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Altanis%2Fgenerational_indices","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Altanis%2Fgenerational_indices/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Altanis%2Fgenerational_indices/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Altanis%2Fgenerational_indices/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Altanis","download_url":"https://codeload.github.com/Altanis/generational_indices/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246140587,"owners_count":20729802,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T21:20:40.217Z","updated_at":"2026-01-07T09:16:36.049Z","avatar_url":"https://github.com/Altanis.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Generational Indices\nA simple library which provides a generational index allocator.\n\n## Example Usage\n```js\n// If you're using TypeScript, you may also import `GenerationalIndex` as a type to describe an index.\n// type GenerationalIndex = { index: number, generation: number }\nconst { GenerationalAllocator } = require(\"generational_indices\");\nconst allocator = new GenerationalAllocator();\n\nconst id = allocator.allocate(); // { index: 0, generation: 0 }\nconst id2 = allocator.allocate(); // { index: 1, generation: 0 }\n\nallocator.free(id.index);\nallocator.is_valid(id); // false (it got freed)\nallocator.is_valid(id2); // true (its still active)\n\nconst id3 = allocator.allocate(); // { index: 0, generation: 1 }\n```\n\n## What are generational indices?\nGenerational indices are a way to reuse indices safely. More specifically, they are a way to reuse indices while avoiding the [ABA Problem](https://en.wikipedia.org/wiki/ABA_problem).\n\nTo summarise the ABA problem:\n- Assume you associate the index `1` with some data:\n```js\nconst data = {};\nconst id = allocator.generate_id(); // 1\ndata[id] = { angle: 0, position: 0 }\n```\n- Your program stores these ids as a reference in multiple places\n```js\n// for simplicity, it will be stored in one place\n// but it will likely be in multiple scattered places\nsocials[id] = { name: \"Altanis\", friends: [] }\n```\n- You destroy the data associated with `1`:\n```js\ndata[id] = undefined;\n```\n- You then create a new entity (and reuse the id `1`)\n```js\nconst id = allocator.generate_id(); // 1\ndata[id] = { angle: 0, position 0 } // this guy is actually Bob and has 500 friends\n```\n- When you lookup Bob's socials, you will see:\n```js\nconsole.log(socials[id]); // { name: \"Altanis\", friends: [] }\n```\n\nUh oh. Even though the `id` is meant for Bob, Altanis's data show's up. This is the ABA problem.\n\n## How do generational indices fix this?\nA generational index stores a generation alongside the index/id. When an index is reused, the generation is incremented. In our previous example, this would fix the issue as such:\n```js\nconst altanis_id = allocator.generate_id(); // { index: 1, generation: 0 }\nsocials[id]; // { name: \"Altanis\", friends: [] }\n\ndata[id] = undefined;\nallocator.free(id);\n\nconst bob_id = allocator.generate_id(); // { index: 1, generation: 1 }\nsocials[bob_id]; // { name: \"Bob\", friends: Array(500) }\nsocials[altanis_id]; // { name: \"Altanis\", friends: [] }\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faltanis%2Fgenerational_indices","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faltanis%2Fgenerational_indices","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faltanis%2Fgenerational_indices/lists"}