started working on a trie!

thanks @foxmoss
This commit is contained in:
neon443
2025-11-03 12:05:04 +00:00
parent 8e9ab5e030
commit d8e31178d6
2 changed files with 86 additions and 0 deletions

View File

@@ -39,6 +39,9 @@
A9B9A8302EB2CD0B004C9245 /* Emoji.swift in Sources */ = {isa = PBXBuildFile; fileRef = A924C3712EA9127200F20781 /* Emoji.swift */; }; A9B9A8302EB2CD0B004C9245 /* Emoji.swift in Sources */ = {isa = PBXBuildFile; fileRef = A924C3712EA9127200F20781 /* Emoji.swift */; };
A9B9A8312EB2CD14004C9245 /* FilterCategory.swift in Sources */ = {isa = PBXBuildFile; fileRef = A935437A2EB2A3C800BB80A4 /* FilterCategory.swift */; }; A9B9A8312EB2CD14004C9245 /* FilterCategory.swift in Sources */ = {isa = PBXBuildFile; fileRef = A935437A2EB2A3C800BB80A4 /* FilterCategory.swift */; };
A9B9A8322EB2CD29004C9245 /* SlackResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = A949B1FA2EA0518800215164 /* SlackResponse.swift */; }; A9B9A8322EB2CD29004C9245 /* SlackResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = A949B1FA2EA0518800215164 /* SlackResponse.swift */; };
A9C172DC2EB8C9AC008A7885 /* Trie.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9C172DB2EB8C9AC008A7885 /* Trie.swift */; };
A9C172DD2EB8C9AC008A7885 /* Trie.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9C172DB2EB8C9AC008A7885 /* Trie.swift */; };
A9C172DE2EB8C9AC008A7885 /* Trie.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9C172DB2EB8C9AC008A7885 /* Trie.swift */; };
A9D15B8B2EB1142C00404792 /* EmojiPack.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9D15B892EB1142C00404792 /* EmojiPack.swift */; }; A9D15B8B2EB1142C00404792 /* EmojiPack.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9D15B892EB1142C00404792 /* EmojiPack.swift */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
@@ -98,6 +101,7 @@
A986A6CB2EB659E000B6E0FA /* StickerBrowserDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StickerBrowserDataSource.swift; sourceTree = "<group>"; }; A986A6CB2EB659E000B6E0FA /* StickerBrowserDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StickerBrowserDataSource.swift; sourceTree = "<group>"; };
A9B9A8232EB2CCB5004C9245 /* StickerSlackTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = StickerSlackTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; A9B9A8232EB2CCB5004C9245 /* StickerSlackTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = StickerSlackTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
A9B9A82C2EB2CCBE004C9245 /* StickerSlackTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StickerSlackTests.swift; sourceTree = "<group>"; }; A9B9A82C2EB2CCBE004C9245 /* StickerSlackTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StickerSlackTests.swift; sourceTree = "<group>"; };
A9C172DB2EB8C9AC008A7885 /* Trie.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Trie.swift; sourceTree = "<group>"; };
A9D15B892EB1142C00404792 /* EmojiPack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiPack.swift; sourceTree = "<group>"; }; A9D15B892EB1142C00404792 /* EmojiPack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmojiPack.swift; sourceTree = "<group>"; };
A9E2ECD72EB74CE00038B2D6 /* Config.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Config.xcconfig; sourceTree = "<group>"; }; A9E2ECD72EB74CE00038B2D6 /* Config.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Config.xcconfig; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
@@ -200,6 +204,7 @@
A924C3712EA9127200F20781 /* Emoji.swift */, A924C3712EA9127200F20781 /* Emoji.swift */,
A9D15B892EB1142C00404792 /* EmojiPack.swift */, A9D15B892EB1142C00404792 /* EmojiPack.swift */,
A935437A2EB2A3C800BB80A4 /* FilterCategory.swift */, A935437A2EB2A3C800BB80A4 /* FilterCategory.swift */,
A9C172DB2EB8C9AC008A7885 /* Trie.swift */,
); );
path = Emoji; path = Emoji;
sourceTree = "<group>"; sourceTree = "<group>";
@@ -387,6 +392,7 @@
A9104C802EB4022500D160EA /* MSSticker.swift in Sources */, A9104C802EB4022500D160EA /* MSSticker.swift in Sources */,
A949B1FB2EA0518800215164 /* SlackResponse.swift in Sources */, A949B1FB2EA0518800215164 /* SlackResponse.swift in Sources */,
A935437B2EB2A3C800BB80A4 /* FilterCategory.swift in Sources */, A935437B2EB2A3C800BB80A4 /* FilterCategory.swift in Sources */,
A9C172DD2EB8C9AC008A7885 /* Trie.swift in Sources */,
A924C3732EA9127200F20781 /* Emoji.swift in Sources */, A924C3732EA9127200F20781 /* Emoji.swift in Sources */,
A9D15B8B2EB1142C00404792 /* EmojiPack.swift in Sources */, A9D15B8B2EB1142C00404792 /* EmojiPack.swift in Sources */,
A949B1F82EA04F2300215164 /* EmojiHoarder.swift in Sources */, A949B1F82EA04F2300215164 /* EmojiHoarder.swift in Sources */,
@@ -401,6 +407,7 @@
A986A6C32EB6598100B6E0FA /* FilterCategory.swift in Sources */, A986A6C32EB6598100B6E0FA /* FilterCategory.swift in Sources */,
A986A6CD2EB659E000B6E0FA /* MessagesViewController.swift in Sources */, A986A6CD2EB659E000B6E0FA /* MessagesViewController.swift in Sources */,
A986A6CE2EB659E000B6E0FA /* StickerBrowserDataSource.swift in Sources */, A986A6CE2EB659E000B6E0FA /* StickerBrowserDataSource.swift in Sources */,
A9C172DE2EB8C9AC008A7885 /* Trie.swift in Sources */,
A986A6C42EB6598500B6E0FA /* SlackResponse.swift in Sources */, A986A6C42EB6598500B6E0FA /* SlackResponse.swift in Sources */,
A986A6C22EB6597600B6E0FA /* Emoji.swift in Sources */, A986A6C22EB6597600B6E0FA /* Emoji.swift in Sources */,
A986A6C12EB6595F00B6E0FA /* EmojiHoarder.swift in Sources */, A986A6C12EB6595F00B6E0FA /* EmojiHoarder.swift in Sources */,
@@ -414,6 +421,7 @@
A9104C7F2EB4022500D160EA /* MSSticker.swift in Sources */, A9104C7F2EB4022500D160EA /* MSSticker.swift in Sources */,
A9B9A8302EB2CD0B004C9245 /* Emoji.swift in Sources */, A9B9A8302EB2CD0B004C9245 /* Emoji.swift in Sources */,
A9B9A82F2EB2CCED004C9245 /* EmojiHoarder.swift in Sources */, A9B9A82F2EB2CCED004C9245 /* EmojiHoarder.swift in Sources */,
A9C172DC2EB8C9AC008A7885 /* Trie.swift in Sources */,
A9B9A8322EB2CD29004C9245 /* SlackResponse.swift in Sources */, A9B9A8322EB2CD29004C9245 /* SlackResponse.swift in Sources */,
A9B9A82E2EB2CCBE004C9245 /* StickerSlackTests.swift in Sources */, A9B9A82E2EB2CCBE004C9245 /* StickerSlackTests.swift in Sources */,
A9B9A8312EB2CD14004C9245 /* FilterCategory.swift in Sources */, A9B9A8312EB2CD14004C9245 /* FilterCategory.swift in Sources */,

