2021-05-18 08:25:32 +02:00
|
|
|
//
|
|
|
|
// AutoCompleteViewModel+Trie.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-5-18.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
public struct Trie<Element: Hashable> {
|
|
|
|
public let isElement: Bool
|
|
|
|
public let valueSet: NSMutableSet
|
|
|
|
public var children: [Element: Trie<Element>]
|
2021-05-18 08:25:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension Trie {
|
2022-09-30 13:28:09 +02:00
|
|
|
public init() {
|
2021-05-18 08:25:32 +02:00
|
|
|
isElement = false
|
|
|
|
valueSet = NSMutableSet()
|
|
|
|
children = [:]
|
|
|
|
}
|
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
public init(_ key: ArraySlice<Element>, value: Any) {
|
2021-05-18 08:25:32 +02:00
|
|
|
if let (head, tail) = key.decomposed {
|
|
|
|
let children = [head: Trie(tail, value: value)]
|
|
|
|
self = Trie(isElement: false, valueSet: NSMutableSet(), children: children)
|
|
|
|
} else {
|
|
|
|
self = Trie(isElement: true, valueSet: NSMutableSet(object: value), children: [:])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Trie {
|
2022-09-30 13:28:09 +02:00
|
|
|
public var elements: [[Element]] {
|
2021-05-18 08:25:32 +02:00
|
|
|
var result: [[Element]] = isElement ? [[]] : []
|
|
|
|
for (key, value) in children {
|
|
|
|
result += value.elements.map { [key] + $0 }
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ArraySlice {
|
2022-10-08 07:43:06 +02:00
|
|
|
public var decomposed: (Element, ArraySlice<Element>)? {
|
2021-05-18 08:25:32 +02:00
|
|
|
return isEmpty ? nil : (self[startIndex], self.dropFirst())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Trie {
|
2022-10-08 07:43:06 +02:00
|
|
|
public func lookup(key: ArraySlice<Element>) -> Bool {
|
2021-05-18 08:25:32 +02:00
|
|
|
guard let (head, tail) = key.decomposed else { return isElement }
|
|
|
|
guard let subtrie = children[head] else { return false }
|
|
|
|
return subtrie.lookup(key: tail)
|
|
|
|
}
|
|
|
|
|
2022-10-08 07:43:06 +02:00
|
|
|
public func lookup(key: ArraySlice<Element>) -> Trie<Element>? {
|
2021-05-18 08:25:32 +02:00
|
|
|
guard let (head, tail) = key.decomposed else { return self }
|
|
|
|
guard let remainder = children[head] else { return nil }
|
|
|
|
return remainder.lookup(key: tail)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Trie {
|
2022-10-08 07:43:06 +02:00
|
|
|
public func complete(key: ArraySlice<Element>) -> [[Element]] {
|
2021-05-18 08:25:32 +02:00
|
|
|
return lookup(key: key)?.elements ?? []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Trie {
|
2022-10-08 07:43:06 +02:00
|
|
|
public mutating func inserted(_ key: ArraySlice<Element>, value: Any) {
|
2021-05-18 08:25:32 +02:00
|
|
|
guard let (head, tail) = key.decomposed else {
|
|
|
|
self.valueSet.add(value)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if var nextTrie = children[head] {
|
|
|
|
nextTrie.inserted(tail, value: value)
|
|
|
|
children[head] = nextTrie
|
|
|
|
} else {
|
|
|
|
children[head] = Trie(tail, value: value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Trie {
|
2022-10-08 07:43:06 +02:00
|
|
|
public func passthrough(_ key: ArraySlice<Element>) -> [Trie<Element>] {
|
2021-05-18 08:25:32 +02:00
|
|
|
guard let (head, tail) = key.decomposed else {
|
|
|
|
return [self]
|
|
|
|
}
|
|
|
|
|
|
|
|
let passthroughed = children[head]?.passthrough(tail) ?? []
|
|
|
|
if isElement {
|
|
|
|
return passthroughed + [self]
|
|
|
|
} else {
|
|
|
|
return passthroughed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-08 07:43:06 +02:00
|
|
|
public var values: NSSet {
|
2021-05-18 08:25:32 +02:00
|
|
|
let valueSet = NSMutableSet(set: self.valueSet)
|
2021-05-31 11:07:32 +02:00
|
|
|
for (_, value) in children {
|
2021-05-18 08:25:32 +02:00
|
|
|
valueSet.addObjects(from: Array(value.values))
|
|
|
|
}
|
|
|
|
|
|
|
|
return valueSet
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|