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 root: TrieNode = TrieNode()
|
||||||
var dict: [String:Emoji] = [:]
|
var dict: [String:Emoji] = [:]
|
||||||
|
|
||||||
|
var wordlist: [String] = []
|
||||||
|
|
||||||
func insert(word: String) {
|
func insert(word: String) {
|
||||||
let word = word.lowercased()
|
let word = word.lowercased()
|
||||||
|
wordlist.append(word)
|
||||||
var currentNode = root
|
var currentNode = root
|
||||||
let indices = word.indices
|
let indices = word.indices
|
||||||
let last = indices.last
|
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
|
var currentNode = root
|
||||||
|
|
||||||
for char in query.lowercased() {
|
for char in query.lowercased() {
|
||||||
|
|||||||
@@ -33,29 +33,25 @@ struct TrieTestingView: View {
|
|||||||
withAnimation(.snappy) {
|
withAnimation(.snappy) {
|
||||||
filterResult = hoarder.trie.search(prefix: filterTerm)
|
filterResult = hoarder.trie.search(prefix: filterTerm)
|
||||||
}
|
}
|
||||||
print("testing: trie search")
|
print("testing: trie search complete")
|
||||||
case .contains:
|
case .contains:
|
||||||
Task.detached {
|
Task.detached {
|
||||||
var results: [[Emoji]] = []
|
var result: [String] = await hoarder.trie.wordlist
|
||||||
var result: [Emoji] = []
|
for word in await filterTerm.split(separator: " ") {
|
||||||
for word in filterTerm.split(separator: " ") {
|
result = result.filter { $0.localizedCaseInsensitiveContains(word) }
|
||||||
results.append(hoarder.emojis.filter({ $0.name.localizedCaseInsensitiveContains(word) }))
|
|
||||||
}
|
|
||||||
result = results.reduce(results[0]) { partialResult, array in
|
|
||||||
partialResult.filter { array.contains($0) }
|
|
||||||
}
|
}
|
||||||
await MainActor.run {
|
await MainActor.run {
|
||||||
withAnimation(.snappy) {
|
withAnimation(.snappy) {
|
||||||
filterResult = result.map { $0.name }
|
filterResult = result
|
||||||
}
|
}
|
||||||
print("testing: contains search")
|
print("testing: contains search complete")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case .exact:
|
case .exact:
|
||||||
withAnimation(.snappy) {
|
withAnimation(.snappy) {
|
||||||
searchStatus = hoarder.trie.search(for: filterTerm)
|
searchStatus = hoarder.trie.search(exactly: filterTerm)
|
||||||
}
|
}
|
||||||
print("testing: exact search")
|
print("testing: exact search complete")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||