multiarray 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/tc_sequence.rb CHANGED
@@ -1,3 +1,19 @@
1
+ # multiarray - Lazy multi-dimensional arrays for Ruby
2
+ # Copyright (C) 2010 Jan Wedekind
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
1
17
  require 'test/unit'
2
18
  begin
3
19
  require 'rubygems'
@@ -7,37 +23,17 @@ Kernel::require 'multiarray'
7
23
 
8
24
  class TC_Sequence < Test::Unit::TestCase
9
25
 
10
- O = Hornetseye::OBJECT
11
- U8 = Hornetseye::UBYTE
12
- S8 = Hornetseye::BYTE
13
- U16 = Hornetseye::USINT
14
- S16 = Hornetseye::SINT
15
- U32 = Hornetseye::UINT
16
- S32 = Hornetseye::INT
17
- U64 = Hornetseye::ULONG
18
- S64 = Hornetseye::LONG
19
- S = Hornetseye::Sequence
20
-
21
- T = [ O, U8, S8, U16, S16, U32, S32 ]
22
- INSPECT = {
23
- O => 'OBJECT',
24
- U8 => 'UBYTE', S8 => 'BYTE',
25
- U16 => 'USINT', S16 => 'SINT',
26
- U32 => 'UINT', S32 => 'INT'
27
- }
28
- SIGNED = {
29
- O => true,
30
- U8 => false, S8 => true,
31
- U16 => false, S16 => true,
32
- U32 => false, S32 => true
33
- }
34
-
35
- def lazy( &action )
36
- Hornetseye::lazy &action
37
- end
38
-
39
- def eager( &action )
40
- Hornetseye::eager &action
26
+ O = Hornetseye::OBJECT
27
+ B = Hornetseye::BOOL
28
+ I = Hornetseye::INT
29
+ S = Hornetseye::Sequence
30
+
31
+ def S( *args )
32
+ Hornetseye::Sequence *args
33
+ end
34
+
35
+ def sum( *args, &action )
36
+ Hornetseye::sum *args, &action
41
37
  end
42
38
 
43
39
  def setup
@@ -46,41 +42,58 @@ class TC_Sequence < Test::Unit::TestCase
46
42
  def teardown
47
43
  end
48
44
 
49
- def S( *args )
50
- Hornetseye::Sequence *args
45
+ def test_sequence_inspect
46
+ assert_equal 'Sequence(OBJECT,3)', S( O, 3 ).inspect
47
+ end
48
+
49
+ def test_sequence_to_s
50
+ assert_equal 'Sequence(OBJECT,3)', S( O, 3 ).to_s
51
51
  end
52
52
 
53
- def test_default
54
- T.each do |t|
55
- s = S( t, 3 ).new
56
- s[] = t.new[]
57
- assert_equal [ t.new[] ] * 3, s.to_a
58
- end
53
+ def test_sequence_default
54
+ assert_equal [ O.default ] * 3, S( O, 3 ).default.to_a
59
55
  end
60
56
 
61
- def test_sequence_inspect
62
- T.each do |t|
63
- assert_equal "Sequence.#{t.inspect.downcase}(3)", S( t, 3 ).inspect
64
- end
57
+ def test_sequence_indgen
58
+ assert_equal [ 0, 1, 2 ], S( O, 3 ).indgen.to_a
59
+ assert_equal [ 1, 2, 3 ], S( O, 3 ).indgen( 1 ).to_a
60
+ assert_equal [ 0, 2, 4 ], S( O, 3 ).indgen( 0, 2 ).to_a
61
+ assert_equal [ 1, 3, 5 ], S( O, 3 ).indgen( 1, 2 ).to_a
65
62
  end
66
63
 
67
- def test_sequence_to_s
68
- T.each do |t|
69
- assert_equal "Sequence.#{t.to_s.downcase}(3)", S( t, 3 ).to_s
70
- end
64
+ def test_sequence_at
65
+ assert_equal [ 1, 2, 3 ], S[ 1, 2, 3 ].to_a
66
+ assert_equal O, S[ :a ].typecode
67
+ assert_equal B, S[ false, true ].typecode
68
+ assert_equal I, S[ -2 ** 31, 2 ** 31 - 1 ].typecode
69
+ end
70
+
71
+ def test_sequence_typecode
72
+ assert_equal O, S( O, 3 ).typecode
71
73
  end
