1 /***************************************
2 $Header: /home/amb/routino/src/RCS/queue.c,v 1.7 2009/11/13 19:24:11 amb Exp $
4 Queue data type functions.
6 Part of the Routino routing software.
7 ******************/ /******************
8 This file Copyright 2008,2009 Andrew M. Bishop
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU Affero General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Affero General Public License for more details.
20 You should have received a copy of the GNU Affero General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ***************************************/
25 #include <sys/types.h>
32 /*+ A queue of results. +*/
35 uint32_t nallocated; /*+ The number of entries allocated. +*/
36 uint32_t noccupied; /*+ The number of entries occupied. +*/
38 Result **data; /*+ The queue of pointers to results. +*/
42 /*++++++++++++++++++++++++++++++++++++++
45 Queue *NewQueueList Returns the queue.
46 ++++++++++++++++++++++++++++++++++++++*/
48 Queue *NewQueueList(void)
52 queue=(Queue*)malloc(sizeof(Queue));
54 queue->nallocated=1023;
57 queue->data=(Result**)malloc(queue->nallocated*sizeof(Result*));
63 /*++++++++++++++++++++++++++++++++++++++
66 Queue *queue The queue to be freed.
67 ++++++++++++++++++++++++++++++++++++++*/
69 void FreeQueueList(Queue *queue)
77 /*++++++++++++++++++++++++++++++++++++++
78 Insert a new item into the queue in the right place.
80 The data is stored in a "Binary Heap" http://en.wikipedia.org/wiki/Binary_heap
81 and this operation is adding an item to the heap.
83 Queue *queue The queue to insert the result into.
85 Result *result The result to insert into the queue.
86 ++++++++++++++++++++++++++++++++++++++*/
88 void InsertInQueue(Queue *queue,Result *result)
92 if(result->queued==NOT_QUEUED)
94 if(queue->noccupied==queue->nallocated)
96 queue->nallocated=2*queue->nallocated+1;
97 queue->data=(Result**)realloc((void*)queue->data,queue->nallocated*sizeof(Result*));
100 index=queue->noccupied;
103 queue->data[index]=result;
104 queue->data[index]->queued=index;
108 index=result->queued;
111 /* Bubble up the new value */
114 queue->data[index]->sortby<queue->data[(index-1)/2]->sortby)
119 newindex=(index-1)/2;
121 temp=queue->data[index];
122 queue->data[index]=queue->data[newindex];
123 queue->data[newindex]=temp;
125 queue->data[index]->queued=index;
126 queue->data[newindex]->queued=newindex;
133 /*++++++++++++++++++++++++++++++++++++++
134 Pop an item from the front of the queue.
136 The data is stored in a "Binary Heap" http://en.wikipedia.org/wiki/Binary_heap
137 and this operation is deleting the root item from the heap.
139 Result *PopFromQueue Returns the top item.
141 Queue *queue The queue to remove the result from.
142 ++++++++++++++++++++++++++++++++++++++*/
144 Result *PopFromQueue(Queue *queue)
149 if(queue->noccupied==0)
152 retval=queue->data[0];
153 retval->queued=NOT_QUEUED;
158 queue->data[index]=queue->data[queue->noccupied];
160 /* Bubble down the newly promoted value */
162 while((2*index+2)<queue->noccupied &&
163 (queue->data[index]->sortby>queue->data[2*index+1]->sortby ||
164 queue->data[index]->sortby>queue->data[2*index+2]->sortby))
169 if(queue->data[2*index+1]->sortby<queue->data[2*index+2]->sortby)
174 temp=queue->data[newindex];
175 queue->data[newindex]=queue->data[index];
176 queue->data[index]=temp;
178 queue->data[index]->queued=index;
179 queue->data[newindex]->queued=newindex;
184 if((2*index+2)==queue->noccupied &&
185 queue->data[index]->sortby>queue->data[2*index+1]->sortby)
192 temp=queue->data[newindex];
193 queue->data[newindex]=queue->data[index];
194 queue->data[index]=temp;
196 queue->data[index]->queued=index;
197 queue->data[newindex]->queued=newindex;