DaoValue* DaoValue_CopyContainer( DaoValue *self, DaoType *tp ) { switch( self->type ){ case DAO_LIST : return (DaoValue*) DaoList_Copy( (DaoList*) self, tp ); case DAO_MAP : return (DaoValue*) DaoMap_Copy( (DaoMap*) self, tp ); case DAO_TUPLE : return (DaoValue*) DaoTuple_Copy( (DaoTuple*) self, tp ); #ifdef DAO_WITH_NUMARRAY case DAO_ARRAY : return (DaoValue*) DaoArray_Copy( (DaoArray*) self, tp ); #endif default : break; } return self; }
static DaoValue* DaoValue_DeepCopy( DaoValue *self ) { DNode *it; daoint i; if( self == NULL ) return NULL; if( self->type <= DAO_ENUM ) return self; /* simple types will be copied at use; */ if( self->type == DAO_ARRAY ) return (DaoValue*) DaoArray_Copy( (DaoArray*) self ); if( self->type == DAO_LIST ){ DaoList *list = (DaoList*) self; DaoList *copy = DaoList_New(); GC_ShiftRC( list->unitype, copy->unitype ); copy->unitype = list->unitype; for(i=0; i<list->items.size; ++i){ DaoValue *value = DaoValue_DeepCopy( list->items.items.pValue[i] ); DaoList_Append( copy, value ); } return (DaoValue*) copy; }else if( self->type == DAO_MAP ){ DaoMap *map = (DaoMap*) self; DaoMap *copy = DaoMap_New( map->items->hashing ); GC_ShiftRC( map->unitype, copy->unitype ); copy->unitype = map->unitype; for(it=DMap_First(map->items); it; it=DMap_Next(map->items,it)){ DaoValue *key = DaoValue_DeepCopy( it->key.pValue ); DaoValue *value = DaoValue_DeepCopy( it->value.pValue ); DaoMap_Insert( copy, key, value ); } return (DaoValue*) copy; }else if( self->type == DAO_TUPLE ){ DaoTuple *tuple = (DaoTuple*) self; DaoTuple *copy = DaoTuple_New( tuple->size ); GC_ShiftRC( tuple->unitype, copy->unitype ); copy->unitype = tuple->unitype; for(i=0; i<tuple->size; ++i){ DaoValue *value = DaoValue_DeepCopy( tuple->items[i] ); DaoTuple_SetItem( copy, value, i ); } return (DaoValue*) copy; } return NULL; }
static void STD_Array( DaoProcess *proc, DaoValue *p[], int N ) { DaoInteger idint = {DAO_INTEGER,0,0,0,0,0}; DaoValue *res, *index = (DaoValue*)(void*)&idint; DaoVmCode *sect = DaoGetSectionCode( proc->activeCode ); DaoArray *array = DaoProcess_PutArray( proc ); DaoArray *first = NULL; DaoArray *sub = NULL; daoint i, j, k, entry, size = 1; /* if multi-dimensional array is disabled, DaoProcess_PutArray() will raise exception. */ #ifdef DAO_WITH_NUMARRAY for(i=0; i<N; i++){ daoint d = p[i]->xInteger.value; if( d < 0 ){ DaoProcess_RaiseException( proc, DAO_ERROR_PARAM, NULL ); break; } size *= d; } if( size == 0 ) return; if( sect == NULL ) return; // TODO exception if( DaoProcess_PushSectionFrame( proc ) == NULL ) return; entry = proc->topFrame->entry; DaoProcess_AcquireCV( proc ); for(i=0; i<size; i++){ idint.value = i; if( sect->b >0 ) DaoProcess_SetValue( proc, sect->a, index ); proc->topFrame->entry = entry; DaoProcess_Execute( proc ); if( proc->status == DAO_PROCESS_ABORTED ) break; res = proc->stackValues[0]; if( i == 0 ){ int D = N; DaoArray_SetDimCount( array, N + (res->type == DAO_ARRAY ? res->xArray.ndim : 0) ); for(j=0; j<N; j++) array->dims[j] = p[j]->xInteger.value; if( res->type == DAO_ARRAY ){ first = DaoArray_Copy( (DaoArray*) res ); if( first->ndim == 2 && (first->dims[0] == 1 || first->dims[1] == 1) ){ D += 1; array->dims[N] = first->dims[ first->dims[0] == 1 ]; }else{ D += first->ndim; memmove( array->dims + N, first->dims, first->ndim*sizeof(daoint) ); } } DaoArray_ResizeArray( array, array->dims, D ); } if( res->type == DAO_ARRAY ){ sub = (DaoArray*) res; if( first == NULL || DaoArray_AlignShape( sub, NULL, first->dims, first->ndim ) ==0 ){ DaoProcess_RaiseException( proc, DAO_ERROR, "inconsistent elements or subarrays" ); break; } k = i * sub->size; for(j=0; j<sub->size; j++){ switch( array->etype ){ case DAO_INTEGER : array->data.i[k+j] = DaoArray_GetInteger( sub, j ); break; case DAO_FLOAT : array->data.f[k+j] = DaoArray_GetFloat( sub, j ); break; case DAO_DOUBLE : array->data.d[k+j] = DaoArray_GetDouble( sub, j ); break; case DAO_COMPLEX : array->data.c[k+j] = DaoArray_GetComplex( sub, j ); break; } } }else{ switch( array->etype ){ case DAO_INTEGER : array->data.i[i] = DaoValue_GetInteger( res ); break; case DAO_FLOAT : array->data.f[i] = DaoValue_GetFloat( res ); break; case DAO_DOUBLE : array->data.d[i] = DaoValue_GetDouble( res ); break; case DAO_COMPLEX : array->data.c[i] = DaoValue_GetComplex( res ); break; } } } DaoProcess_ReleaseCV( proc ); DaoProcess_PopFrame( proc ); if( first ) DaoArray_Delete( first ); #endif }