Class: HarfBuzz::Map

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/harfbuzz/map.rb

Overview

Wraps hb_map_t — an integer-to-integer mapping

Behaves similarly to Ruby's Hash with Integer keys and values. Includes Enumerable.

Constant Summary collapse

HB_MAP_VALUE_INVALID =
0xFFFFFFFF

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMap

Returns a new instance of Map.

Raises:



15
16
17
18
19
20
# File 'lib/harfbuzz/map.rb', line 15

def initialize
  @ptr = C.hb_map_create
  raise AllocationError, "Failed to create map" if @ptr.null?

  HarfBuzz::Map.define_finalizer(self, @ptr)
end

Instance Attribute Details

#ptrObject (readonly)

Returns the value of attribute ptr.



13
14
15
# File 'lib/harfbuzz/map.rb', line 13

def ptr
  @ptr
end

Class Method Details

.define_finalizer(obj, ptr) ⇒ Object



152
153
154
155
# File 'lib/harfbuzz/map.rb', line 152

def self.define_finalizer(obj, ptr)
  destroy = C.method(:hb_map_destroy)
  ObjectSpace.define_finalizer(obj, proc { destroy.call(ptr) })
end

.emptyMap

Returns the singleton empty map

Returns:



24
25
26
# File 'lib/harfbuzz/map.rb', line 24

def self.empty
  wrap_borrowed(C.hb_map_get_empty)
end

.wrap_borrowed(ptr) ⇒ Object



145
146
147
148
149
150
# File 'lib/harfbuzz/map.rb', line 145

def self.wrap_borrowed(ptr)
  obj = allocate
  obj.instance_variable_set(:@ptr, ptr)
  obj.instance_variable_set(:@borrowed, true)
  obj
end

.wrap_owned(ptr) ⇒ Object



138
139
140
141
142
143
# File 'lib/harfbuzz/map.rb', line 138

def self.wrap_owned(ptr)
  obj = allocate
  obj.instance_variable_set(:@ptr, ptr)
  define_finalizer(obj, ptr)
  obj
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if equal.

Parameters:

  • other (Map)

    Map to compare

Returns:

  • (Boolean)

    true if equal



77
78
79
80
81
# File 'lib/harfbuzz/map.rb', line 77

def ==(other)
  return false unless other.is_a?(Map)

  C.from_hb_bool(C.hb_map_is_equal(@ptr, other.ptr))
end

#[](key) ⇒ Integer

Returns Value (HB_MAP_VALUE_INVALID if not found).

Parameters:

  • key (Integer)

    Key

Returns:

  • (Integer)

    Value (HB_MAP_VALUE_INVALID if not found)



51
52
53
54
# File 'lib/harfbuzz/map.rb', line 51

def [](key)
  val = C.hb_map_get(@ptr, key)
  val == HB_MAP_VALUE_INVALID ? nil : val
end

#[]=(key, value) ⇒ Object

Parameters:

  • key (Integer)

    Key

  • value (Integer)

    Value



58
59
60
# File 'lib/harfbuzz/map.rb', line 58

def []=(key, value)
  C.hb_map_set(@ptr, key, value)
end

#allocation_successful?Boolean

Returns true if the last allocation was successful.

Returns:

  • (Boolean)

    true if the last allocation was successful



99
100
101
# File 'lib/harfbuzz/map.rb', line 99

def allocation_successful?
  C.from_hb_bool(C.hb_map_allocation_successful(@ptr))
end

#clearself

Clears all entries

Returns:

  • (self)


70
71
72
73
# File 'lib/harfbuzz/map.rb', line 70

def clear
  C.hb_map_clear(@ptr)
  self
end

#delete(key) ⇒ Object

Deletes a key

Parameters:

  • key (Integer)

    Key to delete



64
65
66
# File 'lib/harfbuzz/map.rb', line 64

def delete(key)
  C.hb_map_del(@ptr, key)
end

#each {|key, value| ... } ⇒ Enumerator

Iterates over key-value pairs

Yields:

  • (key, value)

Returns:

  • (Enumerator)

    if no block given



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/harfbuzz/map.rb', line 120

def each
  return to_enum(:each) unless block_given?

  idx_ptr = FFI::MemoryPointer.new(:int)
  idx_ptr.write_int(-1)
  key_ptr = FFI::MemoryPointer.new(:uint32)
  val_ptr = FFI::MemoryPointer.new(:uint32)

  while C.from_hb_bool(C.hb_map_next(@ptr, idx_ptr, key_ptr, val_ptr))
    yield key_ptr.read_uint32, val_ptr.read_uint32
  end
  self
end

#empty?Boolean

Returns true if empty.

Returns:

  • (Boolean)

    true if empty



36
37
38
# File 'lib/harfbuzz/map.rb', line 36

def empty?
  C.from_hb_bool(C.hb_map_is_empty(@ptr))
end

#has_key?(key) ⇒ Boolean Also known as: include?, key?

Returns true if key exists.

Parameters:

  • key (Integer)

    Key

Returns:

  • (Boolean)

    true if key exists



42
43
44
# File 'lib/harfbuzz/map.rb', line 42

def has_key?(key)
  C.from_hb_bool(C.hb_map_has(@ptr, key))
end

#hashInteger

Returns Hash of this map.

Returns:

  • (Integer)

    Hash of this map



84
85
86
# File 'lib/harfbuzz/map.rb', line 84

def hash
  C.hb_map_hash(@ptr)
end

#inspectObject



134
135
136
# File 'lib/harfbuzz/map.rb', line 134

def inspect
  "#<HarfBuzz::Map size=#{size}>"
end

#keysSet

Returns Set of all keys.

Returns:

  • (Set)

    Set of all keys



104
105
106
107
108
# File 'lib/harfbuzz/map.rb', line 104

def keys
  set = Set.new
  C.hb_map_keys(@ptr, set.ptr)
  set
end

#sizeInteger Also known as: length

Returns Number of entries.

Returns:

  • (Integer)

    Number of entries



29
30
31
# File 'lib/harfbuzz/map.rb', line 29

def size
  C.hb_map_get_population(@ptr)
end

#update(other) ⇒ self Also known as: merge!

Copies entries from another map into this one

Parameters:

  • other (Map)

    Source map

Returns:

  • (self)


91
92
93
94
# File 'lib/harfbuzz/map.rb', line 91

def update(other)
  C.hb_map_update(@ptr, other.ptr)
  self
end

#valuesSet

Returns Set of all values.

Returns:

  • (Set)

    Set of all values



111
112
113
114
115
# File 'lib/harfbuzz/map.rb', line 111

def values
  set = Set.new
  C.hb_map_values(@ptr, set.ptr)
  set
end