Fix bug with Trie search
Exact matches were not being returned first in the array of results Signed-off-by: Terry Yiu <git@tyiu.xyz> Reviewed-by: William Casarin <jb55@jb55.com> Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
@@ -50,18 +50,18 @@ extension Trie {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Perform breadth-first search from matching branch and collect values from all descendants.
|
// Perform breadth-first search from matching branch and collect values from all descendants.
|
||||||
var exactMatches = Set<V>()
|
let exactMatches = Array(currentNode.exactMatchValues)
|
||||||
var substringMatches = Set<V>()
|
var substringMatches = Set<V>(currentNode.substringMatchValues)
|
||||||
var queue = [currentNode]
|
var queue = Array(currentNode.children.values)
|
||||||
|
|
||||||
while !queue.isEmpty {
|
while !queue.isEmpty {
|
||||||
let node = queue.removeFirst()
|
let node = queue.removeFirst()
|
||||||
exactMatches.formUnion(node.exactMatchValues)
|
substringMatches.formUnion(node.exactMatchValues)
|
||||||
substringMatches.formUnion(node.substringMatchValues)
|
substringMatches.formUnion(node.substringMatchValues)
|
||||||
queue.append(contentsOf: node.children.values)
|
queue.append(contentsOf: node.children.values)
|
||||||
}
|
}
|
||||||
|
|
||||||
return Array(exactMatches) + substringMatches
|
return exactMatches + substringMatches
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Inserts value of type V into this trie for the specified key. This function stores all substring endings of the key, not only the key itself.
|
/// Inserts value of type V into this trie for the specified key. This function stores all substring endings of the key, not only the key itself.
|
||||||
|
|||||||
Reference in New Issue
Block a user