let ranges = DividableRange<Int>.rangesFor(baseValue: 0, changes: [
DividableRange<Int>.Divider(idx: 24, changeRightFn: { $0 + 9000 }),
DividableRange<Int>.Divider(idx: 4, changeRightFn: { $0 + 1 }),
DividableRange<Int>.Divider(idx: 7, changeRightFn: { $0 + 1 }),
DividableRange<Int>.Divider(idx: 10, changeRightFn: { $0 - 1 }),
DividableRange<Int>.Divider(idx: 20, changeRightFn: { $0 + 100 }),
DividableRange<Int>.Divider(idx: 13, changeRightFn: { $0 + 1 }),
DividableRange<Int>.Divider(idx: 20, changeRightFn: { $0 + 10 }),
])
var someIndexes = Array(0..<25)
let itemsAtIndexes = someIndexes.map { (idx) -> Int in
return DividableRange<Int>.binarySearch(idx: idx, ranges: ranges)
}
print(itemsAtIndexes) // [0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 112, 112, 112, 112, 9112]
Creation is O(N*log(N))
Lookup is O(log(N))
DividableRange is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'DividableRange'
Oleksii Horishnii, [email protected]
DividableRange is available under the MIT license. See the LICENSE file for more info.