{"id":30739016,"url":"https://github.com/nlfiedler/segarray","last_synced_at":"2026-01-20T17:37:19.187Z","repository":{"id":309494420,"uuid":"1035689945","full_name":"nlfiedler/segarray","owner":"nlfiedler","description":"Segment Arrays in Rust","archived":false,"fork":false,"pushed_at":"2025-08-25T03:58:20.000Z","size":47,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-25T06:46:48.361Z","etag":null,"topics":["data-structures"],"latest_commit_sha":null,"homepage":"https://crates.io/crates/segment-array","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nlfiedler.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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,"zenodo":null}},"created_at":"2025-08-10T23:44:49.000Z","updated_at":"2025-08-25T03:58:22.000Z","dependencies_parsed_at":"2025-08-12T08:39:41.688Z","dependency_job_id":null,"html_url":"https://github.com/nlfiedler/segarray","commit_stats":null,"previous_names":["nlfiedler/segarray"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/nlfiedler/segarray","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nlfiedler%2Fsegarray","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nlfiedler%2Fsegarray/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nlfiedler%2Fsegarray/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nlfiedler%2Fsegarray/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nlfiedler","download_url":"https://codeload.github.com/nlfiedler/segarray/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nlfiedler%2Fsegarray/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273523641,"owners_count":25120864,"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","status":"online","status_checked_at":"2025-09-03T02:00:09.631Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["data-structures"],"created_at":"2025-09-03T22:43:48.062Z","updated_at":"2026-01-20T17:37:19.181Z","avatar_url":"https://github.com/nlfiedler.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Segment Array\n\nThis Rust [crate](https://crates.io/crates/segment-array) contains an implementation of a segment array (also known as a segmented list), as described in this [blog post](https://danielchasehooper.com/posts/segment_array/):\n\n\u003e A data structure with constant time indexing, stable pointers, and works well\n\u003e with arena allocators. ... The idea is straight forward: the structure\n\u003e contains a fixed sized array of pointers to segments. Each segment is twice\n\u003e the size of its predecessor. New segments are allocated as needed. ... Unlike\n\u003e standard arrays, pointers to a segment array’s items are always valid because\n\u003e items are never moved. Leaving items in place also means it never leaves\n\u003e \"holes\" of abandoned memory in arena allocators. The layout also allows us to\n\u003e access any index in constant time.\n\nThe functionality, memory layout, and performance of this implementation should be very similar to that of the C implementation.\n\nThe overhead of the bit-shifts and logarithm operations required for every push operation seems to outweigh the amortized O(1) of the basic geometrically growing `Vec` array. The main benefit of a segment array is that maybe it works well with arena memory allocators.\n\nThis data structure supports `push` and `pop` operations and does _not_ support inserts or removes at other locations within the array. One exception is the `swap/remove` operation which will retrieve a value from a specified index, overwrite that slot with the value at the end of the array, decrement the count, and return the retrieved value.\n\n* [Optimal Arrays (Brodnik et. al.)](https://github.com/nlfiedler/optarray)\n    - Memory overhead on the order of O(√N) and O(1) running time for most operations\n    - Markedly slower for repeated push operations due to the locate calculations\n* [Optimal Arrays (Tarjan and Zwick)](https://github.com/nlfiedler/tzarrays)\n    - O(rN^1/r) space overhead and similar O(1) running time for most operations\n    - Copying during grow/shrink adds significant time to overall performance\n* [Extensible Arrays](https://github.com/nlfiedler/extarray)\n    - Memory overhead on the order of O(√N) and O(1) running time for most operations\n    - Markedly slower for repeated push operations due to the locate calculations\n\n### Memory Usage\n\nThis data structure is meant to hold an unknown, though likely large, number of elements, otherwise `Vec` would be more appropriate. An empty array will have a hefty size of around 224 bytes. The Segment Array has the same growth factor as `Vec` and as such may potentially leave up to 50% of the allocated space unused. Unlike `Vec`, this implementation will deallocate segments as items are removed from the array.\n\nFor a resizable array that offers much better space efficiency, see the [nlfiedler/extarray](https://github.com/nlfiedler/extarray) repository for an implementation of **Space-Efficient Extensible Arrays** in Rust.\n\n## Examples\n\nA simple example copied from the unit tests.\n\n```rust\nlet inputs = [\n    \"one\", \"two\", \"three\", \"four\", \"five\", \"six\", \"seven\", \"eight\", \"nine\",\n];\nlet mut arr: SegmentArray\u003cString\u003e = SegmentArray::new();\nfor item in inputs {\n    arr.push(item.to_owned());\n}\nfor (idx, elem) in arr.iter().enumerate() {\n    assert_eq!(inputs[idx], elem);\n}\n```\n\n## Supported Rust Versions\n\nThe Rust edition is set to `2024` and hence version `1.85.0` is the minimum supported version.\n\n## Troubleshooting\n\n### Memory Leaks\n\nFinding memory leaks with [Address Sanitizer](https://clang.llvm.org/docs/AddressSanitizer.html) is fairly [easy](https://doc.rust-lang.org/beta/unstable-book/compiler-flags/sanitizer.html) and seems to work best on Linux. A Docker container with bash access and [Rust](https://rust-lang.org) nightly installed can be found in the `containers` directory.\n\n```shell\ncd containers\ndocker compose build --pull\ndocker compose run leaktest\nsh leak.sh\n```\n\nIf no problems were detected, a single line of output will appear. When done, clean up the docker artifacts.\n\n```shell\nexit\ndocker compose down\n```\n\n## Other Implementations\n\n* [rmeno12/segarray](https://github.com/rmeno12/segarray)\n    + Similar to the Zig implementation\n\n## Academic Research\n\nPublications related to the _dynamic array problem_ in order of publication:\n\n* \\[1\\]: [Resizable Arrays in Optimal Time and Space (1999)](https://www.semanticscholar.org/paper/Resizable-Arrays-in-Optimal-Time-and-Space-Brodnik-Carlsson/7843ee3731560aa81514be409a9ffc42749af289)\n    - Section 4 discusses the block sizing and segment/offset computation cost, similar to Segment Arrays.\n    - *Data Blocks* are slots and *Super Blocks* are segments.\n* \\[2\\]: [Experiences with the Design and Implementation of Space-Efficient Deques (2001)](https://www.semanticscholar.org/paper/Experiences-with-the-Design-and-Implementation-of-Katajainen-Mortensen/2346307bf5cc3b322ed38e6582cfb854723ebec5)\n* \\[3\\]: [Fast Dynamic Arrays (2017)](https://www.semanticscholar.org/paper/Fast-Dynamic-Arrays-Bille-Christiansen/4f01f5322ef6564d253039a3859ea20f858ac9ef)\n* \\[4\\]: [Immediate-Access Indexing Using Space-Efficient Extensible Arrays (2022)](https://www.semanticscholar.org/paper/Immediate-Access-Indexing-Using-Space-Efficient-Moffat/31e7dd2ee63efa92009035f4f04d9569ed3024c6)\n    - Segment Arrays are similar to the **SPACE-EFFICIENT EXTENSIBLE ARRAYS** described in this paper.\n    - Similar to **Singly Resizable Arrays** in [1] but doubles the number of slots and segments at the same time.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnlfiedler%2Fsegarray","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnlfiedler%2Fsegarray","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnlfiedler%2Fsegarray/lists"}