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



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

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



17
18
19
# File 'lib/pdf/core/name_tree.rb', line 17

def children
  @children
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)


24
25
26
# File 'lib/pdf/core/name_tree.rb', line 24

def document
  @document
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)


21
22
23
# File 'lib/pdf/core/name_tree.rb', line 21

def limit
  @limit
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:



28
29
30
# File 'lib/pdf/core/name_tree.rb', line 28

def parent
  @parent
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:



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.

Parameters:

Returns:

  • (value)


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.

Returns:

  • (Boolean)

See Also:



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

Parameters:

  • name (String)
  • value (any)


70
71
72
# File 'lib/pdf/core/name_tree.rb', line 70

def add(name, value)
  self << Value.new(name, value)
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:



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

Returns:

  • (Boolean)


47
48
49
# File 'lib/pdf/core/name_tree.rb', line 47

def empty?
  children.empty?
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



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.

Returns:

  • (Boolean)


62
63
64
# File 'lib/pdf/core/name_tree.rb', line 62

def leaf?
  children.empty? || children.first.is_a?(Value)
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



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

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


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_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



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