1
0
mirror of https://github.com/mastodon/mastodon-ios.git synced 2024-12-12 00:25:36 +01:00
mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/MastodonCore/Service/Emoji/Trie.swift

110 lines
2.9 KiB
Swift
Raw Normal View History

//
// AutoCompleteViewModel+Trie.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-5-18.
//
import Foundation
public struct Trie<Element: Hashable> {
public let isElement: Bool
public let valueSet: NSMutableSet
public var children: [Element: Trie<Element>]
}
extension Trie {
public init() {
isElement = false
valueSet = NSMutableSet()
children = [:]
}
public init(_ key: ArraySlice<Element>, value: Any) {
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 {
public var elements: [[Element]] {
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>)? {
return isEmpty ? nil : (self[startIndex], self.dropFirst())
}
}
extension Trie {
2022-10-08 07:43:06 +02:00
public func lookup(key: ArraySlice<Element>) -> Bool {
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>? {
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]] {
return lookup(key: key)?.elements ?? []
}
}
extension Trie {
2022-10-08 07:43:06 +02:00
public mutating func inserted(_ key: ArraySlice<Element>, value: Any) {
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>] {
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 {
let valueSet = NSMutableSet(set: self.valueSet)
2021-05-31 11:07:32 +02:00
for (_, value) in children {
valueSet.addObjects(from: Array(value.values))
}
return valueSet
}
}