Skip to content

Commit f5fb9d8

Browse files
Rename Trie
1 parent 4f2b62c commit f5fb9d8

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

Sources/Alchemy/Routing/Router.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public final class Router: HTTPRouter, Service {
5151
var pathPrefixes: [String] = []
5252

5353
/// A trie that holds all the handlers.
54-
private let trie = RouterTrieNode<HTTPMethod, HTTPHandler>()
54+
private let trie = Trie<HTTPMethod, HTTPHandler>()
5555

5656
/// Creates a new router.
5757
init() {}
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/// A trie that stores objects at each node. Supports wildcard path
22
/// elements denoted by a ":" at the beginning.
3-
final class RouterTrieNode<StorageKey: Hashable, StorageObject> {
3+
final class Trie<Key: Hashable, Value> {
44
/// Storage of the objects at this node.
5-
private var storage: [StorageKey: StorageObject] = [:]
5+
private var storage: [Key: Value] = [:]
66
/// This node's children, mapped by their path for instant lookup.
7-
private var children: [String: RouterTrieNode] = [:]
7+
private var children: [String: Trie] = [:]
88
/// Any children with wildcards in their path.
9-
private var wildcardChildren: [String: RouterTrieNode] = [:]
9+
private var wildcardChildren: [String: Trie] = [:]
1010

1111
/// Search this node & it's children for an object at a path,
1212
/// stored with the given key.
@@ -18,24 +18,24 @@ final class RouterTrieNode<StorageKey: Hashable, StorageObject> {
1818
/// - Returns: A tuple containing the object and any parsed path
1919
/// parameters. `nil` if the object isn't in this node or its
2020
/// children.
21-
func search(path: [String], storageKey: StorageKey) -> (value: StorageObject, parameters: [PathParameter])? {
21+
func search(path: [String], storageKey: Key) -> (value: Value, parameters: [PathParameter])? {
2222
if let first = path.first {
2323
let newPath = Array(path.dropFirst())
24-
if let matchingChild = self.children[first] {
24+
if let matchingChild = children[first] {
2525
return matchingChild.search(path: newPath, storageKey: storageKey)
2626
} else {
27-
for (wildcard, node) in self.wildcardChildren {
27+
for (wildcard, node) in wildcardChildren {
2828
guard var val = node.search(path: newPath, storageKey: storageKey) else {
2929
continue
3030
}
3131

32-
val.1.insert(PathParameter(parameter: wildcard, stringValue: first), at: 0)
32+
val.parameters.insert(PathParameter(parameter: wildcard, stringValue: first), at: 0)
3333
return val
3434
}
3535
return nil
3636
}
3737
} else {
38-
return self.storage[storageKey].map { ($0, []) }
38+
return storage[storageKey].map { ($0, []) }
3939
}
4040
}
4141

@@ -46,20 +46,20 @@ final class RouterTrieNode<StorageKey: Hashable, StorageObject> {
4646
/// stored.
4747
/// - storageKey: The key by which to store the value.
4848
/// - value: The value to store.
49-
func insert(path: [String], storageKey: StorageKey, value: StorageObject) {
49+
func insert(path: [String], storageKey: Key, value: Value) {
5050
if let first = path.first {
5151
if first.hasPrefix(":") {
5252
let firstWithoutEscape = String(first.dropFirst())
53-
let child = self.wildcardChildren[firstWithoutEscape] ?? Self()
53+
let child = wildcardChildren[firstWithoutEscape] ?? Self()
5454
child.insert(path: Array(path.dropFirst()), storageKey: storageKey, value: value)
55-
self.wildcardChildren[firstWithoutEscape] = child
55+
wildcardChildren[firstWithoutEscape] = child
5656
} else {
57-
let child = self.children[first] ?? Self()
57+
let child = children[first] ?? Self()
5858
child.insert(path: Array(path.dropFirst()), storageKey: storageKey, value: value)
59-
self.children[first] = child
59+
children[first] = child
6060
}
6161
} else {
62-
self.storage[storageKey] = value
62+
storage[storageKey] = value
6363
}
6464
}
6565
}

Tests/AlchemyTests/Routing/TrieTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import XCTest
33

44
final class TrieTests: XCTestCase {
55
func testTrie() {
6-
let trie = RouterTrieNode<Int, String>()
6+
let trie = Trie<Int, String>()
77

88
trie.insert(path: ["one"], storageKey: 0, value: "foo")
99
trie.insert(path: ["one", "two"], storageKey: 1, value: "bar")

0 commit comments

Comments
 (0)