Class: PDF::Core::NameTree::Node Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • document (Prawn::Document)

    owning document

  • limit (Integer)

    Children limit

  • parent (Node) (defaults to: nil)

    Parent node

Source Code
lib/pdf/core/name_tree.rb, line 36
36
def initialize(document, limit, parent = nil)
37
  @document = document
38
  @children = []
39
  @limit = limit
40
  @parent = parent
41
  @ref = nil
42
end

Instance Attribute Details

#childrenArray<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

Returns:

Source Code
lib/pdf/core/name_tree.rb, line 17
17
def children
18
  @children
19
end

#documentPrawn::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.

Returns:

  • (Prawn::Document)
Source Code
lib/pdf/core/name_tree.rb, line 24
24
def document
25
  @document
26
end

#limitInteger (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

Returns:

  • (Integer)
Source Code
lib/pdf/core/name_tree.rb, line 21
21
def limit
22
  @limit
23
end

#parentNode

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

Returns:

Source Code
lib/pdf/core/name_tree.rb, line 28
28
def parent
29
  @parent
30
end

#refReference

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:

Source Code
lib/pdf/core/name_tree.rb, line 31
31
def ref
32
  @ref
33
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.

Parameters:

Returns:

  • (value)
Source Code
lib/pdf/core/name_tree.rb, line 110
110
def <<(value)
111
  if children.empty?
112
    children << value
113
  elsif leaf?
114
    children.insert(insertion_point(value), value)
115
    split! if children.length > limit
116
  else
117
    fit = children.find { |child| child >= value }
118
    fit ||= children.last
119
    fit << value
120
  end
121
122
  value
123
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.

Returns:

  • (Boolean)

See Also:

Source Code
lib/pdf/core/name_tree.rb, line 131
131
def >=(other)
132
  children.empty? || children.last >= other
133
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

Parameters:

  • name (String)
  • value (any)
Source Code
lib/pdf/core/name_tree.rb, line 70
70
def add(name, value)
71
  self << Value.new(name, value)
72
end

#deep_copyNode

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.

Returns:

Source Code
lib/pdf/core/name_tree.rb, line 153
153
def deep_copy
154
  node = dup
155
  node.instance_variable_set(:@children, Utils.deep_clone(children))
156
  node.instance_variable_set(:@ref, node.ref ? node.ref.deep_copy : nil)
157
  node
158
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

Returns:

  • (Boolean)
Source Code
lib/pdf/core/name_tree.rb, line 47
47
def empty?
48
  children.empty?
49
end

#greatestString

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.

Returns:

  • (String)

    the greatest (in lexicographic order) value name

Source Code
lib/pdf/core/name_tree.rb, line 98
98
def greatest
99
  if leaf?
100
    children.last.name
101
  else
102
    children.last.greatest
103
  end
104
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.

Returns:

  • (Boolean)
Source Code
lib/pdf/core/name_tree.rb, line 62
62
def leaf?
63
  children.empty? || children.first.is_a?(Value)
64
end

#leastString

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.

Returns:

  • (String)

    the least (in lexicographic order) value name

Source Code
lib/pdf/core/name_tree.rb, line 89
89
def least
90
  if leaf?
91
    children.first.name
92
  else
93
    children.first.least
94
  end
95
end

#sizeInteger

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

Returns:

  • (Integer)
Source Code
lib/pdf/core/name_tree.rb, line 54
54
def size
55
  leaf? ? children.size : children.sum(&:size)
56
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.

Source Code
lib/pdf/core/name_tree.rb, line 138
138
def split!
139
  if parent
140
    parent.split(self)
141
  else
142
    left = new_node(self)
143
    right = new_node(self)
144
    split_children(self, left, right)
145
    children.replace([left, right])
146
  end
147
end

#to_hashHash

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.

Returns:

  • (Hash)

    a hash representation of this node

Source Code
lib/pdf/core/name_tree.rb, line 75
75
def to_hash
76
  hash = {}
77
78
  hash[:Limits] = [least, greatest] if parent
79
  if leaf?
80
    hash[:Names] = children if leaf?
81
  else
82
    hash[:Kids] = children.map(&:ref)
83
  end
84
85
  hash
86
end