mirror of
https://codeberg.org/comaps/comaps
synced 2025-12-28 17:03:38 +00:00
Organic Maps sources as of 02.04.2025 (fad26bbf22ac3da75e01e62aa01e5c8e11861005)
To expand with full Organic Maps and Maps.ME commits history run: git remote add om-historic [om-historic.git repo url] git fetch --tags om-historic git replace squashed-history historic-commits
This commit is contained in:
87
iphone/Maps/Core/Search/SearchIndex.swift
Normal file
87
iphone/Maps/Core/Search/SearchIndex.swift
Normal file
@@ -0,0 +1,87 @@
|
||||
@objc
|
||||
final class SearchIndex: NSObject {
|
||||
fileprivate struct Item {
|
||||
let type: SearchItemType
|
||||
let containerIndex: Int
|
||||
}
|
||||
|
||||
fileprivate struct PositionItem {
|
||||
let item: Item
|
||||
var position: Int
|
||||
}
|
||||
|
||||
private var positionItems: [PositionItem] = []
|
||||
private var items: [Item] = []
|
||||
|
||||
@objc var count: Int {
|
||||
return items.count
|
||||
}
|
||||
|
||||
@objc init(suggestionsCount: Int, resultsCount: Int) {
|
||||
for index in 0 ..< resultsCount {
|
||||
let type: SearchItemType = index < suggestionsCount ? .suggestion : .regular
|
||||
let item = Item(type: type, containerIndex: index)
|
||||
positionItems.append(PositionItem(item: item, position: index))
|
||||
}
|
||||
super.init()
|
||||
}
|
||||
|
||||
func addItem(type: SearchItemType, prefferedPosition: Int, containerIndex: Int) {
|
||||
assert(type != .suggestion && type != .regular)
|
||||
let item = Item(type: type, containerIndex: containerIndex)
|
||||
positionItems.append(PositionItem(item: item, position: prefferedPosition))
|
||||
}
|
||||
|
||||
@objc func build() {
|
||||
positionItems.sort(by: >)
|
||||
var itemsDict: [Int: Item] = [:]
|
||||
positionItems.forEach { item in
|
||||
var position = item.position
|
||||
while itemsDict[position] != nil {
|
||||
position += 1
|
||||
}
|
||||
itemsDict[position] = item.item
|
||||
}
|
||||
|
||||
items.removeAll()
|
||||
let keys = itemsDict.keys.sorted()
|
||||
for index in 0 ..< keys.count {
|
||||
let key = keys[index]
|
||||
if index == key {
|
||||
items.append(itemsDict[key]!)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@objc func resultType(row: Int) -> SearchItemType {
|
||||
return items[row].type
|
||||
}
|
||||
|
||||
@objc func resultContainerIndex(row: Int) -> Int {
|
||||
return items[row].containerIndex
|
||||
}
|
||||
}
|
||||
|
||||
extension SearchIndex.PositionItem: Equatable {
|
||||
static func ==(lhs: SearchIndex.PositionItem, rhs: SearchIndex.PositionItem) -> Bool {
|
||||
let lhsCache = lhs.item
|
||||
let rhsCache = rhs.item
|
||||
return lhsCache.type == rhsCache.type &&
|
||||
lhs.position == rhs.position &&
|
||||
lhsCache.containerIndex == rhsCache.containerIndex
|
||||
}
|
||||
}
|
||||
|
||||
extension SearchIndex.PositionItem: Comparable {
|
||||
static func <(lhs: SearchIndex.PositionItem, rhs: SearchIndex.PositionItem) -> Bool {
|
||||
let lhsCache = lhs.item
|
||||
let rhsCache = rhs.item
|
||||
guard lhsCache.type == rhsCache.type else {
|
||||
return lhsCache.type.rawValue < rhsCache.type.rawValue
|
||||
}
|
||||
guard lhs.position == rhs.position else {
|
||||
return lhs.position > rhs.position
|
||||
}
|
||||
return lhsCache.containerIndex < rhsCache.containerIndex
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user