Class: HarfBuzz::Set
Overview
Wraps hb_set_t — a set of Unicode codepoints or glyph IDs
Includes Enumerable for Ruby-friendly iteration.
Constant Summary collapse
- HB_SET_VALUE_INVALID =
C::HB_SET_VALUE_INVALID
Instance Attribute Summary collapse
-
#ptr ⇒ Object
readonly
Returns the value of attribute ptr.
Class Method Summary collapse
- .define_finalizer(obj, ptr) ⇒ Object
-
.empty ⇒ Set
Returns the singleton empty set.
- .wrap_borrowed(ptr) ⇒ Object
- .wrap_owned(ptr) ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
True if equal.
-
#add(value) ⇒ self
(also: #<<)
Adds a value to the set.
-
#add_range(first, last) ⇒ self
Adds a range of values (inclusive).
-
#add_sorted_array(arr) ⇒ self
Adds a sorted array of codepoints.
-
#clear ⇒ self
Clears all values.
-
#delete(value) ⇒ self
Removes a value.
-
#delete_range(first, last) ⇒ self
Removes a range of values (inclusive).
-
#each {|value| ... } ⇒ Enumerator
Iterates over all values in ascending order.
-
#each_range {|first, last| ... } ⇒ Enumerator
Iterates over contiguous ranges in ascending order.
-
#empty? ⇒ Boolean
True if empty.
-
#hash ⇒ Integer
Hash of this set.
-
#include?(value) ⇒ Boolean
(also: #member?)
True if value is in the set.
-
#initialize ⇒ Set
constructor
A new instance of Set.
- #inspect ⇒ Object
-
#intersect(other) ⇒ self
(also: #&)
Removes all elements not in another set (in-place intersection).
-
#invert! ⇒ self
Inverts the set (complements it).
-
#max ⇒ Integer?
Maximum value or nil if empty.
-
#min ⇒ Integer?
Minimum value or nil if empty.
-
#replace(other) ⇒ self
Replaces contents with another set.
-
#reverse_each {|value| ... } ⇒ Enumerator
Iterates over all values in descending order.
-
#size ⇒ Integer
(also: #length)
Number of elements.
-
#subset?(other) ⇒ Boolean
True if self is a subset of other.
-
#subtract(other) ⇒ self
(also: #-)
Removes all elements that are in another set (in-place subtraction).
-
#symmetric_difference(other) ⇒ Set
(also: #^)
Symmetric difference (elements in either but not both).
-
#to_a ⇒ Array<Integer>
Bulk-retrieves all values as an array.
-
#union(other) ⇒ self
(also: #|)
Adds all elements from another set (in-place union).
Constructor Details
#initialize ⇒ Set
Returns a new instance of Set.
14 15 16 17 18 19 |
# File 'lib/harfbuzz/set.rb', line 14 def initialize @ptr = C.hb_set_create raise AllocationError, "Failed to create set" if @ptr.null? HarfBuzz::Set.define_finalizer(self, @ptr) end |
Instance Attribute Details
#ptr ⇒ Object (readonly)
Returns the value of attribute ptr.
12 13 14 |
# File 'lib/harfbuzz/set.rb', line 12 def ptr @ptr end |
Class Method Details
.define_finalizer(obj, ptr) ⇒ Object
259 260 261 262 |
# File 'lib/harfbuzz/set.rb', line 259 def self.define_finalizer(obj, ptr) destroy = C.method(:hb_set_destroy) ObjectSpace.define_finalizer(obj, proc { destroy.call(ptr) }) end |
.empty ⇒ Set
Returns the singleton empty set
23 24 25 |
# File 'lib/harfbuzz/set.rb', line 23 def self.empty wrap_borrowed(C.hb_set_get_empty) end |
.wrap_borrowed(ptr) ⇒ Object
252 253 254 255 256 257 |
# File 'lib/harfbuzz/set.rb', line 252 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
245 246 247 248 249 250 |
# File 'lib/harfbuzz/set.rb', line 245 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.
102 103 104 105 106 |
# File 'lib/harfbuzz/set.rb', line 102 def ==(other) return false unless other.is_a?(Set) C.from_hb_bool(C.hb_set_is_equal(@ptr, other.ptr)) end |
#add(value) ⇒ self Also known as: <<
Adds a value to the set
50 51 52 53 |
# File 'lib/harfbuzz/set.rb', line 50 def add(value) C.hb_set_add(@ptr, value) self end |
#add_range(first, last) ⇒ self
Adds a range of values (inclusive)
61 62 63 64 |
# File 'lib/harfbuzz/set.rb', line 61 def add_range(first, last) C.hb_set_add_range(@ptr, first, last) self end |
#add_sorted_array(arr) ⇒ self
Adds a sorted array of codepoints
69 70 71 72 73 74 |
# File 'lib/harfbuzz/set.rb', line 69 def add_sorted_array(arr) mem = FFI::MemoryPointer.new(:uint32, arr.size) mem.put_array_of_uint32(0, arr) C.hb_set_add_sorted_array(@ptr, mem, arr.size) self end |
#clear ⇒ self
Clears all values
95 96 97 98 |
# File 'lib/harfbuzz/set.rb', line 95 def clear C.hb_set_clear(@ptr) self end |
#delete(value) ⇒ self
Removes a value
79 80 81 82 |
# File 'lib/harfbuzz/set.rb', line 79 def delete(value) C.hb_set_del(@ptr, value) self end |
#delete_range(first, last) ⇒ self
Removes a range of values (inclusive)
88 89 90 91 |
# File 'lib/harfbuzz/set.rb', line 88 def delete_range(first, last) C.hb_set_del_range(@ptr, first, last) self end |
#each {|value| ... } ⇒ Enumerator
Iterates over all values in ascending order
193 194 195 196 197 198 199 200 |
# File 'lib/harfbuzz/set.rb', line 193 def each return to_enum(:each) unless block_given? cp_ptr = FFI::MemoryPointer.new(:uint32) cp_ptr.write_uint32(HB_SET_VALUE_INVALID) yield cp_ptr.read_uint32 while C.from_hb_bool(C.hb_set_next(@ptr, cp_ptr)) self end |
#each_range {|first, last| ... } ⇒ Enumerator
Iterates over contiguous ranges in ascending order
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/harfbuzz/set.rb', line 205 def each_range return to_enum(:each_range) unless block_given? first_ptr = FFI::MemoryPointer.new(:uint32) last_ptr = FFI::MemoryPointer.new(:uint32) first_ptr.write_uint32(HB_SET_VALUE_INVALID) last_ptr.write_uint32(HB_SET_VALUE_INVALID) while C.from_hb_bool(C.hb_set_next_range(@ptr, first_ptr, last_ptr)) yield first_ptr.read_uint32, last_ptr.read_uint32 end self end |
#empty? ⇒ Boolean
Returns true if empty.
35 36 37 |
# File 'lib/harfbuzz/set.rb', line 35 def empty? C.from_hb_bool(C.hb_set_is_empty(@ptr)) end |
#hash ⇒ Integer
Returns Hash of this set.
109 110 111 |
# File 'lib/harfbuzz/set.rb', line 109 def hash C.hb_set_hash(@ptr) end |
#include?(value) ⇒ Boolean Also known as: member?
Returns true if value is in the set.
41 42 43 |
# File 'lib/harfbuzz/set.rb', line 41 def include?(value) C.from_hb_bool(C.hb_set_has(@ptr, value)) end |
#inspect ⇒ Object
241 242 243 |
# File 'lib/harfbuzz/set.rb', line 241 def inspect "#<HarfBuzz::Set size=#{size}>" end |
#intersect(other) ⇒ self Also known as: &
Removes all elements not in another set (in-place intersection)
141 142 143 144 145 |
# File 'lib/harfbuzz/set.rb', line 141 def intersect(other) result = dup_set C.hb_set_intersect(result.ptr, other.ptr) result end |
#invert! ⇒ self
Inverts the set (complements it)
173 174 175 176 |
# File 'lib/harfbuzz/set.rb', line 173 def invert! C.hb_set_invert(@ptr) self end |
#max ⇒ Integer?
Returns Maximum value or nil if empty.
185 186 187 188 |
# File 'lib/harfbuzz/set.rb', line 185 def max val = C.hb_set_get_max(@ptr) val == HB_SET_VALUE_INVALID ? nil : val end |
#min ⇒ Integer?
Returns Minimum value or nil if empty.
179 180 181 182 |
# File 'lib/harfbuzz/set.rb', line 179 def min val = C.hb_set_get_min(@ptr) val == HB_SET_VALUE_INVALID ? nil : val end |
#replace(other) ⇒ self
Replaces contents with another set
122 123 124 125 |
# File 'lib/harfbuzz/set.rb', line 122 def replace(other) C.hb_set_set(@ptr, other.ptr) self end |
#reverse_each {|value| ... } ⇒ Enumerator
Iterates over all values in descending order
221 222 223 224 225 226 227 228 |
# File 'lib/harfbuzz/set.rb', line 221 def reverse_each return to_enum(:reverse_each) unless block_given? cp_ptr = FFI::MemoryPointer.new(:uint32) cp_ptr.write_uint32(HB_SET_VALUE_INVALID) yield cp_ptr.read_uint32 while C.from_hb_bool(C.hb_set_previous(@ptr, cp_ptr)) self end |
#size ⇒ Integer Also known as: length
Returns Number of elements.
28 29 30 |
# File 'lib/harfbuzz/set.rb', line 28 def size C.hb_set_get_population(@ptr) end |
#subset?(other) ⇒ Boolean
Returns true if self is a subset of other.
115 116 117 |
# File 'lib/harfbuzz/set.rb', line 115 def subset?(other) C.from_hb_bool(C.hb_set_is_subset(@ptr, other.ptr)) end |
#subtract(other) ⇒ self Also known as: -
Removes all elements that are in another set (in-place subtraction)
152 153 154 155 156 |
# File 'lib/harfbuzz/set.rb', line 152 def subtract(other) result = dup_set C.hb_set_subtract(result.ptr, other.ptr) result end |
#symmetric_difference(other) ⇒ Set Also known as: ^
Symmetric difference (elements in either but not both)
163 164 165 166 167 |
# File 'lib/harfbuzz/set.rb', line 163 def symmetric_difference(other) result = dup_set C.hb_set_symmetric_difference(result.ptr, other.ptr) result end |
#to_a ⇒ Array<Integer>
Bulk-retrieves all values as an array
232 233 234 235 236 237 238 239 |
# File 'lib/harfbuzz/set.rb', line 232 def to_a return [] if empty? count = size buf = FFI::MemoryPointer.new(:uint32, count) actual = C.hb_set_next_many(@ptr, HB_SET_VALUE_INVALID, buf, count) buf.read_array_of_uint32(actual) end |
#union(other) ⇒ self Also known as: |
Adds all elements from another set (in-place union)
130 131 132 133 134 |
# File 'lib/harfbuzz/set.rb', line 130 def union(other) result = dup_set C.hb_set_union(result.ptr, other.ptr) result end |