72
74
 
73
- def test_sequence_assign
74
- assert_equal [ :a, :b, :c ], S[ :a, :b, :c ].to_a
75
- assert_equal [ :a, :b, [ :c ] ], S[ :a, :b, [ :c ] ].to_a
75
+ def test_sequence_dimension
76
+ assert_equal 1, S( O, 3 ).dimension
77
+ end
78
+
79
+ def test_sequence_shape
80
+ assert_equal [ 3 ], S( O, 3 ).shape
76
81
  end
77
82
 
78
83
  def test_inspect
79
- assert_equal "Sequence.object(3):\n[ :a, :b, :c ]", S[ :a, :b, :c ].inspect
84
+ assert_equal "Sequence(OBJECT,3):\n[ :a, 2, 3 ]", S[ :a, 2, 3 ].inspect
80
85
  end
81
86
 
82
- def test_to_s
83
- # !!!
87
+ def test_typecode
88
+ assert_equal O, S.new( O, 3 ).typecode
89
+ end
90
+
91
+ def test_dimension
92
+ assert_equal 1, S[ 1, 2, 3 ].dimension
93
+ end
94
+
95
+ def test_shape
96
+ assert_equal [ 3 ], S[ 1, 2, 3 ].shape
84
97
  end
85
98
 
86
99
  def test_at_assign
@@ -88,48 +101,129 @@ class TC_Sequence < Test::Unit::TestCase
88
101
  for i in 0 ... 3
89
102
  assert_equal i + 1, s[ i ] = i + 1
90
103
  end
91
- assert_equal [ 1, 2, 3 ], s[].to_a
92
104
  for i in 0 ... 3
93
105
  assert_equal i + 1, s[ i ]
94
106
  end
95
107
  end
96
108
 
97
109
  def test_equal
