ffi-radix_tree 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/ffi/radix_tree.rb +38 -38
- data/lib/ffi/radix_tree/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d36fd469f2823e6b56e5d8a4dab8ff7f9cd133f3
|
4
|
+
data.tar.gz: 5cfcb3ed80dc6b731c95e16270fe48a1f7558e1c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e08601eee9895a0a27bbaad76f44f51dc8f1dfd7f0768b23611734e34950a7edae76d45892a5e8316d4369614b4e32f5aca5ab591da0c326cecaaa828f2c0978
|
7
|
+
data.tar.gz: 80435e020cd98216c0699db2559b4b75d63925d70f2d1f1e8c603b4a7539349d446e3ff3305c53d917cfcdd83f60f77f5ce4392f3967a11ed041bb9b75da5e48
|
data/lib/ffi/radix_tree.rb
CHANGED
@@ -15,7 +15,7 @@ module FFI
|
|
15
15
|
begin
|
16
16
|
# bias the library discovery to a path inside the gem first, then
|
17
17
|
# to the usual system paths
|
18
|
-
gem_base = ::File.join(::File.dirname(__FILE__), '..', '..'
|
18
|
+
gem_base = ::File.join(::File.dirname(__FILE__), '..', '..')
|
19
19
|
inside_gem = ::File.join(gem_base, 'ext')
|
20
20
|
local_path = ::FFI::Platform::IS_WINDOWS ? ENV['PATH'].split(';') : ENV['PATH'].split(':')
|
21
21
|
env_path = [ ENV['RADIX_TREE_LIB_PATH'] ].compact
|
@@ -46,7 +46,7 @@ module FFI
|
|
46
46
|
'/usr/local/lib', '/opt/local/lib', homebrew_path, '/usr/lib64'
|
47
47
|
]
|
48
48
|
else
|
49
|
-
[::File.join(gem_base, "vendor/radixtree
|
49
|
+
[::File.join(gem_base, "vendor/radixtree")]
|
50
50
|
end
|
51
51
|
|
52
52
|
RADIX_TREE_LIB_PATHS = radixtree_lib_paths.
|
@@ -81,49 +81,49 @@ module FFI
|
|
81
81
|
attach_function :longest_prefix_value, [:pointer, :string, :pointer], :pointer
|
82
82
|
attach_function :match_free, [:pointer], :void
|
83
83
|
attach_function :has_key, [:pointer, :string], :bool
|
84
|
-
end
|
85
84
|
|
86
|
-
|
87
|
-
|
88
|
-
|
85
|
+
class Tree
|
86
|
+
DESTROY_METHOD = ::FFI::RadixTree.method(:destroy)
|
87
|
+
FREE_METHOD = ::FFI::RadixTree.method(:match_free)
|
89
88
|
|
90
|
-
|
91
|
-
|
92
|
-
|
89
|
+
def initialize
|
90
|
+
@ptr = ::FFI::AutoPointer.new(::FFI::RadixTree.create, DESTROY_METHOD)
|
91
|
+
end
|
93
92
|
|
94
|
-
|
95
|
-
|
96
|
-
|
93
|
+
def has_key?(key)
|
94
|
+
::FFI::RadixTree.has_key(@ptr, key)
|
95
|
+
end
|
97
96
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
97
|
+
def push(key, value)
|
98
|
+
storage_data = ::MessagePack.pack(value)
|
99
|
+
bytesize = storage_data.bytesize
|
100
|
+
memory_buffer = ::FFI::MemoryPointer.new(:char, bytesize, true)
|
101
|
+
memory_buffer.put_bytes(0, storage_data)
|
102
|
+
::FFI::RadixTree.insert(@ptr, key, memory_buffer, bytesize)
|
103
|
+
end
|
105
104
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
105
|
+
def get(key)
|
106
|
+
byte_length = ::FFI::MemoryPointer.new(:int)
|
107
|
+
byte_pointer = ::FFI::AutoPointer.new(::FFI::RadixTree.fetch(@ptr, key, byte_length), FREE_METHOD)
|
108
|
+
bytesize = byte_length.read_int
|
109
|
+
return nil if bytesize <= 0
|
110
|
+
::MessagePack.unpack(byte_pointer.get_bytes(0, bytesize))
|
111
|
+
end
|
113
112
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
113
|
+
def longest_prefix(string)
|
114
|
+
value, p_out = ::FFI::RadixTree.longest_prefix(@ptr, string)
|
115
|
+
p_out = ::FFI::AutoPointer.new(p_out, FREE_METHOD) unless p_out.nil?
|
116
|
+
value.force_encoding("UTF-8") unless value.nil?
|
117
|
+
value
|
118
|
+
end
|
120
119
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
120
|
+
def longest_prefix_value(string)
|
121
|
+
byte_length = ::FFI::MemoryPointer.new(:int)
|
122
|
+
byte_pointer = ::FFI::AutoPointer.new(::FFI::RadixTree.longest_prefix_value(@ptr, string, byte_length), FREE_METHOD)
|
123
|
+
bytesize = byte_length.read_int
|
124
|
+
return nil if bytesize <= 0
|
125
|
+
::MessagePack.unpack(byte_pointer.get_bytes(0, bytesize))
|
126
|
+
end
|
127
127
|
end
|
128
128
|
end
|
129
129
|
end
|