class HexaPDF::Utils::ObjectHash

There are some structures in a PDF file, for example cross reference tables, that index data based on object and generation numbers. However, there is a restriction that in such structures the object numbers must be unique, e.g. there may not be entries for [1, 0] and [1, 1] at the same time.

This class can be used for storing/retrieving data for such structures.

Attributes

max_oid[R]

The biggest object number that is stored in the object hash or zero if no objects are stored.

Public Class Methods

new() click to toggle source

Creates a new object hash.

# File lib/hexapdf/utils/object_hash.rb, line 54
def initialize
  @table = {}
  @oids = {}
  @max_oid = 0
end

Public Instance Methods

objhash[oid] → data or nil click to toggle source
objhash[oid, gen] → data or nil

Returns the data for the given object number, or for the given object and generation numbers.

If there is no such data, nil is returned.

# File lib/hexapdf/utils/object_hash.rb, line 81
def [](oid, gen = nil)
  (gen.nil? || gen_for_oid(oid) == gen || nil) && @table[oid]
end
objhash[oid, gen] = data click to toggle source

Sets the data for the given object and generation numbers.

If there is already an entry for the given object number (even if the generation number is different), this entry will be removed.

# File lib/hexapdf/utils/object_hash.rb, line 67
def []=(oid, gen, data)
  @table[oid] = data
  @oids[oid] = gen
  @max_oid = oid if oid > @max_oid
end
delete(oid) click to toggle source

Deletes the entry for the given object number.

# File lib/hexapdf/utils/object_hash.rb, line 105
def delete(oid)
  @table.delete(oid)
  @oids.delete(oid)
  @max_oid = oids.max || 0 if oid == @max_oid
end
each {|oid, gen, data| block } → objhash click to toggle source
each → Enumerator

Calls the given block once for every entry, passing an array consisting of the object and generation number and the associated data as arguments.

# File lib/hexapdf/utils/object_hash.rb, line 117
def each
  return to_enum(__method__) unless block_given?
  @oids.keys.each {|oid| yield(oid, @oids[oid], @table[oid]) if @table.key?(oid) }
  self
end
entry?(oid) → true or false click to toggle source
entry?(oid, gen) → true or false

Returns true if there is an entry for the given object number, or for the given object and generation numbers.

# File lib/hexapdf/utils/object_hash.rb, line 100
def entry?(oid, gen = nil)
  (gen ? gen_for_oid(oid) == gen : @oids.key?(oid))
end
gen_for_oid(oid) → Integer or nil click to toggle source

Returns the generation number that is stored along the given object number, or nil if the object number is not used.

# File lib/hexapdf/utils/object_hash.rb, line 90
def gen_for_oid(oid)
  @oids[oid]
end
oids() click to toggle source

Returns all used object numbers as an array.

# File lib/hexapdf/utils/object_hash.rb, line 124
def oids
  @oids.keys
end