Class: VinaryTree::Libdictenstein::Dictionary

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/vinary_tree/libdictenstein.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointer) ⇒ Dictionary

Returns a new instance of Dictionary.



285
286
287
288
# File 'lib/vinary_tree/libdictenstein.rb', line 285

def initialize(pointer)
  @handle = ConcurrentHandle.new(pointer)
  ObjectSpace.define_finalizer(self, self.class.finalizer(@handle))
end

Instance Attribute Details

#handleObject (readonly)

Returns the value of attribute handle.



284
285
286
# File 'lib/vinary_tree/libdictenstein.rb', line 284

def handle
  @handle
end

Class Method Details

.finalizer(handle) ⇒ Object



289
# File 'lib/vinary_tree/libdictenstein.rb', line 289

def self.finalizer(handle) = proc { handle.close rescue nil }

Instance Method Details

#capabilitiesObject



306
# File 'lib/vinary_tree/libdictenstein.rb', line 306

def capabilities = scalar(:ldict_dictionary_capabilities, Native.u64_output, Native.method(:read_u64))

#closeObject



291
292
293
294
295
# File 'lib/vinary_tree/libdictenstein.rb', line 291

def close
  @handle.close
  ObjectSpace.undefine_finalizer(self)
  nil
end

#eachObject



319
320
321
322
323
324
# File 'lib/vinary_tree/libdictenstein.rb', line 319

def each
  stream = entry_stream
  return stream.each unless block_given?
  stream.each { |entry| yield entry }
  self
end

#entriesObject

Host-owned materialized snapshot idioms.



327
# File 'lib/vinary_tree/libdictenstein.rb', line 327

def entries = each.to_a

#entry_stream(max_entries: 256, max_units: 4096, max_values: 256) ⇒ Object



310
311
312
313
314
315
316
317
# File 'lib/vinary_tree/libdictenstein.rb', line 310

def entry_stream(max_entries: 256, max_units: 4096, max_values: 256)
  EntryStream.new(
    @handle,
    max_entries: max_entries,
    max_units: max_units,
    max_values: max_values
  )
end

#get(term) ⇒ Object



337
338
339
340
341
# File 'lib/vinary_tree/libdictenstein.rb', line 337

def get(term)
  found, value, present = Native.byte_output, Native.u64_output, Native.byte_output
  @handle.with_pointer { |pointer| Libdictenstein.check(Native.ldict_dictionary_get_text_value(pointer, term.b, term.bytesize, found, value, present)) }
  Lookup.new(found[0].positive?, present[0].positive? ? Native.read_u64(value) : nil)
end

#get_u64(tokens) ⇒ Object



349
350
351
352
353
# File 'lib/vinary_tree/libdictenstein.rb', line 349

def get_u64(tokens)
  packed = tokens.pack("Q*"); found, value, present = Native.byte_output, Native.u64_output, Native.byte_output
  @handle.with_pointer { |pointer| Libdictenstein.check(Native.ldict_dictionary_get_u64_value(pointer, packed, tokens.length, found, value, present)) }
  Lookup.new(found[0].positive?, present[0].positive? ? Native.read_u64(value) : nil)
end

#include?(term) ⇒ Boolean

Returns:

  • (Boolean)


331
332
333
334
335
# File 'lib/vinary_tree/libdictenstein.rb', line 331

def include?(term)
  output = Native.byte_output
  @handle.with_pointer { |pointer| Libdictenstein.check(Native.ldict_dictionary_contains_text(pointer, term.b, term.bytesize, output)) }
  output[0].positive?
end

#include_u64?(tokens) ⇒ Boolean

Returns:

  • (Boolean)


343
344
345
346
347
# File 'lib/vinary_tree/libdictenstein.rb', line 343

def include_u64?(tokens)
  packed = tokens.pack("Q*"); output = Native.byte_output
  @handle.with_pointer { |pointer| Libdictenstein.check(Native.ldict_dictionary_contains_u64(pointer, packed, tokens.length, output)) }
  output[0].positive?
end

#keysObject



328
# File 'lib/vinary_tree/libdictenstein.rb', line 328

def keys = entries.map(&:key)

#kindObject



305
# File 'lib/vinary_tree/libdictenstein.rb', line 305

def kind = scalar(:ldict_dictionary_kind, Native.u64_output, ->(output) { Native.read_u64(output) & 0xffff_ffff })

#lengthObject Also known as: size



307
# File 'lib/vinary_tree/libdictenstein.rb', line 307

def length = scalar(:ldict_dictionary_len, Native.size_output, Native.method(:read_size))

#valuesObject



329
# File 'lib/vinary_tree/libdictenstein.rb', line 329

def values = entries.map(&:value)

#with_resourceObject



297
298
299
300
301
302
303
# File 'lib/vinary_tree/libdictenstein.rb', line 297

def with_resource
  @handle.with_pointer do |pointer|
    resource = Native::Resource.malloc
    Libdictenstein.check(Native.ldict_dictionary_resource(pointer, resource))
    yield resource.context.to_i, resource.vtable.to_i
  end
end