1
1
/// A trie that stores objects at each node. Supports wildcard path
2
2
/// elements denoted by a ":" at the beginning.
3
- final class RouterTrieNode < StorageKey : Hashable , StorageObject > {
3
+ final class Trie < Key : Hashable , Value > {
4
4
/// Storage of the objects at this node.
5
- private var storage : [ StorageKey : StorageObject ] = [ : ]
5
+ private var storage : [ Key : Value ] = [ : ]
6
6
/// This node's children, mapped by their path for instant lookup.
7
- private var children : [ String : RouterTrieNode ] = [ : ]
7
+ private var children : [ String : Trie ] = [ : ]
8
8
/// Any children with wildcards in their path.
9
- private var wildcardChildren : [ String : RouterTrieNode ] = [ : ]
9
+ private var wildcardChildren : [ String : Trie ] = [ : ]
10
10
11
11
/// Search this node & it's children for an object at a path,
12
12
/// stored with the given key.
@@ -18,24 +18,24 @@ final class RouterTrieNode<StorageKey: Hashable, StorageObject> {
18
18
/// - Returns: A tuple containing the object and any parsed path
19
19
/// parameters. `nil` if the object isn't in this node or its
20
20
/// children.
21
- func search( path: [ String ] , storageKey: StorageKey ) -> ( value: StorageObject , parameters: [ PathParameter ] ) ? {
21
+ func search( path: [ String ] , storageKey: Key ) -> ( value: Value , parameters: [ PathParameter ] ) ? {
22
22
if let first = path. first {
23
23
let newPath = Array ( path. dropFirst ( ) )
24
- if let matchingChild = self . children [ first] {
24
+ if let matchingChild = children [ first] {
25
25
return matchingChild. search ( path: newPath, storageKey: storageKey)
26
26
} else {
27
- for (wildcard, node) in self . wildcardChildren {
27
+ for (wildcard, node) in wildcardChildren {
28
28
guard var val = node. search ( path: newPath, storageKey: storageKey) else {
29
29
continue
30
30
}
31
31
32
- val. 1 . insert ( PathParameter ( parameter: wildcard, stringValue: first) , at: 0 )
32
+ val. parameters . insert ( PathParameter ( parameter: wildcard, stringValue: first) , at: 0 )
33
33
return val
34
34
}
35
35
return nil
36
36
}
37
37
} else {
38
- return self . storage [ storageKey] . map { ( $0, [ ] ) }
38
+ return storage [ storageKey] . map { ( $0, [ ] ) }
39
39
}
40
40
}
41
41
@@ -46,20 +46,20 @@ final class RouterTrieNode<StorageKey: Hashable, StorageObject> {
46
46
/// stored.
47
47
/// - storageKey: The key by which to store the value.
48
48
/// - value: The value to store.
49
- func insert( path: [ String ] , storageKey: StorageKey , value: StorageObject ) {
49
+ func insert( path: [ String ] , storageKey: Key , value: Value ) {
50
50
if let first = path. first {
51
51
if first. hasPrefix ( " : " ) {
52
52
let firstWithoutEscape = String ( first. dropFirst ( ) )
53
- let child = self . wildcardChildren [ firstWithoutEscape] ?? Self ( )
53
+ let child = wildcardChildren [ firstWithoutEscape] ?? Self ( )
54
54
child. insert ( path: Array ( path. dropFirst ( ) ) , storageKey: storageKey, value: value)
55
- self . wildcardChildren [ firstWithoutEscape] = child
55
+ wildcardChildren [ firstWithoutEscape] = child
56
56
} else {
57
- let child = self . children [ first] ?? Self ( )
57
+ let child = children [ first] ?? Self ( )
58
58
child. insert ( path: Array ( path. dropFirst ( ) ) , storageKey: storageKey, value: value)
59
- self . children [ first] = child
59
+ children [ first] = child
60
60
}
61
61
} else {
62
- self . storage [ storageKey] = value
62
+ storage [ storageKey] = value
63
63
}
64
64
}
65
65
}
0 commit comments