98
- # !!!
110
+ assert_equal S[ 2, 3, 5 ], S[ 2, 3, 5 ]
111
+ assert_not_equal S[ 2, 3, 5 ], S[ 2, 3, 7 ]
112
+ #assert_not_equal S[ 2, 3, 5 ], S[ 2, 3 ] # !!!
113
+ #assert_not_equal S[ 2, 3, 5 ], S[ 2, 3, 5, 7 ]
114
+ assert_not_equal S[ 2, 2, 2 ], 2
115
+ end
116
+
117
+ def test_inject
118
+ assert_equal 6, S[ 1, 2, 3 ].inject { |a,b| a + b }
119
+ assert_equal 10, S[ 1, 2, 3 ].inject( 4 ) { |a,b| a + b }
120
+ end
121
+
122
+ def test_sum
123
+ assert_equal 6, sum { |i| S[ 1, 2, 3 ][ i ] }
124
+ assert_equal [ 1, 2, 3 ], sum { || S[ 1, 2, 3 ] }.to_a
125
+ end
126
+
127
+ def dont_test_convolve
128
+ assert_equal S[ 2, 3, 0, 0, 0 ],
129
+ S[ 1, 0, 0, 0, 0 ].convolve( S[ 1, 2, 3 ] )
130
+ assert_equal S[ 1, 2, 3, 0, 0 ],
131
+ S[ 0, 1, 0, 0, 0 ].convolve( S[ 1, 2, 3 ] )
132
+ assert_equal S[ 0, 1, 2, 3, 0 ],
133
+ S[ 0, 0, 1, 0, 0 ].convolve( S[ 1, 2, 3 ] )
134
+ assert_equal S[ 0, 0, 1, 2, 3 ],
135
+ S[ 0, 0, 0, 1, 0 ].convolve( S[ 1, 2, 3 ] )
136
+ assert_equal S[ 0, 0, 0, 1, 2 ],
137
+ S[ 0, 0, 0, 0, 1 ].convolve( S[ 1, 2, 3 ] )
138
+ assert_equal S[ 1, 2, 3, 0 ],
139
+ S[ 0, 1, 0, 0 ].convolve( S[ 1, 2, 3 ] )
140
+ end
141
+
142
+ def test_zero
143
+ assert_equal S[ false, true, false ], S[ -1, 0, 1 ].zero?
144
+ end
145
+
146
+ def test_nonzero
147
+ assert_equal S[ true, false, true ], S[ -1, 0, 1 ].nonzero?
148
+ end
149
+
150
+ def test_not
151
+ assert_equal [ true, false ], S[ false, true ].not.to_a
152
+ assert_equal [ true, false, false ], S[ 0, 1, 2 ].not.to_a
153
+ end
154
+
155
+ def test_and
156
+ assert_equal [ false, false ], S[ false, true ].and( false ).to_a
157
+ assert_equal [ false, false ], false.and( S[ false, true ] ).to_a
158
+ assert_equal [ false, true ], S[ false, true ].and( true ).to_a
159
+ assert_equal [ false, true ], true.and( S[ false, true ] ).to_a
160
+ assert_equal [ false, false, false, true ], S[ false, true, false, true ].
161
+ and( S[ false, false, true, true ] ).to_a
162
+ end
163
+
164
+ def test_or
165
+ assert_equal [ false, true ], S[ false, true ].or( false ).to_a
166
+ assert_equal [ false, true ], false.or( S[ false, true ] ).to_a
167
+ assert_equal [ true, true ], S[ false, true ].or( true ).to_a
168
+ assert_equal [ true, true ], true.or( S[ false, true ] ).to_a
169
+ assert_equal [ false, true, true, true ], S[ false, true, false, true ].
170
+ or( S[ false, false, true, true ] ).to_a
171
+ end
172
+
173
+ def test_bitwise_not
174
+ assert_equal [ 255, 254, 253 ], ( ~S[ 0, 1, 2 ] ).to_a
175
+ assert_equal [ 0, -1, -2, -3 ], ( ~S[ -1, 0, 1, 2 ] ).to_a
176
+ end
177
+
178
+ def test_bitwise_and
179
+ assert_equal [ 0, 1, 0 ], ( S[ 0, 1, 2 ] & 1 ).to_a
180
+ assert_equal [ 0, 1, 0 ], ( 1 & S[ 0, 1, 2 ] ).to_a
181
+ assert_equal [ 0, 1, 0, 2 ], ( S[ 0, 1, 2, 3 ] & S[ 4, 3, 1, 2 ] ).to_a
182
+ end
183
+
184
+ def test_bitwise_or
185
+ assert_equal [ 1, 1, 3 ], ( S[ 0, 1, 2 ] | 1 ).to_a
186
+ assert_equal [ 1, 1, 3 ], ( 1 | S[ 0, 1, 2 ] ).to_a
187
+ assert_equal [ 4, 3, 3, 3 ], ( S[ 0, 1, 2, 3 ] | S[ 4, 3, 1, 2 ] ).to_a
188
+ end
189
+
190
+ def test_bitwise_xor
191
+ assert_equal [ 1, 0, 3 ], ( S[ 0, 1, 2 ] ^ 1 ).to_a
192
+ assert_equal [ 1, 0, 3 ], ( 1 ^ S[ 0, 1, 2 ] ).to_a
193
+ assert_equal [ 4, 2, 3, 1 ], ( S[ 0, 1, 2, 3 ] ^ S[ 4, 3, 1, 2 ] ).to_a
194
+ end
195
+
196
+ def test_shl
197
+ assert_equal [ 2, 4, 6 ], ( S[ 1, 2, 3 ] << 1 ).to_a
198
+ assert_equal [ 6, 12, 24 ], ( 3 << S[ 1, 2, 3 ] ).to_a
199
+ assert_equal [ 8, 8, 6 ], ( S[ 1, 2, 3 ] << S[ 3, 2, 1 ] ).to_a
200
+ end
201
+
202
+ def test_shr
203
+ assert_equal [ 1, 2, 3 ], ( S[ 2, 4, 6 ] >> 1 ).to_a
204
+ assert_equal [ 12, 6, 3 ], ( 24 >> S[ 1, 2, 3 ] ).to_a
205
+ assert_equal [ 2, 1, 3 ], ( S[ 16, 4, 6 ] >> S[ 3, 2, 1 ] ).to_a
99
206
  end
100
207
 
101
208
  def test_negate
