/usr/lib/ruby/vendor_ruby/ffi-glib/hash_table.rb is in ruby-gir-ffi 0.9.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | require 'ffi-glib/container_class_methods'
GLib.load_class :HashTable
module GLib
# Overrides for GHashTable, GLib's hash table implementation.
class HashTable
include Enumerable
extend ContainerClassMethods
attr_reader :key_type
attr_reader :value_type
def initialize(key_type, value_type)
@key_type = key_type
@value_type = value_type
store_pointer Lib.g_hash_table_new(
hash_function_for_key_type, equality_function_for_key_type)
end
# @api private
def self.from_enumerable(typespec, hash)
ghash = new(*typespec)
hash.each do |key, val|
ghash.insert key, val
end
ghash
end
def each
prc = proc do|keyptr, valptr, _userdata|
key = GirFFI::ArgHelper.cast_from_pointer key_type, keyptr
val = GirFFI::ArgHelper.cast_from_pointer value_type, valptr
yield key, val
end
callback = GLib::HFunc.from prc
::GLib::Lib.g_hash_table_foreach to_ptr, callback, nil
end
def to_hash
Hash[to_a]
end
# @override
def insert(key, value)
keyptr = GirFFI::InPointer.from key_type, key
valptr = GirFFI::InPointer.from value_type, value
::GLib::Lib.g_hash_table_insert to_ptr, keyptr, valptr
end
# @api private
def reset_typespec(typespec)
@key_type, @value_type = *typespec
self
end
private
def hash_function_for_key_type
case @key_type
when :utf8
FFI::Function.new(:uint,
[:pointer],
find_support_function('g_str_hash'))
end
end
def equality_function_for_key_type
case @key_type
when :utf8
FFI::Function.new(:int,
[:pointer, :pointer],
find_support_function('g_str_equal'))
end
end
def find_support_function(name)
lib = ::GLib::Lib.ffi_libraries.first
lib.find_function(name)
end
end
end
|