void List_copy(const void *fromList, void *toList, const void *fromListFromNode, const void *fromListToNode, void *toListNextNode, ListNodeCopyFunction listNodeCopyFunction, void *listNodeCopyUserData ) { Node *node; Node *newNode; assert(fromList != NULL); assert(toList != NULL); assert(listNodeCopyFunction != NULL); if (fromListFromNode == LIST_START) fromListFromNode = ((List*)fromList)->head; node = (Node*)fromListFromNode; while (node != fromListToNode) { newNode = listNodeCopyFunction(node,listNodeCopyUserData); List_insert(toList,newNode,toListNextNode); node = node->next; } if (node != NULL) { newNode = listNodeCopyFunction(node,listNodeCopyUserData); List_insert(toList,newNode,toListNextNode); } }
void List_move(void *fromList, void *toList, void *fromListFromNode, void *fromListToNode, void *toListNextNode ) { Node *node; Node *nextNode; assert(fromList != NULL); assert(toList != NULL); if (fromListFromNode == LIST_START) fromListFromNode = ((List*)fromList)->head; node = (Node*)fromListFromNode; while (node != fromListToNode) { nextNode = node->next; List_remove(fromList,node); List_insert(toList,node,toListNextNode); node = nextNode; } if (node != NULL) { List_remove(fromList,node); List_insert(toList,node,toListNextNode); } }
void List_print(Node * front) { if(front == NULL) { return; } printf("%d \n", front->data); List_print(front->next); NOde * List_delete(Node * front, int val) { if(front == NULL) { return NULL; } if((front->val) == val) { Node * p = front->next; //p may be NULL free(front); return p; } front->next = List_delete(front->next, val); return front; } void List_destroy /* List_insert4 (Node ** front, ....) { *front = ... */ main { front = List_insert(front, 5); front = List_insert(front, 8); front = List_insert(front, 9); front = List_insert(front, 11); front2 = List_insert2(front2, 5); front2 = List_insert2(front2, 8); front2 = List_insert2(front2, 9); front2 = List_insert2(front2, 11); return EXIT_SUCCESS; }
void Resource_storeData(unsigned int size) { logAllowed = false; if (position == NULL) position = List_header(resourceList); struct Resource* res = (struct Resource *) malloc(sizeof(struct Resource)); res->size = size; // time if (timeMode == 0) res->time = totalTime; if (timeMode == 1) { long diff = (clock() - startTime) * 100; res->time = diff; // printf("now = %ld, startTime = %ld, diff = %ld, CLOCKS_PER_SEC = %ld\n", clock(),startTime, diff, CLOCKS_PER_SEC); } // space // struct mstats* mem = mstats(); // printf("bytes used: %d\n",mem); if (maxSpace > 0 && totalSpace >= 0) res->space = maxSpace; else res->space = totalSpace; List_insert(res, resourceList, position); position = List_advance(position); //clear Resource_clearData(); }
/** *** \brief Stores a value in the dictionary with the specified key, maintaining the *** keys in alphabetical order. *** *** If a value with that key already exists in the *** dictionary, it will be replaced with the new value *** \note This is a potential memory leak! **/ bool Dict_putSorted(Dict *d, const char *key, void *value) { bool retVal = false; bool added = false; size_t idx = 0; Pair *p = NULL; Pair *pp = NULL; size_t ii; if (keyToIndex(d, key, &idx)) { p = (Pair *)List_get((List *)d, idx); p->value = value; retVal = true; } else { p = new_Pair(key, value); added = false; if (List_length((List *)d) > 0) { for (ii = 0; ii < List_length((List *)d); ++ii) { pp = (Pair *)List_get((List *)d, ii); if (strcmp(key, pp->key) < 0) { List_insert((List *)d, ii, p); added = true; break; } } } if (!added) retVal = List_append((List *)d, p); } Logging_tracef("++++ Added pointer 0x%x to Dict 0x%x", (unsigned int)value, (unsigned int)d); return retVal; }
void csr_Initialization(int nnodes, int nel, int *nnz_out, NodeType *Node, ElementType *Element, NodeListType **List, CSRAuxType *CSRAux) { int I, J, I1, I2, I3, nnz = 0; int *counter = (int*) mycalloc("counter of 'csr_Initialization'",nnodes,sizeof(int)); NodeDataType **NodeData = (NodeDataType**) mycalloc("NodeData of 'csr_Inititalization'",nnodes,sizeof(NodeDataType*)); for (I=0; I<nel; I++) { I1 = Element[I].Vertex[0]; I2 = Element[I].Vertex[1]; I3 = Element[I].Vertex[2]; List_insert(List, I1, I1, counter); List_insert(List, I1, I2, counter); List_insert(List, I1, I3, counter); List_insert(List, I2, II, counter); List_insert(List, I2, I2, counter); List_insert(List, I2, I3, counter); List_insert(List, I3, II, counter); List_insert(List, I3, I2, counter); List_insert(List, I3, I3, counter); } for (I=0; I<nnodes; I++){ NodeData[I] = (NodeDataType*) mycalloc("NodeData of 'csr_Initialization'",counter[I],sizeof(NodeDataType)); Fill_Id_NodeData(List[I],NodeData[I]); } for (I= 0; I < nnodes; I++){ current = List[I]; while (current != NULL){ temp = current; current = current->next; free(temp); } } free(List); for (I= 0; I<nnodes; I++){ for (J=0; J<counter[I]; J++){ for (K=0; K<NDOF; K++){ if (Node[NodeData[I][J].Id].id[K]>=0) nnz++; } } } CSRAux->conter = counter; CSRAux->NodeData = NodeData; }
List * List_insert(List * head, List * add){ if (head == NULL) { return add; } head -> next = List_insert(head -> next, add); return head; }
List List_copy(List list){ List listCopy = List_new(); void* ptr; int i; List_foreach(list, ptr, i){ List_insert(listCopy,ptr); }
/** * Recomputes the distance map. * @param map The map. * @param cars The cars on the track. */ void recomputeDistances(Map map, Car cars[3]) { int i; List takenPositions = List_new(); for(i = 0; i < 3; i++) if(Car_hasArrived(cars[i])) List_insert(takenPositions, Car_getPosition(cars[i])); Map_recomputeDistances(map, takenPositions); List_empty(takenPositions); List_delete(takenPositions); }
/***********************************************************************\ * Name : checkDuplicateNode * Purpose: check if node is already in list * Input : fileName - code file name * lineNb - code line number * list - list where node should be inserted * newNode - new node to insert * Output : - * Return : - * Notes : - \***********************************************************************/ LOCAL void checkDuplicateNode(const char *fileName, ulong lineNb, const List *list, const Node *newNode ) { Node *node; assert(list != NULL); node = list->head; while (node != NULL) { if (node == newNode) { HALT_INTERNAL_ERROR_AT(fileName,lineNb,"Node %p is already in list %p!",node,list); } node = node->next; } } #endif /* not NDEBUG */ Node *List_newNode(ulong size) { return (Node*)malloc(size); } Node *List_deleteNode(Node *node) { Node *nextNode; assert(node != NULL); nextNode = node->next; free(node); return nextNode; } void List_init(void *list) { assert(list != NULL); ((List*)list)->head = NULL; ((List*)list)->tail = NULL; ((List*)list)->count = 0; } void List_done(void *list, ListNodeFreeFunction listNodeFreeFunction, void *listNodeFreeUserData ) { assert(list != NULL); List_clear(list,listNodeFreeFunction,listNodeFreeUserData); } List *List_new(void) { List *list; list = (List*)malloc(sizeof(List)); if (list == NULL) return NULL; List_init(list); return list; } List *List_duplicate(const void *fromList, const void *fromListFromNode, const void *fromListToNode, ListNodeCopyFunction listNodeCopyFunction, void *listNodeCopyUserData ) { List *list; assert(fromList != NULL); assert(listNodeCopyFunction != NULL); list = (List*)malloc(sizeof(List)); if (list == NULL) return NULL; List_init(list); List_copy(fromList, list, fromListFromNode, fromListToNode, NULL, listNodeCopyFunction, listNodeCopyUserData ); return list; } void List_delete(void *list, ListNodeFreeFunction listNodeFreeFunction, void *listNodeFreeUserData ) { assert(list != NULL); List_done(list,listNodeFreeFunction,listNodeFreeUserData);\ free(list); } void List_clear(void *list, ListNodeFreeFunction listNodeFreeFunction, void *listNodeFreeUserData ) { Node *node; assert(list != NULL); if (listNodeFreeFunction != NULL) { while (((List*)list)->tail != NULL) { node = ((List*)list)->tail; ((List*)list)->tail = ((List*)list)->tail->prev; listNodeFreeFunction(node,listNodeFreeUserData); LIST_DELETE_NODE(node); } } else { while (((List*)list)->tail != NULL) { node = ((List*)list)->tail; ((List*)list)->tail = ((List*)list)->tail->prev; LIST_DELETE_NODE(node); } } ((List*)list)->head = NULL; ((List*)list)->count = 0; } void List_copy(const void *fromList, void *toList, const void *fromListFromNode, const void *fromListToNode, void *toListNextNode, ListNodeCopyFunction listNodeCopyFunction, void *listNodeCopyUserData ) { Node *node; Node *newNode; assert(fromList != NULL); assert(toList != NULL); assert(listNodeCopyFunction != NULL); if (fromListFromNode == LIST_START) fromListFromNode = ((List*)fromList)->head; node = (Node*)fromListFromNode; while (node != fromListToNode) { newNode = listNodeCopyFunction(node,listNodeCopyUserData); List_insert(toList,newNode,toListNextNode); node = node->next; } if (node != NULL) { newNode = listNodeCopyFunction(node,listNodeCopyUserData); List_insert(toList,newNode,toListNextNode); } } void List_move(void *fromList, void *toList, void *fromListFromNode, void *fromListToNode, void *toListNextNode ) { Node *node; Node *nextNode; assert(fromList != NULL); assert(toList != NULL); if (fromListFromNode == LIST_START) fromListFromNode = ((List*)fromList)->head; node = (Node*)fromListFromNode; while (node != fromListToNode) { nextNode = node->next; List_remove(fromList,node); List_insert(toList,node,toListNextNode); node = nextNode; } if (node != NULL) { List_remove(fromList,node); List_insert(toList,node,toListNextNode); } } #ifdef NDEBUG void List_insert(void *list, void *node, void *nextNode ) #else /* NDEBUG */ void __List_insert(const char *fileName, ulong lineNb, void *list, void *node, void *nextNode ) #endif /*NDEBUG */ { assert(list != NULL); assert(((((List*)list)->count == 0) && (((List*)list)->head == NULL) && (((List*)list)->tail == NULL)) || ((((List*)list)->count > 0) && (((List*)list)->head != NULL) && (((List*)list)->tail != NULL)) ); assert(node != NULL); #ifndef NDEBUG checkDuplicateNode(fileName,lineNb,(List*)list,(Node*)node); #endif /* not NDEBUG */ if (nextNode != NULL) { ((Node*)node)->prev = ((Node*)nextNode)->prev; ((Node*)node)->next = ((Node*)nextNode); if (((Node*)nextNode)->prev != NULL) ((Node*)nextNode)->prev->next = node; ((Node*)nextNode)->prev = node; if (((List*)list)->head == nextNode) ((List*)list)->head = node; ((List*)list)->count++; } else if (((List*)list)->head != NULL) { ((Node*)node)->prev = ((List*)list)->tail; ((Node*)node)->next = NULL; ((List*)list)->tail->next = node; ((List*)list)->tail = node; ((List*)list)->count++; } else { ((Node*)node)->prev = NULL; ((Node*)node)->next = NULL; ((List*)list)->head = node; ((List*)list)->tail = node; ((List*)list)->count = 1; } assert(((((List*)list)->count == 0) && (((List*)list)->head == NULL) && (((List*)list)->tail == NULL)) || ((((List*)list)->count > 0) && (((List*)list)->head != NULL) && (((List*)list)->tail != NULL)) ); } #ifdef NDEBUG void List_append(void *list, void *node ) #else /* NDEBUG */ void __List_append(const char *fileName, ulong lineNb, void *list, void *node ) #endif /* NDEBUG */ { assert(list != NULL); assert(node != NULL); #ifdef NDEBUG List_insert(list,node,NULL); #else /* NDEBUG */ __List_insert(fileName,lineNb,list,node,NULL); #endif /* NDEBUG */ }
//allocate memory outside void List_init(List *list, Node *n, Node **local_head, Node **local_tail, int *vec, int len) { list->nlist = 0; list->head = NULL; int i; for(i=0; i<len; i++) { (n+i)->index = i; (n+i)->value = vec[i]; if(vec[i] > 0) { List_insert(list, n+i, local_head, local_tail); } } }
/***********************************************************************\ * Name : insertString * Purpose: insert string in string list * Input : __fileName__ - file naem (debug only) * __lineNb__ - line number (debug only) * stringList - string list * string - string to insert * nextNode - next string list node * Output : - * Return : - * Notes : - \***********************************************************************/ #ifdef NDEBUG LOCAL void insertString(StringList *stringList, const String string, StringNode *nextStringNode) #else /* not NDEBUG */ LOCAL void insertString(const char *__fileName__, ulong __lineNb__, StringList *stringList, const String string, StringNode *nextStringNode) #endif /* NDEBUG */ { StringNode *stringNode; assert(stringList != NULL); #ifdef NDEBUG stringNode = LIST_NEW_NODE(StringNode); #else /* not NDEBUG */ stringNode = __LIST_NEW_NODE(__fileName__,__lineNb__,StringNode); #endif /* NDEBUG */ if (stringNode == NULL) { HALT_INSUFFICIENT_MEMORY(); } stringNode->string = string; List_insert(stringList,stringNode,nextStringNode); }
int main(int argc, char * argv[]) { Node * head = NULL; /* must initialize it to NULL */ head = List_insert(head, 917); head = List_insert(head, -504); head = List_insert(head, 326); List_print(head); head = List_delete(head, -504); List_print(head); head = List_insert(head, 138); head = List_insert(head, -64); head = List_insert(head, 263); List_print(head); if (List_search(head, 138) != NULL) { printf("138 is in the list\n"); } else { printf("138 is not in the list\n"); } if (List_search(head, 987) != NULL) { printf("987 is in the list\n"); } else { printf("987 is not in the list\n"); } /* delete the first Node */ head = List_delete(head, 263); List_print(head); /* delete the last Node */ head = List_delete(head, 917); List_print(head); /* delete all Nodes */ List_destroy(head); return EXIT_SUCCESS; }
void Menu_add_input(Menu * menu, Input * input) { if (input) { List_insert(menu->inputs, input); } }
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; }
void Queue_insert(Queue queue, void* ptr) { assert(queue); List_insert(queue, ptr); }
void Menu_add_label(Menu * menu, Label * label) { if (label) { List_insert(menu->labels, label); } }
void FragmentList_addEntry(FragmentNode *fragmentNode, uint64 offset, uint64 length) { FragmentEntryNode *fragmentEntryNode,*deleteFragmentEntryNode; FragmentEntryNode *prevFragmentEntryNode,*nextFragmentEntryNode; assert(fragmentNode != NULL); /* remove all fragments which are completely covered by new fragment */ fragmentEntryNode = fragmentNode->fragmentEntryList.head; while (fragmentEntryNode != NULL) { if ((F0(fragmentEntryNode) >= I0(offset,length)) && (F1(fragmentEntryNode) <= I1(offset,length))) { deleteFragmentEntryNode = fragmentEntryNode; fragmentEntryNode = fragmentEntryNode->next; List_remove(&fragmentNode->fragmentEntryList,deleteFragmentEntryNode); LIST_DELETE_NODE(deleteFragmentEntryNode); } else { fragmentEntryNode = fragmentEntryNode->next; } } /* find prev/next fragment */ prevFragmentEntryNode = NULL; fragmentEntryNode = fragmentNode->fragmentEntryList.head; while ((fragmentEntryNode != NULL) && (F1(fragmentEntryNode) < I1(offset,length))) { prevFragmentEntryNode = fragmentEntryNode; fragmentEntryNode = fragmentEntryNode->next; } nextFragmentEntryNode = NULL; fragmentEntryNode = fragmentNode->fragmentEntryList.tail; while ((fragmentEntryNode != NULL) && (F0(fragmentEntryNode) > I0(offset,length))) { nextFragmentEntryNode = fragmentEntryNode; fragmentEntryNode = fragmentEntryNode->prev; } /* check if existing Fragment can be extended or new Fragment have to be inserted */ if ((prevFragmentEntryNode != NULL) && (F1(prevFragmentEntryNode)+1 >= I0(offset,length))) { /* combine with previous existing fragment */ prevFragmentEntryNode->length = (offset+length)-prevFragmentEntryNode->offset; prevFragmentEntryNode->offset = prevFragmentEntryNode->offset; } else if ((nextFragmentEntryNode != NULL) && (I1(offset,length)+1 >= F0(nextFragmentEntryNode))) { /* combine with next existing fragment */ nextFragmentEntryNode->length = (nextFragmentEntryNode->offset+nextFragmentEntryNode->length)-offset; nextFragmentEntryNode->offset = offset; } else { /* insert new Fragment */ fragmentEntryNode = LIST_NEW_NODE(FragmentEntryNode); if (fragmentEntryNode == NULL) { HALT_INSUFFICIENT_MEMORY(); } fragmentEntryNode->offset = offset; fragmentEntryNode->length = length; List_insert(&fragmentNode->fragmentEntryList,fragmentEntryNode,nextFragmentEntryNode); } }
void Menu_add_button(Menu * menu, Button * button) { if (button) { List_insert(menu->buttons, button); } }