void List_delete(List list) { assert(list); List_head(list); if(!List_isEmpty(list)) LOGWARNING("Deleting a non-empty list"); while(!List_isEmpty(list)) List_remove(list); free(list); }
int main(int argc, char **argv) { List_t l = List_new(); fprintf(stdout, "test List...\n"); int i; for (i = 0; i < 100; i++) { List_addFirst(l, i); } fprintf(stdout, "List_getFirst\n"); int r = (int) List_getFirst(l); assert(r == 99); fprintf(stdout, "List_getIndexOf\n"); for (i = 0; i < 100; i++) { r = (int) List_getIndexOf(l, i); assert(r == (99 - i)); } fprintf(stdout, "List_addLast\n"); List_addLast(l, 200); r = (int) List_getIndexOf(l, 100); assert(r == 200); fprintf(stdout, "List_size\n"); r = List_size(l); assert(r == 101); fprintf(stdout, "List_isEmpty\n"); r = List_isEmpty(l); assert(r == 0); List_t l2 = List_new(); r = List_isEmpty(l2); assert(r == 1); fprintf(stdout, "List_remove\n"); for (i = 0; i < 100; i++) { r = (int) List_removeFirst(l); assert(r == (99 - i)); } r = (int) List_removeFirst(l); assert(r == 200); r = List_isEmpty(l); assert(r == 1); return 0; }
Boolean List_containsSubList(List * list, List * sublist, int (*isEqual)(void *, void*)) { int i; if(list == NULL || sublist == NULL || isEqual == NULL) return false; if(List_isEmpty(sublist)) return true; if(List_getLength(sublist) > List_getLength(list)) return false; if(List_getLength(list) == List_getLength(sublist)) return List_isEqual(list, sublist, isEqual); for(i = 0; i < List_getLength(list) - List_getLength(sublist) + 1; i++) { if(locationContainsSubList(list, sublist, isEqual, i)) return true; } return false; }
int List_positionOfSubListOffset(List * list, List * sublist, int (*isEqual)(void *, void*), int offset) { int i; if(list == NULL || sublist == NULL || isEqual == NULL) return false; if(List_isEmpty(sublist)) return -1; if(List_getLength(sublist) > List_getLength(list)) return -1; if(offset < 0 || offset >= List_getLength(sublist)) return -1; if(List_getLength(list) == List_getLength(sublist) && offset == 0) { if(List_isEqual(list, sublist, isEqual)) return 0; else return -1; } for(i = offset; i < List_getLength(list) - List_getLength(sublist) + 1; i++) { if(locationContainsSubList(list, sublist, isEqual, i)) return i; } return -1; }
static void printStrs (List_t strings) { if (List_isEmpty (strings)) return; print ("\t.data\n"); List_foreach (strings, (Poly_tyVoid)X86_Str_print); }
int List_isTail(List list) { assert(list); if(List_isEmpty(list)) return 1; return list->current == list->tail; }
/* Function to create a complete RCG including the TRUE join conditions for the nodes which are isolated and without any join condition */ void build_complete_RCG (RCG *graph) { syntree *temp_tree, *TRUE_syntree = NULL; List *null_join_list; ListElement *cursor, *cursor1; RCG_node *rcg_node, *ref_rcg_node; ASSERT (graph); // Initialise the list to hold the null join list nodes null_join_list = List_new(); ref_rcg_node = NULL; /* Find all the RCG nodes hanging around without any join predicates and find the reference RCG node */ rcg_node = (RCG_node *)List_getFirst(graph->nodes, &cursor); while (rcg_node != NULL) { temp_tree = (syntree *)List_getFirst(rcg_node->join_predicates_list, &cursor1); if (temp_tree == NULL) { /* Insert into the list all the RCG nodes with null join predicates */ List_insertElement(null_join_list, rcg_node); } else { /* Choose this node as the root reference node for the TRUE join condition */ ref_rcg_node = rcg_node; } rcg_node = (RCG_node *)List_getNext(&cursor); } if (ref_rcg_node == NULL) { List_deleteHead(null_join_list); ref_rcg_node = (RCG_node *)List_getFirst(graph->nodes, &cursor); } /* Initialise the TRUE join condition syntax tree if there are isolated RCG nodes*/ if (!List_isEmpty(null_join_list)) { TRUE_syntree = (syntree *)tman_malloc(sizeof(syntree)); memset(TRUE_syntree, '\0', sizeof(syntree)); TRUE_syntree->type = TRUE_ORDINARY_PRED_TYPE; TRUE_syntree->datum = chars_new("TRUE"); } /* Create the TRUE join connection for all those nodes without any join condition */ rcg_node = (RCG_node *)List_getFirst(null_join_list, &cursor); while (rcg_node != NULL) { // Insert the TRUE condition to the null join condition node */ List_insertElement(rcg_node->join_predicates_list, TRUE_syntree); rcg_node->reference_tuple_variable_name = ref_rcg_node->tuple_variable_name; // Insert the TRUE condition to the reference node also */ List_insertElement(ref_rcg_node->join_predicates_list, TRUE_syntree); rcg_node = (RCG_node *)List_getNext(&cursor); } List_deleteBackbone(null_join_list); }
void List_empty(List list) { assert(list); List_head(list); while(!List_isEmpty(list)) List_remove(list); }
// To merge first list into the second one. First list becomes empty after the merge operation. void List_merge (List *from, List *to) { ASSERT (from); ASSERT (to); if (!List_isEmpty (from)) { if (!List_isEmpty (to)) { to->tail->next = from->head; from->head->prev = to->tail; } else { to->head = from->head; to->tail = from->tail; } from->head = NULL; from->tail = NULL; } }
void freePath(List path) { List_head(path); while(!List_isEmpty(path)) { Position_delete(List_getCurrent(path)); List_remove(path); } List_delete(path); }
static void printMask (File_t file, List_t ms) { if (List_isEmpty (ms)) return; fprintf (file , "\t.data\n" "\t.align 8\n"); List_foldl (ms , file , (Poly_tyFold)X86_Mask_print); }
void printList(const List l) { logAllowed = false; ListPosition p = List_header(l); if (List_isEmpty(l)) printf("Empty list\n"); else { do { p = List_advance(p); struct Resource* res = (struct Resource*) List_retrieve(p); printf("(%d, %ld, %ld) ", res->size, res->time, res->space); } while (!List_isLast(p, l)); printf("\n"); } logAllowed = true; }
/* Check the constant set of the expression signature associated with the given RCG node. If it is empty, then delete the expression signature. The second argument, index, is used to specify which expression signature is to be checked and deleted (if appropriate) from the RCG node */ static void del_expr_sig_when_const_set_is_empty(RCG_node *rcg_node, int index) { if (rcg_node->expression_sig[index]->predicate_type == NON_INDEXABLE) { // Check for empty constant sets based on its organization switch (rcg_node->expression_sig[index]->constant_set_organization) { case CONST_SKIPLIST: if (skiplist_is_empty(rcg_node->expression_sig[index]->constant_sl)) { // Delete the expression signature. delete_empty_expr_sig(rcg_node, index); } break; case CONST_LIST: if (List_isEmpty(rcg_node->expression_sig[index]->constant_list)) { // Delete the expression signature. delete_empty_expr_sig(rcg_node, index); } break; default: logwrite ("Internal error: only lists and skiplists allowed in non-indexable expression signatures."); TMAN_HALT; } } else if (rcg_node->expression_sig[index]->predicate_type == INDEXABLE) { ASSERT (rcg_node->expression_sig[index]->pred_index); if (pred_index_is_empty (rcg_node->expression_sig[index]->pred_index)) { // Delete the expression signature. delete_empty_expr_sig(rcg_node, index); } } else { logwrite ("Internal Error: predicates can only be indexable or non-indexable."); TMAN_HALT; } }
File_t X86_Mask_print (File_t file, M m) { List_t p; Assert_ASSERT(m); fprintf (file, "%s", Id_toString (m->name)); fprintf (file, ":\n\t.int "); fprintf (file, "%s", Int_toString (m->size)); if (List_isEmpty (m->index)){ fprintf (file, "\n"); return file; } fprintf (file, ", "); p = List_getFirst (m->index); while (p){ fprintf (file, "%s", Int_toString ((int)p->data)); if (p->next) fprintf (file, ", "); p = p->next; } fprintf (file, "\n"); return file; }
int Set_isEmpty (T set) { Assert_ASSERT(set); return List_isEmpty (set->list); }
/* Mover dispatcher wakes up the movers whenever any data source gets updated. */ DWORD WINAPI mover_dispatcher(LPVOID ptr) { int task_count, i; int num_of_mover; int num_available_mover; int upd_datasrc_count = 0; const char *updated_datasrc_list = "TM_updated_Datasrc_list"; List *upd_datasrc_list; ListElement *cursor; update_queue_node *upd_datasrc; const char *database_taskque_name = "Database_TaskQueue"; taskqueue_type *taskqueue; while (!g_fTerminate) { // Get the handle to the global database token task queue that stores the unprocessed tokens // from database inserts/updates/deletes. tman_lock_memory(database_taskque_name); taskqueue = tman_named_get(database_taskque_name); // Get the number of outstanding unprocessed tasks task_count = taskqueue->counter; tman_unlock_memory(database_taskque_name); // Get the number of active movers EnterCriticalSection(&mover_num_mutex); num_of_mover = num_mover; LeaveCriticalSection(&mover_num_mutex); // Find if any data sources have been updated lately delivered_list_update(); // Retrieve the list of updated data sources from shared memory. tman_lock_memory(updated_datasrc_list); upd_datasrc_list = tman_named_get (updated_datasrc_list); // If there are any new updated data sources and there is available space in the global // task queue, wake up the movers. // A ceiling is imposed on the task queue to apply back pressure on the movers // to stop after the task queue is full. This will prevent the doers from lagging behind // and movers from moving too many tokens at the same time. if ((task_count < MAX_DATABASE_TASK_QUEUE_SIZE) && (!List_isEmpty(upd_datasrc_list))) { // Database task queue is NOT FULL and new database tokens to process // Calculate the number of updated data sources upd_datasrc_count = 0; upd_datasrc = (update_queue_node *)List_getFirst(upd_datasrc_list, &cursor); while (upd_datasrc != NULL) { upd_datasrc_count++; upd_datasrc = (update_queue_node *)List_getNext(&cursor); } // Calculate the number of available movers num_available_mover = MAXMOVER - num_of_mover; tman_unlock_memory(updated_datasrc_list); // If number of available movers > number of updated data sources, // wake up one mover per data source if (upd_datasrc_count < num_available_mover) { for (i = 0; i < upd_datasrc_count; i++) { ReleaseSemaphore(wakeup_mover_by_mover_dispatcher_sem, 1, NULL); } } // If number of available movers < number of updated data sources, // wake up all the available movers else { for (i = 0; i < num_available_mover; i++) { ReleaseSemaphore(wakeup_mover_by_mover_dispatcher_sem, 1, NULL); } } } else { tman_unlock_memory(updated_datasrc_list); } Sleep(SLEEP_DISPATCHER_PERIOD); } // Release the movers. for (i = 0; i < MAXMOVER; i++) { ReleaseSemaphore(wakeup_mover_by_mover_dispatcher_sem, 1, NULL); } return 1; }
int Queue_isEmpty(Queue queue) { assert(queue); return List_isEmpty(queue); }
//analytical results void Resource_analyseSequence() { logAllowed = false; ListPosition p = List_header(resourceList); if (List_isEmpty(resourceList)) { printf("Empty list\n"); logAllowed = true; return; } // ----------analyse raw data of one test, calc mean of constants------------ int count = 0; const struct CalcResult INITILIZED = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; struct CalcResult sum = INITILIZED; struct CalcResult mean = INITILIZED; struct CalcResult sumDeviation = INITILIZED; struct CalcResult deviation = INITILIZED; unsigned long lastTime = 0l; long lastSpace = 0l; bool constant = true; bool constantSpace = true; do { p = List_advance(p); //calc data struct Resource* res = (struct Resource*) List_retrieve(p); if (count == 0) { lastTime = res->time; lastSpace = res->space; } struct CalcResult* calcRet = Resource_analyse(res->size, res->time, res->space); printf("(%d, %ld, %ld)", res->size, res->time, res->space); printCalcRet("r", *calcRet); res->calcResult = calcRet; //sum of constants sum.cn2 += calcRet->cn2; sum.cnlog2n += calcRet->cnlog2n; sum.cnlogn += calcRet->cnlogn; sum.cn += calcRet->cn; sum.clogn += calcRet->clogn; sum.cn2Space += calcRet->cn2Space; sum.cnlognSpace += calcRet->cnlognSpace; sum.cnSpace += calcRet->cnSpace; sum.clognSpace += calcRet->clognSpace; // constant time/space check constant &= (res->time == lastTime); constantSpace &= (res->space == lastSpace); // update vars lastTime = res->time; count++; } while (!List_isLast(p, resourceList)); if (constant && constantSpace) { printf( "constant time & space! constant time = %ld, constant space = %ld\n", lastTime, lastSpace); //return; } // mean mean.cn2 = sum.cn2 / count; mean.cnlog2n = sum.cnlog2n / count; mean.cnlogn = sum.cnlogn / count; mean.cn = sum.cn / count; mean.clogn = sum.clogn / count; mean.cn2Space = sum.cn2Space / count; mean.cnlognSpace = sum.cnlognSpace / count; mean.cnSpace = sum.cnSpace / count; mean.clognSpace = sum.clognSpace / count; printCalcRet("constant mean", mean); // ---------Calc variance of constants----------- struct Operation* op = operations[operationCount]; p = List_header(resourceList); // open file char data[50]; char filePath[50] = "./plot/"; strcat(filePath, op->path); strcat(filePath, "/"); mkdir(filePath, 7777); strcat(filePath, target); strcat(filePath, ".dat"); FILE *dataFile = fopen(filePath, "w+"); fputs("#size time space\n", dataFile); do { // sum variance p = List_advance(p); struct Resource* res = (struct Resource*) List_retrieve(p); struct CalcResult* ret = res->calcResult; sumDeviation.cn2 += pow((ret->cn2 - mean.cn2), 2) / count; sumDeviation.cnlog2n += pow((ret->cnlog2n - mean.cnlog2n), 2) / count; sumDeviation.cnlogn += pow((ret->cnlogn - mean.cnlogn), 2) / count; sumDeviation.cn += pow((ret->cn - mean.cn), 2) / count; sumDeviation.clogn += pow((ret->clogn - mean.clogn), 2) / count; sumDeviation.cn2Space += pow((ret->cn2Space - mean.cn2Space), 2) / count; sumDeviation.cnlognSpace += pow((ret->cnlognSpace - mean.cnlognSpace), 2) / count; sumDeviation.cnSpace += pow((ret->cnSpace - mean.cnSpace), 2) / count; sumDeviation.clognSpace += pow((ret->clognSpace - mean.clognSpace), 2) / count; // printf(" cnlogn: %f = , sum = %f\n",pow((ret->cnlogn - mean.cnlogn),2) / count, sumDeviation.cnlogn); // write to data file sprintf(data, "%d %lu %ld\n", res->size, res->time, res->space); fputs(data, dataFile); } while (!List_isLast(p, resourceList)); fclose(dataFile); // deviation deviation.cn2 = sqrt(sumDeviation.cn2) / mean.cn2 * 100; deviation.cnlog2n = sqrt(sumDeviation.cnlog2n) / mean.cnlog2n * 100; deviation.cnlogn = sqrt(sumDeviation.cnlogn) / mean.cnlogn * 100; deviation.cn = sqrt(sumDeviation.cn) / mean.cn * 100; deviation.clogn = sqrt(sumDeviation.clogn) / mean.clogn * 100; deviation.cn2Space = fabs(sqrt(sumDeviation.cn2Space) / mean.cn2Space * 100); deviation.cnlognSpace = fabs( sqrt(sumDeviation.cnlognSpace) / mean.cnlognSpace * 100); deviation.cnSpace = fabs(sqrt(sumDeviation.cnSpace) / mean.cnSpace * 100); deviation.clognSpace = fabs( sqrt(sumDeviation.clognSpace) / mean.clognSpace * 100); // determine which class of complexity printCalcRet("Relative standard deviation", deviation); // struct Operation* op = *pointer; float dev = 0.0f; float con = 0.0f; if (constant) { op->timeComplexity = N0; op->timeConstant = lastTime; } else { if (deviation.cnlogn < deviation.cn2) { dev = deviation.cnlogn; con = mean.cnlogn; op->timeComplexity = NLOGN; } else { dev = deviation.cn2; con = mean.cn2; op->timeComplexity = N2; } if (deviation.cn < dev) { dev = deviation.cn; con = mean.cn; op->timeComplexity = N; } if (deviation.clogn < dev) { dev = deviation.clogn; con = mean.clogn; op->timeComplexity = LOGN; } op->timeDeviation = dev; op->timeConstant = con; } if (constantSpace) { op->spaceComplexity = N0; op->spaceConstant = lastSpace; } else { if (deviation.cnlognSpace < deviation.cn2Space) { dev = deviation.cnlognSpace; con = mean.cnlognSpace; op->spaceComplexity = NLOGN; } else { dev = deviation.cn2Space; con = mean.cn2Space; op->spaceComplexity = N2; } if (deviation.cnSpace < dev) { dev = deviation.cnSpace; con = mean.cnSpace; op->spaceComplexity = N; } if (deviation.clognSpace < dev) { dev = deviation.clognSpace; con = mean.clognSpace; op->spaceComplexity = LOGN; } op->spaceDeviation = dev; op->spaceConstant = con; } printf( "Analytical result: class = %d, deviation = %.2f%, constant = %.2f space class = %d, space dev = %.2f%, space constant = %.2f\n", op->timeComplexity, op->timeDeviation, op->timeConstant, op->spaceComplexity, op->spaceDeviation, op->spaceConstant); //pointer++; operationCount++; logAllowed = true; }
List doPathfinding(Map map, Car cars[3]) { Heap openSet = Heap_new(State_compare); List closedSet = List_new(); List finalPath = List_new(); List neighbors; Position neighbor; State state, newState; Vector newSpeed, acceleration; int end = 0, i, j, useBoost, positionTaken, distance; float cost; LOGINFO("A* doin' da werk!"); state = State_new(Car_getPosition(cars[0]), Car_getSpeed(cars[0]), Car_getBoosts(cars[0]), map); Heap_insert(openSet, state); while(!Heap_isEmpty(openSet) && !end) { state = Heap_extractMin(openSet); if(Map_getTile(map, State_getPosition(state)->x, State_getPosition(state)->y) == ARRIVAL) { end = 1; break; } distance = Map_getDistance(map, State_getPosition(state)->x, State_getPosition(state)->y); neighbors = Map_getReachablePositions(map, State_getPosition(state), State_getSpeed(state), State_getBoosts(state)); List_foreach(neighbors, neighbor, i) { if(Map_getDistance(map, neighbor->x, neighbor->y) > distance) { Position_delete(neighbor); continue; } cost = State_getRealCost(state) + 1; newSpeed = Position_findOffset(State_getPosition(state), neighbor); acceleration = Vector_copy(newSpeed); Vector_substract(acceleration, State_getSpeed(state)); useBoost = 0; positionTaken = 0; if(Vector_squaredLength(acceleration) > 2) { useBoost = 1; } for(j = 1; j < 3; j++) { if(Position_equal(neighbor, Car_getPosition(cars[j]))) { positionTaken = 1; } } if(!positionTaken) { newState = State_new(neighbor, newSpeed, State_getBoosts(state) - useBoost, map); State_setRealCost(newState, cost); State_setParent(newState, state); Heap_insert(openSet, newState); } Vector_delete(newSpeed); Vector_delete(acceleration); Position_delete(neighbor); } List_insert(closedSet, state); List_empty(neighbors); List_delete(neighbors); } while(state != NULL) { List_insert(finalPath, Position_copy(State_getPosition(state))); state = State_getParent(state); } List_head(closedSet); while(!List_isEmpty(closedSet)) { state = List_getCurrent(closedSet); List_remove(closedSet); State_delete(state); } List_delete(closedSet); while((state = Heap_extractMin(openSet)) != NULL) { State_delete(state); } Heap_delete(openSet); LOGINFO("A* is done mate"); return finalPath; }
// Drop the trigger object and all the related ADTs int delTrigger(trigger *arg) { DATUM *after_clause_datasrc = NULL; char *after_clause_datasrc_name = NULL; RCG *temp_rcg; RCG_node *rcg_node; ListElement *cursor; // create trigger ADT cannot be null ASSERT(arg); // Get the name of the datasrc in the after clause if there is one. if (arg->after_clause == NULL) { after_clause_datasrc = NULL; } else { get_after_clause_datasrc(arg->after_clause, &after_clause_datasrc); after_clause_datasrc_name = get_chars(after_clause_datasrc); } // Extract the RCG pointer temp_rcg = (RCG *)arg->rule_condition_graph; // The RCG graph cannot be NULL ASSERT(temp_rcg); // Delete the trigger ID nodes of this trigger for all the data sources rcg_node = (RCG_node *)List_getFirst(temp_rcg->nodes, &cursor); while (rcg_node != NULL) { // If the after clause is specified, then check whether the tuple variable name of this RCG node matches the // tuple variable name specified in the after clause. If they match, then this rcg node has a corresponding // trigger id node, otherwise it does not have one. if (after_clause_datasrc != NULL) { if (strcmp(rcg_node->tuple_variable_name, after_clause_datasrc_name) != 0) { // The rcg node's tuple variable name does not match with the after clause. // So it does not have a trigger ID node entry in the SPI, so skip this rcg node. rcg_node = (RCG_node *)List_getNext(&cursor); continue; } } // Find the trigger ID nodes for this rcg node and delete them delete_trigger_ID_node(rcg_node, DEFAULT_INDEX); // If the trigger ID list is empty, then delete the constant. if (List_isEmpty(rcg_node->trigger_ID_list[DEFAULT_INDEX])) { // Delete the trigger ID list for this constant set tman_delete(rcg_node->trigger_ID_list[DEFAULT_INDEX]); // Remove the constant node from the expression list remove_constant_node_from_sig(rcg_node, DEFAULT_INDEX); } // Check whether the constant set for the expression signature is empty. // If yes, then delete the expression signature . del_expr_sig_when_const_set_is_empty(rcg_node, DEFAULT_INDEX); /* If the rcg node is for an INSERT/UPDATE trigger, then we have to delete all the data structures from the UPDATE branch.. */ if( rcg_node->opcode == TM_INSERT_UPDATE ) { delete_trigger_ID_node(rcg_node, INS_UPD_INDEX); if (List_isEmpty(rcg_node->trigger_ID_list[INS_UPD_INDEX])) { // Delete the trigger ID list for this constant set tman_delete(rcg_node->trigger_ID_list[INS_UPD_INDEX]); // Remove the constant node from the expression list remove_constant_node_from_sig(rcg_node, INS_UPD_INDEX); } // Check whether the constant set for the expression signature is empty. // If yes, then delete the expression signature . del_expr_sig_when_const_set_is_empty(rcg_node, INS_UPD_INDEX); } rcg_node = (RCG_node *)List_getNext(&cursor); } // Delete the Gator network for this trigger tman_delete(arg->gator); // Delete the RCG network for this trigger tman_delete(arg->rule_condition_graph); // Delete the create trigger data structure for this trigger // This deletes the actual syntax trees when the trigger was first created tman_delete(arg); return TMAN_OK; }