multiarray 0.2.2 → 0.2.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.
data/.document ADDED
@@ -0,0 +1 @@
1
+ lib/**/*.rb
data/Makefile CHANGED
@@ -2,7 +2,7 @@
2
2
  .SUFFIXES: .gem .o .cc .hh .rb .tar .gz .bz2
3
3
 
4
4
  RUBY_VERSION = 1.8
5
- MULTIARRAY_VERSION = 0.2.2
5
+ MULTIARRAY_VERSION = 0.2.3
6
6
 
7
7
  CP = cp
8
8
  RM = rm -Rf
@@ -15,7 +15,7 @@ TAR = tar
15
15
  GIT = git
16
16
  SITELIBDIR = $(shell $(RUBY) -r mkmf -e "puts \"\#{Config::CONFIG['sitelibdir']}\"")
17
17
 
18
- MAIN = Makefile source.gemspec README COPYING
18
+ MAIN = Makefile source.gemspec README COPYING .document
19
19
  LIB = $(wildcard lib/*.rb)
20
20
  PKG_LIB = $(wildcard lib/multiarray/*.rb)
21
21
  TEST = $(wildcard test/*.rb)
@@ -44,7 +44,7 @@ uninstall-gem::
44
44
  $(GEM) uninstall multiarray || echo Nothing to uninstall
45
45
 
46
46
  yardoc:: README $(LIB)
47
- $(YARDOC)
47
+ $(YARDOC) --no-private
48
48
 
49
49
  check:: $(LIB) $(PKG_LIB) $(TEST)
50
50
  $(RUBY) -rrubygems -Ilib -Itest test/ts_multiarray.rb
data/lib/multiarray.rb CHANGED
@@ -6,14 +6,17 @@ require 'multiarray/type'
6
6
  require 'multiarray/type_operation'
7
7
  require 'multiarray/descriptortype'
8
8
  require 'multiarray/int_'
9
+ require 'multiarray/int'
9
10
  require 'multiarray/composite_type'
10
11
  require 'multiarray/sequence_'
11
12
  require 'multiarray/sequence'
12
13
  require 'multiarray/sequence_operation'
13
14
  require 'multiarray/multiarray'
14
15
 
16
+ # @private
15
17
  class Proc
16
18
 
19
+ # @private
17
20
  unless method_defined? :bind
18
21
  def bind( object )
19
22
  block, time = self, Time.now
@@ -29,8 +32,10 @@ class Proc
29
32
 
30
33
  end
31
34
 
35
+ # @private
32
36
  class Object
33
37
 
38
+ # @private
34
39
  unless method_defined? :instance_exec
35
40
  def instance_exec( *arguments, &block )
36
41
  block.bind( self )[ *arguments ]
@@ -1,30 +1,63 @@
1
1
  module Hornetseye
2
2
 
3
+ # Abstract class for arrays and composite numbers
4
+ #
5
+ # @abstract
3
6
  class CompositeType < Type
4
7
 
5
8
  class << self
6
9
 
10
+ # Type of elements this type is composed of
11
+ #
12
+ # @return [Type,Sequence_] The element type of this type.
7
13
  attr_accessor :element_type
14
+
15
+ # Number of elements this type is composed of
16
+ #
17
+ # @return [Integer] The number of elements this type is composed of.
8
18
  attr_accessor :num_elements
9
19
 
20
+ # Returns the type of storage object for storing values
21
+ #
22
+ # @return [Class] Returns the storage type for the element type.
23
+ #
24
+ # @private
10
25
  def memory
11
26
  element_type.memory
12
27
  end
13
28
 
29
+ # Number of bytes for storing an object of this type
30
+ #
31
+ # @return [Integer] Number of bytes to store +num_elements+ elements of
32
+ # type +element_type+.
33
+ #
34
+ # @private
14
35
  def bytesize
15
36
  element_type.bytesize * num_elements
16
37
  end
17
38
 
39
+ # Returns the element type of this composite type
40
+ #
41
+ # @return [Class] Returns +element_type.basetype+.
42
+ #
43
+ # @private
18
44
  def basetype
19
45
  element_type.basetype
20
46
  end
21
47
 
22
48
  end
23
49
 
50
+ # The element type of this object's type
51
+ #
52
+ # @return [Type,Sequence_] The element type of this object's type.
24
53
  def element_type
25
54
  self.class.element_type
26
55
  end
27
56
 
57
+ # The number of elements this object's type is composed of
58
+ #
59
+ # @return [Integer] The number of elements this object's type is composed
60
+ # of.
28
61
  def num_elements
29
62
  self.class.num_elements
30
63
  end
@@ -1,13 +1,37 @@
1
1
  module Hornetseye
2
2
 
3
+ # Abstract class for describing scalar data types
4
+ #
5
+ # This class is for descriping scalar data types which are known to the Ruby
6
+ # standard library. I.e. there is a descriptor for +Array#pack+ and
7
+ # +String#unpack+.
8
+ #
9
+ # @see Array#pack
10
+ # @see String#unpack
11
+ #
12
+ # @abstract
3
13
  class DescriptorType < Type
4
14
 
5
15
  class << self
6
16
 
17
+ # Convert a Ruby object to a string containing the native representation
18
+ #
19
+ # @return [String] A string with the native representation of the value.
20
+ #
21
+ # @see Array#pack
22
+ #
23
+ # @private
7
24
  def pack( value )
8
25
  [ value ].pack descriptor
9
26
  end
10
27
 
28
+ # Convert a string with the native representation to a Ruby object
29
+ #
30
+ # @return [Object] The corresponding Ruby object to the native value.
31
+ #
32
+ # @see String#unpack
33
+ #
34
+ # @private
11
35
  def unpack( value )
12
36
  value.unpack( descriptor ).first
13
37
  end
@@ -0,0 +1,62 @@
1
+ module Hornetseye
2
+
3
+ # Boolean constant to use as a parameter for creating integer classes
4
+ #
5
+ # The value is +false+.
6
+ #
7
+ # @see #INT
8
+ UNSIGNED = false
9
+
10
+ # Boolean constant to use as a parameter for creating integer classes
11
+ #
12
+ # The value is +true+.
13
+ #
14
+ # @see #INT
15
+ SIGNED = true
16
+
17
+ # Create a class deriving from +INT_+
18
+ #
19
+ # The parameters +bits+ and +signed+ are assigned to the corresponding
20
+ # attributes of the resulting class.
21
+ #
22
+ # @param [Integer] bits Number of bits of native integer.
23
+ # @param [FalseClass,TrueClass] signed Specify +UNSIGNED+ or +SIGNED+ here.
24
+ # @return [Class] A class deriving from +INT_+.
25
+ #
26
+ # @see INT_
27
+ # @see INT_.bits
28
+ # @see INT_.signed
29
+ def INT( bits, signed )
30
+ retval = Class.new INT_
31
+ retval.bits = bits
32
+ retval.signed = signed
33
+ retval
34
+ end
35
+
36
+ module_function :INT
37
+
38
+ # 8-bit signed integer
39
+ BYTE = INT 8, SIGNED
40
+
41
+ # 8-bit unsigned integer
42
+ UBYTE = INT 8, UNSIGNED
43
+
44
+ # 16-bit signed integer
45
+ SINT = INT 16, SIGNED
46
+
47
+ # 16-bit unsigned integer
48
+ USINT = INT 16, UNSIGNED
49
+
50
+ # 32-bit signed integer
51
+ INT = INT 32, SIGNED
52
+
53
+ # 32-bit unsigned integer
54
+ UINT = INT 32, UNSIGNED
55
+
56
+ # 64-bit signed integer
57
+ LONG = INT 64, SIGNED
58
+
59
+ # 64-bit unsigned integer
60
+ ULONG = INT 64, UNSIGNED
61
+
62
+ end
@@ -1,28 +1,64 @@
1
1
  module Hornetseye
2
2
 
3
+ # Abstract class for representing native integers
4
+ #
5
+ # @see #INT
6
+ #
7
+ # @abstract
3
8
  class INT_ < DescriptorType
4
9
 
5
10
  class << self
6
11
 
12
+ # The number of bits of native integers represented by this class
13
+ #
14
+ # @return [Integer] Number of bits of native integer.
15
+ #
16
+ # @see signed
17
+ #
18
+ # @private
7
19
  attr_accessor :bits
20
+
21
+ # A boolean indicating whether this is a signed integer or not
22
+ #
23
+ # @return [FalseClass,TrueClass] Boolean indicating whether this is a
24
+ # signed integer.
25
+ #
26
+ # @see bits
27
+ #
28
+ # @private
8
29
  attr_accessor :signed
9
30
 
31
+ # Returns the type of storage object for storing values
32
+ #
33
+ # @return [Class] Returns +Memory+.
34
+ #
35
+ # @private
10
36
  def memory
11
37
  Memory
12
38
  end
13
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
14
46
  def bytesize
15
- bits / 8
47
+ ( bits + 7 ).div 8
16
48
  end
17
49
 
50
+ # Default value for integers
51
+ #
52
+ # @return [Integer] Returns +0+.
53
+ #
54
+ # @private
18
55
  def default
19
56
  0
20
57
  end
21
58
 
22
- def inspect
23
- to_s
24
- end
25
-
59
+ # Get string with information about this type
60
+ #
61
+ # @return [String] Information about this integer type.
26
62
  def to_s
27
63
  case [ bits, signed ]
28
64
  when [ 8, true ]
@@ -46,6 +82,19 @@ module Hornetseye
46
82
  end
47
83
  end
48
84
 
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
49
98
  def descriptor
50
99
  case [ bits, signed ]
51
100
  when [ 8, true ]
@@ -73,25 +122,4 @@ module Hornetseye
73
122
 
74
123
  end
75
124
 
76
- UNSIGNED = false
77
- SIGNED = true
78
-
79
- def INT( bits, signed )
80
- retval = Class.new INT_
81
- retval.bits = bits
82
- retval.signed = signed
83
- retval
84
- end
85
-
86
- module_function :INT
87
-
88
- BYTE = INT 8, SIGNED
89
- UBYTE = INT 8, UNSIGNED
90
- SINT = INT 16, SIGNED
91
- USINT = INT 16, UNSIGNED
92
- INT = INT 32, SIGNED
93
- UINT = INT 32, UNSIGNED
94
- LONG = INT 64, SIGNED
95
- ULONG = INT 64, UNSIGNED
96
-
97
125
  end
@@ -1,42 +1,108 @@
1
1
  module Hornetseye
2
2
 
3
+ # Class for creating views on Ruby arrays
4
+ #
5
+ # @see Storage
6
+ # @see Memory
7
+ # @private
3
8
  class List < Storage
4
9
 
5
10
  class << self
6
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
7
19
  def alloc( size )
8
- List.new Array.new( size )
20
+ new Array.new( size )
9
21
  end
10
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
11
29
  def import( arr )
12
- List.new arr
30
+ new arr
13
31
  end
14
32
 
15
33
  end
16
34
 
35
+ # Offset of this view
36
+ #
37
+ # @private
17
38
  attr_accessor :offset
18
39
 
40
+ # Create zero-offset view on a Ruby array
41
+ #
42
+ # @param [Array<Object>] arr A Ruby array.
43
+ #
44
+ # @private
19
45
  def initialize( arr )
20
46
  super arr
21
47
  @offset = 0
22
48
  end
23
49
 
50
+ # Retrieve an element from the array
51
+ #
52
+ # @param [Type] typecode This parameter is ignored.
53
+ # @return [Object] The element from the array.
54
+ #
55
+ # @see #store
56
+ # @see Memory#load
57
+ # @private
24
58
  def load( typecode )
25
59
  @data[ @offset ]
26
60
  end
27
61
 
62
+ # Store an element in the array
63
+ #
64
+ # @param [Type] typecode This parameter is ignored.
65
+ # @param [Object] value The Ruby object to store.
66
+ # @return [Object] Returns the parameter +value+.
67
+ #
68
+ # @see #load
69
+ # @see Memory#store
70
+ # @private
28
71
  def store( typecode, value )
29
72
  @data[ @offset ] = value
30
73
  end
31
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
32
83
  def import( data )
33
84
  @data[ @offset ... @offset + data.size ] = data
34
85
  end
35
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.
91
+ #
92
+ # @see #import
93
+ # @see Memory#export
94
+ # @private
36
95
  def export( size )
37
96
  @data[ @offset ... @offset + size ]
38
97
  end
39
98
 
99
+ # Create a new view with the specified offset
100
+ #
101
+ # @param [Integer] offset A non-negative offset.
102
+ # @return [List] A new view for the specified part of the array.
103
+ #
104
+ # @see Memory#+
105
+ # @private
40
106
  def +( offset )
41
107
  retval = List.new @data
42
108
  retval.offset = @offset + offset
@@ -1,13 +1,33 @@
1
1
  module Hornetseye
2
2
 
3
+ # Class for creating views on raw memory
4
+ #
5
+ # @see Storage
6
+ # @see List
7
+ # @private
3
8
  class Memory < Storage
4
9
 
5
10
  class << self
6
11
 
12
+ # Create a +Memory+ object viewing a new +Malloc+ object
13
+ #
14
+ # @param [Integer] bytesize Number of bytes to allocate.
15
+ # @return [Memory] The new +Memory+ object.
16
+ #
17
+ # @see Malloc
18
+ # @private
7
19
  def alloc( bytesize )
8
20
  Memory.new Malloc.new( bytesize )
9
21
  end
10
22
 
23
+ # Create a +Memory+ object viewing a new +Malloc+ object initialised with
24
+ # the content of a string
25
+ #
26
+ # @param [String] str A Ruby string with data to write to memory.
27
+ # object.
28
+ # @return [Memory] The new +Memory+ object initialised with the data.
29
+ #
30
+ # @private
11
31
  def import( str )
12
32
  retval = alloc str.bytesize
13
33
  retval.import str
@@ -16,26 +36,73 @@ module Hornetseye
16
36
 
17
37
  end
18
38
 
39
+ # Create zero-offset view on a +Malloc+ object
40
+ #
41
+ # @param [Malloc] ptr A +Malloc+ object with the raw data.
42
+ #
43
+ # @private
19
44
  def initialize( ptr )
20
45
  super ptr
21
46
  end
22
47
 
48
+ # Read an element from the memory
49
+ #
50
+ # @param [Type] typecode Information for converting native data type to
51
+ # a Ruby object.
52
+ # @return [Object] The element from the memory.
53
+ #
54
+ # @see #store
55
+ # @see List#load
56
+ # @private
23
57
  def load( typecode )
24
58
  typecode.unpack export( typecode.bytesize )
25
59
  end
26
60
 
61
+ # Write an element to the memory
62
+ #
63
+ # @param [Type] typecode Information for converting Ruby object to native
64
+ # data type.
65
+ # @return [Object] Returns the parameter +value+.
66
+ #
67
+ # @see #load
68
+ # @see List#store
69
+ # @private
27
70
  def store( typecode, value )
28
71
  import typecode.pack( value )
72
+ value
29
73
  end
30
74
 
75
+ # Write multiple elements to memory
76
+ #
77
+ # @param [String] data A ruby string with data to write to memory.
78
+ # @return [String] The parameter +data+.
79
+ #
80
+ # @see #export
81
+ # @see List#import
82
+ # @private
31
83
  def import( data )
32
84
  @data.write data
33
85
  end
34
86
 
87
+ # Read multiple elements from memory
88
+ #
89
+ # @param [Integer] bytesize Number of bytes to read from memory.
90
+ # @return [String] A Ruby string with the resulting data.
91
+ #
92
+ # @see #import
93
+ # @see List#export
94
+ # @private
35
95
  def export( bytesize )
36
96
  @data.read bytesize
37
97
  end
38
98
 
99
+ # Create a new view with the specified offset
100
+ #
101
+ # @param [Integer] offset A non-negative offset.
102
+ # @return [Memory] A new view for the specified part of the memory.
103
+ #
104
+ # @see List#+
105
+ # @private
39
106
  def +( offset )
40
107
  Memory.new @data + offset
41
108
  end
@@ -4,6 +4,19 @@ module Hornetseye
4
4
 
5
5
  class << self
6
6
 
7
+ # Create a multi-dimensional array
8
+ #
9
+ # Creates a multi-dimensional array with elements of type
10
+ # +element_type+ and dimensions +*shape+.
11
+ #
12
+ # @param [Class] element_type Element type of the array. Should derive
13
+ # from +Type+.
14
+ # @param [Array<Integer>] *shape The dimensions of the array.
15
+ # @return [Type,Sequence_] An array with the specified element type and
16
+ # the specified dimensions.
17
+ #
18
+ # @see #MultiArray
19
+ # @see Sequence.new
7
20
  def new( element_type, *shape )
8
21
  Hornetseye::MultiArray( element_type, *shape ).new
9
22
  end
@@ -12,6 +25,18 @@ module Hornetseye
12
25
 
13
26
  end
14
27
 
28
+ # Create a multi-dimensional array class
29
+ #
30
+ # Creates a multi-dimensional array class with elements of type
31
+ # +element_type+ and dimensions +*shape+.
32
+ #
33
+ # @param [Class] element_type Element type of the array type. Should derive
34
+ # from +Type+.
35
+ # @param [Array<Integer>] *shape The dimensions of the array type.
36
+ # @return [Class] A class deriving from +Type+ or +Sequence_+.
37
+ #
38
+ # @see MultiArray.new
39
+ # @see #Sequence
15
40
  def MultiArray( element_type, *shape )
16
41
  if shape.empty?
17
42
  element_type
@@ -4,6 +4,18 @@ module Hornetseye
4
4
 
5
5
  class << self
6
6
 
7
+ # Create a one-dimensional array
8
+ #
9
+ # Create an array with +num_elements+ elements of type +element_type+.
10
+ #
11
+ # @param [Class] element_type Element type of the array. Should derive
12
+ # from +Type+.
13
+ # @param [Integer] num_elements Number of elements of the array.
14
+ # @return [Sequence_] An array with the specified element type and the
15
+ # specified number of elements.
16
+ #
17
+ # @see #Sequence
18
+ # @see MultiArray.new
7
19
  def new( element_type, num_elements )
8
20
  Hornetseye::Sequence( element_type, num_elements ).new
9
21
  end
@@ -12,6 +24,23 @@ module Hornetseye
12
24
 
13
25
  end
14
26
 
27
+ # Create a class deriving from +Sequence_+
28
+ #
29
+ # The parameters +element_type+, +num_elements+, and +stride+ are assigned
30
+ # to the corresponding attributes of the resulting class.
31
+ #
32
+ # @param [Class] element_type Element type of the array type. Should derive
33
+ # from +Type+.
34
+ # @param [Integer] num_elements Number of elements of the array type.
35
+ # @param [Integer] stride Optional stride size for transposed or
36
+ # non-contiguous array types.
37
+ # @return [Class] A class deriving from +Sequence_+.
38
+ #
39
+ # @see Sequence.new
40
+ # @see #MultiArray
41
+ # @see CompositeType.element_type
42
+ # @see CompositeType.num_elements
43
+ # @see Sequence_.stride
15
44
  def Sequence( element_type, num_elements,
16
45
  stride = element_type.size )
17
46
  retval = Class.new Sequence_
@@ -1,21 +1,50 @@
1
1
  module Hornetseye
2
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
3
11
  class Sequence_ < CompositeType
4
12
 
5
13
  class << self
6
14
 
15
+ # Distance of two consecutive elements divided by size of single element
16
+ #
17
+ # @return [Integer] Stride size to iterate over array.
18
+ #
19
+ # @see #Sequence
20
+ # @see List#+
21
+ # @see Memory#+
22
+ #
23
+ # @private
7
24
  attr_accessor :stride
8
25
 
26
+ # Get string with information about this type
27
+ #
28
+ # @return [String] Information about this array type.
9
29
  def inspect
10
30
  to_s
11
31
  end
12
32
 
33
+ # Get default value for this array type.
34
+ #
35
+ # @return [Sequence_] Returns a multi-dimensional array filled with the
36
+ # default value of the element type.
37
+ #
38
+ # @private
13
39
  def default
14
40
  retval = new
15
41
  retval.set
16
42
  retval
17
43
  end
18
44
 
45
+ # Get string with information about this type
46
+ #
47
+ # @return [String] Information about this array type.
19
48
  def to_s
20
49
  if element_type
21
50
  shortcut = element_type < Sequence_ ? 'MultiArray' : 'Sequence'
@@ -30,24 +59,47 @@ module Hornetseye
30
59
  end
31
60
  end
32
61
 
62
+ # Returns the element type of this array
63
+ #
64
+ # @return [Class] Returns +element_type.typecode+.
33
65
  def typecode
34
66
  element_type.typecode
35
67
  end
36
68
 
69
+ # Check whether arrays of this type are empty or not
70
+ #
71
+ # @return [FalseClass,TrueClass] Returns boolean indicating whether
72
+ # arrays of this type are empty or not.
37
73
  def empty?
38
74
  num_elements == 0
39
75
  end
40
76
 
77
+ # Get shape of multi-dimensional array
78
+ #
79
+ # @return [Array<Integer>] Ruby array with the shape of this array type.
41
80
  def shape
42
81
  element_type.shape + [ num_elements ]
43
82
  end
44
83
 
45
84
  end
46
85
 
86
+ # Distance of two consecutive elements divided by size of single element
87
+ #
88
+ # @return [Integer] Stride size to iterate over array.
89
+ #
90
+ # @see #Sequence
91
+ # @see List#+
92
+ # @see Memory#+
93
+ #
94
+ # @private
47
95
  def stride
48
96
  self.class.stride
49
97
  end
50
98
 
99
+ # Display type and values of this array
100
+ #
101
+ # @return [String] Returns string with information about the type and the
102
+ # values of this array.
51
103
  def inspect( indent = nil, lines = nil )
52
104
  if indent
53
105
  prepend = ''
@@ -96,10 +148,16 @@ module Hornetseye
96
148
  prepend + retval
97
149
  end
98
150
 
151
+ # Display values of this array
152
+ #
153
+ # @return [String] Returns string with the values of this array.
99
154
  def to_s
100
155
  to_a.to_s
101
156
  end
102
157
 
158
+ # Convert to Ruby array
159
+ #
160
+ # @return [Array<Object>] Result of the conversion.
103
161
  def to_a
104
162
  ( 0 ... num_elements ).collect do |i|
105
163
  x = at i
@@ -1,7 +1,9 @@
1
1
  module Hornetseye
2
2
 
3
+ # @private
3
4
  module SequenceOperation
4
5
 
6
+ # @private
5
7
  def set( value = typecode.default )
6
8
  if value.is_a? Array
7
9
  for i in 0 ... num_elements
@@ -13,10 +15,12 @@ module Hornetseye
13
15
  value
14
16
  end
15
17
 
18
+ # @private
16
19
  def get
17
20
  self
18
21
  end
19
22
 
23
+ # @private
20
24
  def sel( *indices )
21
25
  if indices.empty?
22
26
  super *indices
@@ -30,6 +34,7 @@ module Hornetseye
30
34
  end
31
35
  end
32
36
 
37
+ # @private
33
38
  def op( *args, &action )
34
39
  for i in 0 ... num_elements
35
40
  sub_args = args.collect do |arg|
@@ -1,9 +1,19 @@
1
1
  module Hornetseye
2
2
 
3
+ # Abstract class inherited by +Memory+ and +List+
4
+ #
5
+ # @see Memory
6
+ # @see List
7
+ #
8
+ # @private
9
+ # @abstract
3
10
  class Storage
4
11
 
5
- attr_reader :ptr
6
-
12
+ # Create storage object based on raw data or Ruby array
13
+ #
14
+ # @param [Malloc,Array] data Delegate object for storing the data.
15
+ #
16
+ # @private
7
17
  def initialize( data )
8
18
  @data = data
9
19
  end
@@ -1,91 +1,199 @@
1
1
  module Hornetseye
2
2
 
3
+ # This class is used to map Ruby objects to native data types
4
+ #
5
+ # @abstract
3
6
  class Type
4
7
 
5
8
  class << self
6
9
 
10
+ # Allocate +Storage+ object for storing a value
11
+ #
12
+ # @return [Storage] Object for storing a value of this type.
13
+ #
14
+ # @private
7
15
  def alloc
8
16
  memory.alloc bytesize
9
17
  end
10
18
 
19
+ # Create new instance viewing the data of the indicated +Storage+ object
20
+ #
21
+ # @return [Type] Object of this class.
22
+ #
23
+ # @private
11
24
  def wrap( memory )
12
- new :memory => memory
25
+ new nil, :memory => memory
13
26
  end
14
27
 
28
+ # Returns the element type for arrays. Otherwise it returns +self+
29
+ #
30
+ # @return [Class] Returns +self+.
15
31
  def typecode
16
32
  self
17
33
  end
18
34
 
35
+ # Returns the element type for arrays and composite numbers
36
+ #
37
+ # Otherwise it returns +self+.
38
+ #
39
+ # @return [Class] Returns +self+.
40
+ #
41
+ # @private
19
42
  def basetype
20
43
  self
21
44
  end
22
45
 
46
+ # Check whether an array is empty or not
47
+ #
48
+ # Returns +false+ if this is not an array.
49
+ #
50
+ # @return [FalseClass,TrueClass] Returns +false+.
23
51
  def empty?
24
52
  size == 0
25
53
  end
26
54
 
55
+ # Get shape of multi-dimensional array
56
+ #
57
+ # Returns +[]+ if this is not an array.
58
+ #
59
+ # @return [Array<Integer>] Returns +[]+.
27
60
  def shape
28
61
  []
29
62
  end
30
63
 
64
+ # Get number of elements of multi-dimensional array
65
+ #
66
+ # Returns +1+ if this is not an array.
67
+ #
68
+ # @return [Integer] Number of elements of array. +1+ if this is not an
69
+ # array.
31
70
  def size
32
71
  shape.inject( 1 ) { |a,b| a * b }
33
72
  end
34
73
 
35
74
  end
36
75
 
76
+ # Get +Storage+ object used to store the data of this instance
77
+ #
78
+ # @return [Storage]
79
+ #
80
+ # @private
37
81
  attr_accessor :memory
38
82
 
83
+ # Get number of bytes memory required to store the data of an instance
84
+ #
85
+ # @return [Integer] Number of bytes.
86
+ #
87
+ # @private
39
88
  def bytesize
40
89
  self.class.bytesize
41
90
  end
42
91
 
92
+ # Returns the element type for arrays
93
+ #
94
+ # Otherwise it returns +self.class+.
95
+ #
96
+ # @return [Class] Element type for arrays. Returns +self.class+ if this is
97
+ # not an array.
43
98
  def typecode
44
99
  self.class.typecode
45
100
  end
46
101
 
102
+ # Returns the element type for arrays and composite numbers
103
+ #
104
+ # Otherwise it returns +self.class+.
105
+ #
106
+ # @return [Class] Element type for arrays and composite numbers. Returns
107
+ # +self.class+ if this is not an array.
108
+ #
109
+ # @private
47
110
  def basetype
48
111
  self.class.basetype
49
112
  end
50
113
 
114
+ # Check whether an array is empty or not
115
+ #
116
+ # Returns +false+ if this is not an array.
117
+ #
118
+ # @return [FalseClass,TrueClass] Returns boolean indicating whether the
119
+ # array is empty or not. Returns +false+ if this is not an array.
51
120
  def empty?
52
121
  self.class.empty?
53
122
  end
54
123
 
124
+ # Get shape of multi-dimensional array
125
+ #
126
+ # Returns +[]+ if this is not an array.
127
+ #
128
+ # @return [Array<Integer>] Returns shape of array or +[]+ if this is not
129
+ # an array.
55
130
  def shape
56
131
  self.class.shape
57
132
  end
58
133
 
134
+ # Get number of elements of multi-dimensional array
135
+ #
136
+ # Returns +1+ if this is not an array.
137
+ #
138
+ # @return [Integer] Number of elements of array. +1+ if this is not an
139
+ # array.
59
140
  def size
60
141
  self.class.size
61
142
  end
62
143
 
63
- def initialize( *args )
64
- options = args.last.is_a?( Hash ) ? args.pop : {}
65
- raise ArgumentError.new( 'Too many arguments' ) unless args.size <= 1
66
- value = args.empty? ? nil : args.first
144
+ # Create new instance of this type
145
+ #
146
+ # @param value [Object] Optional initial value for this instance.
147
+ # @option options [Storage] :memory (self.class.alloc) Use specified
148
+ # +Storage+ object instead of creating a new one.
149
+ #
150
+ # @see alloc
151
+ #
152
+ # @private
153
+ def initialize( value = nil, options = {} )
67
154
  @memory = options[ :memory ] ? options[ :memory ] : self.class.alloc
68
155
  set value unless value.nil?
69
156
  end
70
157
 
158
+ # Display type and value of this instance
159
+ #
160
+ # @return [String] Returns string with information about type and value.
71
161
  def inspect
72
162
  "#{self.class.inspect}(#{to_s})"
73
163
  end
74
164
 
165
+ # Display value of this instance
166
+ #
167
+ # @return [String] Returns string with the value of this instance.
75
168
  def to_s
76
169
  get.to_s
77
170
  end
78
171
 
172
+ # Convert value of this instance to array
173
+ #
174
+ # @return [Array] Result of calling +to_a+ on value of this instance.
79
175
  def to_a
80
176
  get.to_a
81
177
  end
82
178
 
179
+ # Retrieve element of array
180
+ #
181
+ # @param *indices [Array<Integer>] Index/indices to access element.
182
+ # @return [Object,Type] Ruby object with value of element.
183
+ #
184
+ # @see #[]
83
185
  def at( *indices )
84
186
  sel( *indices ).get
85
187
  end
86
188
 
87
189
  alias_method :[], :at
88
190
 
191
+ # Assign value to element of array
192
+ #
193
+ # @param *args [Array<Integer,Object>] Index/indices to access element.
194
+ # The last element of +args+ is the new value to store in the array.
195
+ #
196
+ # @return [Object] Returns +args.last+.
89
197
  def assign( *args )
90
198
  sel( *args[ 0 ... -1 ] ).set args.last
91
199
  end
@@ -1,20 +1,25 @@
1
1
  module Hornetseye
2
2
 
3
+ # @private
3
4
  module TypeOperation
4
5
 
6
+ # @private
5
7
  def set( value = typecode.default )
6
8
  @memory.store self.class, value
7
9
  value
8
10
  end
9
11
 
12
+ # @private
10
13
  def get
11
14
  @memory.load self.class
12
15
  end
13
16
 
17
+ # @private
14
18
  def sel
15
19
  self
16
20
  end
17
21
 
22
+ # @private
18
23
  def op( *args, &action )
19
24
  instance_exec *args, &action
20
25
  self
data/source.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'date'
2
2
  Gem::Specification.new do |s|
3
3
  s.name = %q{multiarray}
4
- s.version = '0.2.2'
4
+ s.version = '0.2.3'
5
5
  s.platform = Gem::Platform::RUBY
6
6
  s.date = Date.today.to_s
7
7
  s.summary = %q{Multi-dimensional and uniform Ruby arrays}
@@ -9,7 +9,8 @@ Gem::Specification.new do |s|
9
9
  s.author = %q{Jan Wedekind}
10
10
  s.email = %q{jan@wedesoft.de}
11
11
  s.homepage = %q{http://wedesoft.github.com/multiarray/}
12
- s.files = [ 'source.gemspec', 'Makefile', 'README', 'COPYING' ] +
12
+ s.files = [ 'source.gemspec', 'Makefile', 'README', 'COPYING',
13
+ '.document' ] +
13
14
  Dir.glob( 'lib/*.rb' ) +
14
15
  Dir.glob( 'lib/multiarray/*.rb' ) +
15
16
  Dir.glob( 'test/*.rb' )
@@ -17,7 +18,7 @@ Gem::Specification.new do |s|
17
18
  s.require_paths = [ 'lib' ]
18
19
  s.rubyforge_project = %q{hornetseye}
19
20
  s.has_rdoc = 'yard'
20
- # s.extra_rdoc_files = [ 'README' ]
21
- # s.rdoc_options = %w{--exclude=/Makefile|.*\.(rb)/ --main README}
21
+ s.extra_rdoc_files = []
22
+ s.rdoc_options = %w{--no-private}
22
23
  s.add_dependency %q<malloc>, [ '~> 0.2' ]
23
24
  end
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.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Wedekind
@@ -35,30 +35,32 @@ files:
35
35
  - Makefile
36
36
  - README
37
37
  - COPYING
38
+ - .document
38
39
  - lib/multiarray.rb
39
40
  - lib/multiarray/sequence_.rb
40
- - lib/multiarray/storage.rb
41
- - lib/multiarray/type_operation.rb
42
- - lib/multiarray/list.rb
43
- - lib/multiarray/sequence.rb
44
- - lib/multiarray/int_.rb
45
- - lib/multiarray/multiarray.rb
46
41
  - lib/multiarray/composite_type.rb
47
- - lib/multiarray/type.rb
42
+ - lib/multiarray/multiarray.rb
43
+ - lib/multiarray/sequence.rb
44
+ - lib/multiarray/list.rb
48
45
  - lib/multiarray/descriptortype.rb
49
- - lib/multiarray/sequence_operation.rb
46
+ - lib/multiarray/type_operation.rb
47
+ - lib/multiarray/int.rb
48
+ - lib/multiarray/type.rb
49
+ - lib/multiarray/storage.rb
50
+ - lib/multiarray/int_.rb
50
51
  - lib/multiarray/memory.rb
51
- - test/tc_sequence.rb
52
+ - lib/multiarray/sequence_operation.rb
52
53
  - test/tc_multiarray.rb
53
54
  - test/ts_multiarray.rb
54
55
  - test/tc_int.rb
56
+ - test/tc_sequence.rb
55
57
  has_rdoc: yard
56
58
  homepage: http://wedesoft.github.com/multiarray/
57
59
  licenses: []
58
60
 
59
61
  post_install_message:
60
- rdoc_options: []
61
-
62
+ rdoc_options:
63
+ - --no-private
62
64
  require_paths:
63
65
  - lib
64
66
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -81,6 +83,6 @@ signing_key:
81
83
  specification_version: 3
82
84
  summary: Multi-dimensional and uniform Ruby arrays
83
85
  test_files:
84
- - test/tc_sequence.rb
85
86
  - test/tc_multiarray.rb
86
87
  - test/tc_int.rb
88
+ - test/tc_sequence.rb