102
- s = S[ 1, 2, 3 ].to_type O
103
- assert_equal [ -1, -2, -3 ], ( -s ).to_a
209
+ assert_equal S[ -1, 2, -3 ], -S[ 1, -2, 3 ]
104
210
  end
105
211
 
106
212
  def test_plus
107
- s = S[ 1, 2, 3 ].to_type O
108
- assert_equal [ 2, 4, 6 ], ( s + s ).to_a
109
- assert_equal [ 2, 3, 4 ], ( s + O.new( 1 ) ).to_a
110
- assert_equal [ 2, 3, 4 ], ( O.new( 1 ) + s ).to_a
111
- end
112
-
113
- def test_lazy_unary
114
- s = S[ 1, 2, 3 ].to_type O
115
- u = lazy { -s }
116
- assert_equal 'Sequence.object(3):<delayed>', u.inspect
117
- assert_equal [ -1, -2, -3 ], u.force.to_a
118
- u = lazy { --s }
119
- assert_equal 'Sequence.object(3):<delayed>', u.inspect
120
- assert_equal [ 1, 2, 3 ], u.force.to_a
121
- u = -lazy { -s }
122
- assert_equal "Sequence.object(3):\n[ 1, 2, 3 ]", u.inspect
123
- assert_equal [ 1, 2, 3 ], u.to_a
124
- u = lazy { -lazy { -s } }
125
- assert_equal 'Sequence.object(3):<delayed>', u.inspect
126
- assert_equal [ 1, 2, 3 ], u.force.to_a
127
- u = eager { lazy { -s } }
128
- assert_equal 'Sequence.object(3):<delayed>', u.inspect
129
- assert_equal [ -1, -2, -3 ], u.force.to_a
130
- u = lazy { eager { -lazy { -s } } }
131
- assert_equal "Sequence.object(3):\n[ 1, 2, 3 ]", u.inspect
132
- assert_equal [ 1, 2, 3 ], u.to_a
213
+ assert_equal S[ 'ax', 'bx' ], S[ 'a', 'b' ] + 'x'
214
+ assert_equal S[ 'xa', 'xb' ], O.new( 'x' ) + S[ 'a', 'b' ]
215
+ assert_equal S[ 'ac', 'bd' ], S[ 'a', 'b' ] + S[ 'c', 'd' ]
216
+ assert_equal S[ 2, 3, 5 ], S[ 1, 2, 4 ] + 1
217
+ assert_equal S[ 2, 3, 5 ], 1 + S[ 1, 2, 4 ]
218
+ assert_equal S[ 2, 3, 5 ], S[ 1, 2, 3 ] + S[ 1, 1, 2 ]
219
+ end
220
+
221
+ def test_indgen
222
+ assert_equal [ 0, 1, 2 ], S( I, 3 ).indgen.to_a
223
+ assert_equal [ 1, 2, 3 ], ( S( I, 3 ).indgen + 1 ).to_a
224
+ assert_equal [ 1, 2, 3 ], S( I, 3 ).indgen( 1 ).to_a
225
+ assert_equal [ 1, 3, 5 ], ( 2 * S( I, 3 ).indgen + 1 ).to_a
226
+ assert_equal [ 1, 3, 5 ], S( I, 3 ).indgen( 1, 2 ).to_a
133
227
  end
134
228
 
135
229
  end
@@ -1,4 +1,21 @@
1
1
  #!/usr/bin/env ruby
2
+ # multiarray - Lazy multi-dimensional arrays for Ruby
3
+ # Copyright (C) 2010 Jan Wedekind
4
+ #
5
+ # This program is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
2
17
  require 'tc_object'
18
+ require 'tc_bool'
19
+ require 'tc_int'
3
20
  require 'tc_sequence'
4
21
  require 'tc_multiarray'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multiarray
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Wedekind
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-28 00:00:00 +00:00
12
+ date: 2010-06-18 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,17 @@ dependencies:
20
20
  requirements:
21
21
  - - ~>
22
22
  - !ruby/object:Gem::Version
23
- version: "0.2"
23
+ version: "1.0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
24
34
  version:
