multiarray 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,81 +0,0 @@
1
- module Hornetseye
2
-
3
- class Lazy
4
-
5
- def initialize( *values )
6
- options = values.last.is_a?( Hash ) ? values.pop : {}
7
- @values = values
8
- if options[ :action ]
9
- @action = options[ :action ]
10
- else
11
- unless @values.size == 1
12
- raise "#{@values.size} value(s) where specified without defining " +
13
- ":action"
14
- end
15
- @action = proc { |*x| x.first }
16
- end
17
- end
18
-
19
- def inspect
20
- '<delayed>'
21
- end
22
-
23
- def force
24
- @action.call *@values.collect { |value| value.force }
25
- end
26
-
27
- def fetch( type )
28
- type.new Lazy.new( self, :action => proc { |x| x.fetch } )
29
- end
30
-
31
- def element( index )
32
- values = @values.collect { |value| value.element index }
33
- Lazy.new( *( values + [ :action => @action ] ) )
34
- end
35
-
36
- def elements( range )
37
- values = @values.collect { |value| value.elements range }
38
- Lazy.new( *( values + [ :action => @action ] ) )
39
- end
40
-
41
- def -@
42
- Lazy.new self, :action => proc { |x| -x }
43
- end
44
-
45
- def +@
46
- self
47
- end
48
-
49
- def +( other )
50
- Lazy.new self, other, :action => proc { |x,y| x + y }
51
- end
52
-
53
- end
54
-
55
- def lazy
56
- previous = Thread.current[ :lazy ]
57
- Thread.current[ :lazy ] = true
58
- begin
59
- retval = yield
60
- ensure
61
- Thread.current[ :lazy ] = previous
62
- end
63
- retval
64
- end
65
-
66
- module_function :lazy
67
-
68
- def eager
69
- previous = Thread.current[ :lazy ]
70
- Thread.current[ :lazy ] = false
71
- begin
72
- retval = yield
73
- ensure
74
- Thread.current[ :lazy ] = previous
75
- end
76
- retval
77
- end
78
-
79
- module_function :eager
80
-
81
- end
@@ -1,260 +0,0 @@
1
- module Hornetseye
2
-
3
- class Pointer_ < Type
4
-
5
- class << self
6
-
7
- attr_accessor :primitive
8
-
9
- def inspect
10
- if primitive
11
- if primitive < Sequence_
12
- primitive.inspect
13
- else
14
- "Pointer(#{primitive.inspect})"
15
- end
16
- else
17
- super
18
- end
19
- end
20
-
21
- def to_s
22
- if primitive
23
- if primitive < Sequence_
24
- primitive.to_s
25
- else
26
- "Pointer(#{primitive.to_s})"
27
- end
28
- else
29
- super
30
- end
31
- end
32
-
33
- def default
34
- if Thread.current[ :lazy ]
35
- Lazy.new :action => proc { self.class.primitive.typecode.new }
36
- else
37
- primitive.memory.new primitive.storage_size
38
- end
39
- end
40
-
41
- def dereference
42
- primitive.dereference
43
- end
44
-
45
- def to_type( typecode, options = {} )
46
- Hornetseye::Pointer primitive.to_type( typecode, options )
47
- end
48
-
49
- def coercion( other )
50
- if other < Pointer_
51
- Hornetseye::Pointer primitive.coercion( other.primitive )
52
- else
53
- Hornetseye::Pointer primitive.coercion( other )
54
- end
55
- end
56
-
57
- def coerce( other )
58
- if other < Pointer_
59
- return other, self
60
- else
61
- super other
62
- end
63
- end
64
-
65
- end
66
-
67
- def inspect( indent = nil, lines = nil )
68
- if self.class.primitive < Sequence_
69
- if @value.is_a? Lazy
70
- "#{self.class.primitive.inspect}:#{@value.inspect}"
71
- else
72
- if indent
73
- prepend = ''
74
- else
75
- prepend = "#{self.class.inspect}:\n"
76
- indent = 0
77
- lines = 0
78
- end
79
- if empty?
80
- retval = '[]'
81
- else
82
- retval = '[ '
83
- for i in 0 ... self.class.primitive.num_elements
84
- x = self[i]
85
- if x.is_a? Pointer_
86
- if i > 0
87
- retval += ",\n "
88
- lines += 1
89
- if lines >= 10
90
- retval += '...' if indent == 0
91
- break
92
- end
93
- retval += ' ' * indent
94
- end
95
- str = x.inspect indent + 1, lines
96
- lines += str.count "\n"
97
- retval += str
98
- if lines >= 10
99
- retval += '...' if indent == 0
100
- break
101
- end
102
- else
103
- retval += ', ' if i > 0
104
- str = x.inspect
105
- if retval.size + str.size >= 74 - '...'.size -
106
- '[ ]'.size * indent.succ
107
- retval += '...'
108
- break
109
- else
110
- retval += str
111
- end
112
- end
113
- end
114
- retval += ' ]' unless lines >= 10
115
- end
116
- prepend + retval
117
- end
118
- else
119
- super()
120
- end
121
- end
122
-
123
- def to_a
124
- if self.class.primitive < Sequence_
125
- ( 0 ... self.class.primitive.num_elements ).collect do |i|
126
- element( i ).to_a
127
- end
128
- else
129
- force.fetch.get
130
- end
131
- end
132
-
133
- def []( *args )
134
- if args.empty?
135
- if self.class.primitive < Sequence_
136
- self
137
- else
138
- fetch[]
139
- end
140
- else
141
- index = args.pop
142
- case index
143
- when Range
144
- elements( index )[ *args ]
145
- else
146
- element( index )[ *args ]
147
- end
148
- end
149
- end
150
-
151
- def []=( *args )
152
- value = args.pop
153
- if args.empty?
154
- if self.class.primitive < Sequence_
155
- if value.is_a? Array
156
- for i in 0 ... self.class.primitive.num_elements
157
- if i < value.size
158
- element( i )[] = value[ i ]
159
- else
160
- element( i )[] = self.class.primitive.typecode.default
161
- end
162
- end
163
- else
164
- operation( self.class.primitive.new( value ) ) { |x| set x }
165
- end
166
- else
167
- store self.class.primitive.new( value )
168
- end
169
- else
170
- index = args.pop
171
- element( index )[ *args ] = value
172
- end
173
- end
174
-
175
- def set( value )
176
- unless value.is_a?( List ) or value.is_a?( Malloc ) or
177
- value.is_a?( Lazy )
178
- store self.class.primitive.new( value )
179
- else
180
- super value
181
- end
182
- value
183
- end
184
-
185
- def fetch
186
- if self.class.primitive < Sequence_
187
- delay
188
- else
189
- get.fetch self.class.primitive
190
- end
191
- end
192
-
193
- def force
194
- if Thread.current[ :lazy ] or not @value.is_a? Lazy
195
- self
196
- else
197
- if self.class.primitive < Sequence_ and @value.is_a? Lazy
198
- self.class.new.operation( self ) { |x| set x }
199
- else
200
- super
201
- end
202
- end
203
- end
204
-
205
- def store( value )
206
- value.force.store @value
207
- end
208
-
209
- def element( index )
210
- target = Hornetseye::Pointer self.class.primitive.element_type
211
- if @value.is_a? Lazy
212
- target.new @value.element( index )
213
- else
214
- pointer = @value + index * self.class.primitive.stride *
215
- self.class.primitive.typecode.storage_size
216
- target.new pointer
217
- end
218
- end
219
-
220
- def elements( range )
221
- target = Sequence self.class.primitive.element_type, range.size,
222
- self.class.primitive.stride
223
- if @value.is_a? Lazy
224
- target.new @value.elements( range )
225
- else
226
- pointer = @value + range.min * self.class.primitive.stride *
227
- self.class.primitive.typecode.storage_size
228
- target.new pointer
229
- end
230
- end
231
-
232
- def empty?
233
- self.class.primitive.size == 0
234
- end
235
-
236
- def operation( *args, &action )
237
- if self.class.primitive < Sequence_
238
- if Thread.current[ :lazy ]
239
- super *args.collect { |arg| arg.delay }, &action
240
- else
241
- for i in 0 ... self.class.primitive.num_elements
242
- subargs = args.collect do |arg|
243
- if arg.is_a?( Pointer_ ) and arg.class.primitive < Sequence_
244
- arg.element( i ).fetch
245
- else
246
- arg.force.fetch
247
- end
248
- end
249
- element( i ).operation *subargs, &action
250
- end
251
- self
252
- end
253
- else
254
- super *args, &action
255
- end
256
- end
257
-
258
- end
259
-
260
- end
@@ -1,155 +0,0 @@
1
- module Hornetseye
2
-
3
- # Abstract class for representing multi-dimensional arrays
4
- #
5
- # @see #Sequence
6
- # @see #MultiArray
7
- # @see Sequence
8
- # @see MultiArray
9
- #
10
- # @abstract
11
- class Sequence_ < Type
12
-
13
- class << self
14
-
15
- # Get memory type for storing objects of this type
16
- #
17
- # @return [Class] Returns +element_type.memory+.
18
- #
19
- # @see Malloc
20
- #
21
- # @private
22
- def memory
23
- element_type.memory
24
- end
25
-
26
- # Type of elements this type is composed of
27
- #
28
- # @return [Type,Sequence_] The element type of this type.
29
- attr_accessor :element_type
30
-
31
- # Number of elements this type is composed of
32
- #
33
- # @return [Integer] The number of elements this type is composed of.
34
- attr_accessor :num_elements
35
-
36
- # Distance of two consecutive elements divided by size of single element
37
- #
38
- # @return [Integer] Stride size to iterate over array.
39
- #
40
- # @see #Sequence
41
- # @see List#+
42
- # @see Malloc#+
43
- #
44
- # @private
45
- attr_accessor :stride
46
-
47
- # Get string with information about this type
48
- #
49
- # @return [String] Information about this array type.
50
- def inspect
51
- if element_type and num_elements
52
- shortcut = element_type < Sequence_ ? 'MultiArray' : 'Sequence'
53
- typename = typecode.inspect
54
- if typename =~ /^[A-Z]+$/
55
- "#{shortcut}.#{typename.downcase}(#{shape.join ','})"
56
- else
57
- "#{shortcut}(#{typename},#{shape.join ','})"
58
- end
59
- else
60
- super
61
- end
62
- end
63
-
64
- # Get string with information about this type
65
- #
66
- # @return [String] Information about this array type.
67
- def to_s
68
- if element_type and num_elements
69
- shortcut = element_type < Sequence_ ? 'MultiArray' : 'Sequence'
70
- typename = typecode.to_s
71
- if typename =~ /^[A-Z]+$/
72
- "#{shortcut}.#{typename.downcase}(#{shape.join ','})"
73
- else
74
- "#{shortcut}(#{typename},#{shape.join ','})"
75
- end
76
- else
77
- super
78
- end
79
- end
80
-
81
- # Returns the element type of this array
82
- #
83
- # @return [Class] Returns +element_type.typecode+.
84
- def typecode
85
- element_type.typecode
86
- end
87
-
88
- def size
89
- num_elements * element_type.size
90
- end
91
-
92
- def shape
93
- element_type.shape + [ num_elements ]
94
- end
95
-
96
- def dimension
97
- element_type.dimension.succ
98
- end
99
-
100
- def storage_size
101
- element_type.storage_size * num_elements
102
- end
103
-
104
- def to_type( typecode, options = {} )
105
- options = { :preserve_strides => false }.merge options
106
- target_element = element_type.to_type typecode, options
107
- target_stride = options[ :preserve_strides ] ?
108
- stride : target_element.size
109
- Hornetseye::Sequence( target_element, num_elements,
110
- target_stride ).dereference
111
- end
112
-
113
- def coercion( other )
114
- if other < Sequence_
115
- Hornetseye::Sequence( element_type.coercion( other.element_type ),
116
- num_elements ).primitive
117
- else
118
- Hornetseye::Sequence( element_type.coercion( other ),
119
- num_elements ).primitive
120
- end
121
- end
122
-
123
- def coerce( other )
124
- if other < Sequence_
125
- return other, self
126
- else
127
- return Hornetseye::Sequence( other, num_elements ), self
128
- end
129
- end
130
-
131
- end
132
-
133
- module RubyMatching
134
-
135
- def fit( *values )
136
- n = values.inject( 0 ) do |size,value|
137
- value.is_a?( Array ) ? [ size, value.size ].max : size
138
- end
139
- if n > 0
140
- subvalues = values.inject( [] ) do |flat,value|
141
- flat + ( value.is_a?( Array ) ? value : [ value ] )
142
- end
143
- Hornetseye::Sequence fit( *subvalues ), n
144
- else
145
- super *values
146
- end
147
- end
148
-
149
- end
150
-
151
- Type.extend RubyMatching
152
-
153
- end
154
-
155
- end