static void GRAPH_GetEdges( DaoProcess *proc, DaoValue *p[], int N ) { DaoxGraph *self = (DaoxGraph*) p[0]; DaoList *res = DaoProcess_PutList( proc ); daoint i; for(i=0; i<self->edges->size; i++) DaoList_PushBack( res, self->edges->items.pValue[i] ); }
static void STD_List( DaoProcess *proc, DaoValue *p[], int N ) { DaoInteger idint = {DAO_INTEGER,0,0,0,0,0}; DaoValue *res = p[N==2], *index = (DaoValue*)(void*)&idint; DaoVmCode *sect = DaoGetSectionCode( proc->activeCode ); DaoList *list = DaoProcess_PutList( proc ); daoint i, entry, size = p[0]->xInteger.value; daoint fold = N == 2; if( fold ) DaoList_Append( list, res ); if( sect == NULL || size < 0 ) return; // TODO exception if( DaoProcess_PushSectionFrame( proc ) == NULL ) return; entry = proc->topFrame->entry; DaoProcess_AcquireCV( proc ); for(i=fold; i<size; i++){ idint.value = i; if( sect->b >0 ) DaoProcess_SetValue( proc, sect->a, index ); if( sect->b >1 && N ==2 ) DaoProcess_SetValue( proc, sect->a+1, res ); proc->topFrame->entry = entry; DaoProcess_Execute( proc ); if( proc->status == DAO_PROCESS_ABORTED ) break; res = proc->stackValues[0]; DaoList_Append( list, res ); } DaoProcess_ReleaseCV( proc ); DaoProcess_PopFrame( proc ); }
static void DaoIO_ReadLines( DaoProcess *proc, DaoValue *p[], int N ) { DaoList *list = DaoProcess_PutList( proc ); int count = p[1]->xInteger.value; int chop = p[2]->xBoolean.value; if( DaoIO_CheckMode( (DaoStream*) p[0], proc, DAO_STREAM_READABLE ) == 0 ) return; DaoStream_ReadLines( (DaoStream*) p[0], list, proc, count, chop ); }
static void NODE_GetEdges( DaoProcess *proc, DaoValue *p[], int N ) { DaoxNode *self = (DaoxNode*) p[0]; DaoList *res = DaoProcess_PutList( proc ); daoint i, n; if( self->graph->directed && p[1]->xEnum.value == 0 ){ for(i=0,n=self->ins->size; i<n; i++) DaoList_PushBack( res, self->ins->items.pValue[i] ); }else{ for(i=0,n=self->outs->size; i<n; i++) DaoList_PushBack( res, self->outs->items.pValue[i] ); } }
static void DaoState_Waitlist( DaoProcess *proc, DaoValue *p[], int N ) { DaoState *self = (DaoState*)DaoValue_CastCstruct( p[0], NULL ); DaoList *list = DaoProcess_PutList( proc ); DNode *node; DaoMutex_Lock( self->lock ); node = DaoMap_First( self->demands ); while( node ){ DaoList_PushBack( list, DNode_Key( node ) ); node = DaoMap_Next( self->demands, node ); } DaoMutex_Unlock( self->lock ); }
static void NODE_Search( DaoProcess *proc, DaoValue *p[], int N ) { DList *nodes; DaoList *list = DaoProcess_PutList( proc ); DaoxNode *self = (DaoxNode*) p[0]; DaoVmCode *sect = DaoProcess_InitCodeSection( proc, 1 ); daoint method = p[1]->xEnum.value; daoint which = p[2]->xEnum.value; daoint i, j, entry; if( sect == NULL ) return; for(i=0; i<self->graph->nodes->size; i++){ DaoxNode *node = (DaoxNode*) self->graph->nodes->items.pVoid[i]; node->state = 0; } nodes = DList_New(0); DList_PushBack( nodes, self ); entry = proc->topFrame->entry; while( nodes->size ){ DaoxNode *node = NULL; if( method ){ node = (DaoxNode*) DList_Front( nodes ); DList_PopFront( nodes ); }else{ node = (DaoxNode*) DList_Back( nodes ); DList_PopBack( nodes ); } if( node->state ) continue; node->state = 1; if( sect->b >0 ) DaoProcess_SetValue( proc, sect->a, (DaoValue*) node ); proc->topFrame->entry = entry; DaoProcess_Execute( proc ); if( proc->status == DAO_PROCESS_ABORTED ) break; if( proc->stackValues[0]->xInteger.value ){ DaoList_PushBack( list, (DaoValue*) node ); if( which == 0 ) break; } for(j=0; j<node->outs->size; j++){ DaoxEdge *edge = (DaoxEdge*) node->outs->items.pVoid[j]; DaoxNode *node2 = node == edge->first ? edge->second : edge->first; DList_PushBack( nodes, node2 ); } } DaoProcess_PopFrame( proc ); DList_Delete( nodes ); }
static void GRAPH_ConnectedComponents( DaoProcess *proc, DaoValue *p[], int N ) { DaoxGraph *self = (DaoxGraph*) p[0]; DaoList *graphs = DaoProcess_PutList( proc ); DList *cclist; daoint i, n; if( self->nodes->size == 0 ){ DaoList_PushBack( graphs, (DaoValue*)self ); return; } cclist = DList_New(0); DaoxGraph_ConnectedComponents( self, cclist ); for(i=0,n=cclist->size; i<n; i++) DaoList_PushBack( graphs, cclist->items.pValue[i] ); DList_Delete( cclist ); }
static void DaoIO_ReadLines( DaoProcess *proc, DaoValue *p[], int N ) { DString *fname; DaoValue *res; DaoString *line; DaoVmCode *sect = DaoGetSectionCode( proc->activeCode ); DaoList *list = DaoProcess_PutList( proc ); int chop = p[1]->xInteger.value; char buf[IO_BUF_SIZE]; FILE *fin; if( proc->vmSpace->options & DAO_OPTION_SAFE ){ DaoProcess_RaiseException( proc, DAO_ERROR, "not permitted" ); return; } fin = DaoIO_OpenFile( proc, p[0]->xString.data, "r", 0 ); if( fin == NULL ) return; if( sect == NULL || DaoProcess_PushSectionFrame( proc ) == NULL ){ line = DaoString_New(1); while( DaoFile_ReadLine( fin, line->data ) ){ if( chop ) DString_Chop( line->data ); DaoList_Append( list, (DaoValue*) line ); } DaoString_Delete( line ); }else{ ushort_t entry = proc->topFrame->entry; DaoString tmp = {DAO_STRING,0,0,0,1,NULL}; tmp.data = p[0]->xString.data; line = (DaoString*) DaoProcess_SetValue( proc, sect->a, (DaoValue*)(void*) &tmp ); DaoProcess_AcquireCV( proc ); while( DaoFile_ReadLine( fin, line->data ) ){ if( chop ) DString_Chop( line->data ); proc->topFrame->entry = entry; DaoProcess_Execute( proc ); if( proc->status == DAO_PROCESS_ABORTED ) break; res = proc->stackValues[0]; if( res && res->type != DAO_NONE ) DaoList_Append( list, res ); } DaoProcess_ReleaseCV( proc ); DaoProcess_PopFrame( proc ); } fclose( fin ); }
static void DaoSTD_Try( DaoProcess *proc, DaoValue *p[], int n ) { DaoVmCode *sect = DaoProcess_InitCodeSection( proc, 0 ); int i, ecount = proc->exceptions->size; if( sect == NULL ) return; DaoProcess_Execute( proc ); DaoProcess_PopFrame( proc ); if( proc->exceptions->size > (ecount+1) ){ DaoList *list = DaoProcess_PutList( proc ); for(i=ecount; i<proc->exceptions->size; ++i){ DaoList_Append( list, proc->exceptions->items.pValue[i] ); } DList_Erase( proc->exceptions, ecount, -1 ); }else if( proc->exceptions->size > ecount ){ DaoProcess_PutValue( proc, proc->exceptions->items.pValue[proc->exceptions->size-1] ); DList_PopBack( proc->exceptions ); }else{ DaoProcess_PutValue( proc, proc->stackValues[0] ); } }
static void GRAPH_FindEdges( DaoProcess *proc, DaoValue *p[], int N ) { DaoxGraph *self = (DaoxGraph*) p[0]; DaoList *list = DaoProcess_PutList( proc ); DaoVmCode *sect = DaoProcess_InitCodeSection( proc, 1 ); daoint which = p[1]->xEnum.value; daoint i, j, entry; if( sect == NULL ) return; entry = proc->topFrame->entry; for(i=0; i<self->edges->size; i++){ DaoxEdge *edge = (DaoxEdge*) self->edges->items.pVoid[i]; if( sect->b >0 ) DaoProcess_SetValue( proc, sect->a, (DaoValue*) edge ); proc->topFrame->entry = entry; DaoProcess_Execute( proc ); if( proc->status == DAO_PROCESS_ABORTED ) break; if( proc->stackValues[0]->xInteger.value ){ DaoList_PushBack( list, (DaoValue*) edge ); if( which == 0 ) break; } } DaoProcess_PopFrame( proc ); }
static void DaoIO_ReadLines2( DaoProcess *proc, DaoValue *p[], int N ) { DaoValue *res; DaoString *line; DaoVmCode *sect = DaoGetSectionCode( proc->activeCode ); DaoList *list = DaoProcess_PutList( proc ); DaoStream *self = & p[0]->xStream; daoint i = 0, count = p[1]->xInteger.value; int chop = p[2]->xInteger.value; if( sect == NULL || DaoProcess_PushSectionFrame( proc ) == NULL ){ line = DaoString_New(1); while( (i++) < count && DaoStream_ReadLine( self, line->data ) ){ if( chop ) DString_Chop( line->data ); DaoList_Append( list, (DaoValue*) line ); } DaoString_Delete( line ); }else{ ushort_t entry = proc->topFrame->entry; DaoString tmp = {DAO_STRING,0,0,0,1,NULL}; DString tmp2 = DString_WrapMBS( "" ); tmp.data = & tmp2; line = (DaoString*) DaoProcess_SetValue( proc, sect->a, (DaoValue*)(void*) &tmp ); DaoProcess_AcquireCV( proc ); while( (i++) < count && DaoStream_ReadLine( self, line->data ) ){ if( chop ) DString_Chop( line->data ); proc->topFrame->entry = entry; DaoProcess_Execute( proc ); if( proc->status == DAO_PROCESS_ABORTED ) break; res = proc->stackValues[0]; if( res && res->type != DAO_NONE ) DaoList_Append( list, res ); } DaoProcess_ReleaseCV( proc ); DaoProcess_PopFrame( proc ); } }
static void STD_Argv( DaoProcess *proc, DaoValue *p[], int N ) { int i; DaoList *list = DaoProcess_PutList( proc ); for(i=0; i<proc->topFrame->parCount; i++) DaoList_Append( list, proc->activeValues[i] ); }
static void DaoMT_Functional( DaoProcess *proc, DaoValue *P[], int N, int F ) { DMutex mutex; DCondVar condv; DaoTaskData *tasks; DaoValue *param = P[0]; DaoValue *result = NULL; DaoList *list = NULL; DaoArray *array = NULL; DaoVmCode *sect = DaoGetSectionCode( proc->activeCode ); int i, entry, threads = P[1]->xInteger.value; daoint index = -1, status = 0, joined = 0; DNode *node = NULL; switch( F ){ case DVM_FUNCT_MAP : if( param->type == DAO_ARRAY ){ array = DaoProcess_PutArray( proc ); result = (DaoValue*) array; }else{ list = DaoProcess_PutList( proc ); result = (DaoValue*) list; } break; case DVM_FUNCT_APPLY : DaoProcess_PutValue( proc, param ); break; case DVM_FUNCT_FIND : DaoProcess_PutValue( proc, dao_none_value ); break; } if( threads <= 0 ) threads = 2; if( sect == NULL || DaoMT_PushSectionFrame( proc ) == 0 ) return; if( list ){ DArray_Clear( & list->items ); if( param->type == DAO_LIST ) DArray_Resize( & list->items, param->xList.items.size, NULL ); if( param->type == DAO_MAP ) DArray_Resize( & list->items, param->xMap.items->size, NULL ); #ifdef DAO_WITH_NUMARRAY }else if( array && F == DVM_FUNCT_MAP ){ DaoArray_GetSliceShape( (DaoArray*) param, & array->dims, & array->ndim ); DaoArray_ResizeArray( array, array->dims, array->ndim ); #endif } DMutex_Init( & mutex ); DCondVar_Init( & condv ); entry = proc->topFrame->entry; tasks = (DaoTaskData*) dao_calloc( threads, sizeof(DaoTaskData) ); DaoProcess_PopFrame( proc ); for(i=0; i<threads; i++){ DaoTaskData *task = tasks + i; task->param = param; task->result = result; task->proto = proc; task->sect = sect; task->funct = F; task->entry = entry; task->first = i; task->step = threads; task->index = & index; task->node = & node; task->joined = & joined; task->condv = & condv; task->mutex = & mutex; task->clone = DaoVmSpace_AcquireProcess( proc->vmSpace ); task->clone->mutex = & mutex; if( i ) DaoCallServer_AddTask( DaoMT_RunFunctional, task, 1 ); } DaoMT_RunFunctional( tasks ); DMutex_Lock( & mutex ); while( joined < threads ) DCondVar_TimedWait( & condv, & mutex, 0.01 ); DMutex_Unlock( & mutex ); for(i=0; i<threads; i++){ DaoTaskData *task = tasks + i; DaoVmSpace_ReleaseProcess( proc->vmSpace, task->clone ); status |= task->status; } if( F == DVM_FUNCT_FIND ){ DaoTuple *tuple = DaoProcess_PutTuple( proc, 0 ); if( param->type == DAO_LIST && index != -1 ){ DaoValue **items = param->xList.items.items.pValue; GC_ShiftRC( items[index], tuple->items[1] ); tuple->items[1] = items[index]; tuple->items[0]->xInteger.value = index; }else if( param->type == DAO_MAP && node ){ GC_ShiftRC( node->key.pValue, tuple->items[0] ); GC_ShiftRC( node->value.pValue, tuple->items[1] ); tuple->items[0] = node->key.pValue; tuple->items[1] = node->value.pValue; } } if( status ) DaoProcess_RaiseException( proc, DAO_ERROR, "code section execution failed!" ); DMutex_Destroy( & mutex ); DCondVar_Destroy( & condv ); dao_free( tasks ); }