25
35
  description: This Ruby-extension defines Hornetseye::MultiArray and other native datatypes. Hornetseye::MultiArray provides multi-dimensional Ruby arrays with elements of same type. The extension is designed to be mostly compatible with Masahiro Tanaka's NArray. However it allows the definition of custom element types and operations on them. This work was also inspired by Ronald Garcia's boost::multi_array and by Todd Veldhuizen's Blitz++.
26
36
  email: jan@wedesoft.de
@@ -32,28 +42,41 @@ extra_rdoc_files: []
32
42
 
33
43
  files:
34
44
  - Rakefile
35
- - README
45
+ - README.md
36
46
  - COPYING
37
47
  - TODO
38
48
  - .document
39
- - lib/multiarray.rb
40
- - lib/multiarray/sequence_.rb
49
+ - lib/multiarray/object.rb
50
+ - lib/multiarray/operations.rb
51
+ - lib/multiarray/variable.rb
52
+ - lib/multiarray/gcctype.rb
53
+ - lib/multiarray/bool.rb
54
+ - lib/multiarray/element.rb
55
+ - lib/multiarray/unary.rb
56
+ - lib/multiarray/gcccache.rb
57
+ - lib/multiarray/int.rb
58
+ - lib/multiarray/gccfunction.rb
59
+ - lib/multiarray/diagonal.rb
60
+ - lib/multiarray/inject.rb
61
+ - lib/multiarray/list.rb
41
62
  - lib/multiarray/pointer.rb
42
- - lib/multiarray/multiarray.rb
43
- - lib/multiarray/lazy.rb
63
+ - lib/multiarray/lambda.rb
44
64
  - lib/multiarray/sequence.rb
45
- - lib/multiarray/list.rb
65
+ - lib/multiarray/multiarray.rb
66
+ - lib/multiarray/node.rb
46
67
  - lib/multiarray/malloc.rb
47
- - lib/multiarray/int.rb
48
- - lib/multiarray/pointer_.rb
49
- - lib/multiarray/type.rb
50
- - lib/multiarray/object.rb
51
- - lib/multiarray/int_.rb
68
+ - lib/multiarray/binary.rb
69
+ - lib/multiarray/index.rb
70
+ - lib/multiarray/gcccontext.rb
71
+ - lib/multiarray/lookup.rb
72
+ - lib/multiarray/gccvalue.rb
73
+ - lib/multiarray.rb
52
74
  - test/ts_multiarray.rb
75
+ - test/tc_sequence.rb
53
76
  - test/tc_multiarray.rb
77
+ - test/tc_bool.rb
54
78
  - test/tc_object.rb
55
79
  - test/tc_int.rb
56
- - test/tc_sequence.rb
57
80
  has_rdoc: yard
58
81
  homepage: http://wedesoft.github.com/multiarray/
59
82
  licenses: []
@@ -83,7 +106,8 @@ signing_key:
83
106
  specification_version: 3
84
107
  summary: Multi-dimensional and uniform Ruby arrays
85
108
  test_files:
109
+ - test/tc_sequence.rb
86
110
  - test/tc_multiarray.rb
111
+ - test/tc_bool.rb
87
112
  - test/tc_object.rb
88
113
  - test/tc_int.rb
