Skip to content

Commit

Permalink
Allow overriding values in a prefix tree
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Aug 30, 2023
1 parent 267d022 commit 731d494
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/ruby_indexer/lib/ruby_indexer/prefix_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def insert(key, value)
node = node.children[char] ||= Node.new(char, value, node)
end

# This line is to allow a value to be overridden. When we are indexing files, we want to be able to update entries
# for a given fully qualified name if we find more occurrences of it. Without being able to override, that would
# not be possible
node.value = value
node.leaf = true
end

Expand Down Expand Up @@ -112,7 +116,7 @@ class Node
attr_reader :key

sig { returns(Value) }
attr_reader :value
attr_accessor :value

sig { returns(T::Boolean) }
attr_accessor :leaf
Expand Down
10 changes: 10 additions & 0 deletions lib/ruby_indexer/test/prefix_tree_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,15 @@ def test_deleted_node_is_removed_from_the_tree
root = tree.instance_variable_get(:@root)
assert_empty(root.children)
end

def test_overriding_values
tree = PrefixTree[Integer].new

tree.insert("foo/bar", 123)
assert_equal([123], tree.search("foo/bar"))

tree.insert("foo/bar", 456)
assert_equal([456], tree.search("foo/bar"))
end
end
end

0 comments on commit 731d494

Please sign in to comment.