carray 1.5.2 → 1.5.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +32 -0
  3. data/NEWS.md +56 -3
  4. data/README.md +34 -18
  5. data/Rakefile +1 -1
  6. data/TODO.md +5 -4
  7. data/carray.gemspec +10 -11
  8. data/ext/ca_obj_bitarray.c +4 -7
  9. data/ext/ca_obj_bitfield.c +3 -5
  10. data/ext/ca_obj_block.c +1 -6
  11. data/ext/ca_obj_refer.c +6 -8
  12. data/ext/ca_obj_unbound_repeat.c +21 -52
  13. data/ext/carray.h +4 -0
  14. data/ext/carray_access.c +77 -45
  15. data/ext/carray_attribute.c +16 -1
  16. data/ext/carray_cast.c +1 -1
  17. data/ext/carray_conversion.c +8 -1
  18. data/ext/carray_core.c +22 -16
  19. data/ext/carray_generate.c +3 -3
  20. data/ext/carray_mask.c +12 -2
  21. data/ext/carray_math.rb +20 -2
  22. data/ext/carray_numeric.c +32 -51
  23. data/ext/carray_order.c +159 -0
  24. data/ext/carray_test.c +13 -0
  25. data/ext/carray_undef.c +0 -8
  26. data/ext/carray_utils.c +126 -47
  27. data/ext/extconf.rb +13 -1
  28. data/ext/mkmath.rb +1 -1
  29. data/ext/ruby_carray.c +8 -1
  30. data/ext/ruby_ccomplex.c +1 -1
  31. data/ext/version.h +4 -4
  32. data/lib/carray.rb +7 -9
  33. data/lib/carray/autoload.rb +0 -2
  34. data/lib/carray/basic.rb +3 -5
  35. data/lib/carray/broadcast.rb +78 -22
  36. data/lib/carray/compose.rb +34 -10
  37. data/lib/carray/construct.rb +36 -14
  38. data/lib/carray/info.rb +1 -3
  39. data/lib/carray/inspect.rb +3 -5
  40. data/lib/carray/io/imagemagick.rb +1 -3
  41. data/lib/carray/iterator.rb +2 -3
  42. data/lib/carray/mask.rb +18 -7
  43. data/lib/carray/math.rb +4 -6
  44. data/lib/carray/math/histogram.rb +1 -3
  45. data/lib/carray/math/recurrence.rb +1 -3
  46. data/lib/carray/mkmf.rb +1 -3
  47. data/lib/carray/object/ca_obj_iterator.rb +1 -3
  48. data/lib/carray/object/ca_obj_link.rb +1 -3
  49. data/lib/carray/object/ca_obj_pack.rb +1 -3
  50. data/lib/carray/obsolete.rb +1 -17
  51. data/lib/carray/ordering.rb +1 -3
  52. data/lib/carray/serialize.rb +34 -25
  53. data/lib/carray/string.rb +1 -3
  54. data/lib/carray/struct.rb +3 -5
  55. data/lib/carray/testing.rb +5 -10
  56. data/lib/carray/time.rb +1 -3
  57. data/lib/carray/transform.rb +12 -3
  58. data/misc/NOTE +16 -38
  59. data/spec/Classes/CAUnboudRepeat_spec.rb +24 -0
  60. data/spec/Features/feature_attributes_spec.rb +6 -6
  61. data/spec/Features/feature_boolean_spec.rb +15 -14
  62. data/spec/Features/feature_broadcast.rb +16 -0
  63. data/spec/Features/feature_cast_spec.rb +1 -1
  64. data/spec/Features/feature_mask_spec.rb +6 -0
  65. metadata +10 -11
data/lib/carray/info.rb CHANGED
@@ -3,10 +3,8 @@
3
3
  # carray/info.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
 
@@ -3,10 +3,8 @@
3
3
  # carray/inspect.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
 
@@ -159,7 +157,7 @@ class CArray::Inspector # :nodoc:
159
157
  dim[level].times do |i|
160
158
  idx[level] = i
161
159
  v = @carray[*idx]
