{"id":17462381,"url":"https://github.com/ben-ng/swift-range-tree","last_synced_at":"2025-06-17T03:06:34.886Z","repository":{"id":62452531,"uuid":"68468952","full_name":"ben-ng/swift-range-tree","owner":"ben-ng","description":"Orthogonal range searches in polylog time","archived":false,"fork":false,"pushed_at":"2016-09-24T18:44:44.000Z","size":44,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-02T11:57:29.384Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","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/ben-ng.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-17T18:02:01.000Z","updated_at":"2023-09-26T07:20:01.000Z","dependencies_parsed_at":"2022-11-01T23:46:12.382Z","dependency_job_id":null,"html_url":"https://github.com/ben-ng/swift-range-tree","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/ben-ng/swift-range-tree","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ben-ng%2Fswift-range-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ben-ng%2Fswift-range-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ben-ng%2Fswift-range-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ben-ng%2Fswift-range-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ben-ng","download_url":"https://codeload.github.com/ben-ng/swift-range-tree/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ben-ng%2Fswift-range-tree/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260281569,"owners_count":22985629,"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-10-18T08:43:26.133Z","updated_at":"2025-06-17T03:06:34.859Z","avatar_url":"https://github.com/ben-ng.png","language":"Swift","readme":"# RangeTree\n\n[![CI Status](http://img.shields.io/travis/ben-ng/swift-range-tree.svg?style=flat)](https://travis-ci.org/ben-ng/swift-range-tree)\n[![Version](https://img.shields.io/cocoapods/v/RangeTree.svg?style=flat)](http://cocoapods.org/pods/RangeTree)\n[![License](https://img.shields.io/cocoapods/l/RangeTree.svg?style=flat)](http://cocoapods.org/pods/RangeTree)\n[![Platform](https://img.shields.io/cocoapods/p/RangeTree.svg?style=flat)](http://cocoapods.org/pods/RangeTree)\n\nA range tree allows you to perform orthorgonal range searches in polylogarithmic time. i.e. If you had a bunch of points in two-dimensional space, and you want to quickly figure out what points have an x coordinate between a and b, and a y coordinate between c and d, this is the module for you.\n\nThis module implements an n-dimensional range tree. You define how many dimensions you need by implementing the `RangeTreePoint` protocol. Note that you will see a performance penalty versus a naive `Array.filter` if your dataset is too small. See the section on [performance](#performance) to gauge if your dataset is large enough to benefit from this data structure.\n\n## Usage\n\n1. Implement the `RangeTreePoint` protocol (it's already implemented on `Int`, `Double`, and `CGPoint` for you as part of this module)\n\n\t```swift\n\tpublic protocol RangeTreePoint {\n\t    associatedtype Position: Comparable\n\t    static var dimensions: Int { get}\n\t    func positionIn(dimension: Int) -\u003e Position\n\t}\n\n\t// Sample implementation\n\textension CGPoint: RangeTreePoint {\n\t    public typealias Position = CGFloat\n\t    public static var dimensions = 2\n\t    public func positionIn(dimension: Int) -\u003e Position {\n\t        return dimension == 0 ? self.x : self.y\n\t    }\n\t}\n\t```\n2. Construct a RangeTree\n\n\t```swift\n\tlet a = RangeTree\u003cDouble\u003e(values: [8, 1, 10, 4, 2, 7, 5, 3, 9, 6])\n\t```\n3. Query for a range\n\n\t```swift\n\ta.valuesInRange(rangePerDimension: (3.0, 6.0)) // =\u003e [3.0, 4.0, 5.0, 6.0]\n\t```\n4. Modify the RangeTree\n\n\t```swift\n\ta.insert(4.5)\n\ta.valuesInRange(rangePerDimension: (3.0, 6.0)) // =\u003e [3.0, 4.0, 4.5, 5.0, 6.0]\n\t```\n\n## Performance\n\nQuad-core 2.8 GHz i7 Retina MacBook Pro:\n\n * Constructing a tree\n \t* 1000 points: 0.133s\n \t* 10000 points: 1.104s\n \t* 100000 points: 8.420s\n \t* 1000000 points: 55.377s\n * Querying a tree\n \t* 1000 points: 0.001s (naive `Array.filter`: 0.000s) -- _slower_\n \t* 10000 points: 0.002s (naive `Array.filter`: 0.004s) -- 2x faster\n \t* 100000 points: 0.008s (naive `Array.filter`: 0.029s) - 3.6x faster\n \t* 1000000 points: 0.056s (naive `Array.filter`: 0.266s) - 4.8x faster\n\n1.3 GHz Core m7 12\" MacBook:\n\n * Constructing a tree\n \t* 1000 points: 0.206s\n \t* 10000 points: 1.323s\n \t* 100000 points: 10.413s\n \t* 1000000 points: 85.719s\n * Querying a tree\n \t* 1000 points: 0.002s (naive `Array.filter`: 0.001s) -- _slower_\n \t* 10000 points: 0.004s (naive `Array.filter`: 0.007s) -- 1.8x faster\n \t* 100000 points: 0.013s (naive `Array.filter`: 0.038s) -- 2.9x faster\n \t* 1000000 points: 0.089s (naive `Array.filter`: 0.436s) -- 4.9x faster\n\n## Example \u0026 Tests\n\nTo run the example project, clone the repo, and run `pod install` from the Example directory first.\n\n## Installation\n\nRangeTree is available through [CocoaPods](http://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod \"RangeTree\"\n```\n\n## Author\n\nBen Ng, me@benng.me\n\n## License\n\nRangeTree is available under the MIT license. See the LICENSE file for more info.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fben-ng%2Fswift-range-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fben-ng%2Fswift-range-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fben-ng%2Fswift-range-tree/lists"}