88
99import Foundation
1010
11- public struct Trie < T: Hashable > {
12- public typealias Node = TrieNode < T >
11+ struct Trie < T: Hashable > {
12+ typealias Node = TrieNode < T >
1313
1414 var root : Node
1515
16- public init ( root: Node ? = nil ) {
16+ init ( root: Node ? = nil ) {
1717 self . root = root ?? Node ( )
1818 }
1919}
2020
21- public extension Trie {
21+ extension Trie {
2222 func insert( _ element: any Sequence < T > ) {
2323 var node = root
2424 for item in element {
@@ -63,7 +63,7 @@ public extension Trie {
6363 }
6464}
6565
66- public extension Trie {
66+ extension Trie {
6767 /// Only used for testing, could migrate to collection
6868 func get( _ element: any Sequence < T > ) -> Node ? {
6969 var node = root
@@ -76,18 +76,18 @@ public extension Trie {
7676}
7777
7878// TODO: maybe store the scores here if it's helpful?
79- public class TrieNode < T: Hashable > {
79+ class TrieNode < T: Hashable > {
8080 var isLeaf : Bool = false
8181 var children : [ T : TrieNode ] = [ : ]
8282}
8383
84- public struct LeavesWithCommonPrefixIterator < T: Hashable > : Sequence , IteratorProtocol {
84+ struct LeavesWithCommonPrefixIterator < T: Hashable > : Sequence , IteratorProtocol {
8585 var node : TrieNode < T >
8686 var text : any Sequence < T >
8787 var seq : [ T ] = [ ]
8888 lazy var iterator = text. makeIterator ( ) as any IteratorProtocol < T >
8989
90- public mutating func next( ) -> [ T ] ? {
90+ mutating func next( ) -> [ T ] ? {
9191 while true {
9292 guard let item = iterator. next ( ) else { return nil }
9393 seq. append ( item)
0 commit comments