162
- if v.undef?
160
+ if v == UNDEF
163
161
  io << '_'
164
162
  else
165
163
  io << formatter[v]
@@ -178,7 +176,7 @@ class CArray::Inspector # :nodoc:
178
176
  if over
179
177
  idx[level] = dim[level] - 1
180
178
  v = @carray[*idx]
181
- if v.undef?
179
+ if v == UNDEF
182
180
  io << ", _"
183
181
  else
184
182
  io << ", " + formatter[v]
@@ -3,10 +3,8 @@
3
3
  # carray/io/imagemagick.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
 
@@ -3,10 +3,8 @@
3
3
  # carray/iterator.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
 
@@ -318,6 +316,7 @@ class CAClassIterator < CAIterator # :nodoc:
318
316
  @reference = reference
319
317
  @classifier = classifier || @reference.uniq.sort
320
318
  @null = CArray.new(@reference.data_type,[0])
319
+ @null.data_class = @reference.data_class if @reference.has_data_class?
321
320
  @table = {}
322
321
  @ndim = 1
323
322
  @dim = [0]
data/lib/carray/mask.rb CHANGED
@@ -3,21 +3,32 @@
3
3
  # carray/mask.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
 
13
11
  class CArray
14
-
15
- # mask
16
-
12
+
13
+ # Guard methods for handling variables that can be UNDEF values.
14
+ # Provide different processing depending on whether
15
+ # the given value is UNDEF or not.
17
16
  #
18
- # Returns the number of masked elements.
17
+ # @param value [Object] target object
18
+ # @param fill_value [Object] alternative value if the given value is UNDEF
19
+ #
20
+ # @return fill_value if the given value is UNDEF, block return value if block
21
+ # is given, or value itself
19
22
  #
23
+ def self.guard_undef (*values, fill_value: UNDEF, &block)
24
+ return fill_value if values.any?{|v| v == UNDEF }
25
+ return block.(*values)
26
+ end
20
27
 
28
+ # mask
29
+ #
30
+ # Returns the number of masked elements.
31
+ #
21
32
  def count_masked (*axis)
22
33
  if has_mask?
23
34
  return mask.int64.accumulate(*axis)
data/lib/carray/math.rb CHANGED
@@ -3,10 +3,8 @@
3
3
  # carray/math.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
 
@@ -318,7 +316,7 @@ class CArray
318
316
  count = x.count_not_masked
319
317
  xm = x.mean(:min_count => min_count)
320
318
  ym = y.mean(:min_count => min_count)
321
- if ( xm.undef? or ym.undef? )
319
+ if ( xm == UNDEF or ym == UNDEF )
322
320
  return fill_value || UNDEF
323
321
  else
324
322
  return (x-xm).wsum(y-ym)/count
@@ -337,7 +335,7 @@ class CArray
337
335
  count = x.count_not_masked
338
336
  xm = x.mean(:min_count=>min_count)
339
337
  ym = y.mean(:min_count=>min_count)
340
- if ( xm.undef? or ym.undef? )
338
+ if ( xm == UNDEF or ym == UNDEF )
341
339
  return fill_value || UNDEF
342
340
  else
343
341
  return (x-xm).wsum(y-ym)/(count-1)
@@ -355,7 +353,7 @@ class CArray
355
353
  y.inherit_mask(x)
356
354
  xm = x.mean(:min_count=>min_count)
357
355
  ym = y.mean(:min_count=>min_count)
358
- if ( xm.undef? or ym.undef? )
356
+ if ( xm == UNDEF or ym == UNDEF )
359
357
  return fill_value || UNDEF
360
358
  else
361
359
  xd, yd = x-xm, y-ym
@@ -3,10 +3,8 @@
3
3
  # carray/math/histogram.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
 
@@ -3,10 +3,8 @@
3
3
  # carray/math/recurrence.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
 
data/lib/carray/mkmf.rb CHANGED
@@ -3,10 +3,8 @@
3
3
  # carray/mkmf.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
 
@@ -3,10 +3,8 @@
3
3
  # carray/object/ca_obj_iterator.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
 
