BlitzMax Extended  0.8.19
Pushing Blitz to the Max.
blitz_array.h
Go to the documentation of this file.
1 
11 #ifndef BLITZ_ARRAY_H
12 #define BLITZ_ARRAY_H
13 
14 #include "_common.h"
15 
16 #include "blitz_class.h"
17 #include "blitz_object.h"
18 
19 BB_BEGIN_DECLS
20 
42 struct BBArray
43 {
50  int refs;
51 
66  const char* type;
67 
69  BBSize dims;
70 
76  BBSize size;
77 
83  BBSize scales[1];
84 };
85 
86 #define BBARRAY_HEADER_SIZE (sizeof (BBArray) - sizeof (BBSize))
87 
88 // Maximum value of an array's data
89 #define BBARRAY_MAX_DATASIZE (BBSIZE_MAX - sizeof (BBArray))
90 
91 // Absolute maximum length (value of scales[0]) of an array.
92 // Depends on the data type the array stores
93 #define BBARRAY_MAX_LENGTH (BBARRAY_MAX_DATASIZE)
94 
112 #define BBARRAY_SIZE_CALC(size, num_dims) \
113  (BBARRAY_HEADER_SIZE + (num_dims) * sizeof (int) + (size))
114 
122 #define BBARRAY_SIZE(arr) BBARRAY_SIZE_CALC((arr)->size, (arr)->dims)
123 
134 #define BBARRAY_DATAPTR_CALC(arr, num_dims) \
135  ((void *) \
136  ((char *)(arr) + BBARRAY_HEADER_SIZE + (num_dims) * sizeof (int)) \
137  )
138 
149 #define BBARRAY_DATAPTR(arr) BBARRAY_DATAPTR_CALC((arr), (arr)->dims)
150 
171 #define BBARRAY_ALLOC(size, dims, flags) \
172  ({ \
173  (BBArray *) \
174  bbGCAllocObject( \
175  BBARRAY_SIZE_CALC((size), (dims)), \
176  &bbArrayClass, \
177  (flags) \
178  ); \
179  })
180 
191 #define BBARRAY_TYPE_ISNUMERIC(type) \
192  ( \
193  (type)[0] == 'i' || \
194  (type)[0] == 'f' || \
195  (type)[0] == 'd' || \
196  (type)[0] == 'b' || \
197  (type)[0] == 's' || \
198  (type)[0] == 'l' \
199  )
200 
211 #define BBARRAY_TYPE_ISPRIMITIVE(type) \
212  ( \
213  BBARRAY_TYPE_ISNUMERIC(type) || \
214  (type)[0] == '(' || (type)[0] == '*' \
215  )
216 
227 #define BBARRAY_TYPE_ISCOMPLEX(type) \
228  ( \
229  (type)[0] == ':' || \
230  (type)[0] == '$' || \
231  (type)[0] == '[' \
232  )
233 
244 #define BBARRAY_TYPE_ISVALID(type) \ \
245  ( \
246  BBARRAY_TYPE_ISPRIMITIVE(type) || \
247  BBARRAY_TYPE_ISCOMPLEX(type) \
248  )
249 
262 #define BBARRAY_ISCOMPLEX(arr) BBARRAY_TYPE_ISCOMPLEX((arr)->type)
263 
275 #define BBARRAY_ISNUMERIC(arr) BBARRAY_TYPE_ISNUMERIC((arr)->type)
276 
288 #define BBARRAY_ISPRIMITIVE(arr) BBARRAY_TYPE_ISPRIMITIVE((arr)->type)
289 
299 extern BBClass bbArrayClass;
300 
308 extern BBArray bbEmptyArray;
309 
316 #define BBNULLARRAY (&bbEmptyArray)
317 
323 {
325  int refs;
326 
327  const char *type;
328  BBSize dims;
329  BBSize size;
330  BBSize scales[1];
331 
334 };
335 
341 {
343  int refs;
344 
345  const char *type;
346  BBSize dims;
347  BBSize size;
348  BBSize scales[1];
349 
352 };
353 
359 {
361  int refs;
362 
363  const char *type;
364  BBSize dims;
365  BBSize size;
366  BBSize scales[1];
367 
370 };
371 
377 {
379  int refs;
380 
381  const char *type;
382  BBSize dims;
383  BBSize size;
384  BBSize scales[1];
385 
388 };
389 
395 {
397  int refs;
398 
399  const char *type;
400  BBSize dims;
401  BBSize size;
402  BBSize scales[1];
403 
406 };
407 
413 {
415  int refs;
416 
417  const char *type;
418  BBSize dims;
419  BBSize size;
420  BBSize scales[1];
421 
424 };
425 
431 {
433  int refs;
434 
435  const char *type;
436  BBSize dims;
437  BBSize size;
438  BBSize scales[1];
439 
442 };
443 
449 {
451  int refs;
452 
453  const char *type;
454  BBSize dims;
455  BBSize size;
456  BBSize scales[1];
457 
460 };
461 
467 {
469  int refs;
470 
471  const char *type;
472  BBSize dims;
473  BBSize size;
474  BBSize scales[1];
475 
478 };
479 
480 
495 BBArray* bb_array_alloc(const char *type, BBSize dims, const BBSize *lens);
496 
515 BBArray* bb_array_alloc1d(const char *type, BBSize len);
516 
536 void bb_array_init(BBArray *arr);
537 
538 BBByteArray* bb_bytearray_reverse(const BBByteArray *arr);
539 BBShortArray* bb_shortarray_reverse(const BBShortArray *arr);
540 BBIntArray* bb_intarray_reverse(const BBIntArray *arr);
541 BBLongArray* bb_longarray_reverse(const BBLongArray *arr);
542 BBFloatArray* bb_floatarray_reverse(const BBFloatArray *arr);
543 BBDoubleArray* bb_doublearray_reverse(const BBDoubleArray *arr);
544 
545 BBObjectArray* bb_objectarray_reverse(BBObjectArray *arr);
546 
547 #define bb_stringarray_reverse(arr) bb_objectarray_reverse((BBStringArray *)(arr))
548 #define bb_arrayarray_reverse(arr) bb_objectarray_reverse((BBArrayArray *)(arr))
549 
550 
551 // API //
552 
561 BBArray* bbArrayNew(const char *type, BBInt dims, ...);
562 
577 BBArray* bbArrayNew1D(const char *type, BBInt length);
578 
590 BBArray* bbArrayNewEx(const char *type, BBInt dims, BBInt *lens);
591 
600 BBArray* bbArraySlice(const char *type, BBArray *arr, BBInt begin, BBInt end);
601 
606 BBArray* bbArrayFromData(const char *type, BBInt length, const void *data);
607 
614 BBArray* bbArrayCastFromObject(const BBObject *obj, const char *typetag);
615 
623 void bbArraySort(BBArray *arr, BBInt ascending);
624 
632 BBArray* bbArrayDimensions(const BBArray *arr);
633 
643 BBArray* bbArrayConcat(const char *type, BBArray *arr, BBArray *rhs);
644 
645 
646 // EXTENDED //
647 
648 BBArray* bbArrayFromCArray(const void *buf, BBInt size, BBTypeKey typekey, const BBIntArray *dims);
649 
656 BBIntArray* bbArrayFromInts(const BBInt *buf, BBInt size);
657 
658 BBFloatArray* bbArrayFromFloats(const BBFloat *buf, BBInt size);
659 
683 BBInt bbArrayCompare(const BBArray *arr, const BBArray *rhs) __attribute__ ((pure));
684 
698 BBString* bbArrayToString(const BBArray *arr);
699 
703 void* bbArrayToCArray(const BBArray *arr);
704 
719 BBArray* bbArrayClone(const BBArray *arr);
720 
730 BBBool bbArrayEquals(const BBArray *arr, const BBArray *rhs) __attribute__ ((pure));
731 
732 
733 
739 #define BBARRAY_NOTFOUND (-1)
740 
747 BBInt bbArrayFind(const BBArray *arr, const BBArray *sub, BBInt start_pos) __attribute__ ((pure));
748 
756 BBInt bbArrayFindInt(const BBArray *arr, BBInt num, BBInt start_pos) __attribute__ ((pure));
757 
763 BBInt bbArrayFindLong(const BBLongArray *arr, BBLong num, BBInt start_pos) __attribute__ ((pure));
764 
770 BBInt bbArrayFindFloat(const BBFloatArray *arr, BBFloat num, BBInt start_pos) __attribute__ ((pure));
771 
777 BBInt bbArrayFindDouble(const BBDoubleArray *arr, BBDouble num, BBInt start_pos) __attribute__ ((pure));
778 
784 BBInt bbArrayFindObject(const BBObjectArray *arr, const BBObject *obj, BBInt start_pos) __attribute__ ((pure));
785 
791 BBInt bbArrayFindString(const BBStringArray *arr, const BBString *str, BBInt start_pos) __attribute__ ((pure));
792 
793 
794 
802 BBBool bbArrayStartsWith(const BBArray *arr, const BBArray *rhs) __attribute__ ((pure));
803 
811 BBBool bbArrayEndsWith(const BBArray *arr, const BBArray *rhs) __attribute__ ((pure));
812 
813 
814 
822 BBBool bbArrayContains(const BBArray *arr, const BBArray *sub) __attribute__ ((pure));
823 
831 BBBool bbArrayContainsInt(const BBArray *arr, BBInt num) __attribute__ ((pure));
832 
840 BBBool bbArrayContainsLong(const BBLongArray *arr, BBLong num) __attribute__ ((pure));
841 
849 BBBool bbArrayContainsFloat(const BBFloatArray *arr, BBFloat num) __attribute__ ((pure));
850 
858 BBBool bbArrayContainsDouble(const BBDoubleArray *arr, BBDouble num) __attribute__ ((pure));
859 
867 BBBool bbArrayContainsObject(const BBObjectArray *arr, const BBObject *obj) __attribute__ ((pure));
868 
876 BBBool bbArrayContainsString(const BBStringArray *arr, const BBString *str) __attribute__ ((pure));
877 
878 
879 
885 BBArray* bbArrayReverse(const BBArray *arr);
886 
901 BBArray* bbArrayRotate(const BBArray *arr, BBInt count);
902 
903 
904 BBIntArray* bb_intarray_filter(const BBIntArray *arr, BBBool (*filter)(BBInt));
905 BBFloatArray* bb_floatarray_filter(const BBFloatArray *arr, BBBool (*filer)(BBFloat));
906 BBLongArray* bb_longarray_filter(const BBLongArray *arr, BBBool (*filter)(BBLong));
907 BBDoubleArray* bb_doublearray_filter(const BBDoubleArray *arr, BBBool (*filter)(BBDouble));
908 BBObjectArray* bb_objectarray_filter(const BBObjectArray *arr, BBBool (*filter)(BBObject *));
909 
923 BBArray* bbArrayFilter(const BBArray *arr, BBBool (*filter)(void *));
924 
925 
926 BBIntArray* bb_intarray_map(const BBIntArray *arr, BBInt (*callback)(BBInt));
927 BBLongArray* bb_longarray_map(const BBLongArray *arr, BBLong (*callback)(BBLong));
928 BBFloatArray* bb_floatarray_map(const BBFloatArray *arr, BBFloat (*callback)(BBFloat));
929 BBDoubleArray* bb_doublearray_map(const BBDoubleArray *arr, BBDouble (*callback)(BBDouble));
930 BBObjectArray* bb_objectarray_map(BBObjectArray *arr, BBObject* (*callback)(BBObject *));
931 
939 BBArray* bbArrayMap(BBArray *arr, void* (*callback)(void *));
940 
941 
942 #ifndef BB_DISABLE_DEPRECATED
943 
944 # define BBARRAYSIZE(q,n) (20+(n)*sizeof(int)+(q))
945 # define BBARRAYDATA(p,n) ((void*)((char*)(p)+20+(n)*sizeof(int)))
946 
947 #endif // !BB_DISABLE_DEPRECATED
948 
949 BB_END_DECLS
950 
951 #endif // BLITZ_ARRAY_H