Class: HarfBuzz::Map
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
-
#ptr ⇒ Object
readonly
Returns the value of attribute ptr.
Class Method Summary collapse
- .define_finalizer(obj, ptr) ⇒ Object
-
.empty ⇒ Map
Returns the singleton empty map.
- .wrap_borrowed(ptr) ⇒ Object
- .wrap_owned(ptr) ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
True if equal.
-
#[](key) ⇒ Integer
Value (HB_MAP_VALUE_INVALID if not found).
- #[]=(key, value) ⇒ Object
-
#allocation_successful? ⇒ Boolean
True if the last allocation was successful.
-
#clear ⇒ self
Clears all entries.
-
#delete(key) ⇒ Object
Deletes a key.
-
#each {|key, value| ... } ⇒ Enumerator
Iterates over key-value pairs.
-
#empty? ⇒ Boolean
True if empty.
-
#has_key?(key) ⇒ Boolean
(also: #include?, #key?)
True if key exists.
-
#hash ⇒ Integer
Hash of this map.
-
#initialize ⇒ Map
constructor
A new instance of Map.
- #inspect ⇒ Object
-
#keys ⇒ Set
Set of all keys.
-
#size ⇒ Integer
(also: #length)
Number of entries.
-
#update(other) ⇒ self
(also: #merge!)
Copies entries from another map into this one.
-
#values ⇒ Set
Set of all values.
Constructor Details
#initialize ⇒ Map
Returns a new instance of Map.
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
#ptr ⇒ Object (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 |
.empty ⇒ Map
Returns the singleton empty map
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.
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).
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
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.
99 100 101 |
# File 'lib/harfbuzz/map.rb', line 99 def allocation_successful? C.from_hb_bool(C.hb_map_allocation_successful(@ptr)) end |
#clear ⇒ self
Clears all entries
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
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
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.
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.
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 |
#hash ⇒ Integer
Returns Hash of this map.
84 85 86 |
# File 'lib/harfbuzz/map.rb', line 84 def hash C.hb_map_hash(@ptr) end |
#inspect ⇒ Object
134 135 136 |
# File 'lib/harfbuzz/map.rb', line 134 def inspect "#<HarfBuzz::Map size=#{size}>" end |
#keys ⇒ Set
Returns 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 |
#size ⇒ Integer Also known as: length
Returns 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
91 92 93 94 |
# File 'lib/harfbuzz/map.rb', line 91 def update(other) C.hb_map_update(@ptr, other.ptr) self end |