@@ -3,10 +3,8 @@
3
3
  # carray/object/ca_obj_link.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
 
@@ -3,10 +3,8 @@
3
3
  # carray/object/ca_obj_pack.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
 
@@ -3,10 +3,8 @@
3
3
  # carray/obsolete.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
 
@@ -25,20 +23,6 @@ class CArray
25
23
  self
26
24
  end
27
25
 
28
- # pulled
29
-
30
- def pulled (*args)
31
- warn "CArray#pulled will be obsolete"
32
- idx = args.map{|s| s.nil? ? :% : s}
33
- return self[*idx]
34
- end
35
-
36
- def pull (*args)
37
- warn "CArray#pull will be obsolete"
38
- idx = args.map{|s| s.nil? ? :% : s}
39
- return self[*idx].to_ca
40
- end
41
-
42
26
  # Returns the array eliminated all the duplicated elements.
43
27
  def duplicated_values
44
28
  warn "CArray#duplicated_values will be obsolete"
@@ -3,10 +3,8 @@
3
3
  # carray/ordering.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
 
@@ -3,10 +3,8 @@
3
3
  # carray/serialize.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
  #
@@ -41,8 +39,10 @@
41
39
  # has_attribute : int32
42
40
  #
43
41
  # offset 256 bytes
44
- # data : bytes*elements : value data
45
- # mask : int8*elements : mask data if has_mask == 1
42
+ # data : bytes*elements : value data
43
+ # mask : int8*elements : mask data if has_mask == 1
44
+ # attribute : object marshal : attribute
45
+ # data_class_name : string marshal : data_class_name
46
46
 
47
47
  require "stringio"
48
48
 
@@ -59,6 +59,7 @@ class CArray::Serializer # :nodoc:
59
59
  int32 :has_mask
60
60
  array :dim, :type => CArray.int64(CA_RANK_MAX)
61
61
  int32 :has_attr
62
+ int32 :has_data_class
62
63
  }
63
64
 
