multiarray 0.2.4 → 0.4.0

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.
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'rake/packagetask'
6
6
  require 'rbconfig'
7
7
 
8
8
  PKG_NAME = 'multiarray'
9
- PKG_VERSION = '0.2.4'
9
+ PKG_VERSION = '0.4.0'
10
10
  RB_FILES = FileList[ 'lib/**/*.rb' ]
11
11
  TC_FILES = FileList[ 'test/tc_*.rb' ]
12
12
  TS_FILES = FileList[ 'test/ts_*.rb' ]
@@ -20,7 +20,10 @@ HOMEPAGE = %q{http://wedesoft.github.com/multiarray/}
20
20
 
21
21
  $SITELIBDIR = Config::CONFIG[ 'sitelibdir' ]
22
22
 
23
- task :default do
23
+ task :default => :all
24
+
25
+ desc 'Do nothing (default)'
26
+ task :all do
24
27
  end
25
28
 
26
29
  desc 'Install Ruby extension'
data/lib/multiarray.rb CHANGED
@@ -1,24 +1,11 @@
1
- require 'malloc'
2
- require 'multiarray/storage'
3
- require 'multiarray/list'
4
- require 'multiarray/memory'
5
- require 'multiarray/type'
6
- require 'multiarray/type_operation'
7
- require 'multiarray/descriptortype'
8
- require 'multiarray/object'
9
- require 'multiarray/int_'
10
- require 'multiarray/int'
11
- require 'multiarray/composite_type'
12
- require 'multiarray/sequence_'
13
- require 'multiarray/sequence'
14
- require 'multiarray/sequence_operation'
15
- require 'multiarray/multiarray'
16
-
17
- # @private
1
+ # Proc#bind is defined if it does not exist already
18
2
  class Proc
19
3
 
20
- # @private
21
4
  unless method_defined? :bind
5
+
6
+ # Proc#bind is defined if it does not exist already
7
+ #
8
+ # @private
22
9
  def bind( object )
23
10
  block, time = self, Time.now
24
11
  ( class << object; self end ).class_eval do
@@ -29,18 +16,56 @@ class Proc
29
16
  method
30
17
  end.bind object
31
18
  end
19
+
32
20
  end
33
21
 
34
22
  end
35
23
 
36
- # @private
24
+ # Object#instance_exec is defined if it does not exist already
37
25
  class Object
38
26
 
39
- # @private
40
27
  unless method_defined? :instance_exec
28
+
29
+ # Object#instance_exec is defined if it does not exist already
30
+ #
31
+ # @private
41
32
  def instance_exec( *arguments, &block )
42
33
  block.bind( self )[ *arguments ]
43
34
  end
35
+
44
36
  end
45
37
 
46
38
  end
39
+
40
+ # Module#alias_method_chain is defined.
41
+ #
42
+ # @private
43
+ class Module
44
+
45
+ unless method_defined? :alias_method_chain
46
+
47
+ # Method for creating alias chains.
48
+ #
49
+ # @private
50
+ def alias_method_chain( target, feature, vocalize = target )
51
+ alias_method "#{vocalize}_without_#{feature}", target
52
+ alias_method target, "#{vocalize}_with_#{feature}"
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+
59
+ require 'malloc'
60
+ require 'multiarray/lazy'
61
+ require 'multiarray/list'
62
+ require 'multiarray/malloc'
63
+ require 'multiarray/type'
64
+ require 'multiarray/object'
65
+ require 'multiarray/int_'
66
+ require 'multiarray/int'
67
+ require 'multiarray/pointer_'
68
+ require 'multiarray/pointer'
69
+ require 'multiarray/sequence_'
70
+ require 'multiarray/sequence'
71
+ require 'multiarray/multiarray'
@@ -28,7 +28,7 @@ module Hornetseye
28
28
  # @see INT_.signed
29
29
  def INT( bits, signed )
30
30
  retval = Class.new INT_
31
- retval.bits = bits
31
+ retval.bits = bits
32
32
  retval.signed = signed
33
33
  retval
34
34
  end
@@ -5,10 +5,25 @@ module Hornetseye
5
5
  # @see #INT
6
6
  #
7
7
  # @abstract
8
- class INT_ < DescriptorType
8
+ class INT_ < Type
9
9
 
10
10
  class << self
11
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
+
12
27
  # The number of bits of native integers represented by this class
13
28
  #
14
29
  # @return [Integer] Number of bits of native integer.
@@ -28,73 +43,36 @@ module Hornetseye
28
43
  # @private
29
44
  attr_accessor :signed
30
45
 
31
- # Returns the type of storage object for storing values
32
- #
33
- # @return [Class] Returns +Memory+.
34
- #
35
- # @private
36
- def storage
37
- Memory
38
- end
39
-
40
- # Number of bytes for storing an object of this type
41
- #
42
- # @return [Integer] Number of bytes to store a native integer of this
43
- # type.
44
- #
45
- # @private
46
- def bytesize
47
- ( bits + 7 ).div 8
48
- end
49
-
50
- # Default value for integers
51
- #
52
- # @return [Object] Returns +0+.
53
- #
54
- # @private
55
- def default
56
- 0
57
- end
58
-
59
46
  # Get string with information about this type
60
47
  #
61
48
  # @return [String] Information about this integer type.
62
49
  def to_s
63
- case [ bits, signed ]
64
- when [ 8, true ]
65
- 'BYTE'
66
- when [ 8, false ]
67
- 'UBYTE'
68
- when [ 16, true ]
69
- 'SINT'
70
- when [ 16, false ]
71
- 'USINT'
72
- when [ 32, true ]
73
- 'INT'
74
- when [ 32, false ]
75
- 'UINT'
76
- when [ 64, true ]
77
- 'LONG'
78
- when [ 64, false ]
79
- 'ULONG'
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
80
71
  else
81
- "INT(#{bits.to_s},#{ signed ? "SIGNED" : "UNSIGNED" })"
72
+ super
82
73
  end
83
74
  end
84
75
 
85
- # Get string with information about this type
86
- #
87
- # @return [String] Information about this integer type.
88
- def inspect
89
- to_s
90
- end
91
-
92
- # Get descriptor for packing/unpacking native values
93
- #
94
- # @see DescriptorType.pack
95
- # @see DescriptorType.unpack
96
- #
97
- # @private
98
76
  def descriptor
99
77
  case [ bits, signed ]
100
78
  when [ 8, true ]
@@ -118,6 +96,55 @@ module Hornetseye
118
96
  end
119
97
  end
120
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 ==( other )
124
+ if other.is_a? Class
125
+ if other < INT_ and bits == other.bits and signed == other.signed
126
+ true
127
+ else
128
+ false
129
+ end
130
+ else
131
+ false
132
+ end
133
+ end
134
+
135
+ def hash
136
+ [ :INT_, bits, signed ].hash
137
+ end
138
+
139
+ def eql?( other )
140
+ self == other
141
+ end
142
+
143
+ end
144
+
145
+ def store( ptr )
146
+ ptr.write [ @value ].pack( self.class.descriptor )
147
+ self
121
148
  end
122
149
 
123
150
  end
@@ -0,0 +1,76 @@
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
+ elements = @values.collect { |value| value.element index }
33
+ Lazy.new( *( elements + [ :action => @action ] ) )
34
+ end
35
+
36
+ def -@
37
+ Lazy.new self, :action => proc { |x| -x }
38
+ end
39
+
40
+ def +@
41
+ self
42
+ end
43
+
44
+ def +( other )
45
+ Lazy.new self, other, :action => proc { |x,y| x + y }
46
+ end
47
+
48
+ end
49
+
50
+ def lazy
51
+ previous = Thread.current[ :lazy ]
52
+ Thread.current[ :lazy ] = true
53
+ begin
54
+ retval = yield
55
+ ensure
56
+ Thread.current[ :lazy ] = previous
57
+ end
58
+ retval
59
+ end
60
+
61
+ module_function :lazy
62
+
63
+ def eager
64
+ previous = Thread.current[ :lazy ]
65
+ Thread.current[ :lazy ] = false
66
+ begin
67
+ retval = yield
68
+ ensure
69
+ Thread.current[ :lazy ] = previous
70
+ end
71
+ retval
72
+ end
73
+
74
+ module_function :eager
75
+
76
+ end
@@ -1,99 +1,60 @@
1
1
  module Hornetseye
2
-
2
+
3
3
  # Class for creating views on Ruby arrays
4
4
  #
5
- # @see Storage
6
- # @see Memory
7
5
  # @private
8
- class List < Storage
9
-
10
- class << self
11
-
12
- # Create a +List+ object viewing a new Ruby array
13
- #
14
- # @param [Integer] size Number of elements the new Ruby array should
15
- # have.
16
- # @return [List] The new +List+ object.
17
- #
18
- # @private
19
- def alloc( size )
20
- new Array.new( size )
21
- end
22
-
23
- # Create a +List+ object viewing an existing Ruby array
24
- #
25
- # @param [Array<Object>] arr Existing Ruby array.
26
- # @return [List] The new +List+ object.
27
- #
28
- # @private
29
- def import( arr )
30
- new arr
31
- end
32
-
33
- end
6
+ class List
34
7
 
35
- # Offset of this view
8
+ # Create view on a Ruby array
9
+ #
10
+ # @param [Integer] n Number of elements of the view.
11
+ # @option options [Array<Object>] :array ([nil] * n) The Ruby array.
12
+ # @option options [Integer] :offset (0) Offset of the view.
36
13
  #
37
14
  # @private
38
- attr_accessor :offset
15
+ def initialize( n, options = {} )
16
+ @array = options[ :array ] || [ nil ] * n
17
+ @offset = options[ :offset ] || 0
18
+ end
39
19
 
40
- # Create zero-offset view on a Ruby array
20
+ # Display information about this object
41
21
  #
42
- # @param [Array<Object>] arr A Ruby array.
22
+ # @return [String] A string with information about the size of this view.
23
+ def inspect
24
+ "List(#{@array.size - @offset})"
25
+ end
26
+
27
+ # Retrieve and map element from the array
43
28
  #
44
- # @private
45
- def initialize( arr )
46
- super arr
47
- @offset = 0
29
+ # @param [Class] type Native data type to create
30
+ # @return [Type] The mapped element.
31
+ def fetch( type )
32
+ type.new read
48
33
  end
49
34
 
50
35
  # Retrieve an element from the array
51
36
  #
52
- # @param [Type] typecode This parameter is ignored.
53
37
  # @return [Object] The element from the array.
54
38
  #
55
- # @see #store
56
- # @see Memory#load
39
+ # @see #write
40
+ # @see Malloc#read
41
+ #
57
42
  # @private
58
- def load( typecode )
59
- @data[ @offset ]
43
+ def read
44
+ @array[ @offset ]
60
45
  end
61
46
 
62
47
  # Store an element in the array
63
48
  #
64
- # @param [Type] typecode This parameter is ignored.
65
49
  # @param [Object] value The Ruby object to store.
66
50
  # @return [Object] Returns the parameter +value+.
67
51
  #
68
- # @see #load
69
- # @see Memory#store
70
- # @private
71
- def store( typecode, value )
72
- @data[ @offset ] = value
73
- end
74
-
75
- # Store multiple elements in the array
76
- #
77
- # @param [Array<Object>] data A Ruby array with the new data.
78
- # @return [Array<Object>] The parameter +data+.
79
- #
80
- # @see #export
81
- # @see Memory#import
82
- # @private
83
- def import( data )
84
- @data[ @offset ... @offset + data.size ] = data
85
- end
86
-
87
- # Retrieve multiple elements from the array
88
- #
89
- # @param [Integer] size Number of elements to retrieve
90
- # @return [Array<Object>] A Ruby array with the elements.
52
+ # @see #read
53
+ # @see Malloc#write
91
54
  #
92
- # @see #import
93
- # @see Memory#export
94
55
  # @private
95
- def export( size )
96
- @data[ @offset ... @offset + size ]
56
+ def write( value )
57
+ @array[ @offset ] = value
97
58
  end
98
59
 
99
60
  # Create a new view with the specified offset
@@ -101,12 +62,10 @@ module Hornetseye
101
62
  # @param [Integer] offset A non-negative offset.
102
63
  # @return [List] A new view for the specified part of the array.
103
64
  #
104
- # @see Memory#+
65
+ # @see Malloc#+
105
66
  # @private
106
67
  def +( offset )
107
- retval = List.new @data
108
- retval.offset = @offset + offset
109
- retval
68
+ List.new 0, :array => @array, :offset => @offset + offset
110
69
  end
111
70
 
112
71
  end