numo-narray 0.9.1.0 → 0.9.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,133 +0,0 @@
1
- class CountLnString < String
2
-
3
- def initialize(filename)
4
- @filename = filename
5
- @lnchar = "\n"
6
- @buf = ""
7
- @str = ""
8
- @countln = 1
9
- @current = 1
10
- super("\n"+report_line)
11
- end
12
-
13
- def report_line
14
- "#line #{@current} \"#{@filename}\"\n"
15
- end
16
-
17
- def concat0(s)
18
- ln(caller[0])
19
- @buf.concat(s)
20
- @str.concat(s)
21
- end
22
-
23
- def concat1(s)
24
- ln(caller[0])
25
- @buf.concat(s)
26
- end
27
-
28
- def ln(status=nil)
29
- case status
30
- when /:(\d+):/
31
- n = $1.to_i
32
- else
33
- n = status.to_i
34
- end
35
- return if n == @current
36
- if @current != @countln || @postpone
37
- if /\A\s*\z/ =~ @str || /\A#line / =~ @buf
38
- @postpone = true
39
- elsif @in_comment
40
- @postpone = false
41
- else
42
- if self[-1] != "\n"
43
- concat("\n")
44
- end
45
- concat(report_line)
46
- @postpone = false
47
- end
48
- end
49
- concat(@buf)
50
-
51
- b = @buf.gsub(/".*?(?<!\\)"/,'""')
52
- /^.*(\/\*)(.*?)$/ =~ b
53
- x = $2
54
- /^.*(\*\/)(.*?)$/ =~ b
55
- y = $2
56
- if x
57
- if y
58
- if x.size < y.size
59
- #:in_comment
60
- @in_comment = true
61
- else
62
- #:out_comment
63
- @in_comment = false
64
- end
65
- else
66
- #:in_comment
67
- @in_comment = true
68
- end
69
- else
70
- if y
71
- #:out_comment
72
- @in_comment = false
73
- else
74
- #:keep
75
- end
76
- end
77
-
78
- @countln = @current + @buf.count(@lnchar)
79
- @current = n
80
- @buf = ""
81
- @str = ""
82
- end
83
-
84
- def d(s)
85
- p [s, [x,y], r]
86
- r
87
- end
88
-
89
- def final
90
- concat(@buf)
91
- end
92
-
93
- end
94
-
95
- class ERB
96
- alias result_orig result
97
-
98
- def result(b=new_toplevel)
99
- src = src_with_cpp_line
100
- if @safe_level
101
- proc {
102
- $SAFE = @safe_level
103
- eval(src, b, (@filename || '(erb)'), 0)
104
- }.call
105
- else
106
- #open("tmpout","w"){|f| f.write src} if /dtype/=~@filename
107
- eval(src, b, (@filename || '(erb)'), 0)
108
- end
109
- end
110
-
111
- alias src_orig src
112
-
113
- def src
114
- src_with_cpp_line
115
- end
116
-
117
- def src_with_cpp_line
118
- @src.each_line.with_index.map do |line, num|
119
- line.gsub!(/_erbout.concat "/,'_erbout.concat0 "')
120
- line.gsub!(/_erbout.concat\(/,'_erbout.concat1(')
121
- if num==0
122
- # skip
123
- elsif num==1
124
- f = @filename.dump
125
- line.sub!(/_erbout = (\+?''|String\.new);/, "_erbout = CountLnString.new(#{f});")
126
- elsif /^; _erbout\.force_encoding/ =~ line
127
- line.sub!(/^;/,";_erbout.ln(#{num});")
128
- end
129
- line
130
- end.join+";_erbout.final;"
131
- end
132
-
133
- end
@@ -1,381 +0,0 @@
1
- require 'erbpp'
2
-
3
- module DefMethod
4
-
5
- def def_id(meth,var=nil)
6
- IdVar.new(self, meth, var)
7
- end
8
-
9
- def def_method(meth, n_arg, tmpl=nil, opts={})
10
- h = {:meth => meth, :n_arg => n_arg}
11
- h.merge!(opts)
12
- tmpl ||= meth
13
- Function.new(self, tmpl, h)
14
- end
15
-
16
- def def_singleton(meth, n_arg, tmpl=nil, opts={})
17
- def_method(meth, n_arg, tmpl, :singleton => true)
18
- end
19
-
20
- def def_alias(dst, src)
21
- Alias.new(self, dst, src)
22
- end
23
-
24
- def def_allocate(tmpl)
25
- h = {:meth => "allocate", :singleton => true}
26
- Allocate.new(self, tmpl, h)
27
- end
28
-
29
- def binary(meth, ope=nil)
30
- ope = meth if ope.nil?
31
- def_method(meth, 1, "binary", :op => ope)
32
- end
33
-
34
- def binary2(meth, ope=nil)
35
- ope = meth if ope.nil?
36
- def_method(meth, 1, "binary2", :op =>ope)
37
- end
38
-
39
- def unary(meth, ope=nil)
40
- def_method(meth, 0, "unary", :op => ope)
41
- end
42
-
43
- def pow
44
- def_method("pow", 1, "pow", :op => "**")
45
- end
46
-
47
- def unary2(meth, dtype, tpclass)
48
- h = {:dtype => dtype, :tpclass => tpclass}
49
- def_method(meth, 0, "unary2", h)
50
- end
51
-
52
- def set2(meth, dtype, tpclass)
53
- h = {:dtype => dtype, :tpclass => tpclass}
54
- def_method(meth, 1, "set2", h)
55
- end
56
-
57
- def cond_binary(meth,op=nil)
58
- op = meth unless op
59
- def_method(meth, 1, "cond_binary", :op => op)
60
- end
61
-
62
- def cond_unary(meth)
63
- def_method(meth, 0, "cond_unary")
64
- end
65
-
66
- def bit_count(meth)
67
- def_method(meth, -1, "bit_count")
68
- end
69
-
70
- def bit_reduce(meth, init_bit)
71
- h = {:init_bit=>init_bit}
72
- def_method(meth, -1, "bit_reduce", h)
73
- end
74
-
75
- def accum(meth, dtype, tpclass)
76
- h = {:dtype => dtype, :tpclass => tpclass}
77
- def_method(meth, -1, "accum", h)
78
- end
79
-
80
- def accum_index(meth)
81
- def_method(meth, -1, "accum_index")
82
- end
83
-
84
- def cum(meth, cmacro)
85
- def_method(meth, -1, "cum", cmacro:cmacro)
86
- end
87
-
88
- def accum_binary(meth, ope=nil)
89
- ope = meth if ope.nil?
90
- def_method(meth, -1, "accum_binary", :op => ope)
91
- end
92
-
93
- def qsort(tp, dtype, dcast, suffix="")
94
- h = {:tp => tp, :dtype => dtype, :dcast => dcast, :suffix => suffix}
95
- NodefFunction.new(self, "qsort", h)
96
- end
97
-
98
- def def_mod_func(meth, n_arg, tmpl=nil, opts={})
99
- h = {:meth => meth, :n_arg => n_arg}
100
- h.merge!(opts)
101
- tmpl ||= meth
102
- ModuleFunction.new(self, tmpl, h)
103
- end
104
-
105
- def math(meth, n=1, tmpl=nil)
106
- h = {:mod_var => 'mTM'}
107
- if tmpl.nil?
108
- case n
109
- when 1
110
- tmpl = "unary_s"
111
- when 2
112
- tmpl = "binary_s"
113
- when 3
114
- tmpl = "ternary_s"
115
- else
116
- raise "invalid n=#{n}"
117
- end
118
- end
119
- def_mod_func(meth, n, tmpl, h)
120
- end
121
-
122
- def store_numeric
123
- StoreNum.new(self,"store_numeric")
124
- end
125
-
126
- def store_array
127
- StoreArray.new(self,"store_array")
128
- end
129
-
130
- def cast_array
131
- CastArray.new(self,"cast_array")
132
- end
133
-
134
- def store_from(cname,dtype,macro)
135
- Store.new(self,"store_from",cname.downcase,dtype,"numo_c"+cname,macro)
136
- end
137
-
138
- def store_bit(cname)
139
- Store.new(self,"store_bit",cname.downcase,nil,"numo_c"+cname,nil)
140
- end
141
-
142
- def store
143
- Function.new(self,"store","store")
144
- end
145
-
146
- def find_method(meth)
147
- Function::DEFS.find{|x| x.kind_of?(Function) and meth == x.meth }
148
- end
149
-
150
- def find_tmpl(meth)
151
- Function::DEFS.find{|x| x.kind_of?(Function) and meth == x.tmpl }
152
- end
153
-
154
- def cast_func
155
- "numo_#{tp}_s_cast"
156
- end
157
- end
158
-
159
- # ----------------------------------------------------------------------
160
-
161
- class DataType < ErbPP
162
- include DefMethod
163
-
164
- def initialize(erb_path, type_file)
165
- super(nil, erb_path)
166
- @class_alias = []
167
- @upcast = []
168
- @mod_var = "cT"
169
- load_type(type_file) if type_file
170
- dirs = template_dir || ["tmpl"]
171
- @tmpl_dirs = dirs.map{|d| File.join(File.dirname(erb_path),d)}
172
- end
173
-
174
- attr_reader :tmpl_dirs
175
-
176
- def load_type(file)
177
- eval File.read(file)
178
- end
179
-
180
- attrs = %w[
181
- class_name
182
- ctype
183
- template_dir
184
- blas_char
185
- complex_class_name
186
- complex_type
187
- real_class_name
188
- real_ctype
189
- has_math
190
- is_bit
191
- is_int
192
- is_unsigned
193
- is_float
194
- is_real
195
- is_complex
196
- is_object
197
- is_comparable
198
- is_double_precision
199
- mod_var
200
- ]
201
- define_attrs attrs
202
-
203
- def type_name
204
- @type_name ||= class_name.downcase
205
- end
206
- alias tp type_name
207
-
208
- def type_var
209
- @type_var ||= "numo_c"+class_name
210
- end
211
-
212
- def math_var
213
- @math_var ||= "numo_m"+class_name+"Math"
214
- end
215
-
216
- def real_class_name(arg=nil)
217
- if arg.nil?
218
- @real_class_name ||= class_name
219
- else
220
- @real_class_name = arg
221
- end
222
- end
223
-
224
- def real_ctype(arg=nil)
225
- if arg.nil?
226
- @real_ctype ||= ctype
227
- else
228
- @real_ctype = arg
229
- end
230
- end
231
-
232
- def real_type_var
233
- @real_type_var ||= "numo_c"+real_class_name
234
- end
235
-
236
- def real_type_name
237
- @real_type_name ||= real_class_name.downcase
238
- end
239
-
240
- def class_alias(*args)
241
- @class_alias.concat(args)
242
- end
243
-
244
- def upcast(c=nil,t=nil)
245
- if c
246
- if t
247
- t = "numo_c#{t}"
248
- else
249
- t = "cT"
250
- end
251
- @upcast << "rb_hash_aset(hCast, numo_c#{c}, #{t});"
252
- else
253
- @upcast
254
- end
255
- end
256
-
257
- def upcast_rb(c,t=nil)
258
- if t
259
- t = "numo_c#{t}"
260
- else
261
- t = "cT"
262
- end
263
- if c=="Integer"
264
- @upcast << "#ifdef RUBY_INTEGER_UNIFICATION"
265
- @upcast << "rb_hash_aset(hCast, rb_cInteger, #{t});"
266
- @upcast << "#else"
267
- @upcast << "rb_hash_aset(hCast, rb_cFixnum, #{t});"
268
- @upcast << "rb_hash_aset(hCast, rb_cBignum, #{t});"
269
- @upcast << "#endif"
270
- else
271
- @upcast << "rb_hash_aset(hCast, rb_c#{c}, #{t});"
272
- end
273
- end
274
- end
275
-
276
-
277
- # ----------------------------------------------------------------------
278
-
279
-
280
- class Allocate < Function
281
- def definition
282
- "rb_define_alloc_func(#{mod_var}, #{c_func});"
283
- end
284
- end
285
-
286
- # ----------------------------------------------------------------------
287
-
288
- class Store < Function
289
- DEFS = []
290
-
291
- def initialize(parent,tmpl,tpname,dtype,tpclass,macro)
292
- super(parent,tmpl)
293
- @tpname=tpname
294
- @dtype=dtype
295
- @tpclass=tpclass
296
- @macro=macro
297
- DEFS.push(self)
298
- end
299
- attr_reader :tmpl, :tpname, :dtype, :tpclass, :macro
300
-
301
- def c_func
302
- "numo_#{tp}_store_#{tpname}"
303
- end
304
-
305
- def c_iter
306
- "iter_#{tp}_store_#{tpname}"
307
- end
308
-
309
- def definition
310
- nil
311
- end
312
-
313
- def condition(klass)
314
- "#{klass}==#{tpclass}"
315
- end
316
-
317
- def extract_data(ptr,pos,x)
318
- case tpname
319
- when "bit"
320
- "{BIT_DIGIT b; LOAD_BIT(#{ptr},#{pos},b); x = m_from_real(b);}"
321
- when "robject"
322
- "#{x} = m_num_to_data(*(#{dtype}*)(#{ptr}+#{pos}))"
323
- when /complex/
324
- "{#{dtype} *p = (#{dtype}*)(#{ptr}+#{pos}); #{x} = c_new(REAL(*p),IMAG(*p));}"
325
- else
326
- "#{x} = m_from_real(*(#{dtype}*)(#{ptr}+#{pos}))"
327
- end
328
- end
329
-
330
- def self.definitions
331
- a = []
332
- DEFS.each do |x|
333
- if x.condition("")
334
- if x.tpname == x.parents[0].class_name.downcase
335
- a.unshift(x)
336
- else
337
- a.push(x)
338
- end
339
- end
340
- end
341
- a
342
- end
343
- end
344
-
345
- class StoreNum < Store
346
- def initialize(parent,tmpl)
347
- super(parent,tmpl,"numeric",nil,nil,nil)
348
- end
349
-
350
- def condition(klass)
351
- "IS_INTEGER_CLASS(#{klass}) || #{klass}==rb_cFloat || #{klass}==rb_cComplex"
352
- end
353
- end
354
-
355
- class StoreArray < Store
356
- def initialize(parent,tmpl)
357
- super(parent,tmpl,"array",nil,nil,nil)
358
- end
359
-
360
- def c_func
361
- "numo_#{tp}_#{tmpl}"
362
- end
363
-
364
- def condition(klass)
365
- "#{klass}==rb_cArray"
366
- end
367
- end
368
-
369
- class CastArray < StoreArray
370
- def condition(klass)
371
- nil
372
- end
373
-
374
- def c_func
375
- "numo_#{tp}_cast_#{tpname}"
376
- end
377
-
378
- def c_iter
379
- "iter_#{tp}_cast_#{tpname}"
380
- end
381
- end