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,207 +0,0 @@
1
- module Hornetseye
2
-
3
- # This class is used to map Ruby objects to native data types
4
- #
5
- # @abstract
6
- class Type
7
-
8
- class << self
9
-
10
- # Returns the dereferenced type for pointers
11
- #
12
- # Returns the dereferenced type for pointers.
13
- # Otherwise it returns +self+.
14
- #
15
- # @return [Class] Returns +self+.
16
- def dereference
17
- self
18
- end
19
-
20
- # Returns the element type for arrays
21
- #
22
- # Returns +self+ if this is not an array.
23
- #
24
- # @return [Class] Returns +self+.
25
- def typecode
26
- self
27
- end
28
-
29
- # Dimensions of arrays
30
- #
31
- # Returns +[]+ if this is not an array.
32
- #
33
- # @return [Array] Returns +[]+.
34
- def shape
35
- []
36
- end
37
-
38
- def dimension
39
- 0
40
- end
41
-
42
- # Number of elements
43
- #
44
- # @return [Integer] Number of elements
45
- #
46
- # @return [Integer] Returns +1+.
47
- def size
48
- shape.inject( 1 ) { |a,b| a * b }
49
- end
50
-
51
- def to_type( typecode, options = {} )
52
- typecode
53
- end
54
-
55
- def coercion( other )
56
- if self == other
57
- self
58
- else
59
- x, y = other.coerce self
60
- x.coercion y
61
- end
62
- end
63
-
64
- def coerce( other )
65
- if self == other
66
- return other, self
67
- else
68
- raise "Cannot coerce #{self} and #{other.inspect}"
69
- end
70
- end
71
-
72
- def ===( other )
73
- ( other == self ) or ( other.is_a? self ) or ( other.class == self )
74
- end
75
-
76
- end
77
-
78
- # Create new instance of this type
79
- #
80
- # @param [Object] value Optional initial value for this instance.
81
- def initialize( value = self.class.default )
82
- set value
83
- end
84
-
85
- # Display type and value of this instance
86
- #
87
- # @return [String] Returns string with information about type and value.
88
- def inspect
89
- "#{self.class.inspect}(#{@value.inspect})"
90
- end
91
-
92
- # Display value of this instance
93
- #
94
- # @return [String] Returns string with the value of this instance.
95
- def to_s
96
- get.to_s
97
- end
98
-
99
- # Retrieve Ruby value of object
100
- #
101
- # @return [Object] Ruby value of native data type.
102
- def []
103
- get
104
- end
105
-
106
- # Set Ruby value of object
107
- #
108
- # @param [Object] value New Ruby value for native data type.
109
- #
110
- # @return [Object] The parameter +value+ or the default value.
111
- def []=( value )
112
- set value
113
- end
114
-
115
- # Retrieve Ruby value of object
116
- #
117
- # @return [Object] Ruby value of native data type.
118
- #
119
- # @private
120
- def get
121
- delay.instance_exec { @value }
122
- end
123
-
124
- # Set Ruby value of object
125
- #
126
- # Set to specified value.
127
- #
128
- # @param [Object] value New Ruby value for native data type.
129
- #
130
- # @return [Object] The parameter +value+ or the default value.
131
- #
132
- # @private
133
- def set( value )
134
- @value = value
135
- end
136
-
137
- def fetch
138
- self
139
- end
140
-
141
- def operation( *args, &action )
142
- instance_exec *args.collect { |arg| arg.force.fetch.get }, &action
143
- self
144
- end
145
-
146
- def delay
147
- if not Thread.current[ :lazy ] or @value.is_a? Lazy
148
- self
149
- else
150
- self.class.new Lazy.new( self )
151
- end
152
- end
153
-
154
- def force
155
- if Thread.current[ :lazy ] or not @value.is_a? Lazy
156
- self
157
- else
158
- @value.force
159
- end
160
- end
161
-
162
- def to_type( target, options = {} )
163
- self.class.to_type( target, options ).new.operation( self ) { |x| set x }
164
- end
165
-
166
- def -@
167
- if is_a?( Pointer_ ) and self.class.primitive < Sequence_
168
- retval = self.class.new
169
- else
170
- retval = self.class.dereference.new
171
- end
172
- retval.operation( self ) { |x| set -x }
173
- retval
174
- end
175
-
176
- def +@
177
- self
178
- end
179
-
180
- def +( other )
181
- target = self.class.coercion( other.class )
182
- if target < Pointer_ and target.primitive < Sequence_
183
- retval = target.new
184
- else
185
- retval = target.dereference.new
186
- end
187
- retval.operation( self, other ) { |x,y| set x + y }
188
- retval
189
- end
190
-
191
- def match( value, context = nil )
192
- retval = fit value
193
- retval = retval.align context if context
194
- retval
195
- end
196
-
197
- def ==( other )
198
- if other.class == self.class
199
- other.get == get
200
- else
201
- false
202
- end
203
- end
204
-
205
- end
206
-
207
- end