carray 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (231) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +56 -0
  3. data/GPL +340 -0
  4. data/Gemfile +9 -0
  5. data/Gemfile.lock +33 -0
  6. data/LEGAL +50 -0
  7. data/NOTE +73 -0
  8. data/Rakefile +20 -0
  9. data/TODO +5 -0
  10. data/ca_iter_block.c +242 -0
  11. data/ca_iter_dimension.c +287 -0
  12. data/ca_iter_window.c +202 -0
  13. data/ca_obj_array.c +1189 -0
  14. data/ca_obj_bitarray.c +523 -0
  15. data/ca_obj_bitfield.c +636 -0
  16. data/ca_obj_block.c +885 -0
  17. data/ca_obj_fake.c +405 -0
  18. data/ca_obj_farray.c +482 -0
  19. data/ca_obj_field.c +625 -0
  20. data/ca_obj_grid.c +738 -0
  21. data/ca_obj_mapping.c +614 -0
  22. data/ca_obj_object.c +777 -0
  23. data/ca_obj_reduce.c +299 -0
  24. data/ca_obj_refer.c +627 -0
  25. data/ca_obj_repeat.c +640 -0
  26. data/ca_obj_select.c +558 -0
  27. data/ca_obj_shift.c +952 -0
  28. data/ca_obj_transpose.c +582 -0
  29. data/ca_obj_unbound_repeat.c +557 -0
  30. data/ca_obj_window.c +1023 -0
  31. data/carray.h +1381 -0
  32. data/carray_access.c +1798 -0
  33. data/carray_attribute.c +903 -0
  34. data/carray_call_cfunc.c +1107 -0
  35. data/carray_cast.c +1155 -0
  36. data/carray_cast_func.rb +498 -0
  37. data/carray_class.c +132 -0
  38. data/carray_conversion.c +518 -0
  39. data/carray_copy.c +453 -0
  40. data/carray_core.c +1307 -0
  41. data/carray_element.c +572 -0
  42. data/carray_generate.c +681 -0
  43. data/carray_iterator.c +630 -0
  44. data/carray_loop.c +462 -0
  45. data/carray_mask.c +1174 -0
  46. data/carray_math.rb +834 -0
  47. data/carray_numeric.c +257 -0
  48. data/carray_operator.c +582 -0
  49. data/carray_order.c +1040 -0
  50. data/carray_random.c +529 -0
  51. data/carray_sort_addr.c +261 -0
  52. data/carray_stat.c +2102 -0
  53. data/carray_stat_proc.rb +1990 -0
  54. data/carray_test.c +602 -0
  55. data/carray_undef.c +69 -0
  56. data/carray_utils.c +740 -0
  57. data/ext/calculus/carray_calculus.c +792 -0
  58. data/ext/calculus/carray_interp.c +355 -0
  59. data/ext/calculus/extconf.rb +12 -0
  60. data/ext/calculus/lib/autoload/autoload_math_calculus.rb +2 -0
  61. data/ext/calculus/lib/math/calculus.rb +119 -0
  62. data/ext/calculus/lib/math/interp/adapter_interp1d.rb +31 -0
  63. data/ext/dataframe/API.txt +11 -0
  64. data/ext/dataframe/extconf.rb +3 -0
  65. data/ext/dataframe/lib/carray/autoload/autoload_dataframe_dataframe.rb +14 -0
  66. data/ext/dataframe/lib/carray/dataframe/dataframe.rb +1104 -0
  67. data/ext/dataframe/sample/test_uniq_sort.rb +5 -0
  68. data/ext/fortio/extconf.rb +3 -0
  69. data/ext/fortio/lib/carray/autoload/autoload_fortran_format.rb +5 -0
  70. data/ext/fortio/lib/carray/io/fortran_format.rb +43 -0
  71. data/ext/fortio/lib/fortio.rb +3 -0
  72. data/ext/fortio/lib/fortio/fortran_format.rb +603 -0
  73. data/ext/fortio/lib/fortio/fortran_format.tab.rb +536 -0
  74. data/ext/fortio/lib/fortio/fortran_format.y +215 -0
  75. data/ext/fortio/lib/fortio/fortran_namelist.rb +151 -0
  76. data/ext/fortio/lib/fortio/fortran_namelist.tab.rb +470 -0
  77. data/ext/fortio/lib/fortio/fortran_namelist.y +213 -0
  78. data/ext/fortio/lib/fortio/fortran_sequential.rb +345 -0
  79. data/ext/fortio/ruby_fortio.c +182 -0
  80. data/ext/fortio/test/test_H.rb +5 -0
  81. data/ext/fortio/test/test_T.rb +7 -0
  82. data/ext/fortio/test/test_fortran_format.rb +86 -0
  83. data/ext/fortio/test/test_namelist.rb +25 -0
  84. data/ext/fortio/test/test_sequential.rb +13 -0
  85. data/ext/fortio/test/test_sequential2.rb +13 -0
  86. data/ext/fortio/work/test.rb +10 -0
  87. data/ext/fortio/work/test_e.rb +19 -0
  88. data/ext/fortio/work/test_ep.rb +10 -0
  89. data/ext/fortio/work/test_parse.rb +12 -0
  90. data/ext/imagemap/carray_imagemap.c +495 -0
  91. data/ext/imagemap/doc/call_graph.dot +28 -0
  92. data/ext/imagemap/draw.c +567 -0
  93. data/ext/imagemap/extconf.rb +13 -0
  94. data/ext/imagemap/lib/autoload/autoload_graphics_imagemap.rb +1 -0
  95. data/ext/imagemap/lib/graphics/imagemap.rb +273 -0
  96. data/ext/imagemap/lib/image_map.rb +4 -0
  97. data/ext/imagemap/test/swath_index.rb +83 -0
  98. data/ext/imagemap/test/swath_warp.rb +99 -0
  99. data/ext/imagemap/test/test.rb +23 -0
  100. data/ext/imagemap/test/test_image.rb +42 -0
  101. data/ext/imagemap/test/test_line.rb +14 -0
  102. data/ext/imagemap/test/test_rotate.rb +17 -0
  103. data/ext/imagemap/test/test_triangle.rb +20 -0
  104. data/ext/imagemap/test/test_warp.rb +26 -0
  105. data/ext/mathfunc/carray_mathfunc.c +321 -0
  106. data/ext/mathfunc/extconf.rb +18 -0
  107. data/ext/mathfunc/lib/autoload/autoload_math_mathfunc.rb +1 -0
  108. data/ext/mathfunc/lib/math/mathfunc.rb +15 -0
  109. data/ext/mathfunc/test/test_hypot.rb +5 -0
  110. data/ext/mathfunc/test/test_j0.rb +22 -0
  111. data/ext/mathfunc/test/test_jn.rb +8 -0
  112. data/ext/mathfunc/test/test_sph.rb +9 -0
  113. data/ext/narray/README +22 -0
  114. data/ext/narray/ca_wrap_narray.c +491 -0
  115. data/ext/narray/carray_narray.c +21 -0
  116. data/ext/narray/extconf.rb +57 -0
  117. data/ext/narray/lib/autoload/autoload_math_narray.rb +1 -0
  118. data/ext/narray/lib/autoload/autoload_math_narray_miss.rb +11 -0
  119. data/ext/narray/lib/math/narray.rb +17 -0
  120. data/ext/narray/lib/math/narray_miss.rb +45 -0
  121. data/extconf.rb +3 -25
  122. data/lib/carray.rb +28 -0
  123. data/lib/carray/autoload/autoload_base.rb +23 -0
  124. data/lib/carray/autoload/autoload_graphics_gnuplot.rb +2 -0
  125. data/lib/carray/autoload/autoload_io_csv.rb +14 -0
  126. data/lib/carray/autoload/autoload_io_excel.rb +5 -0
  127. data/lib/carray/autoload/autoload_io_imagemagick.rb +6 -0
  128. data/lib/carray/autoload/autoload_io_pg.rb +6 -0
  129. data/lib/carray/autoload/autoload_io_sqlite3.rb +12 -0
  130. data/lib/carray/autoload/autoload_io_table.rb +1 -0
  131. data/lib/carray/autoload/autoload_math_histogram.rb +5 -0
  132. data/lib/carray/autoload/autoload_math_interp.rb +4 -0
  133. data/lib/carray/autoload/autoload_math_recurrence.rb +6 -0
  134. data/lib/carray/autoload/autoload_object_iterator.rb +1 -0
  135. data/lib/carray/autoload/autoload_object_link.rb +1 -0
  136. data/lib/carray/autoload/autoload_object_pack.rb +2 -0
  137. data/lib/carray/base/autoload.rb +94 -0
  138. data/lib/carray/base/basic.rb +1051 -0
  139. data/lib/carray/base/inspect.rb +252 -0
  140. data/lib/carray/base/iterator.rb +367 -0
  141. data/lib/carray/base/math.rb +403 -0
  142. data/lib/carray/base/obsolete.rb +93 -0
  143. data/lib/carray/base/serialize.rb +260 -0
  144. data/lib/carray/base/struct.rb +634 -0
  145. data/lib/carray/graphics/gnuplot.rb +2116 -0
  146. data/lib/carray/info.rb +112 -0
  147. data/lib/carray/io/csv.rb +560 -0
  148. data/lib/carray/io/excel.rb +26 -0
  149. data/lib/carray/io/imagemagick.rb +231 -0
  150. data/lib/carray/io/pg.rb +101 -0
  151. data/lib/carray/io/sqlite3.rb +202 -0
  152. data/lib/carray/io/table.rb +77 -0
  153. data/lib/carray/math/histogram.rb +179 -0
  154. data/lib/carray/math/interp.rb +57 -0
  155. data/lib/carray/math/interp/adapter_gsl_spline.rb +47 -0
  156. data/lib/carray/math/recurrence.rb +95 -0
  157. data/lib/carray/mkmf.rb +145 -0
  158. data/lib/carray/object/ca_obj_iterator.rb +52 -0
  159. data/lib/carray/object/ca_obj_link.rb +52 -0
  160. data/lib/carray/object/ca_obj_pack.rb +101 -0
  161. data/mkmath.rb +731 -0
  162. data/mt19937ar.c +182 -0
  163. data/mt19937ar.h +86 -0
  164. data/rdoc_main.rb +27 -0
  165. data/rdoc_math.rb +5 -0
  166. data/rdoc_stat.rb +31 -0
  167. data/ruby_carray.c +242 -0
  168. data/ruby_ccomplex.c +497 -0
  169. data/ruby_float_func.c +83 -0
  170. data/spec/CABlockIterator/CABlockIterator_spec.rb +113 -0
  171. data/spec/CArray/bug/store_spec.rb +27 -0
  172. data/spec/CArray/index/repeat_spec.rb +10 -0
  173. data/spec/CArray/method/eq_spec.rb +80 -0
  174. data/spec/CArray/method/is_nan_spec.rb +12 -0
  175. data/spec/CArray/method/ne_spec.rb +18 -0
  176. data/spec/CArray/method/round_spec.rb +11 -0
  177. data/spec/CArray/object/_attribute_spec.rb +32 -0
  178. data/spec/CArray/object/s_new_spec.rb +31 -0
  179. data/spec/CArray/serialize/Serialization_spec.rb +89 -0
  180. data/spec/spec_all.rb +11 -0
  181. data/test/test_ALL.rb +50 -0
  182. data/test/test_CABitfield.rb +59 -0
  183. data/test/test_CABlock.rb +208 -0
  184. data/test/test_CAField.rb +40 -0
  185. data/test/test_CAGrid.rb +76 -0
  186. data/test/test_CAMapping.rb +106 -0
  187. data/test/test_CAMmap.rb +11 -0
  188. data/test/test_CARefer.rb +94 -0
  189. data/test/test_CARepeat.rb +66 -0
  190. data/test/test_CASelect.rb +23 -0
  191. data/test/test_CAShift.rb +17 -0
  192. data/test/test_CATranspose.rb +61 -0
  193. data/test/test_CAVirtual.rb +214 -0
  194. data/test/test_CAWindow.rb +55 -0
  195. data/test/test_CAWrap.rb +9 -0
  196. data/test/test_CArray.rb +228 -0
  197. data/test/test_CComplex.rb +83 -0
  198. data/test/test_CScalar.rb +91 -0
  199. data/test/test_attribute.rb +281 -0
  200. data/test/test_block_iterator.rb +17 -0
  201. data/test/test_boolean.rb +99 -0
  202. data/test/test_cast.rb +33 -0
  203. data/test/test_class.rb +85 -0
  204. data/test/test_complex.rb +43 -0
  205. data/test/test_composite.rb +125 -0
  206. data/test/test_convert.rb +79 -0
  207. data/test/test_copy.rb +141 -0
  208. data/test/test_creation.rb +85 -0
  209. data/test/test_element.rb +146 -0
  210. data/test/test_extream.rb +55 -0
  211. data/test/test_generate.rb +75 -0
  212. data/test/test_index.rb +71 -0
  213. data/test/test_mask.rb +578 -0
  214. data/test/test_math.rb +98 -0
  215. data/test/test_narray.rb +64 -0
  216. data/test/test_order.rb +147 -0
  217. data/test/test_random.rb +15 -0
  218. data/test/test_ref_store.rb +211 -0
  219. data/test/test_stat.rb +414 -0
  220. data/test/test_struct.rb +72 -0
  221. data/test/test_virtual.rb +49 -0
  222. data/utils/ca_ase.rb +21 -0
  223. data/utils/ca_methods.rb +15 -0
  224. data/utils/cast_checker.rb +30 -0
  225. data/utils/create_rdoc.sh +9 -0
  226. data/utils/diff_method.rb +52 -0
  227. data/utils/extract_rdoc.rb +27 -0
  228. data/utils/make_tgz.sh +3 -0
  229. data/utils/remove_resource_fork.sh +5 -0
  230. data/version.h +3 -3
  231. metadata +266 -1
