mirror of
https://github.com/neon443/StickerSlack.git
synced 2026-11-30 20:54:51 +00:00
faster new contains search
added a wordlist to the trie, containing an array of all the words
This commit is contained in:
2 files changed
+12
-13
No files matched your search
@@ -18,8 +18,11 @@ class Trie: ObservableObject {
|
||||
var root: TrieNode = TrieNode()
|
||||
var dict: [String:Emoji] = [:]
|
||||
|
||||
var wordlist: [String] = []
|
||||
|
||||
func insert(word: String) {
|
||||
let word = word.lowercased()
|
||||
wordlist.append(word)
|
||||
var currentNode = root
|
||||
let indices = word.indices
|
||||
let last = indices.last
|
||||
@@ -38,7 +41,7 @@ class Trie: ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
func search(for query: String) -> Bool {
|
||||
func search(exactly query: String) -> Bool {
|
||||
var currentNode = root
|
||||
|
||||
for char in query.lowercased() {
|
||||
|
||||
@@ -33,29 +33,25 @@ struct TrieTestingView: View {
|
||||
withAnimation(.snappy) {
|
||||
filterResult = hoarder.trie.search(prefix: filterTerm)
|
||||
}
|
||||
print("testing: trie search")
|
||||
print("testing: trie search complete")
|
||||
case .contains:
|
||||
Task.detached {
|
||||
var results: [[Emoji]] = []
|
||||
var result: [Emoji] = []
|
||||
for word in filterTerm.split(separator: " ") {
|
||||
results.append(hoarder.emojis.filter({ $0.name.localizedCaseInsensitiveContains(word) }))
|
||||
}
|
||||
result = results.reduce(results[0]) { partialResult, array in
|
||||
partialResult.filter { array.contains($0) }
|
||||
var result: [String] = await hoarder.trie.wordlist
|
||||
for word in await filterTerm.split(separator: " ") {
|
||||
result = result.filter { $0.localizedCaseInsensitiveContains(word) }
|
||||
}
|
||||
await MainActor.run {
|
||||
withAnimation(.snappy) {
|
||||
filterResult = result.map { $0.name }
|
||||
filterResult = result
|
||||
}
|
||||
print("testing: contains search")
|
||||
print("testing: contains search complete")
|
||||
}
|
||||
}
|
||||
case .exact:
|
||||
withAnimation(.snappy) {
|
||||
searchStatus = hoarder.trie.search(for: filterTerm)
|
||||
searchStatus = hoarder.trie.search(exactly: filterTerm)
|
||||
}
|
||||
print("testing: exact search")
|
||||
print("testing: exact search complete")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||