{"id":20639209,"url":"https://github.com/alexdremov/treearray","last_synced_at":"2025-12-05T17:02:26.772Z","repository":{"id":146181959,"uuid":"587870655","full_name":"alexdremov/TreeArray","owner":"alexdremov","description":"Swift tree-based array implementation. Efficient for random insertions/deletions","archived":false,"fork":false,"pushed_at":"2023-01-17T22:11:48.000Z","size":1105,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-17T08:29:36.237Z","etag":null,"topics":["algorithms","array","efficiency","efficient-algorithm","ios","ios-app","ios-swift","optimization","optimization-algorithms","optimizations","swift","swift-collection","swift-package-manager","treap","tree-structure"],"latest_commit_sha":null,"homepage":"","language":"Swift","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/alexdremov.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":"2023-01-11T19:30:43.000Z","updated_at":"2024-11-02T17:58:48.000Z","dependencies_parsed_at":null,"dependency_job_id":"f1627f1b-2600-454d-9499-03361e035915","html_url":"https://github.com/alexdremov/TreeArray","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdremov%2FTreeArray","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdremov%2FTreeArray/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdremov%2FTreeArray/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexdremov%2FTreeArray/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexdremov","download_url":"https://codeload.github.com/alexdremov/TreeArray/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242667531,"owners_count":20166311,"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":["algorithms","array","efficiency","efficient-algorithm","ios","ios-app","ios-swift","optimization","optimization-algorithms","optimizations","swift","swift-collection","swift-package-manager","treap","tree-structure"],"created_at":"2024-11-16T15:22:59.752Z","updated_at":"2025-12-05T17:02:26.712Z","avatar_url":"https://github.com/alexdremov.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TreeArray\n\nSwift implementation of implicit treap. Data structure with efficient random insert / remove.\n\n## Usage\n\nTreeArray **behaves like a usual array** but has a different implementation under the hood that allows some operations to work faster. You can just replace `Array` with `TreeArray` and it should work as expected.\n\n```swift\nimport TreeArray\n\nvar foo: TreeArray = [1, 2, 3, 4, 5]\nfoo.insert(0, at: 0)\nprint(foo) // [0, 1, 2, 3, 4, 5]\n```\n\n## Complexity\n\nAccording to performance tests, the visible difference starts to appear around 16k elements. After that, `TreeArray` outperform `Array` and `Deque` on random insertions and deletions.\n\n| **Operation**                | **Complexity** | **Complexity Array** |\n|------------------------------|----------------|----------------------|\n| append                       | `O(log n)`     | `O(1)`               |\n| subscript                    | `O(log n)`     | `O(1)`               |\n| random insert                | `O(log n)`     | `O(n)`               |\n| random delete                | `O(log n)`     | `O(n)`               |\n| iteration (iterator)         | `O(n)`         | `O(n)`               |\n| iteration (subscript)        | `O(n * log n)` | `O(n)`               |\n| build from array             | `O(n)`         | `O(n)`               |\n| build from unknown-sized seq | `O(n * log n)` | `O(n)`               |\n| reverse                      | `O(n)`         | `O(n)`               |\n| contains                     | `O(n)`         | `O(n)`               |\n| append array                 | `O(m + log n)` | `O(m)`               |\n| insert array                 | `O(m + log n)` | `O(m + n)`           |\n\n## Comparison\n\nThe data structure is built upon a self-balancing random search tree. It allows performing random insertions and deletions efficiently with the cost of worse performance on other operations. In the directory [Benchmarks/results](Benchmarks/results) you can explore full comparison results. Here, I will note only the most important cases.\n\n### Random insertions\n![](Benchmarks/results/Results/17%20random%20insertions.svg)\n\n### Random removals\n\n![](Benchmarks/results/Results/21%20random%20removals.svg)\n\n### Prepend\n\n![](Benchmarks/results/Results/13%20prepend.svg)\n\n### Remove first\n\n![](Benchmarks/results/Results/20%20removeFirst.svg)\n\n\u003chr\u003e\n\nHowever, on other tests, it works worse. For example, iteration or build.\n\n![](Benchmarks/results/Results/02%20init%20from%20unsafe%20buffer.svg)\n![](Benchmarks/results/Results/03%20sequential%20iteration.svg)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexdremov%2Ftreearray","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexdremov%2Ftreearray","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexdremov%2Ftreearray/lists"}