SwiftRangeable
May 10, 2026 · View on GitHub
Reference Swift implementation of Rangeable<Element> — a generic, integer-coordinate, closed-interval set container with first-insert ordered active queries.
Installation
Swift Package Manager
In your Package.swift:
dependencies: [
.package(url: "https://github.com/ZhgChgLi/SwiftRangeable.git", from: "1.0.0"),
],
targets: [
.target(name: "YourTarget", dependencies: ["Rangeable"]),
]
Or add the package via Xcode → File → Add Packages → paste the repository URL.
Usage
import Rangeable
enum InlineMarkup: Hashable, RangeableElement {
case strong
case italic
case link(String)
}
var r = Rangeable<InlineMarkup>()
try r.insert(.strong, start: 2, end: 5)
try r.insert(.strong, start: 3, end: 7) // merges with [2, 5] → [2, 7]
try r.insert(.strong, start: 9, end: 11) // disjoint
try r.insert(.italic, start: 3, end: 8)
r.getRange(of: .strong) // [Interval(lo: 2, hi: 7), Interval(lo: 9, hi: 11)]
r.getRange(of: .italic) // [Interval(lo: 3, hi: 8)]
r[4].objs // [.strong, .italic] first-insert order
r[8].objs // [.italic]
r[10].objs // [.strong]
Sweep iteration via transitions
let events = r.transitions(over: 0...15)
for event in events {
switch event.kind {
case .open:
print("\(event.coordinate ?? Int.max): open \(event.element)")
case .close:
print("\(event.coordinate ?? Int.max): close \(event.element)")
}
}
API
| Member | Type | Notes |
|---|---|---|
Rangeable<Element>() | initializer | empty |
mutating func insert(_:start:end:) throws | mutates self | throws RangeableError.invalidInterval if start > end |
subscript(_ i: Int) | Slot<Element> | exposes .objs |
getRange(of:) | [Interval] | merged disjoint ranges |
transitions(over:) | [TransitionEvent<Element>] | accepts ClosedRange<Int> |
count | Int | distinct elements |
isEmpty | Bool | |
| iteration | Sequence conformance | for (element, ranges) in r |
Semantics
- End is inclusive:
[a, b]coversa...b, both ends. - Same-element merging: equal elements (by
Hashable) merge on overlap or integer adjacency.[2, 4] ∪ [5, 7] = [2, 7]. - Idempotent insert: re-inserting a contained interval costs no version bump.
- Out-of-order rejected:
try insert(_, start: 5, end: 2)throws. - Active-set ordering: deterministic — first-insert order of the element.
- Coordinate sentinel: a close event for an interval ending at
Int.maxcarriescoordinate == nil(None == +∞ per RFC § 4.7). - Value semantics + COW:
Rangeableis astructwith copy-on-write storage; assignment is O(1).
See RangeableRFC § 4 for normative semantics and § 10 for the 23-case test contract.
Cross-language consistency
This Swift implementation joins the Ruby, Python, JS, Kotlin and Go implementations. All six share a 160-op / 86-probe JSON fixture and produce byte-identical outputs.
See also
- RangeableRFC — normative specification.
- RubyRangeable — Ruby reference (
gem install rangeable). - PythonRangeable — Python reference (
pip install rangeable). - JSRangeable — TypeScript reference (
npm i rangeable-js). - KotlinRangeable — Kotlin/JVM reference (JitPack).
- GoRangeable — Go reference (
go get github.com/ZhgChgLi/GoRangeable).
Development
$ swift test
38 tests cover the full RFC § 10 contract, additional coverage for count / isEmpty / iteration / COW semantics, a property test against a brute-force oracle, and the cross-language fixture.
License
MIT © ZhgChgLi
Buy me a beer ❤️❤️❤️
If this project has helped you, feel free to sponsor me a cup of coffee, thank you.