Class: PDF::Core::NameTree::Node Private
- Inherits:
-
Object
- Object
- PDF::Core::NameTree::Node
- Defined in:
- lib/pdf/core/name_tree.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Name Tree node
Instance Attribute Summary collapse
-
#children ⇒ Array<Node>
readonly
private
Child nodes.
- #document ⇒ Prawn::Document readonly private
-
#limit ⇒ Integer
readonly
private
Children number limit.
-
#parent ⇒ Node
private
Parent node.
- #ref ⇒ Reference private
Instance Method Summary collapse
-
#<<(value) ⇒ value
private
Insert value maintaining order and rebalancing tree if needed.
-
#>=(other) ⇒ Boolean
private
This is a compatibility method to allow uniform comparison between nodes and values.
-
#add(name, value) ⇒ Object
private
Adds a value.
-
#deep_copy ⇒ Node
private
Returns a deep copy of this node, without copying expensive things like the ‘ref` to `document`.
-
#empty? ⇒ Boolean
private
Tells whether there are any children nodes.
-
#greatest ⇒ String
private
The greatest (in lexicographic order) value name.
-
#initialize(document, limit, parent = nil) ⇒ Node
constructor
private
A new instance of Node.
-
#leaf? ⇒ Boolean
private
Tells whether this is a leaf node.
-
#least ⇒ String
private
The least (in lexicographic order) value name.
-
#size ⇒ Integer
private
Number of all (including nested) children nodes.
-
#split! ⇒ void
private
Split the tree at the node.
-
#to_hash ⇒ Hash
private
A hash representation of this node.
Constructor Details
#initialize(document, limit, parent = nil) ⇒ Node
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Node.
36 37 38 39 40 41 42 |
# File 'lib/pdf/core/name_tree.rb', line 36 def initialize(document, limit, parent = nil) @document = document @children = [] @limit = limit @parent = parent @ref = nil end |
Instance Attribute Details
#children ⇒ Array<Node> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Child nodes
17 18 19 |
# File 'lib/pdf/core/name_tree.rb', line 17 def children @children end |
#document ⇒ Prawn::Document (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
24 25 26 |
# File 'lib/pdf/core/name_tree.rb', line 24 def document @document end |
#limit ⇒ Integer (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Children number limit
21 22 23 |
# File 'lib/pdf/core/name_tree.rb', line 21 def limit @limit end |
#parent ⇒ Node
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parent node
28 29 30 |
# File 'lib/pdf/core/name_tree.rb', line 28 def parent @parent end |
#ref ⇒ Reference
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
31 32 33 |
# File 'lib/pdf/core/name_tree.rb', line 31 def ref @ref end |
Instance Method Details
#<<(value) ⇒ value
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Insert value maintaining order and rebalancing tree if needed.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/pdf/core/name_tree.rb', line 110 def <<(value) if children.empty? children << value elsif leaf? children.insert(insertion_point(value), value) split! if children.length > limit else fit = children.find { |child| child >= value } fit ||= children.last fit << value end value end |
#>=(other) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This is a compatibility method to allow uniform comparison between nodes and values.
131 132 133 |
# File 'lib/pdf/core/name_tree.rb', line 131 def >=(other) children.empty? || children.last >= other end |
#add(name, value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a value
70 71 72 |
# File 'lib/pdf/core/name_tree.rb', line 70 def add(name, value) self << Value.new(name, value) end |
#deep_copy ⇒ Node
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a deep copy of this node, without copying expensive things like the ‘ref` to `document`.
153 154 155 156 157 158 |
# File 'lib/pdf/core/name_tree.rb', line 153 def deep_copy node = dup node.instance_variable_set(:@children, Utils.deep_clone(children)) node.instance_variable_set(:@ref, node.ref ? node.ref.deep_copy : nil) node end |
#empty? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Tells whether there are any children nodes
47 48 49 |
# File 'lib/pdf/core/name_tree.rb', line 47 def empty? children.empty? end |
#greatest ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the greatest (in lexicographic order) value name.
98 99 100 101 102 103 104 |
# File 'lib/pdf/core/name_tree.rb', line 98 def greatest if leaf? children.last.name else children.last.greatest end end |
#leaf? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Tells whether this is a leaf node. A leaf node is the one that has no children or only Value children.
62 63 64 |
# File 'lib/pdf/core/name_tree.rb', line 62 def leaf? children.empty? || children.first.is_a?(Value) end |
#least ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the least (in lexicographic order) value name.
89 90 91 92 93 94 95 |
# File 'lib/pdf/core/name_tree.rb', line 89 def least if leaf? children.first.name else children.first.least end end |
#size ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Number of all (including nested) children nodes
54 55 56 |
# File 'lib/pdf/core/name_tree.rb', line 54 def size leaf? ? children.size : children.sum(&:size) end |
#split! ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Split the tree at the node.
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/pdf/core/name_tree.rb', line 138 def split! if parent parent.split(self) else left = new_node(self) right = new_node(self) split_children(self, left, right) children.replace([left, right]) end end |
#to_hash ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a hash representation of this node.
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/pdf/core/name_tree.rb', line 75 def to_hash hash = {} hash[:Limits] = [least, greatest] if parent if leaf? hash[:Names] = children if leaf? else hash[:Kids] = children.map(&:ref) end hash end |