OUTPUTDEVICE *InitMacOutputDevice (void) { char buffer[32]; /* create output device */ if ((MacOutputDevice=CreateOutputDevice("screen"))==NULL) return(NULL); /* set global variables */ qdgray = GetQDGlobalsBlack(qdgray); /* init output device 'screen' */ MacOutputDevice->OpenOutput = Mac_OpenOutput; MacOutputDevice->CloseOutput = Mac_CloseOutput; MacOutputDevice->ActivateOutput = Mac_ActivateOutput; MacOutputDevice->UpdateOutput = Mac_UpdateOutput; MacOutputDevice->v.locked = 1; InitMacPort (); /* get gui heapsize */ if (GetDefaultValue(DEFAULTSFILENAME,"guimemory",buffer)==0) sscanf(buffer," %d ",&guiHeapSize); /* allocate gui heap */ if ((guiHeap=NewHeap(GENERAL_HEAP,guiHeapSize,malloc(guiHeapSize)))==NULL) return(NULL); return (MacOutputDevice); }
void Dijkstra(node*list, int destiny, int count_nodes, int**node_distance, int**node_hops) { adj_node*links; int dijkstra_u=0; int dijkstra_identifier; Heap*heap; int i, *heap_place; heap_place=malloc(count_nodes*sizeof(int)); if(heap_place==NULL)exit(-1); for(i=0; i<count_nodes; i++) heap_place[i]=-1; if(count_nodes>0) { heap=NewHeap(count_number_nodes(list, count_nodes)); Initialize_distance_matrix(count_nodes, &(*node_distance), &(*node_hops), list, destiny, heap, &heap_place); while(HeapEmpty(heap)) { dijkstra_identifier= RemoveMax(heap, (*node_distance), &heap_place, (*node_hops)); //printf("removed %d from heap\n", dijkstra_identifier); dijkstra_u = dijkstra_identifier - 1; if((*node_distance)[dijkstra_u]!=-1) { /*for each uv*/ for(links=(list[dijkstra_u]).link; links!=NULL; links=links->next) { if(heap_place[(links->identifier)-1]!=-1) { if(((*node_distance)[(links->identifier)-1] <= min((*node_distance)[dijkstra_u],links->preference))&& ((links->preference)<=(*node_distance)[dijkstra_u])) { /*if it is a consecutive peer route, the route is not usable*/ if(!(((*node_distance)[dijkstra_u]==2)&&((links->preference)==2))) { if(((*node_distance)[(links->identifier)-1])==min((*node_distance)[dijkstra_u],links->preference)) { /*same route type, chooses the one with less hops*/ if(((*node_hops)[(links->identifier)-1])>((*node_hops)[dijkstra_u]+1)) (*node_hops)[(links->identifier)-1]=(*node_hops)[dijkstra_u]+1; } else { /*changed route type so just updates the number of hops*/ (*node_hops)[(links->identifier)-1]=(*node_hops)[dijkstra_u]+1; //these 2 lines were outside the else (*node_distance)[(links->identifier)-1] = min((*node_distance)[dijkstra_u],links->preference); } FixUp(heap, heap_place[(links->identifier)-1], (*node_distance), &heap_place, (*node_hops)); } } } } } } invert_weights(&(*node_distance), count_nodes); } free(heap_place); return; }
int main () { int heapSize = 12; int intArray[4] = {4, 6, 2, 8}; heapHndl testHeap, builtHeap; testHeap = NewHeap (heapSize); if (IsEmpty (testHeap)) { printf ("working\n"); } Insert (testHeap, 9); Insert (testHeap, 3); Insert (testHeap, 5); Insert (testHeap, 2); Insert (testHeap, 32); Insert (testHeap, 6); Insert (testHeap, 1); Insert (testHeap, 43); Insert (testHeap, 23); Insert (testHeap, 42); Insert (testHeap, 65); Insert (testHeap, 57); if (IsFull (testHeap)) { printf ("heap currently full\n"); } DeleteMax (testHeap); builtHeap = BuildHeap (5, intArray, 4); printf ("%d\n", MaxValue (testHeap)); printf ("%d\n", MaxValue (builtHeap)); FreeHeap (testHeap); FreeHeap (builtHeap); return 0; }