@@ -0,0 +1,112 @@
1
+ # ----------------------------------------------------------------------------
2
+ #
3
+ # carray/info.rb
4
+ #
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
+ #
9
+ # Copyright (C) 2005 Hiroki Motoyoshi
10
+ #
11
+ # ----------------------------------------------------------------------------
12
+
13
+
14
+ require "carray"
15
+
16
+ printf "\n"
17
+ printf "CArray Environment Information\n"
18
+ printf "==============================\n"
19
+ printf "\n"
20
+ printf "# version\n"
21
+ printf " CArray::VERSION -> %s\n", CArray::VERSION
22
+ printf " CArray::VERSION_DATE -> %s\n", CArray::VERSION_DATE
23
+ printf "\n"
24
+ printf "# supports\n"
25
+ printf " CArray::HAVE_COMPLEX -> %s\n", CArray::HAVE_COMPLEX
26
+ printf " CArray::HAVE_NARRAY -> %s\n", CArray::HAVE_NARRAY
27
+ printf "\n"
28
+ printf "# limitations\n"
29
+ printf " CA_RANK_MAX -> %2i\n", CA_RANK_MAX
30
+ printf "\n"
31
+ printf "# data types\n"
32
+ [
33
+ "CA_FIXLEN",
34
+ "CA_INT8",
35
+ "CA_UINT8",
36
+ "CA_INT16",
37
+ "CA_UINT16",
38
+ "CA_INT32",
39
+ "CA_UINT32",
40
+ "CA_INT64",
41
+ "CA_UINT64",
42
+ "CA_FLOAT32",
43
+ "CA_FLOAT64",
44
+ "CA_FLOAT128",
45
+ "CA_CMPLX64",
46
+ "CA_CMPLX128",
47
+ "CA_CMPLX256",
48
+ "CA_OBJECT",
49
+ "CA_BYTE",
50
+ "CA_SHORT",
51
+ "CA_INT",
52
+ "CA_FLOAT",
53
+ "CA_DOUBLE",
54
+ "CA_COMPLEX",
55
+ "CA_DCOMPLEX",
56
+ ].each do |name|
57
+ data_type = eval(name)
58
+ if CArray.data_type?(data_type)
59
+ # printf(" %-12s -> %2i [bytes -> %2i]\n",
60
+ # name, data_type, CArray.sizeof(data_type))
61
+ else
62
+ printf(" %-12s -> not available\n", name)
63
+ end
64
+ end
65
+
66
+ printf "\n"
67
+ printf "# default CArray classes\n"
68
+ carrays = []
69
+ cavirtuals = []
70
+ ObjectSpace.each_object(Class) do |c|
71
+ if c <= CAVirtual
72
+ cavirtuals.push c
73
+ elsif c <= CArray
74
+ carrays.push c
75
+ end
76
+ end
77
+ printf " entity -> %2i [%s]\n", carrays.size, carrays.join(", ")
78
+ printf " virtual -> %2i\n", cavirtuals.size
79
+
80
+ printf "\n"
81
+ printf "# default CArray methods\n"
82
+ o = Object.new
83
+ a = CArray.int(3)
84
+ list = a.methods - o.methods
85
+ printf " methods -> %2i\n", list.size
86
+
87
+ def which (cmd)
88
+ return ENV["PATH"].split(":").detect do |dir|
89
+ filename = File.join(dir, cmd)
90
+ File.executable?(filename)
91
+ end
92
+ end
93
+
94
+ printf "\n"
95
+ printf "# helper commands\n"
96
+
97
+ [
98
+ ["gnuplot", ["carray/graphics/gnuplot.rb"]],
99
+ ["convert", ["carray/io/imagemagick.rb"]],
100
+ ["display", ["carray/io/imagemagick.rb"]],
101
+ ["identify", ["carray/io/imagemagick.rb"]],
102
+ ["stream", ["carray/io/imagemagick.rb"]],
103
+ ].each do |cmd, libs|
104
+ path = which(cmd)
105
+ if path
106
+ printf(" %s -> installed, required by %s\n", cmd, libs.join(","))
107
+ else
108
+ printf(" %s -> not installed, required by %s\n", cmd, libs.join(","))
109
+ end
110
+ end
111
+
112
+ printf "\n"
@@ -0,0 +1,560 @@
1
+ # ----------------------------------------------------------------------------
2
+ #
3
+ # carray/io/csv.rb
4
+ #
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
+ #
9
+ # Copyright (C) 2005 Hiroki Motoyoshi
10
+ #
11
+ # ----------------------------------------------------------------------------
12
+ #
13
+ # CSV data reader/writer for CArray (in DSL approach)
14
+ #
15
+ # For reading (explicit form)
16
+ #
17
+ # csv = CA::CSVReader.new {
18
+ # header # read a line as "names" header
19
+ # header :units # read a line as "units" header
20
+ # skip 1 # skip a line
21
+ # body 10 # read values
22
+ # process { |name, column| # post processing
23
+ # case name # name : name of columns (string|integer)
24
+ # when "Date" # column : column carray
25
+ # column.map!{|x| Date.parse(x) }
26
+ # else
27
+ # column[] = column.double
28
+ # end
29
+ # }
30
+ # }
31
+ #
32
+ # data1 = csv.read_file(file1)
33
+ # data2 = csv.read_file(file2)
34
+ # data3 = csv.read_file(file3)
35
+ #
36
+ # For reading (implicit form)
37
+ #
38
+ # data = CArray.load_csv(file) { ... definitions ... }
39
+ # data = CArray.from_csv(io|string) { ... definitions ... }
40
+ #
41
+ # For writing (explict form)
42
+ #
43
+ # csv = CA::CSVWriter.new {
44
+ # names ["Date", "a", "b", "c"] # set name of columns
45
+ # process { |name, column| # pre processing
46
+ # case name # name : name of column
47
+ # when "Date" # column : column carray
48
+ # column.map!{|x| x.to_s }
49
+ # else
50
+ # column.map!{|x| "%.2f" % x }
51
+ # end
52
+ # }
53
+ # puts "sample CSV data" # print any string
54
+ # header # write names in header
55
+ # header ["","mm","cm","m"] # write units in header
56
+ # write # write values
57
+ # }
58
+ #
59
+ # data1.write_file(file1)
60
+ # data2.write_file(file2)
61
+ # data3.write_file(file3)
62
+ #
63
+ # For writing (implicit form)
64
+ #
65
+ # data.save_csv(file) { ... definitions ... }
66
+ # data.to_csv([io|string]) { ... definitions ... }
67
+ #
68
+
69
+ require "stringio"
70
+ require "strscan"
71
+ require "carray/io/table"
72
+
73
+ module CA
74
+
75
+ class CSVReader # :nodoc:
76
+
77
+ def initialize (sep=",", rs=$/, &block)
78
+ @block = block
79
+ @sep = sep
80
+ @rs = rs
81
+ end
82
+
83
+ def read_io (io)
84
+ return Processor.new(io, @sep, @rs, &@block).run
85
+ end
86
+
87
+ def read_string (string)
88
+ return read_io(StringIO.new(string))
89
+ end
90
+
91
+ def read_file (filename)
92
+ open(filename) { |io|
93
+ return read_io(io)
94
+ }
95
+ end
96
+
97
+ class Processor # :nodoc:
98
+
99
+ def initialize (io, sep, rs=$/, &block)
100
+ @io = io
101
+ @sep = sep
102
+ @rs = rs
103
+ @sc = StringScanner.new("")
104
+ @block = block || proc { body }
105
+ @namelist = nil
106
+ @names = nil
107
+ @headerlist = []
108
+ @header = {}
109
+ @note = ""
110
+ @table = nil
111
+ @regexp_simple1 = /#{@sep}/
112
+ @regexp_simple2 = / *#{@sep} */
113
+ @regexp_pat1 = /\A([^"#{@sep}][^#{@sep}]*) *#{@sep} */
114
+ @regexp_pat2 = /\A"([^"]+)" *#{@sep} */
115
+ @regexp_pat3 = /\A"((?:[^"]+|"")+)" *#{@sep} */
116
+ @regexp_pat4 = /\A(?:""|) *#{@sep} */
117
+ end
118
+
119
+ def run
120
+ case @block.arity
121
+ when 1
122
+ @block.call(self)
123
+ when -1, 0
124
+ instance_exec(&@block)
125
+ else
126
+ raise "invalid block paramter"
127
+ end
128
+ header = @header
129
+ note = @note
130
+ @table.instance_exec{
131
+ @names = header["names"]
132
+ @header = header
133
+ @note = note
134
+ }
135
+ @table.extend(CA::TableMethods)
136
+ @table.column_names = header["names"]
137
+ class << @table
138
+ attr_reader :note
139
+ def header (name=nil)
140
+ if name
141
+ return @header[name.to_s]
142
+ else
143
+ return @header
144
+ end
145
+ end
146
+ end
147
+ return @table
148
+ end
149
+
150
+ def column_names (*namelist)
151
+ if @header.has_key?("names")
152
+ warn "override header['names']"
153
+ end
154
+ @names = namelist.map(&:to_s)
155
+ @header["names"] = @names
156
+ return namelist
157
+ end
158
+
159
+ def header (name = "names")
160
+ name = name.to_s
161
+ list = csv_feed()
162
+ if name == "names"
163
+ if @names
164
+ raise "already 'names' defined"
165
+ end
166
+ @names = list
167
+ end
168
+ @header[name] = list
169
+ @headerlist.push(name)
170
+ return list
171
+ end
172
+
173
+ attr_reader :names
174
+
175
+ def note (n=1)
176
+ list = []
177
+ n.times { list << @io.gets(@rs) }
178
+ @note << (text = list.join)
179
+ return text
180
+ end
181
+
182
+ def skip (n=1)
183
+ n.times { @io.gets(@rs) }
184
+ end
185
+
186
+ def body (n=nil, cols=nil)
187
+ unless n
188
+ n = 0x80000000
189
+ end
190
+ data = []
191
+ count = 0
192
+
193
+ if cols
194
+ @cols = cols
195
+ elsif @names
196
+ @cols = @names.size
197
+ else
198
+ list = csv_feed()
199
+ if list.nil?
200
+ @rows = 0
201
+ @table = CArray.object(@rows, @cols)
202
+ return
203
+ end
204
+ data.push(list)
205
+ count += 1
206
+ @cols = list.size
207
+ end
208
+
209
+ lsize = nil
210
+ while count < n and list = csv_feed(@cols)
211
+ lsize = list.size
212
+ if lsize == @cols
213
+ data.push(list)
214
+ elsif lsize <= @cols
215
+ record = Array.new(@cols, nil)
216
+ record[0,lsize] = list
217
+ data.push(record)
218
+ else
219
+ extra = Array.new(lsize - @cols, nil)
220
+ data.each do |row|
221
+ row.push(*extra)
222
+ end
223
+ data.push(list)
224
+ @cols = lsize
225
+ # raise "csv parse error : too large column number at line #{@io.lineno}"
226
+ end
227
+ count += 1
228
+ end
229
+
230
+ @rows = data.size
231
+ @table = CArray.object(@rows, @cols){ data }
232
+ @table[:eq,""] = nil
233
+ end
234
+
235
+ def select (*namelist)
236
+ @namelist = namelist.empty? ? nil : namelist
237
+ case @namelist
238
+ when nil
239
+ when Array
240
+ index = (0...@cols).map.to_a
241
+ index_list = @namelist.map{ |x|
242
+ case x
243
+ when Integer
244
+ x
245
+ when Range
246
+ index[x]
247
+ when String, Symbol
248
+ if @names and i = @names.index(x.to_s)
249
+ i
250
+ else
251
+ raise "invalid argument #{x}"
252
+ end
253
+ else
254
+ raise "invalid argument"
255
+ end
256
+ }.flatten
257
+ @table = @table[nil, CA_INT(index_list)].to_ca
258
+ @header.keys.each do |k|
259
+ @header[k] = @header[k].values_at(*index_list)
260
+ end
261
+ else
262
+ raise
263
+ end
264
+ end
265
+
266
+ def process
267
+ if @namelist
268
+ @namelist.each_with_index do |name, i|
269
+ yield(name, @table[nil, i])
270
+ end
271
+ elsif @names
272
+ @names.each_with_index do |name, i|
273
+ yield(name, @table[nil, i])
274
+ end
275
+ else
276
+ @table.dim1.times do |i|
277
+ yield(i, @table[nil,i])
278
+ end
279
+ end
280
+ end
281
+
282
+ def convert (data_type, options={}, &block)
283
+ if block_given?
284
+ if data_type.is_a?(Class) and data_type < CA::Struct
285
+ @table = @table[:i, nil].convert(data_type, &block)
286
+ else
287
+ @table = @table.convert(data_type, options, &block)
288
+ end
289
+ else
290
+ if data_type.is_a?(Class) and data_type < CA::Struct
291
+ @table = @table[:i,nil].convert(data_type) { |b|
292
+ data_type.new(*b[0,nil])
293
+ }
294
+ else
295
+ @table = @table.to_type(data_type, options)
296
+ end
297
+ end
298
+ end
299
+
300
+ private
301
+
302
+ def csv_feed (cols=nil)
303
+ if @io.eof?
304
+ return nil
305
+ end
306
+ line = nil
307
+ loop do
308
+ if newline = @io.gets(@rs)
309
+ if line
310
+ line << newline
311
+ else
312
+ line = newline
313
+ end
314
+ count_quote = line.count('"')
315
+ else
316
+ line = ""
317
+ count_quote = 0
318
+ end
319
+ if count_quote == 0
320
+ line.chomp!
321
+ if line.count(' ') == 0
322
+ return line.split(@sep, -1) ### /#{@sep}/
323
+ else
324
+ return line.split(@regexp_simple2, -1) ### / *#{@sep} */
325
+ end
326
+ end
327
+ if count_quote % 2 == 0
328
+ line.chomp!
329
+ return csv_split(line, cols)
330
+ else
331
+ if newline
332
+ next
333
+ else
334
+ raise "csv parse error"
335
+ end
336
+ end
337
+ end
338
+ end
339
+
340
+ def csv_split (text, cols=nil)
341
+ if cols
342
+ csv = Array.new(cols)
343
+ else
344
+ csv = []
345
+ end
346
+ text << @sep
347
+ @sc.string = text
348
+ i = 0
349
+ begin
350
+ case
351
+ when @sc.scan(@regexp_pat1)
352
+ ### /\A([^"#{@sep}][^#{@sep}]*) *#{@sep} */
353
+ csv[i] = @sc[1]
354
+ when @sc.scan(@regexp_pat2)
355
+ ### /\A"([^"]+)" *#{@sep} */
356
+ csv[i] = @sc[1]
357
+ when @sc.scan(@regexp_pat3)
358
+ ### /\A"((?:[^"]+|"")+)" *#{@sep} */
359
+ s = @sc[1]
360
+ if s =~ /"/
361
+ csv[i] = s.gsub(/""/, '"')
362
+ else
363
+ csv[i] = s
364
+ end
365
+ when @sc.scan(@regexp_pat4)
366
+ ### /\A(?:""|) *#{@sep} */
367
+ csv[i] = nil
368
+ else
369
+ raise "csv parse error"
370
+ end
371
+ i += 1
372
+ end until @sc.eos?
373
+ return csv
374
+ end
375
+
376
+ end
377
+ end
378
+ end
379
+
380
+ module CA
381
+
382
+ class CSVWriter # :nodoc:
383
+
384
+ def initialize (sep=",", rs=$/, &block)
385
+ @block = block
386
+ @sep = sep
387
+ @rs = rs
388
+ end
389
+
390
+ def write_io (table, io)
391
+ return Processor.new(table, io, @sep, @rs, &@block).run
392
+ end
393
+
394
+ def write_string (table, string)
395
+ write_io(table, StringIO.new(string))
396
+ return string
397
+ end
398
+
399
+ def write_file (table, filename, mode="w")
400
+ open(filename, mode) { |io|
401
+ return write_io(table, io)
402
+ }
403
+ end
404
+
405
+ class Processor # :nodoc:
406
+
407
+ def initialize (table, io, sep, rs, &block)
408
+ @io = io
409
+ @sep = sep
410
+ @rs = rs
411
+ @block = block || proc { body }
412
+ if table.has_data_class?
413
+ @names = table.members
414
+ @table = CArray.merge(CA_OBJECT, table[nil].fields)
415
+ else
416
+ @names = table.instance_exec{ @names }
417
+ case
418
+ when table.rank > 2
419
+ @table = table.reshape(false,nil).object
420
+ when table.rank == 1
421
+ @table = table[:%,1].object ### convert to CA_OBJECT
422
+ else
423
+ @table = table.object ### convert to CA_OBJECT
424
+ end
425
+ end
426
+ @regexp_simple = /#{@sep}/o
427
+ end
428
+
429
+ def csv_quote (text)
430
+ text = text.dup
431
+ if text.gsub!(/"/, '""') or text =~ @regexp_simple ### /#{@sep}|"/
432
+ text = '"' + text + '"'
433
+ end
434
+ return text
435
+ end
436
+
437
+ def run
438
+ case @block.arity
439
+ when 1
440
+ @block.call(self)
441
+ when -1, 0
442
+ instance_exec(&@block)
443
+ else
444
+ raise "invalid block parameter"
445
+ end
446
+ end
447
+
448
+ # set @names
449
+ def names (list)
450
+ @names = list
451
+ end
452
+
453
+ # puts header
454
+ def header (list=@names)
455
+ @io.write list.map{|s| csv_quote(s)}.join(@sep)
456
+ @io.write(@rs)
457
+ end
458
+
459
+ # puts any strings
460
+ def puts (*argv)
461
+ @io.print(*argv)
462
+ @io.write(@rs)
463
+ end
464
+
465
+ # write value
466
+ # If option :strict is set, do csv_quote for string element
467
+ def body (opt = {:strict=>true})
468
+ if opt[:strict]
469
+ case @table.data_type
470
+ when CA_OBJECT
471
+ table = @table
472
+ table[:is_kind_of, String].map! { |s| csv_quote(s) }
473
+ when CA_FIXLEN
474
+ table = @table.object
475
+ table.map! { |s| csv_quote(s) }
476
+ else
477
+ table = @table.object
478
+ end
479
+ table.dim0.times do |i|
480
+ @io.write table[i,nil].to_a.join(@sep)
481
+ @io.write(@rs)
482
+ end
483
+ else
484
+ @table.dim0.times do |i|
485
+ @io.write @table[i,nil].to_a.join(@sep)
486
+ @io.write(@rs)
487
+ end
488
+ end
489
+ end
490
+
491
+ # pre processing data
492
+ def process (namelist = @names)
493
+ if namelist
494
+ namelist.each_with_index do |name, i|
495
+ yield(name, @table[nil, i])
496
+ end
497
+ else
498
+ @table.dim1.times do |i|
499
+ yield(i, @table[nil,i])
500
+ end
501
+ end
502
+ end
503
+ end
504
+ end
505
+ end
506
+
507
+ class CArray
508
+
509
+ def self.load_csv (file, option={}, rs: $/, sep: ",", &block)
510
+ option = {:sep=>sep, :rs=>rs}.update(option)
511
+ reader = CA::CSVReader.new(option[:sep], option[:rs], &block)
512
+ return reader.read_file(file)
513
+ end
514
+
515
+ def self.from_csv (io, option={}, rs: $/, sep: ",", &block)
516
+ option = {:sep=>sep, :rs=>rs}.update(option)
517
+ reader = CA::CSVReader.new(option[:sep], option[:rs], &block)
518
+ case io
519
+ when IO, StringIO
520
+ return reader.read_io(io)
521
+ when String
522
+ return reader.read_string(io)
523
+ else
524
+ raise "invalid argument"
525
+ end
526
+ end
527
+
528
+ def save_csv (file, option = {}, rs: $/, sep: ",", mode: "w", &block)
529
+ option = {:sep=>sep, :rs=>rs, :mode=>mode}.update(option)
530
+ writer = CA::CSVWriter.new(option[:sep], option[:rs], &block)
531
+ return writer.write_file(self, file, option[:mode])
532
+ end
533
+
534
+ def to_csv (io="", option ={}, rs: $/, sep: ",", &block)
535
+ option = {:sep=>sep, :rs=>rs}.update(option)
536
+ writer = CA::CSVWriter.new(option[:sep], option[:rs], &block)
537
+ case io
538
+ when IO, StringIO
539
+ return writer.write_io(self, io)
540
+ when String
541
+ return writer.write_string(self, io)
542
+ end
543
+ end
544
+
545
+ def to_tabular (option = {})
546
+ option = {:sep=>" ", :names=>nil}.update(option)
547
+ if option[:names]
548
+ names = option[:names]
549
+ elsif self.respond_to?(:names)
550
+ names = self.names
551
+ end
552
+ sep = option[:sep]
553
+ data = self.to_ca.map! {|s| s.to_s }
554
+ table = CArray.join([names.to_ca], [data])
555
+ length = table.convert{|s| s.length}.max(0)
556
+ table.map_with_index! {|s, idx| s.rjust(length[idx[1]]) }.to_csv.gsub(/,/,sep)
557
+ end
558
+
559
+ end
560
+