forked from masa16/narray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
narray.c
1309 lines (1083 loc) · 29.9 KB
/
narray.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
narray.c
Numerical Array Extention for Ruby
(C) Copyright 1999-2008 by Masahiro TANAKA
This program is free software.
You can distribute/modify this program
under the same terms as Ruby itself.
NO WARRANTY.
*/
#define NARRAY_C
#include <ruby.h>
#include "narray.h"
#include "narray_local.h"
/* global variables within this module */
VALUE cNArray, cNArrayScalar, cComplex;
ID na_id_beg, na_id_end, na_id_exclude_end;
ID na_id_minus, na_id_abs, na_id_power;
ID na_id_compare, na_id_ne, na_id_and, na_id_or;
ID na_id_class_dim;
ID na_id_add, na_id_sbt, na_id_mul, na_id_div, na_id_mod;
ID na_id_real, na_id_imag;
ID na_id_coerce_rev;
ID na_id_new;
ID na_id_Complex;
static ID na_id_to_i, na_id_usec, na_id_now;
const int na_sizeof[NA_NTYPES+1] = {
0,
sizeof(u_int8_t),
sizeof(int16_t),
sizeof(int32_t),
sizeof(float),
sizeof(double),
sizeof(scomplex),
sizeof(dcomplex),
sizeof(VALUE),
0
};
const char *na_typestring[] = {
"none",
"byte", /* 1 */
"sint", /* 2 */
"int", /* 3 */
"sfloat", /* 4 */
"float", /* 5 */
"scomplex", /* 6 */
"complex", /* 7 */
"object", /* 8 */
"ntypes" /* 9 */
};
#ifdef NARRAY_GC
static int mem_count = 0;
static int na_gc_freq = 2500000; /* Frequency of Garbage Collection */
#endif
void Init_nmath(void);
void Init_na_funcs(void);
void Init_na_linalg(void);
void Init_na_random(void);
#ifdef DEBUG
void na_xfree(void *ptr)
{
if (!ptr) abort();
free(ptr);
}
#endif
/* mark items */
static void
na_mark_obj(struct NARRAY *ary)
{
int i;
VALUE *ptr;
ptr = (VALUE*) ary->ptr;
for (i=ary->total; i>0; --i)
rb_gc_mark(*ptr++);
}
static void
na_mark_ref(struct NARRAY *ary)
{
struct NARRAY *a2;
rb_gc_mark( ary->ref );
GetNArray(ary->ref,a2);
if (a2->type == NA_ROBJ) na_mark_obj(a2);
}
static void
na_free(struct NARRAY* ary)
{
if ( ary->total > 0 ) {
if (ary->ref == Qnil || ary->ref == Qtrue) { /* non reference */
xfree(ary->ptr);
}
xfree(ary->shape);
#ifdef DEBUG
ary->shape = NULL;
ary->ptr = NULL;
#endif
}
xfree(ary);
}
/* allocation of NARRAY */
struct NARRAY*
na_alloc_struct(int type, int rank, int *shape)
{
int total=1;
int i, memsz;
struct NARRAY *ary;
for (i=0; i<rank; ++i)
total *= shape[i];
if (rank<=0 || total<=0) {
/* empty array */
ary = ALLOC(struct NARRAY);
ary->rank =
ary->total = 0;
ary->shape = NULL;
ary->ptr = NULL;
ary->type = type;
}
else {
memsz = na_sizeof[type] * total;
/* Garbage Collection */
#ifdef NARRAY_GC
mem_count += memsz;
if ( mem_count > na_gc_freq ) { rb_gc(); mem_count=0; }
#endif
ary = ALLOC(struct NARRAY);
ary->shape = ALLOC_N(int, rank);
ary->ptr = ALLOC_N(char, memsz);
ary->rank = rank;
ary->total = total;
ary->type = type;
for (i=0; i<rank; ++i)
ary->shape[i] = shape[i];
}
ary->ref = Qtrue;
return ary;
}
#if !defined RCLASS_SUPER
#define RCLASS_SUPER(v) (RCLASS(v)->super)
#endif
static void
na_check_class_narray(VALUE v)
{
if (TYPE(v) != T_CLASS) {
rb_raise(rb_eRuntimeError, "class required");
}
while (v) {
if (v == cNArray || RCLASS(v)->m_tbl == RCLASS(cNArray)->m_tbl)
return;
v = RCLASS_SUPER(v);
}
rb_raise(rb_eRuntimeError, "need NArray or its subclass");
}
static VALUE
na_wrap_struct_class(struct NARRAY *ary, VALUE klass)
{
VALUE v;
int class_dim;
/* Extract element */
if (ary->rank==0 && ary->total==1) {
SetFuncs[NA_ROBJ][ary->type](1,&v,0,ary->ptr,0);
na_free(ary);
return v;
}
/* check NArray >= klass */
na_check_class_narray(klass);
/* Check dimension */
class_dim = NUM2INT(rb_const_get(klass, na_id_class_dim));
if (ary->rank < class_dim)
rb_raise(rb_eTypeError, "array.dim(=%i) < CLASS_DIMENSION(=%i)",
ary->rank, class_dim);
if (ary->ref == Qnil)
rb_raise(rb_eRuntimeError, "already wrapped object");
/* Turn on WRAPPED flag */
if (ary->ref == Qtrue) {
ary->ref = Qnil;
if (NA_IsROBJ(ary))
return Data_Wrap_Struct(klass, na_mark_obj, na_free, ary);
else
return Data_Wrap_Struct(klass, 0, na_free, ary);
}
/* reference to another NArray*/
return Data_Wrap_Struct(klass, na_mark_ref, na_free, ary);
}
static VALUE
na_wrap_struct(struct NARRAY *ary, VALUE obj)
{
return na_wrap_struct_class(ary,CLASS_OF(obj));
}
VALUE
na_make_object(int type, int rank, int *shape, VALUE klass)
{
struct NARRAY *na;
na = na_alloc_struct(type, rank, shape);
if (type==NA_ROBJ) {
rb_mem_clear((VALUE*)(na->ptr), na->total);
}
return na_wrap_struct_class(na, klass);
}
/* restriction: Integer, Float, Complex === obj */
VALUE
na_make_scalar(VALUE obj, int type)
{
static int shape=1;
VALUE v;
struct NARRAY *ary;
v = na_make_object(type,1,&shape,cNArrayScalar);
GetNArray(v,ary);
SetFuncs[ary->type][NA_ROBJ](1, ary->ptr,0, &obj,0);
return v;
}
VALUE
na_make_empty(int type, VALUE klass)
{
struct NARRAY *na;
na = na_alloc_struct(type, 0, NULL);
return na_wrap_struct_class(na, klass);
}
/* allocate reference to NArray */
struct NARRAY*
na_ref_alloc_struct(VALUE obj)
{
int i;
struct NARRAY *orig, *ary;
GetNArray(obj,orig);
if (orig->rank<=0)
rb_raise(rb_eRuntimeError, "cannot create NArrayRefer of Empty NArray");
ary = ALLOC(struct NARRAY);
ary->shape = ALLOC_N(int, orig->rank);
ary->ptr = orig->ptr;
ary->rank = orig->rank;
ary->total = orig->total;
ary->type = orig->type;
for (i=0; i<orig->rank; ++i)
ary->shape[i] = orig->shape[i];
ary->ref = obj;
return ary;
}
/* method: self.refer */
static VALUE
na_refer(VALUE self)
{
return na_wrap_struct(na_ref_alloc_struct(self), self);
}
/* singleton method: NArray.refer( other ) */
static VALUE
na_s_refer(VALUE klass, VALUE self)
{
return na_wrap_struct_class(na_ref_alloc_struct(self), klass);
}
/* method: self.original */
static VALUE
na_original(VALUE self)
{
struct NARRAY *ary;
GetNArray(self,ary);
return ary->ref;
}
void
na_clear_data(struct NARRAY *ary)
{
if (NA_IsROBJ(ary))
rb_mem_clear((VALUE*)(ary->ptr), ary->total);
else
MEMZERO(ary->ptr, char, na_sizeof[ary->type]*ary->total);
}
/* local function for new array creation */
static VALUE
na_new2(int argc, VALUE *argv, int type, VALUE klass)
{
int i, *shape;
struct NARRAY *ary;
VALUE v;
if (argc == 0)
rb_raise(rb_eArgError, "Argument required");
shape = ALLOCA_N(int,argc);
for (i=0; i<argc; ++i) shape[i]=NUM2INT(argv[i]);
v = na_make_object(type,argc,shape,klass);
GetNArray(v,ary);
if (ary->type != NA_ROBJ)
na_clear_data(ary);
/* rb_obj_call_init(v, 0, 0); */
return v;
}
/* Convert type arguments -> typecode */
int
na_get_typecode(VALUE v)
{
struct NARRAY *na;
int i;
if (v==rb_cFloat) return NA_DFLOAT;
if (v==rb_cInteger) return NA_LINT;
if (v==cComplex) return NA_DCOMPLEX;
if (v==rb_cObject) return NA_ROBJ;
if (FIXNUM_P(v)) {
i = NUM2INT(v);
if (i<=NA_NONE || i>=NA_NTYPES)
rb_raise(rb_eArgError, "Wrong type code");
return i;
}
if (NA_IsNArray(v)) {
GetNArray(v,na);
return na->type;
}
if (TYPE(v)==T_STRING) {
for (i=1; i<NA_NTYPES; ++i) {
if ( !strncmp( RSTRING_PTR(v), na_typestring[i], RSTRING_LEN(v)) )
return i;
}
}
rb_raise(rb_eArgError, "Unrecognized NArray type");
return 0;
}
/* class method: new(type, size1,size2,...,sizeN) */
static VALUE
na_s_new(int argc, VALUE *argv, VALUE klass)
{
if (argc == 0)
rb_raise(rb_eArgError, "Argument required");
return na_new2(argc-1, argv+1, na_get_typecode(argv[0]), klass);
}
/* class method: byte(size1,size2,...,sizeN) */
static VALUE
na_s_new_byte(int argc, VALUE *argv, VALUE klass)
{ return na_new2(argc, argv, NA_BYTE, klass); }
/* class method: sint(size1,size2,...,sizeN) */
static VALUE
na_s_new_sint(int argc, VALUE *argv, VALUE klass)
{ return na_new2(argc, argv, NA_SINT, klass); }
/* class method: int(size1,size2,...,sizeN) */
static VALUE
na_s_new_int(int argc, VALUE *argv, VALUE klass)
{ return na_new2(argc, argv, NA_LINT, klass); }
/* class method: sfloat(size1,size2,...,sizeN) */
static VALUE
na_s_new_sfloat(int argc, VALUE *argv, VALUE klass)
{ return na_new2(argc, argv, NA_SFLOAT, klass); }
/* class method: float(size1,size2,...,sizeN) */
static VALUE
na_s_new_float(int argc, VALUE *argv, VALUE klass)
{ return na_new2(argc, argv, NA_DFLOAT, klass); }
/* class method: scomplex(size1,size2,...,sizeN) */
static VALUE
na_s_new_scomplex(int argc, VALUE *argv, VALUE klass)
{ return na_new2(argc, argv, NA_SCOMPLEX, klass); }
/* class method: complex(size1,size2,...,sizeN) */
static VALUE
na_s_new_complex(int argc, VALUE *argv, VALUE klass)
{ return na_new2(argc, argv, NA_DCOMPLEX, klass); }
/* class method: object(size1,size2,...,sizeN) */
static VALUE
na_s_new_object(int argc, VALUE *argv, VALUE klass)
{ return na_new2(argc, argv, NA_ROBJ, klass); }
/* method: dup() */
VALUE
na_clone(VALUE self)
{
struct NARRAY *org, *cpy;
GetNArray(self,org);
cpy = na_alloc_struct(org->type,org->rank,org->shape);
memcpy(cpy->ptr, org->ptr, na_sizeof[org->type] * org->total);
return na_wrap_struct(cpy,self);
}
/* local function */
void
na_copy_nary(struct NARRAY *dst, struct NARRAY *src)
{
if (dst->total != src->total)
rb_raise(rb_eRuntimeError, "src and dst array sizes mismatch");
if (dst->type == src->type)
memcpy(dst->ptr, src->ptr, src->total*na_sizeof[src->type]);
else {
SetFuncs[dst->type][src->type]( src->total,
dst->ptr, na_sizeof[dst->type],
src->ptr, na_sizeof[src->type] );
}
}
/* method: to_type(type) */
static VALUE
na_to_type(VALUE self, VALUE vtype)
{
struct NARRAY *a1, *a2;
VALUE v;
GetNArray(self,a1);
v = na_make_object(na_get_typecode(vtype), a1->rank, a1->shape,
CLASS_OF(self));
GetNArray(v,a2);
na_copy_nary(a2,a1);
return v;
}
/* method: to_f() */
static VALUE
na_to_float(VALUE self)
{
struct NARRAY *a1, *a2;
VALUE v;
GetNArray(self,a1);
v = na_make_object(na_upcast[NA_SFLOAT][a1->type], a1->rank, a1->shape,
CLASS_OF(self));
GetNArray(v,a2);
na_copy_nary(a2,a1);
return v;
}
/* method: to_i() */
static VALUE
na_to_integer(VALUE self)
{
int type;
struct NARRAY *a1, *a2;
VALUE v;
GetNArray(self,a1);
if (!NA_IsINTEGER(a1))
type = NA_LINT;
else
type = a1->type;
v = na_make_object(type, a1->rank, a1->shape, CLASS_OF(self));
GetNArray(v,a2);
na_copy_nary(a2,a1);
return v;
}
/* method: shape() -- returns an array of shape of each rank */
static VALUE
na_shape(VALUE self)
{
struct NARRAY *ary;
VALUE *shape;
int i;
GetNArray(self,ary);
shape = ALLOCA_N(VALUE,ary->rank);
for (i = 0; i < ary->rank; ++i)
shape[i] = INT2FIX(ary->shape[i]);
return rb_ary_new4(ary->rank,shape);
}
/* method: rank() -- returns the rank of the array */
static VALUE
na_rank(VALUE self)
{
struct NARRAY *ary;
GetNArray(self,ary);
return INT2FIX(ary->rank);
}
/* method: size() -- returns the total number of elements */
static VALUE
na_size(VALUE self)
{
struct NARRAY *ary;
GetNArray(self,ary);
return INT2FIX(ary->total);
}
/* method: typecode -- returns the type of the array */
static VALUE
na_typecode(VALUE self)
{
struct NARRAY *ary;
GetNArray(self,ary);
return INT2FIX(ary->type);
}
/* method: element_size -- returns the element size of the array type */
static VALUE
na_element_size(VALUE self)
{
struct NARRAY *ary;
GetNArray(self,ary);
return INT2FIX(na_sizeof[ary->type]);
}
/* method: empty? -- returns true if empty array */
static VALUE
na_is_empty(VALUE self)
{
struct NARRAY *ary;
GetNArray(self,ary);
if (ary->total==0) return Qtrue;
return Qfalse;
}
/* Binary copy of String => NArray */
static VALUE
na_str_to_na(int argc, VALUE *argv, VALUE str)
{
struct NARRAY *ary;
VALUE v;
int i, type, len=1, str_len, *shape, rank=argc-1;
if (argc < 1)
rb_raise(rb_eArgError, "Type and Size Arguments required");
type = na_get_typecode(argv[0]);
str_len = RSTRING_LEN(str);
if (argc == 1) {
rank = 1;
shape = ALLOCA_N(int,rank);
if ( str_len % na_sizeof[type] != 0 )
rb_raise(rb_eArgError, "string size mismatch");
shape[0] = str_len / na_sizeof[type];
}
else {
shape = ALLOCA_N(int,rank);
for (i=0; i<rank; ++i)
len *= shape[i] = NUM2INT(argv[i+1]);
len *= na_sizeof[type];
if ( len != str_len )
rb_raise(rb_eArgError, "size mismatch");
}
v = na_make_object( type, rank, shape, cNArray );
GetNArray(v,ary);
memcpy( ary->ptr, RSTRING_PTR(str), ary->total*na_sizeof[type] );
return v;
}
/* method: to_s -- convert the data contents to a binary string */
static VALUE
na_to_s(VALUE self)
{
struct NARRAY *ary;
GetNArray(self,ary);
if (NA_IsROBJ(ary))
rb_raise(rb_eTypeError,"cannot convert object-type NArray");
return rb_str_new(ary->ptr,ary->total*na_sizeof[ary->type]);
}
/* method: to_binary -- convert the data contents to a BYTE type NArray */
static VALUE
na_to_binary(VALUE self)
{
struct NARRAY *a1, *a2;
int i, *shape, rank;
VALUE v;
GetNArray(self,a1);
rank = a1->rank+1;
shape = ALLOCA_N(int,rank);
shape[0] = na_sizeof[a1->type];
for (i=1; i<rank; ++i)
shape[i] = a1->shape[i-1];
v = na_make_object( NA_BYTE, rank, shape, cNArray );
GetNArray(v,a2);
MEMCPY(a2->ptr,a1->ptr,char,a2->total);
return v;
}
/* method: to_type_as_binary(type) */
static VALUE
na_to_type_as_binary(VALUE self, VALUE vtype)
{
struct NARRAY *a1, *a2;
int size, total, type;
VALUE v;
type = na_get_typecode(vtype);
GetNArray(self,a1);
size = a1->total * na_sizeof[a1->type];
if ( size % na_sizeof[type] != 0 )
rb_raise(rb_eRuntimeError, "bina1 size mismatch");
total = size / na_sizeof[type];
v = na_make_object( type, 1, &total, cNArray );
GetNArray(v,a2);
MEMCPY(a2->ptr,a1->ptr,char,size);
return v;
}
static void
na_to_string_binary(int n, char *p1, int i1, char *p2, int i2)
{
for (; n>0; --n) {
*(VALUE*)p1 = rb_str_new(p2,i2);
p1+=i1; p2+=i2;
}
}
/* method: to_string */
static VALUE
na_to_string(VALUE self)
{
VALUE v;
struct NARRAY *a1, *a2;
GetNArray(self,a1);
if (a1->total==0)
v = na_make_empty(NA_ROBJ, CLASS_OF(self));
else
if (a1->type==NA_BYTE) {
if (a1->rank==1)
return rb_str_new(a1->ptr,a1->shape[0]);
v = na_make_object(NA_ROBJ, a1->rank-1, a1->shape+1, cNArray);
GetNArray(v,a2);
na_to_string_binary( a2->total,
a2->ptr, sizeof(VALUE),
a1->ptr, a1->shape[0] );
} else {
v = na_make_object(NA_ROBJ, a1->rank, a1->shape, CLASS_OF(self));
GetNArray(v,a2);
ToStrFuncs[a1->type]( a2->total,
a2->ptr, sizeof(VALUE),
a1->ptr, na_sizeof[a1->type] );
}
return v;
}
/* singleton method:
NArray.to_na( string, type, size1,size2,...,sizeN )
NArray.to_na( array )
*/
static VALUE
na_s_to_na(int argc, VALUE *argv, VALUE klass)
{
static int shape=1;
VALUE v;
struct NARRAY *ary;
if (argc < 1) {
rb_raise(rb_eArgError, "Argument is required");
}
if (TYPE(argv[0]) == T_STRING) {
return na_str_to_na(argc-1,argv+1,argv[0]);
}
if (argc > 1) {
rb_raise(rb_eArgError, "Only one array argument must be provided");
}
if (TYPE(argv[0]) == T_ARRAY) {
return na_ary_to_nary( argv[0], klass );
}
if (NA_IsNArray(argv[0])) {
return argv[0];
}
rb_raise(rb_eTypeError, "Argument must be Array or String (or NArray)");
return Qnil;
}
/* singleton method:
NArray[object]
*/
static VALUE
na_s_bracket(int argc, VALUE *argv, VALUE klass)
{
VALUE v = rb_ary_new4(argc, argv);
return na_ary_to_nary( v, klass );
}
/* method: coerce(other) */
static VALUE na_coerce(VALUE self, VALUE other)
{
struct NARRAY *a1;
GetNArray(self,a1);
return rb_assoc_new( na_cast_object(other,a1->type), self );
}
/* method: inspect() -- returns the inspect of the array */
static VALUE
na_inspect(VALUE self)
{
VALUE str;
struct NARRAY *ary;
int i;
char buf[256];
const char *classname;
char *ref = "%s(ref).%s(%i";
char *org = "%s.%s(%i";
GetNArray(self,ary);
classname = rb_class2name(CLASS_OF(self));
str = rb_str_new(0,0);
if (ary->rank < 1) {
sprintf(buf, "%s.%s(): []", classname, na_typestring[ary->type]);
rb_str_cat(str,buf,strlen(buf));
}
else {
sprintf(buf, (ary->ref==Qnil) ? org:ref,
classname, na_typestring[ary->type], ary->shape[0]);
rb_str_cat(str,buf,strlen(buf));
for (i=1; i<ary->rank; ++i) {
sprintf(buf,",%i",ary->shape[i]);
rb_str_cat(str,buf,strlen(buf));
}
rb_str_cat(str,")",1);
rb_str_cat(str,": \n",3);
rb_str_concat(str, na_make_inspect(self));
}
return str;
}
/* private function for reshape */
static void
na_reshape(int argc, VALUE *argv, struct NARRAY *ary, VALUE self)
{
int *shape, class_dim;
int i, total=1, unfixed=-1;
VALUE klass;
if (ary->total==0)
rb_raise(rb_eRuntimeError, "cannot reshape empty array");
klass = CLASS_OF(self);
class_dim = NUM2INT(rb_const_get(klass, na_id_class_dim));
if (argc == 0) { /* trim ranks of size=1 */
shape = ALLOCA_N(int,ary->rank+1);
for (i=0; i<class_dim; ++i) shape[i]=0;
for ( ; i<ary->rank; ++i) shape[i]=1;
na_shrink_rank( self, class_dim, shape );
if (ary->rank==0) ary->rank=1;
return;
}
/* get shape from argument */
shape = ALLOC_N(int,argc);
for (i=0; i<argc; ++i)
switch(TYPE(argv[i])) {
case T_FIXNUM:
total *= shape[i] = NUM2INT(argv[i]);
break;
case T_TRUE:
unfixed = i;
break;
default:
rb_raise(rb_eArgError,"illegal type");
}
if (unfixed>=0) {
if (ary->total % total != 0)
rb_raise(rb_eArgError, "Total size size must be divisor");
shape[unfixed] = ary->total / total;
}
else if (total != ary->total)
rb_raise(rb_eArgError, "Total size must be same");
/* exchange */
xfree(ary->shape);
ary->shape = shape;
ary->rank = argc;
}
/* method: reshape!(size1,size2,...,sizeN) */
static VALUE
na_reshape_bang(int argc, VALUE *argv, VALUE self)
{
struct NARRAY *ary;
GetNArray(self,ary);
na_reshape(argc, argv, ary, self);
return self;
}
/* method: reshape(size1,size2,...,sizeN) */
static VALUE
na_reshape_ref(int argc, VALUE *argv, VALUE self)
{
struct NARRAY *ary;
GetNArray(self,ary);
ary = na_ref_alloc_struct(self);
na_reshape(argc, argv, ary, self);
return na_wrap_struct(ary,self);
}
/* method: flatten! */
static VALUE
na_flatten_bang(VALUE self)
{
struct NARRAY *ary;
GetNArray(self,ary);
if (ary->total==0 || ary->rank==0)
rb_raise(rb_eRuntimeError, "cannot reshape empty array");
ary->shape[0] = ary->total;
ary->rank = 1;
return self;
}
/* method: flatten */
static VALUE
na_flatten_ref(VALUE self)
{
return na_flatten_bang( na_wrap_struct( na_ref_alloc_struct(self), self ));
}
/* private function for newdim */
static void
na_newdim(int argc, VALUE *argv, struct NARRAY *ary)
{
int *shape, *count;
int i, j;
if (argc==0)
rb_raise(rb_eArgError, "Argument required");
if (ary->rank + argc > NA_MAX_RANK-1)
rb_raise(rb_eArgError, "Exceed maximum ranks");
if (ary->total==0)
rb_raise(rb_eRuntimeError, "cannot extend empty array");
/* count new rank */
count = ALLOCA_N(int,ary->rank+1);
for (i=0; i<=ary->rank; ++i)
count[i]=0;
for (i=0; i<argc; ++i) {
j = NUM2INT(argv[i]);
if (j<0) /* negative rank : -1=>append after last rank */
j += ary->rank+1;
if (j<0 || j>ary->rank) /* range check */
rb_raise(rb_eArgError, "rank out of range");
++count[j];
}
/* extend shape shape */
shape = ALLOC_N(int,ary->rank+argc);
for (j=i=0; i<ary->rank; ++i) {
while (count[i]-->0) shape[j++] = 1;
shape[j++] = ary->shape[i];
}
while (count[i]-->0) shape[j++] = 1;
/* exchange shape */
xfree(ary->shape);
ary->shape = shape;
ary->rank += argc;
}
/* method: newdim!(size1,size2,...,sizeN) */
static VALUE
na_newdim_bang(int argc, VALUE *argv, VALUE self)
{
struct NARRAY *ary;
GetNArray(self,ary);
na_newdim(argc, argv, ary);
return self;
}
/* method: newdim(size1,size2,...,sizeN) */
VALUE
na_newdim_ref(int argc, VALUE *argv, VALUE self)
{
struct NARRAY *ary;
GetNArray(self,ary);
ary = na_ref_alloc_struct(self);
na_newdim(argc, argv, ary);
return na_wrap_struct(ary,self);
}
/* method: fill!(val) */
VALUE na_fill(VALUE self, volatile VALUE val)
{
struct NARRAY *a1, *a2;
GetNArray(self,a1);
val = na_cast_unless_narray(val,a1->type);
GetNArray(val,a2);
if (a2->total != 1)
rb_raise(rb_eArgError, "single-element argument required");
SetFuncs[a1->type][a2->type]( a1->total,
a1->ptr, na_sizeof[a1->type],
a2->ptr, 0 );
return self;
}
/* method: indgen!([start,[step]]) */
VALUE
na_indgen(int argc, VALUE *argv, VALUE self)
{
int start=0, step=1;