Class: Foundries::Similarity::StructureTree
- Inherits:
-
Object
- Object
- Foundries::Similarity::StructureTree
- Defined in:
- lib/foundries/similarity/structure_tree.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #collapse_into(parent_name) ⇒ Object
- #contains?(other) ⇒ Boolean
- #descendant_count ⇒ Object
- #embeds?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(name, children: []) ⇒ StructureTree
constructor
A new instance of StructureTree.
- #normalize ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name, children: []) ⇒ StructureTree
Returns a new instance of StructureTree.
8 9 10 11 |
# File 'lib/foundries/similarity/structure_tree.rb', line 8 def initialize(name, children: []) @name = name.to_s @children = children end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
6 7 8 |
# File 'lib/foundries/similarity/structure_tree.rb', line 6 def children @children end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/foundries/similarity/structure_tree.rb', line 6 def name @name end |
Class Method Details
.root(children:) ⇒ Object
13 14 15 |
# File 'lib/foundries/similarity/structure_tree.rb', line 13 def self.root(children:) new("__root__", children: children) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
39 40 41 42 43 |
# File 'lib/foundries/similarity/structure_tree.rb', line 39 def ==(other) other.is_a?(self.class) && name == other.name && children == other.children end |
#collapse_into(parent_name) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/foundries/similarity/structure_tree.rb', line 27 def collapse_into(parent_name) if name == parent_name && !children.empty? children else [self] end end |
#contains?(other) ⇒ Boolean
51 52 53 54 55 |
# File 'lib/foundries/similarity/structure_tree.rb', line 51 def contains?(other) return true if self == other (other) || children.any? { |child| child.contains?(other) } end |
#descendant_count ⇒ Object
35 36 37 |
# File 'lib/foundries/similarity/structure_tree.rb', line 35 def descendant_count children.sum { |c| 1 + c.descendant_count } end |
#embeds?(other) ⇒ Boolean
57 58 59 60 61 62 63 |
# File 'lib/foundries/similarity/structure_tree.rb', line 57 def (other) return false unless name == other.name other.children.all? do |other_child| children.any? { |child| child.(other_child) } end end |
#hash ⇒ Object
47 48 49 |
# File 'lib/foundries/similarity/structure_tree.rb', line 47 def hash [name, children].hash end |
#normalize ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/foundries/similarity/structure_tree.rb', line 17 def normalize normalized_children = children.map(&:normalize) collapsed = normalized_children.flat_map { |child| child.collapse_into(name) } deduped = collapsed .group_by(&:name) .map { |_name, group| group.max_by(&:descendant_count) } .sort_by(&:name) self.class.new(name, children: deduped) end |
#to_s ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/foundries/similarity/structure_tree.rb', line 65 def to_s if children.empty? name else "#{name} > [#{children.join(", ")}]" end end |