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