DVector* DVector_Copy( DVector *self ) { DVector *copy = DVector_New( self->stride ); copy->type = self->type; DVector_Resize( copy, self->size ); memcpy( copy->data.base, self->data.base, self->size * self->stride ); return copy; }
void DVector_Reset( DVector *self, daoint size ) { if( size <= self->capacity ){ self->size = size; return; } DVector_Resize( self, size ); }
static int SliceRange2( DVector *slice, daoint N, daoint first, daoint count ) { DVector_Resize( slice, 3 ); slice->data.daoints[0] = SLICE_RANGE; slice->data.daoints[1] = 0; slice->data.daoints[2] = 0; if( first <0 ) first += N; if( first <0 || first >= N ) return 0; slice->data.daoints[2] = first; if( first + count > N ) return 0; slice->data.daoints[1] = count; return 1; }
static int SliceRange( DVector *slice, daoint N, daoint first, daoint last ) { DVector_Resize( slice, 3 ); slice->data.daoints[0] = SLICE_RANGE; slice->data.daoints[1] = 0; slice->data.daoints[2] = 0; if( first <0 ) first += N; if( last <0 ) last += N; if( first <0 || first >= N || last <0 || last >= N ) return 0; slice->data.daoints[2] = first; if( first <= last ) slice->data.daoints[1] = last - first + 1; return 1; }
static void DaoxDataFrame_MakeFullSlice( DaoxDataFrame *self, DArray *slices ) { DVector *tmp = DVector_New( sizeof(daoint) ); daoint i, D = 3; /* slices: DArray<DVector<int> > */ DArray_Clear( slices ); DVector_Resize( tmp, 3 ); tmp->data.daoints[0] = SLICE_RANGE; tmp->data.daoints[2] = 0; for(i=0; i<D; ++i){ tmp->data.daoints[1] = self->dims[i]; DArray_Append( slices, tmp ); } DVector_Delete( tmp ); }
void DVector_Assign( DVector *left, DVector *right ) { assert( left->stride == right->stride ); DVector_Resize( left, right->size ); memcpy( left->data.base, right->data.base, right->size * right->stride ); }
static void MakeSlice( DaoProcess *proc, DaoValue *pid, daoint N, DVector *slice ) { daoint j, id, from, to, rc = 1; if( pid == NULL || pid->type == 0 ){ SliceRange2( slice, N, 0, N ); return; } switch( pid->type ){ case DAO_INTEGER : case DAO_FLOAT : case DAO_DOUBLE : { id = DaoValue_GetInteger( pid ); rc = SliceRange2( slice, N, id, 1 ); break; } case DAO_STRING : { break; } case DAO_TUPLE : { DaoValue **data = pid->xTuple.items; DVector_Clear( slice ); if( data[0]->type == DAO_INTEGER && data[1]->type == DAO_INTEGER ){ from = data[0]->xInteger.value; to = data[1]->xInteger.value; rc = SliceRange( slice, N, from, to ); }else if( data[0]->type == DAO_NONE && data[1]->type == DAO_NONE ){ rc = SliceRange2( slice, N, 0, N ); }else if( data[0]->type <= DAO_DOUBLE && data[1]->type == DAO_NONE ){ from = DaoValue_GetInteger( data[0] ); rc = SliceRange( slice, N, from, -1 ); }else if( data[0]->type == DAO_NONE && data[1]->type <= DAO_DOUBLE ){ to = DaoValue_GetInteger( data[1] ); rc = SliceRange( slice, N, 0, to ); }else if( data[0]->type == DAO_STRING && data[1]->type == DAO_STRING ){ }else if( data[0]->type == DAO_STRING && data[1]->type == DAO_NONE ){ }else if( data[0]->type == DAO_NONE && data[1]->type == DAO_STRING ){ }else{ DaoProcess_RaiseException( proc, DAO_ERROR_INDEX, "need number" ); } break; } case DAO_LIST : { DaoList *list = & pid->xList; DaoValue **v = list->items.items.pValue; DVector_Resize( slice, list->items.size + 2 ); slice->data.daoints[0] = SLICE_ENUM; slice->data.daoints[1] = list->items.size; for( j=0; j<list->items.size; j++){ if( v[j]->type < DAO_INTEGER || v[j]->type > DAO_DOUBLE ) DaoProcess_RaiseException( proc, DAO_ERROR_INDEX, "need number" ); id = DaoValue_GetInteger( v[j] ); if( id <0 ) id += N; if( id <0 || id >= N ){ rc = id = 0; break; } slice->data.daoints[j+2] = id; } break; } case DAO_ARRAY : { DaoArray *na = & pid->xArray; daoint *p; if( na->etype == DAO_COMPLEX ){ DaoProcess_RaiseException( proc, DAO_ERROR_INDEX, "complex array can not be used as index" ); break; } DVector_Resize( slice, na->size + 2 ); slice->data.daoints[0] = SLICE_ENUM; slice->data.daoints[1] = na->size; p = slice->data.daoints + 2; for( j=0; j<na->size; j++){ id = DaoArray_GetInteger( na, j ); if( id <0 ) id += N; if( id <0 || id >= N ){ rc = id = 0; break; } p[j] = id; } break; } default: break; } if( slice->size < 2 ) SliceRange2( slice, N, 0, N ); if( rc == 0 ) DaoProcess_RaiseException( proc, DAO_ERROR_INDEX_OUTOFRANGE, "" ); }
static void DaoxDataFrame_SliceFrom( DaoxDataFrame *self, DaoxDataFrame *orig, DArray *slices ) { DVector *rows = DVector_New( sizeof(daoint) ); DVector *cols = DVector_New( sizeof(daoint) ); DVector *deps = DVector_New( sizeof(daoint) ); daoint N = slices->items.pVector[0]->data.daoints[1]; daoint M = slices->items.pVector[1]->data.daoints[1]; daoint K = slices->items.pVector[2]->data.daoints[1]; daoint d, i, j, k; daoint *maps[3]; DaoxDataFrame_Reset( self ); DVector_Resize( rows, orig->dims[0] ); DVector_Resize( cols, orig->dims[1] ); DVector_Resize( deps, orig->dims[2] ); for(i=0; i<rows->size; ++i) rows->data.daoints[i] = -1; for(i=0; i<cols->size; ++i) cols->data.daoints[i] = -1; for(i=0; i<deps->size; ++i) deps->data.daoints[i] = -1; maps[0] = rows->data.daoints; maps[1] = cols->data.daoints; maps[2] = deps->data.daoints; for(j=0; j<M; ++j){ daoint jj = DaoSlice_GetIndex( slices->items.pVector[1], j ); DaoxDataColumn *source = (DaoxDataColumn*) orig->columns->items.pVoid[jj]; DaoxDataColumn *target = DaoxDataFrame_MakeColumn( self, source->type ); int datatype = DaoType_GetDataType( target->type ); cols->data.daoints[jj] = j; DArray_Append( self->columns, target ); DaoxDataColumn_Reset( target, N*K ); for(i=0; i<N; ++i){ daoint ii = DaoSlice_GetIndex( slices->items.pVector[0], i ); rows->data.daoints[ii] = i; for(k=0; k<K; ++k){ daoint kk = DaoSlice_GetIndex( slices->items.pVector[2], k ); daoint id2 = kk * orig->dims[0] + ii; daoint id3 = k * N + i; void *src = DVector_Get( source->cells, id2 ); void *des = DVector_Get( target->cells, id3 ); deps->data.daoints[kk] = k; if( datatype == 0 ){ /* DaoValue */ DaoValue *value = source->cells->data.values[id2]; GC_IncRC( value ); } memcpy( des, src, source->cells->stride ); } } } self->dims[0] = N; self->dims[1] = M; self->dims[2] = K; for(d=0; d<3; ++d){ DArray *labels = orig->labels[d]; for(i=0; i<labels->size; ++i){ DMap *labmap = labels->items.pMap[i]; DNode *it; DaoxDataFrame_AddLabelGroup( self, d ); for(it=DMap_First(labmap); it; it=DMap_Next(labmap,it)){ daoint id = maps[d][it->value.pInt]; if( id < 0 ) continue; DaoxDataFrame_AddLabel( self, d, it->key.pString->mbs, id ); } } } DVector_Delete( rows ); DVector_Delete( cols ); DVector_Delete( deps ); }