Skip to content

Commit

Permalink
Rename trie
Browse files Browse the repository at this point in the history
  • Loading branch information
Joannis committed May 8, 2024
1 parent 93746a3 commit a3ff4ec
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Sources/Hummingbird/Router/RouterResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import NIOCore

public struct RouterResponder<Context: BaseRequestContext>: HTTPResponder {
let trie: BinaryTrie<EndpointResponders<Context>>
let trie: RouterTrie<EndpointResponders<Context>>
let notFoundResponder: any HTTPResponder<Context>
let options: RouterOptions

Expand All @@ -25,7 +25,7 @@ public struct RouterResponder<Context: BaseRequestContext>: HTTPResponder {
options: RouterOptions,
notFoundResponder: any HTTPResponder<Context>
) {
self.trie = BinaryTrie(base: trie)
self.trie = RouterTrie(base: trie)
self.options = options
self.notFoundResponder = notFoundResponder
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,27 @@
//
//===----------------------------------------------------------------------===//

enum BinaryTrieTokenKind: UInt8 {
enum TrieTokenKind: UInt8 {
case null = 0
case path, capture, prefixCapture, suffixCapture, wildcard, prefixWildcard, suffixWildcard, recursiveWildcard
case deadEnd
}

struct BinaryTrieNode: Sendable {
struct TrieNode: Sendable {
let valueIndex: UInt16
let token: BinaryTrieTokenKind
let token: TrieTokenKind
var nextSiblingNodeIndex: UInt16
var constant: UInt16?
var parameter: UInt16?

/// How many bytes a serialized BinaryTrieNode uses
static let serializedSize = 7
}

struct Trie: Sendable {
var nodes = [BinaryTrieNode]()
var nodes = [TrieNode]()
var parameters = [Substring]()
var constants = [Substring]()
}

@_spi(Internal) public final class BinaryTrie<Value: Sendable>: Sendable {
typealias Integer = UInt8
@_spi(Internal) public final class RouterTrie<Value: Sendable>: Sendable {
let trie: Trie
let values: [Value?]

Expand All @@ -51,7 +47,7 @@ struct Trie: Sendable {
)

trie.nodes.append(
BinaryTrieNode(
TrieNode(
valueIndex: 0,
token: .deadEnd,
nextSiblingNodeIndex: .max
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import NIOCore

extension BinaryTrie {
extension RouterTrie {
/// Resolve a path to a `Value` if available
@_spi(Internal) public func resolve(_ path: String) -> (value: Value, parameters: Parameters)? {
let pathComponents = path.split(separator: "/", omittingEmptySubsequences: true)
Expand Down Expand Up @@ -65,7 +65,7 @@ extension BinaryTrie {
_ component: Substring,
atNodeIndex nodeIndex: inout Int,
parameters: inout Parameters
) -> BinaryTrieNode {
) -> TrieNode {
while nodeIndex < trie.nodes.count {
let node = trie.nodes[nodeIndex]
let result = self.matchComponent(
Expand All @@ -83,7 +83,7 @@ extension BinaryTrie {
}

// should never get here
return BinaryTrieNode(valueIndex: 0, token: .deadEnd, nextSiblingNodeIndex: .max)
return TrieNode(valueIndex: 0, token: .deadEnd, nextSiblingNodeIndex: .max)
}

private enum MatchResult {
Expand All @@ -92,7 +92,7 @@ extension BinaryTrie {

private func matchComponent(
_ component: Substring,
node: BinaryTrieNode,
node: TrieNode,
parameters: inout Parameters
) -> MatchResult {
switch node.token {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import NIOCore

extension BinaryTrie {
extension RouterTrie {
static func serialize(
_ node: RouterPathTrieBuilder<Value>.Node,
trie: inout Trie,
Expand All @@ -24,7 +24,7 @@ extension BinaryTrie {
let valueIndex = UInt16(values.count)
values.append(node.value)

let token: BinaryTrieTokenKind
let token: TrieTokenKind
let constant: UInt16?
let parameter: UInt16?

Expand Down Expand Up @@ -89,7 +89,7 @@ extension BinaryTrie {

let nodeIndex = trie.nodes.count
trie.nodes.append(
BinaryTrieNode(
TrieNode(
valueIndex: valueIndex,
token: token,
nextSiblingNodeIndex: .max,
Expand Down
2 changes: 1 addition & 1 deletion Sources/Hummingbird/Router/TrieRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import HummingbirdCore
}
}

@_spi(Internal) public func build() -> BinaryTrie<Value> {
@_spi(Internal) public func build() -> RouterTrie<Value> {
.init(base: self)
}

Expand Down

0 comments on commit a3ff4ec

Please sign in to comment.