ndav 0.0.2 → 0.0.3
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/ndav/converter.rb +23 -0
- data/lib/ndav/ffi.rb +28 -43
- data/ndav.gemspec +1 -1
- data/test/test_ffi.rb +40 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 443c009b1751ab850206b12f6ae175989fef5bb3e9dfd6094d069b338262474a
|
|
4
|
+
data.tar.gz: fc5ef147dcbe89690add67f9aca9ed11a71a56ed2d801284936753066da2be2c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7ce73fa38eba65d0c1933cef3e787a6a770854847f7946098ca4ff7e26ba0e588ee6d5a1d79e469abb27a0c303722f69e9030943a73b5295e7a422c73dee96f5
|
|
7
|
+
data.tar.gz: 917725d70d4a3269da345d389b93562d54bec9b0911e88229a469a98d79203026ab37bbe834039aded9b1635c827d6723b5b82f23dcacd3864e9df0908dcab20
|
data/lib/ndav/converter.rb
CHANGED
|
@@ -3,6 +3,29 @@ class NDAV
|
|
|
3
3
|
alias from_memory_view new
|
|
4
4
|
alias from_fiddle_memory_view new
|
|
5
5
|
alias from_fiddle_pointer new
|
|
6
|
+
|
|
7
|
+
def register(cls, mdl, name:)
|
|
8
|
+
if mdl.const_defined?(:FromNDAV)
|
|
9
|
+
cls.extend mdl::FromNDAV
|
|
10
|
+
|
|
11
|
+
define_method "to_#{name}" do |*args, **kwargs|
|
|
12
|
+
cls.from_ndav(self, *args, **kwargs)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
if mdl.const_defined?(:ToNDAV)
|
|
17
|
+
cls.include mdl::ToNDAV
|
|
18
|
+
|
|
19
|
+
define_singleton_method "from_#{name}" do |array, *args, **kwargs|
|
|
20
|
+
array.to_ndav(*args, **kwargs)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
if mdl.const_defined?(:Converter)
|
|
25
|
+
Converter.prepend mdl::Converter
|
|
26
|
+
Converter.singleton_class.prepend mdl::Converter
|
|
27
|
+
end
|
|
28
|
+
end
|
|
6
29
|
end
|
|
7
30
|
|
|
8
31
|
module Converter
|
data/lib/ndav/ffi.rb
CHANGED
|
@@ -18,17 +18,24 @@ class NDAV
|
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
module ToNDAV
|
|
21
|
-
def to_ndav(format:
|
|
22
|
-
|
|
21
|
+
def to_ndav(format: TYPE_SIZE_TO_FORMAT[type_size], lifetime: self, **)
|
|
22
|
+
shape = [size / type_size]
|
|
23
|
+
byte_size = shape.reduce(ITEM_SIZES[format], :*)
|
|
24
|
+
ptr = ::Fiddle::Pointer.new(address, byte_size)
|
|
25
|
+
::NDAV.new(ptr, shape:, format:, lifetime:, **)
|
|
23
26
|
end
|
|
24
27
|
end
|
|
25
28
|
|
|
26
29
|
module Converter
|
|
27
|
-
def
|
|
28
|
-
if
|
|
29
|
-
array
|
|
30
|
+
def FFIMemoryPointer(array, *, **)
|
|
31
|
+
if ::FFI::MemoryPointer === array
|
|
32
|
+
array
|
|
33
|
+
elsif array.respond_to? :to_ffi_memory_pointer
|
|
34
|
+
array.to_ffi_memory_pointer(*, **)
|
|
35
|
+
elsif array.respond_to? :to_ndav
|
|
36
|
+
array.to_ndav(*, **).to_ffi_memory_pointer
|
|
30
37
|
else
|
|
31
|
-
|
|
38
|
+
::NDAV.new(array, *, **).to_ffi_memory_pointer
|
|
32
39
|
end
|
|
33
40
|
end
|
|
34
41
|
end
|
|
@@ -41,53 +48,31 @@ class NDAV
|
|
|
41
48
|
end
|
|
42
49
|
end
|
|
43
50
|
|
|
44
|
-
module
|
|
45
|
-
def to_ndav(format:
|
|
46
|
-
|
|
51
|
+
module ToNDAV
|
|
52
|
+
def to_ndav(shape:, format: TYPE_SIZE_TO_FORMAT[type_size], lifetime: self, **)
|
|
53
|
+
byte_size = shape.reduce(ITEM_SIZES[format], :*)
|
|
54
|
+
ptr = ::Fiddle::Pointer.new(address, byte_size)
|
|
55
|
+
::NDAV.new(ptr, shape:, format:, lifetime:, **)
|
|
47
56
|
end
|
|
48
57
|
end
|
|
49
58
|
|
|
50
59
|
module Converter
|
|
51
|
-
def
|
|
52
|
-
# use instance_of? instead of kind_of?
|
|
60
|
+
def FFIPointer(array, *, **)
|
|
61
|
+
# use instance_of? instead of kind_of? beacuase MemoryPointer is subclass of Pointer
|
|
53
62
|
if array.instance_of? ::FFI::Pointer
|
|
54
|
-
array
|
|
63
|
+
array
|
|
64
|
+
elsif array.respond_to? :to_ffi_pointer
|
|
65
|
+
array.to_ffi_pointer(*, **)
|
|
66
|
+
elsif array.respond_to? :to_ndav
|
|
67
|
+
array.to_ndav(*, **).to_ffi_pointer
|
|
55
68
|
else
|
|
56
|
-
|
|
69
|
+
::NDAV.new(array, *, **).to_ffi_pointer
|
|
57
70
|
end
|
|
58
71
|
end
|
|
59
72
|
end
|
|
60
73
|
end
|
|
61
74
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
byte_size = shape.reduce(ITEM_SIZES[format], :*)
|
|
65
|
-
ptr = ::Fiddle::Pointer.new(array.address, byte_size)
|
|
66
|
-
new(ptr, shape:, format:, **)
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
def from_ffi_pointer(array, lifetime:, **)
|
|
70
|
-
from_ffi_memory_pointer(array, lifetime:, **)
|
|
71
|
-
end
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
module ToFFI
|
|
75
|
-
def to_ffi_memory_pointer
|
|
76
|
-
::FFI::MemoryPointer.from_ndav(self)
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
def to_ffi_pointer
|
|
80
|
-
::FFI::Pointer.from_ndav(self)
|
|
81
|
-
end
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
::FFI::MemoryPointer.extend MemoryPointer::FromNDAV
|
|
85
|
-
::FFI::MemoryPointer.include MemoryPointer::ToNDAV
|
|
86
|
-
::FFI::Pointer.extend Pointer::FromNDAV
|
|
87
|
-
::FFI::Pointer.include Pointer::ToDAV
|
|
88
|
-
::NDAV.extend FromFFI
|
|
89
|
-
::NDAV.include ToFFI
|
|
90
|
-
::NDAV::Converter.singleton_class.prepend Pointer::Converter
|
|
91
|
-
::NDAV::Converter.singleton_class.prepend MemoryPointer::Converter
|
|
75
|
+
::NDAV.register ::FFI::MemoryPointer, MemoryPointer, name: :ffi_memory_pointer
|
|
76
|
+
::NDAV.register ::FFI::Pointer, Pointer, name: :ffi_pointer
|
|
92
77
|
end
|
|
93
78
|
end
|
data/ndav.gemspec
CHANGED
data/test/test_ffi.rb
CHANGED
|
@@ -57,6 +57,26 @@ class TestFFI < TestBase
|
|
|
57
57
|
assert_int_array ndav
|
|
58
58
|
end
|
|
59
59
|
|
|
60
|
+
def test_ffi_memory_pointer_from_ndav
|
|
61
|
+
ndav = ::NDAV.new(Arrow::Int16Array.new([1, 2, 3]))
|
|
62
|
+
|
|
63
|
+
assert_instance_of ::FFI::MemoryPointer, ::NDAV::Converter::FFIMemoryPointer(ndav)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def test_FFIMemoryPointer
|
|
67
|
+
c = Class.new {
|
|
68
|
+
include ::NDAV::Converter
|
|
69
|
+
|
|
70
|
+
def to_ffi_memory_pointer(ndav)
|
|
71
|
+
FFIMemoryPointer(ndav)
|
|
72
|
+
end
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
ndav = ::NDAV.new(Arrow::Int16Array.new([1, 2, 3]))
|
|
76
|
+
|
|
77
|
+
assert_instance_of ::FFI::MemoryPointer, c.new.to_ffi_memory_pointer(ndav)
|
|
78
|
+
end
|
|
79
|
+
|
|
60
80
|
def test_ndav_from_ffi_pointer
|
|
61
81
|
str = [1, 2, 3].pack("s*")
|
|
62
82
|
address = ::Fiddle::Pointer[str].to_i
|
|
@@ -65,5 +85,25 @@ class TestFFI < TestBase
|
|
|
65
85
|
|
|
66
86
|
assert_int_array ndav
|
|
67
87
|
end
|
|
88
|
+
|
|
89
|
+
def test_ffi_pointer_from_ndav
|
|
90
|
+
ndav = ::NDAV.new(Arrow::Int16Array.new([1, 2, 3]))
|
|
91
|
+
|
|
92
|
+
assert_instance_of ::FFI::Pointer, ::NDAV::Converter::FFIPointer(ndav)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def test_FFIPointer
|
|
96
|
+
c = Class.new {
|
|
97
|
+
include ::NDAV::Converter
|
|
98
|
+
|
|
99
|
+
def to_ffi_pointer(ndav)
|
|
100
|
+
FFIPointer(ndav)
|
|
101
|
+
end
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
ndav = ::NDAV.new(Arrow::Int16Array.new([1, 2, 3]))
|
|
105
|
+
|
|
106
|
+
assert_instance_of ::FFI::Pointer, c.new.to_ffi_pointer(ndav)
|
|
107
|
+
end
|
|
68
108
|
end
|
|
69
109
|
end
|