📄 array.mx
字号:
BUNfastins(bn,&o,&val); } } if (!(bn->batDirty&2)) bn = BATsetaccess(bn, BAT_READ); \ *ret= bn->batCacheid; BBPkeepref(*ret); BBPreleaseref(b->batCacheid); BBPreleaseref(r->batCacheid); return MAL_SUCCEED;}@c@:arraymultiply(sht,lng)@@:arraymultiply(sht,int)@@:arraymultiply(int,int)@@:arraymultiply(int,lng)@@:arraymultiply(lng,lng)@strARRAYproduct(int *ret, int *bid, int *rid){ BAT *bn, *b, *r; BUN p, q, s, t; int x1, x2; if ((b = BATdescriptor(*bid)) == NULL) { throw(MAL, "array.product", "Cannot access descriptor"); } if ((r = BATdescriptor(*rid)) == NULL) { BBPreleaseref(b->batCacheid); throw(MAL, "array.product", "Cannot access descriptor"); } if (BATcount(b) > BATcount(r) || (BATcount(b) % BATcount(r)) != BATcount(b)) { BBPreleaseref(b->batCacheid); BBPreleaseref(r->batCacheid); throw(MAL, "array.product", "Illegal argument bounds"); } bn = BATnew(b->ttype, r->ttype, BATcount(r)); BATloopFast(r, s, t, x2) { BATloopFast(b, p, q, x1) { BUNfastins(bn, BUNtail(b, p), BUNtail(r, s)); s += x2; } s -= x2; } /* not sorted at best we have some fixed offset partial sorting */ bn->hsorted = 0; bn->tsorted = 0; if (!(bn->batDirty&2)) bn = BATsetaccess(bn, BAT_READ); \ *ret = bn->batCacheid; BBPkeepref(*ret); BBPreleaseref(b->batCacheid); BBPreleaseref(r->batCacheid); return MAL_SUCCEED;}strARRAYproject(int *ret, int *bid, int *cst){ BAT *bn, *b; int *ptr, i; if ((b = BATdescriptor(*bid)) == NULL) { throw(MAL, "array.project", "Cannot access descriptor"); } new_int_bat(bn, BATcount(b)); if (bn == 0) throw(MAL, "array.project", "failed to create array\n"); i = BATcount(b); add_ints(bn, i); ptr = get_int_ptr(bn); while (i-- > 0) (*(ptr ++)) = *cst; if (!(bn->batDirty&2)) bn = BATsetaccess(bn, BAT_READ); \ *ret = bn->batCacheid; BBPkeepref(*ret); return MAL_SUCCEED;}strARRAYprint(MalBlkPtr mb, MalStkPtr stk, InstrPtr pci){ (void) mb; (void) stk; (void) pci; throw(MAL, "array.print", "not supported yet\n");}/*---------------------------------------------------------------------------*//* memory-hungry function; it internally builds the index to be able to print the array in-order.*/intprint_array(BAT *val, ...){ va_list ap; BAT *tmp, *batptr[MAX_ARRAY_DIM]; int *minvector; int *idxvector; int *maxvector; int *lenvector; oid *arrayindex; int i, dims; size_t j, c; stream *st = GDKout; int type_id = BATttype(val); va_start(ap, val); dims = 0; while ((tmp = va_arg(ap, BAT *)) != NULL) { if (dims == MAX_ARRAY_DIM) { GDKerror("print_array: Error, too high dimensionality.\n"); goto exit_err_none; } batptr[dims++] = tmp; } if (dims == 0) { GDKerror("print_array: Error, no index bats provided.\n"); goto exit_err_none; } va_end(ap);#ifdef EXCESSIVE_DEBUGGING fprintf(stderr, "[PRINT_ARRAY] Dims = %d\n", dims);#endif minvector = GDKmalloc(sizeof(ptr) * dims); idxvector = GDKmalloc(sizeof(int) * dims); maxvector = GDKmalloc(sizeof(ptr) * dims); lenvector = GDKmalloc(sizeof(ptr) * dims); c = 1; j = BATcount(val); for (i = 0; i < dims; i++) { if (BATttype(batptr[i]) != TYPE_int) { GDKerror("print_array: Index %d error, not int type\n", i); goto exit_err_part; } if (BATcount(batptr[i]) != j) { GDKerror("print_array: Index %d error, count doesn't match\n", i); goto exit_err_part; } BATmin(batptr[i], &(minvector[i])); BATmax(batptr[i], &(maxvector[i])); lenvector[i] = maxvector[i] - minvector[i] + 1; c *= lenvector[i];#ifdef EXCESSIVE_DEBUGGING fprintf(stderr, "[PRINT_ARRAY] Axis %d = %d..%d\n", i, minvector[i], maxvector[i]);#endif } if (c != j) { GDKerror("print_array: array size does not match array count\n"); goto exit_err_part; } arrayindex = GDKmalloc(sizeof(oid) * c); /* Iterate over the first index BAT. */ /* And fetch additional indices (for a given oid) */ /* building the full M-D structure (of oid's). */ { register BUN p, q; p = BUNfirst(batptr[0]); q = BUNlast(batptr[0]); while (p < q) { register oid *o = (oid *) BUNhead(batptr[0], p); register int offset; offset = *((int *) BUNtail(batptr[0], p)) - minvector[0]; for (i = 1; i < dims; i++) { register BUN p = BUNfnd(batptr[i], o); if (p == NULL) { GDKerror("print_array: Index %d missing for oid %d.\n", i, *o); goto exit_err_full; } offset = (offset * lenvector[i]) + ((*((int *) p)) - minvector[i]); } arrayindex[offset] = *o; p = BUNnext(batptr[0], p); } } /* Iterate over all valid indices */ /* (note: normalized to [0..l-1] ranges !) */ /* And print the values within the 'correct' */ /* bracket structure... */#ifdef EXCESSIVE_DEBUGGING fprintf(stderr, "[PRINT_ARRAY] ");#endif for (i = 0; i < dims; i++) idxvector[i] = 0; while (idxvector[dims - 1] < lenvector[dims - 1]) { int offset; for (i = 0; i < dims; i++) { if (idxvector[i] != 0) {#ifdef EXCESSIVE_DEBUGGING fprintf(stderr, ",");#endif stream_printf(st, ","); break; } } i = 0; while (idxvector[i] == 0 && i < dims) {#ifdef EXCESSIVE_DEBUGGING fprintf(stderr, "[");#endif stream_printf(st, "["); i++; } for (offset = idxvector[0], i = 1; i < dims; i++) { offset = (offset * lenvector[i]) + idxvector[i]; } { register BUN p = BUNfnd(val, &arrayindex[offset]); if (p == NULL) { GDKerror("print_array: Value missing for oid %d.\n", arrayindex[offset]); goto exit_err_full; }#ifdef EXCESSIVE_DEBUGGING fprintf(stderr, "%d\@0", arrayindex[offset]);#endif ATOMprint(type_id, BUNtail(val, p), st); if (stream_errnr(st)) goto exit_err_full; } i = 0; while ((++idxvector[i]) == lenvector[i] && i < dims) {#ifdef EXCESSIVE_DEBUGGING fprintf(stderr, "]");#endif stream_printf(st, "]"); if (i < dims - 1) { idxvector[i] = 0; i++; } } }#ifdef EXCESSIVE_DEBUGGING fprintf(stderr, "\n");#endif stream_printf(st, "\n"); /* Exit strategies... */ GDKfree(arrayindex); GDKfree(maxvector); GDKfree(idxvector); GDKfree(minvector); GDKfree(lenvector); return GDK_SUCCEED;exit_err_full: GDKfree(arrayindex);exit_err_part: GDKfree(maxvector); GDKfree(idxvector); GDKfree(minvector); GDKfree(lenvector);exit_err_none: return GDK_FAIL;}/*============================================================================*/@milPROC cons(int n,any::1 v) : bat[void,any::1] { return project(grid(1,n,1,0),v);}ADDHELP("cons", "A.R. van Ballegooij", "Sep 20 2003","generates a bat with a constant column", "array");PROC concat(bat[oid,any::1] a,bat[oid,any::1] b,int o) : bat[oid,any::1] { return access(insert(access(copy(a),BAT_WRITE), reverse([oid]([+]([int](reverse(b)),o)))) , BAT_READ);}ADDHELP("concat", "A.R. van Ballegooij", "Sep 20 2003","Adds 'offset' two second arrays indexes and merges tables", "array");if (mod() = "main"){ print("Array module loaded...");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -