Skip to content

Commit

Permalink
Use #children_tree instead of #children when possible
Browse files Browse the repository at this point in the history
Add `#first_child` method to achieve this
  • Loading branch information
gonzedge committed Feb 12, 2018
1 parent 77c943d commit 16b1d90
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## 1.0.3 [compare][compare_v1_0_2_and_master]

- Use `#children_tree` instead of `#children` when possible [@gonzedge][github_user_gonzedge]
- Remove unnecessary assignment in `#letter=` [@gonzedge][github_user_gonzedge]
- Use `#each_value` instead of `#values`.`#each` in `Enumerable#each` [@gonzedge][github_user_gonzedge]
- Use `#each_key` instead of `#keys`.`#each` in `CompressedNode#current_key` [@gonzedge][github_user_gonzedge]
Expand Down
4 changes: 2 additions & 2 deletions lib/rambling/trie/compressor.rb
Expand Up @@ -16,7 +16,7 @@ def compress node
private

def merge_with_child_and_compress node
child = node.children.first
child = node.first_child

letter = node.letter.to_s << child.letter.to_s
new_node = new_compressed_node node, letter, child.terminal?
Expand All @@ -28,7 +28,7 @@ def merge_with_child_and_compress node
def copy_node_and_compress_children node
new_node = new_compressed_node node, node.letter, node.terminal?

node.children.each do |child|
node.children_tree.each_value do |child|
compressed_child = compress child

compressed_child.parent = new_node
Expand Down
10 changes: 10 additions & 0 deletions lib/rambling/trie/node.rb
Expand Up @@ -48,6 +48,16 @@ def children
children_tree.values
end

# First child node.
# @return [Node, nil] the first child contained in the current node.
def first_child
return if children_tree.empty?

children_tree.each_value do |child|
return child
end
end

# Indicates if the current node is the root node.
# @return [Boolean] `true` if the node does not have a parent, `false`
# otherwise.
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/rambling/trie/raw_node_spec.rb
Expand Up @@ -68,7 +68,7 @@
end

it 'includes a child with the expected letter' do
expect(node.children.first.letter).to eq :a
expect(node.first_child.letter).to eq :a
end

it 'has the expected letter as a key' do
Expand Down

0 comments on commit 16b1d90

Please sign in to comment.