64
65
  Header_Legacy = CA.struct(:pack=>1, :size=>256) {
@@ -83,7 +84,7 @@ class CArray::Serializer # :nodoc:
83
84
  end
84
85
  end
85
86
 
86
- def save (ca, opt = {})
87
+ def save (ca, **opt)
87
88
  endian = opt[:endian] || CArray.endian
88
89
  # ---
89
90
  header = Header.new()
@@ -95,15 +96,16 @@ class CArray::Serializer # :nodoc:
95
96
  header[:ndim] = ca.ndim
96
97
  header[:elements] = ca.elements
97
98
  header[:has_mask] = ca.has_mask? ? 1 : 0
99
+ header[:has_data_class] = ca.has_data_class? ? 1 : 0
98
100
  header[:dim][[0,ca.ndim]] = ca.dim
99
- attr = nil
101
+ attributes = nil
100
102
  if ca.attribute
101
- attr = ca.attribute.clone
103
+ attributes = ca.attribute.clone
102
104
  end
103
105
  if opt[:attribute]
104
- (attr ||= {}).update(opt[:attribute])
106
+ (attributes ||= {}).update(opt[:attribute])
105
107
  end
106
- header[:has_attr] = attr.empty? ? 0 : 1
108
+ header[:has_attr] = attributes.empty? ? 0 : 1
107
109
  unless CArray.endian == endian
108
110
  header.swap_bytes!
109
111
  end
@@ -121,14 +123,17 @@ class CArray::Serializer # :nodoc:
121
123
  if ca.has_mask?
122
124
  ca.mask.dump_binary(@io)
123
125
  end
124
- if attr
125
- Marshal.dump(attr, @io)
126
+ if attributes
127
+ Marshal.dump(attributes, @io)
128
+ end
129
+ if ca.has_data_class?
130
+ Marshal.dump(ca.data_class.to_s, @io)
126
131
  end
127
132
  return ca
128
133
  end
129
134
 
130
- def load (opt = {}, legacy: false)
131
- if legacy
135
+ def load (**opt)
136
+ if opt[:legacy]
132
137
  header = Header_Legacy.decode(@io.read(256))
133
138
  else
134
139
  header = Header.decode(@io.read(256))
@@ -151,7 +156,8 @@ class CArray::Serializer # :nodoc:
151
156
  elements = header[:elements]
152
157
  has_mask = header[:has_mask] != 0 ? true : false
153
158
  dim = header[:dim][[0, ndim]].to_a
154
- has_attr = header[:has_attr]
159
+ has_attr = header[:has_attr] != 0 ? true : false
160
+ has_data_class = header[:has_data_class] != 0 ? true : false
155
161
  if data_type == 255
156
162
  data_type = header[:data_type_name].strip.to_sym
157
163
  end
@@ -172,9 +178,12 @@ class CArray::Serializer # :nodoc:
172
178
  ca.mask = 0
173
179
  ca.mask.load_binary(@io)
174
180
  end
175
- if has_attr == 1
181
+ if has_attr
176
182
  ca.attribute = Marshal.load(@io)
177
183
  end
184
+ if has_data_class
185
+ ca.data_class = Kernel.const_get(Marshal.load(@io))
186
+ end
178
187
  return ca
179
188
  end
180
189
 
@@ -182,36 +191,36 @@ end
182
191
 
183
192
  class CArray
184
193
 
185
- def self.save(ca, output, opt={})
194
+ def self.save(ca, output, **opt)
186
195
  case output
187
196
  when String
188
197
  open(output, "wb:ASCII-8BIT") { |io|
189
- return Serializer.new(io).save(ca, opt)
198
+ return Serializer.new(io).save(ca, **opt)
190
199
  }
191
200
  else
192
- return Serializer.new(output).save(ca, opt)
201
+ return Serializer.new(output).save(ca, **opt)
193
202
  end
194
203
  end
195
204
 
196
- def self.load (input, opt={})
205
+ def self.load (input, **opt)
197
206
  case input
198
207
  when String
199
208
  if input.length >= 256 and input =~ /\A_CARRAY_.{8}_(LE|BE)_/
200
209
  io = StringIO.new(input)
201
- return Serializer.new(io).load(opt)
210
+ return Serializer.new(io).load(**opt)
202
211
  else
203
212
  open(input, "rb:ASCII-8BIT") { |io|
204
- return Serializer.new(io).load(opt)
213
+ return Serializer.new(io).load(**opt)
205
214
  }
206
215
  end
207
216
  else
208
- return Serializer.new(input).load(opt)
217
+ return Serializer.new(input).load(**opt)
209
218
  end
210
219
  end
211
220
 
212
- def self.dump (ca, opt={})
221
+ def self.dump (ca, **opt)
213
222
  io = StringIO.new("")
214
- Serializer.new(io).save(ca, opt)
223
+ Serializer.new(io).save(ca, **opt)
215
224
  return io.string
216
225
  end
217
226
 
data/lib/carray/string.rb CHANGED
@@ -3,10 +3,8 @@
3
3
  # carray/string.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
 
data/lib/carray/struct.rb CHANGED
@@ -3,19 +3,17 @@
3
3
  # carray/struct.rb
4
4
  #
5
5
  # This file is part of Ruby/CArray extension library.
6
- # You can redistribute it and/or modify it under the terms of
7
- # the Ruby Licence.
8
6
  #
9
- # Copyright (C) 2005 Hiroki Motoyoshi
7
+ # Copyright (C) 2005-2020 Hiroki Motoyoshi
10
8
  #
11
9
  # ----------------------------------------------------------------------------
12
10
  #
13
11
  # The data class for fixed length carray are required to satisfy only
14
- # four conditions.
12
+ # five conditions.
15
13
  #
16
14
  # * constant data_class::DATA_SIZE -> integer
17
- # * constant data_class::MEMBERS -> array of string
18
15
  # * constant data_class::MEMBER_TABLE -> hash
16
+ # * constant data_class::MEMBERS -> array (MEMBER_TABLE.keys as usual)
19
17
  # * method data_class.decode(data) -> new data_class object
20
18
  # * method data_class#encode() -> string
21
19
  #