faster new contains search

added a wordlist to the trie, containing an array of all the words
This commit is contained in:
neon443 committed 2026-03-15 01:28:02 +00:00
1 parent 9dac5f6939
commit 9e2bb928ee
2 files changed
+13 -14

No files matched your search

+5 -2
View File
@@ -17,9 +17,12 @@ class TrieNode: Codable {
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() {
+8 -12
View File
@@ -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")
}
}
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.