89
- - test/tc_sequence.rb
data/README DELETED
@@ -1 +0,0 @@
1
- This Ruby-extension defines Hornetseye::MultiArray and other native datatypes. Hornetseye::MultiArray provides multi-dimensional Ruby arrays with elements of same type. The extension is designed to be mostly compatible with Masahiro Tanaka's NArray. However it allows the definition of custom element types and operations on them. This work was also inspired by Ronald Garcia's boost::multi_array and by Todd Veldhuizen's Blitz++.
@@ -1,198 +0,0 @@
1
- module Hornetseye
2
-
3
- # Abstract class for representing native integers
4
- #
5
- # @see #INT
6
- #
7
- # @abstract
8
- class INT_ < Type
9
-
10
- class << self
11
-
12
- # Get memory type for storing objects of this type
13
- #
14
- # @return [Class] Returns +Malloc+.
15
- #
16
- # @see Malloc
17
- #
18
- # @private
19
- def memory
20
- Malloc
21
- end
22
-
23
- def import( str )
24
- new str.unpack( descriptor ).first
25
- end
26
-
27
- # The number of bits of native integers represented by this class
28
- #
29
- # @return [Integer] Number of bits of native integer.
30
- #
31
- # @see signed
32
- #
33
- # @private
34
- attr_accessor :bits
35
-
36
- # A boolean indicating whether this is a signed integer or not
37
- #
38
- # @return [FalseClass,TrueClass] Boolean indicating whether this is a
39
- # signed integer.
40
- #
41
- # @see bits
42
- #
43
- # @private
44
- attr_accessor :signed
45
-
46
- # Get string with information about this type
47
- #
48
- # @return [String] Information about this integer type.
49
- def to_s
50
- if bits and signed != nil
51
- case [ bits, signed ]
52
- when [ 8, true ]
53
- 'BYTE'
54
- when [ 8, false ]
55
- 'UBYTE'
56
- when [ 16, true ]
57
- 'SINT'
58
- when [ 16, false ]
59
- 'USINT'
60
- when [ 32, true ]
61
- 'INT'
62
- when [ 32, false ]
63
- 'UINT'
64
- when [ 64, true ]
65
- 'LONG'
66
- when [ 64, false ]
67
- 'ULONG'
68
- else
69
- "INT(#{bits.inspect},#{ signed ? 'SIGNED' : 'UNSIGNED' })"
70
- end
71
- else
72
- super
73
- end
74
- end
75
-
76
- def descriptor
77
- case [ bits, signed ]
78
- when [ 8, true ]
79
- 'c'
80
- when [ 8, false ]
81
- 'C'
82
- when [ 16, true ]
83
- 's'
84
- when [ 16, false ]
85
- 'S'
86
- when [ 32, true ]
87
- 'i'
88
- when [ 32, false ]
89
- 'I'
90
- when [ 64, true ]
91
- 'q'
92
- when [ 64, false ]
93
- 'Q'
94
- else
95
- raise "No descriptor for packing/unpacking #{self}"
96
- end
97
- end
98
-
99
- # Get string with information about this type
100
- #
101
- # @return [String] Information about this integer type.
102
- def inspect
103
- to_s
104
- end
105
-
106
- # Default value for Ruby objects
107
- #
108
- # @return [Integer] Returns +0+.
109
- #
110
- # @private
111
- def default
112
- 0
113
- end
114
-
115
- #def fetch( ptr )
116
- # new ptr.read( storage_size ).unpack( descriptor ).first
117
- #end
118
-
119
- def storage_size
120
- ( bits + 7 ).div 8
121
- end
122
-
123
- def coercion( other )
124
- if other < INT_
125
- Hornetseye::INT [ bits, other.bits ].max, ( signed or other.signed )
126
- else
127
- super other
128
- end
129
- end
130
-
131
- def coerce( other )
132
- if other < INT_
133
- return other, self
134
- else
135
- super other
136
- end
137
- end
138
-
139
- def ==( other )
140
- if other.is_a? Class
141
- if other < INT_ and bits == other.bits and signed == other.signed
142
- true
143
- else
144
- false
145
- end
146
- else
147
- false
148
- end
149
- end
150
-
151
- def hash
152
- [ :INT_, bits, signed ].hash
153
- end
154
-
155
- def eql?( other )
156
- self == other
157
- end
158
-
159
- end
160
-
161
- def store( ptr )
162
- ptr.write [ @value ].pack( self.class.descriptor )
163
- self
164
- end
165
-
166
- module RubyMatching
167
-
168
- def fit( *values )
169
- if values.all? { |value| value.is_a? Integer }
170
- bits = 8
171
- ubits = 8
172
- signed = false
173
- values.each do |value|
174
- bits *= 2 until ( -2**(bits-1) ... 2**(bits-1) ).include? value
175
- if value < 0
176
- signed = true
177
- else
178
- ubits *= 2 until ( 0 ... 2**ubits ).include? value
179
- end
180
- end
181
- bits = signed ? bits : ubits
182
- if bits <= 64
183
- Hornetseye::INT bits, signed
184
- else
185
- super *values
186
- end
187
- else
188
- super *values
189
- end
190
- end
191
-
192
- end
193
-
194
- Type.extend RubyMatching
195
-
196
- end
197
-
198
- end