View File

@@ -0,0 +1,78 @@
//
// Trie.swift
// StickerSlack
//
// Created by neon443 on 03/11/2025.
//
import Foundation
import SwiftUI
import Combine
class TrieNode: ObservableObject {
@Published var children: [Character: TrieNode] = [:]
@Published var isEndOfWord: Bool = false
}
class Trie: ObservableObject {
@Published var root: TrieNode = TrieNode()
func insert(word: String) {
var currentNode = root
for char in word {
if let node = currentNode.children[char] {
print("node \(char) exists")
currentNode = node
} else {
print("node \(char) didnt exist creating")
currentNode.children[char] = TrieNode()
currentNode = currentNode.children[char]!
}
}
}
}
struct TrieTestingView: View {
@ObservedObject var trie: Trie = Trie()
@State var id: UUID = UUID()
@State var newWord: String = ""
@State var searchTerm: String = ""
var body: some View {
VStack {
TextField("", text: $newWord)
.textFieldStyle(.roundedBorder)
.border(.red)
Button("add word") {
trie.insert(word: newWord)
id = UUID()
}
TextField("", text: $searchTerm)
Text("\(trie.root.children.count)")
List {
TrieNodeView(trieNode: trie.root)
}
.id(id)
}
}
}
struct TrieNodeView: View {
@State var trieNode: TrieNode
var body: some View {
ForEach(trieNode.children.map { $0.key }, id: \.self) { key in
Text(String(key))
.frame(width: 20, height: 20)
TrieNodeView(trieNode: trieNode.children[key]!)
.padding(.leading, 20)
}
}
}
#Preview {
TrieTestingView()
}