优先级队列的C语言实现

 

1. priority_queue.h

 1 #ifndef PRIORITY_QUEUE_H_
 2 #define PRIORITY_QUEUE_H_
 3 
 4 #include <stdlib.h>
 5 
 6 #ifdef __cplusplus
 7 extern "C" {
 8 #else
 9 typedef int bool;
10 #define true  1
11 #define false 0
12 #endif
13 
14 typedef size_t ElemType;
15 typedef int (*Comparator)(const ElemType a, const ElemType b);
16 typedef void (*Notifier)(ElemType o, int index);
17 
18 #define INVALID_ELEM ((ElemType) 0xFFFFFFFF)
19 #define INVALID_INDEX -1
20 
21 typedef struct PriorityQueue
22 {
23     int         capacity;
24     int         size;
25     ElemType   *queue;
26     Comparator  comparator;
27     Notifier    notifier;
28 } PriorityQueue;
29 
30 void ConstructPriorityQueueDefault(PriorityQueue *priorityQueue);
31 void ConstructPriorityQueueParams(PriorityQueue *priorityQueue, int initialCapacity,
32         Comparator comparator, Notifier notifier);
33 void ConstructPriorityQueueArrayDefault(PriorityQueue *priorityQueue, const ElemType *array,
34         int size);
35 void ConstructPriorityQueueArrayParams(PriorityQueue *priorityQueue, const ElemType *array,
36         int size, int initialCapacity, Comparator comparator, Notifier notifier);
37 
38 void DestructPriorityQueue(PriorityQueue *priorityQueue);
39 
40 int PriorityQueue_Size(const PriorityQueue *priorityQueue);
41 void PriorityQueue_Clear(PriorityQueue *priorityQueue);
42 bool PriorityQueue_IsEmpty(const PriorityQueue *priorityQueue);
43 void PriorityQueue_Offer(PriorityQueue *priorityQueue, ElemType e);
44 ElemType PriorityQueue_Poll(PriorityQueue *priorityQueue);
45 ElemType PriorityQueue_Peek(PriorityQueue *priorityQueue);
46 bool PriorityQueue_Remove(PriorityQueue *priorityQueue, ElemType o);
47 bool PriorityQueue_RemoveEq(PriorityQueue *priorityQueue, ElemType o);
48 bool PriorityQueue_RemoveAt(PriorityQueue *priorityQueue, int i);
49 int PriorityQueue_IndexOf(const PriorityQueue *priorityQueue, ElemType o);
50 
51 #ifdef __cplusplus
52 }
53 #endif
54 
55 #endif

 

2. priority_queue.c

  1 #include "priority_queue.h"
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 
  6 #define _DEBUG 0
  7 
  8 #if _DEBUG
  9 #define LOG(fmt, ...) printf(fmt, ##__VA_ARGS__)
 10 #else
 11 #define LOG(fmt, ...)
 12 #endif
 13 
 14 #ifndef NULL
 15 #define NULL (void*)0
 16 #endif
 17 
 18 #define MIN(a, b) ((a) < (b) ? (a) : (b))
 19 #define MAX(a, b) ((a) > (b) ? (a) : (b))
 20 
 21 const static int DEFAULT_INITIAL_CAPACITY = 11;
 22 const static int MAX_ARRAY_SIZE = 0x7fffffff - 8;
 23 
 24 static void PriorityQueue_NotifyIndex(PriorityQueue *priorityQueue);
 25 static void PriorityQueue_NotifyInvalidIndex(PriorityQueue *priorityQueue);
 26 static void PriorityQueue_SiftUp(PriorityQueue *priorityQueue, int k, ElemType x);
 27 static void PriorityQueue_SiftUpNotify(PriorityQueue *priorityQueue, int k, ElemType x);
 28 static void PriorityQueue_SiftDown(PriorityQueue *priorityQueue, int k, ElemType x);
 29 static void PriorityQueue_SiftDownNotify(PriorityQueue *priorityQueue, int k, ElemType x);
 30 static void PriorityQueue_Heapify(PriorityQueue *priorityQueue);
 31 static void PriorityQueue_HeapifyNotify(PriorityQueue *priorityQueue);
 32 
 33 void ConstructPriorityQueueDefault(PriorityQueue *priorityQueue)
 34 {
 35     ConstructPriorityQueueParams(priorityQueue, DEFAULT_INITIAL_CAPACITY, NULL, NULL);
 36 }
 37 
 38 void ConstructPriorityQueueParams(PriorityQueue *priorityQueue, int initialCapacity,
 39         Comparator comparator, Notifier notifier)
 40 {
 41     if (priorityQueue == NULL) return;
 42     initialCapacity = MAX(initialCapacity, 1);
 43     priorityQueue->capacity   = initialCapacity;
 44     priorityQueue->size       = 0;
 45     priorityQueue->comparator = comparator;
 46     priorityQueue->notifier   = notifier;
 47     priorityQueue->queue      = (ElemType*)malloc(initialCapacity * sizeof(ElemType));
 48 }
 49 
 50 void ConstructPriorityQueueArrayDefault(PriorityQueue *priorityQueue, const ElemType *array,
 51         int size)
 52 {
 53     ConstructPriorityQueueArrayParams(priorityQueue, array, size, DEFAULT_INITIAL_CAPACITY, NULL, NULL);
 54 }
 55 
 56 void ConstructPriorityQueueArrayParams(PriorityQueue *priorityQueue, const ElemType *array,
 57         int size, int initialCapacity, Comparator comparator, Notifier notifier)
 58 {
 59     if (priorityQueue == NULL) return;
 60     if (array == NULL) size = 0;
 61     else size = MAX(0, size);
 62     priorityQueue->capacity   = MAX(initialCapacity, 1);
 63     priorityQueue->size       = size;
 64     priorityQueue->comparator = comparator;
 65     priorityQueue->notifier   = notifier;
 66     priorityQueue->queue      = (ElemType*)malloc(priorityQueue->capacity * sizeof(ElemType));
 67     memcpy(priorityQueue->queue, array, size * sizeof(ElemType));
 68     if (priorityQueue->notifier != NULL) {
 69         PriorityQueue_NotifyIndex(priorityQueue);
 70         PriorityQueue_HeapifyNotify(priorityQueue);
 71     }
 72     else
 73         PriorityQueue_Heapify(priorityQueue);
 74 }
 75 
 76 void DestructPriorityQueue(PriorityQueue *priorityQueue)
 77 {
 78     if (priorityQueue == NULL) return;
 79     if (priorityQueue->notifier != NULL)
 80         PriorityQueue_NotifyInvalidIndex(priorityQueue);
 81     priorityQueue->capacity = 0;
 82     priorityQueue->size = 0;
 83     priorityQueue->comparator = NULL;
 84     priorityQueue->notifier = NULL;
 85     free(priorityQueue->queue);
 86 }
 87 
 88 int PriorityQueue_Size(const PriorityQueue *priorityQueue)
 89 {
 90     if (priorityQueue == NULL) return -1;
 91     return priorityQueue->size;
 92 }
 93 
 94 /**
 95  * Removes all of the elements from this priority queue.
 96  * The queue will be empty after this call returns.
 97  */
 98 void PriorityQueue_Clear(PriorityQueue *priorityQueue)
 99 {
100     if (priorityQueue == NULL) return;
101     if (priorityQueue->notifier != NULL)
102         PriorityQueue_NotifyInvalidIndex(priorityQueue);
103     priorityQueue->size = 0;
104 }
105 
106 bool PriorityQueue_IsEmpty(const PriorityQueue *priorityQueue)
107 {
108     if (priorityQueue == NULL) return true;
109     return priorityQueue->size == 0;
110 }
111 
112 static void PriorityQueue_NotifyIndex(PriorityQueue *priorityQueue)
113 {
114     for (int i = 0; i < priorityQueue->size; ++i)
115         priorityQueue->notifier(priorityQueue->queue[i], i);
116 }
117 
118 static void PriorityQueue_NotifyInvalidIndex(PriorityQueue *priorityQueue)
119 {
120     for (int i = 0; i < priorityQueue->size; ++i)
121         priorityQueue->notifier(priorityQueue->queue[i], INVALID_INDEX);
122 }
123 
124 /**
125  * Increases the capacity of the array.
126  *
127  * @param minCapacity the desired minimum capacity
128  */
129 static void PriorityQueue_Grow(PriorityQueue *priorityQueue, int minCapacity)
130 {
131     int oldCapacity = priorityQueue->capacity;
132     // Double size if small; else grow by 50%
133     int newCapacity = oldCapacity + ((oldCapacity < 64) ?
134                                      (oldCapacity + 2) :
135                                      (oldCapacity >> 1));
136     // overflow-conscious code
137     newCapacity = MAX(minCapacity, newCapacity);
138     newCapacity = MIN(MAX_ARRAY_SIZE, newCapacity);
139     if (newCapacity > oldCapacity)
140     {
141         ElemType *newQueue = realloc(priorityQueue->queue, newCapacity * sizeof(ElemType));
142         if (newQueue != NULL)
143         {
144             priorityQueue->queue = newQueue;
145             priorityQueue->capacity = newCapacity;
146         }
147     }
148     LOG("PriorityQueue_Grow: %u, 0x%p \n", newCapacity, priorityQueue->queue);
149 }
150 
151 /**
152  * Inserts the specified element into this priority queue.
153  */
154 void PriorityQueue_Offer(PriorityQueue *priorityQueue, ElemType e)
155 {
156     if (priorityQueue == NULL) return;
157     int i = priorityQueue->size;
158     if (i >= priorityQueue->capacity)
159         PriorityQueue_Grow(priorityQueue, i + 1);
160     if (i >= priorityQueue->capacity) return;
161     priorityQueue->size = i + 1;
162     if (priorityQueue->notifier == NULL)
163         if (i == 0)
164             priorityQueue->queue[0] = e;
165         else
166             PriorityQueue_SiftUp(priorityQueue, i, e);
167     else
168         if (i == 0) {
169             priorityQueue->queue[0] = e;
170             priorityQueue->notifier(e, 0);
171         }
172         else
173             PriorityQueue_SiftUpNotify(priorityQueue, i, e);
174 }
175 
176 ElemType PriorityQueue_Poll(PriorityQueue *priorityQueue)
177 {
178     if (priorityQueue == NULL || priorityQueue->size == 0)
179         return INVALID_ELEM;
180     int s = --priorityQueue->size;
181     ElemType result = priorityQueue->queue[0];
182     ElemType x = priorityQueue->queue[s];
183     if (priorityQueue->notifier != NULL) {
184         if (s != 0)
185             PriorityQueue_SiftDownNotify(priorityQueue, 0, x);
186         priorityQueue->notifier(result, INVALID_INDEX);
187     }
188     else {
189         if (s != 0)
190             PriorityQueue_SiftDown(priorityQueue, 0, x);
191     }
192     return result;
193 }
194 
195 ElemType PriorityQueue_Peek(PriorityQueue *priorityQueue)
196 {
197     return  (priorityQueue == NULL || priorityQueue->size == 0) ?
198             INVALID_ELEM : priorityQueue->queue[0];
199 }
200 
201 int PriorityQueue_IndexOf(const PriorityQueue *priorityQueue, ElemType o)
202 {
203     for (int i = 0; i < priorityQueue->size; ++i)
204         if (o == priorityQueue->queue[i])
205             return i;
206     return INVALID_INDEX;
207 }
208 
209 /**
210  * Removes a single instance of the specified element from this queue,
211  * if it is present.  More formally, removes an element {@code e} such
212  * that {@code o.equals(e)}, if this queue contains one or more such
213  * elements.  Returns {@code true} if and only if this queue contained
214  * the specified element (or equivalently, if this queue changed as a
215  * result of the call).
216  *
217  * @param o element to be removed from this queue, if present
218  * @return {@code true} if this queue changed as a result of the call
219  */
220 bool PriorityQueue_Remove(PriorityQueue *priorityQueue, ElemType o)
221 {
222     if (priorityQueue == NULL) return false;
223     int i = PriorityQueue_IndexOf(priorityQueue, o);
224     if (i == -1) return false;
225     return PriorityQueue_RemoveAt(priorityQueue, i);
226 }
227 
228 /**
229  * Version of remove using reference equality, not equals.
230  * Needed by iterator.remove.
231  *
232  * @param o element to be removed from this queue, if present
233  * @return {@code true} if removed
234  */
235 bool PriorityQueue_RemoveEq(PriorityQueue *priorityQueue, ElemType o)
236 {
237     if (priorityQueue->comparator == NULL) return false;
238     for (int i = 0; i < priorityQueue->size; i++) {
239         if (priorityQueue->comparator(o, priorityQueue->queue[i]) == 0) {
240             PriorityQueue_RemoveAt(priorityQueue, i);
241             return true;
242         }
243     }
244     return false;
245 }
246 
247 /**
248  * Removes the ith element from queue.
249  *
250  * Normally this method leaves the elements at up to i-1,
251  * inclusive, untouched.  Under these circumstances, it returns
252  * null.  Occasionally, in order to maintain the heap invariant,
253  * it must swap a later element of the list with one earlier than
254  * i.  Under these circumstances, this method returns the element
255  * that was previously at the end of the list and is now at some
256  * position before i. This fact is used by iterator.remove so as to
257  * avoid missing traversing elements.
258  */
259 bool PriorityQueue_RemoveAt(PriorityQueue *priorityQueue, int i)
260 {
261     if ((priorityQueue == NULL) || (i < 0) || (i >= priorityQueue->size))
262         return false;
263     int s = --priorityQueue->size;
264     if (s == i) {
265         if (priorityQueue->notifier != NULL)
266             priorityQueue->notifier(priorityQueue->queue[i], INVALID_INDEX);
267         return true;
268     }
269 
270     ElemType moved = priorityQueue->queue[s];
271     if (priorityQueue->notifier != NULL) {
272         priorityQueue->notifier(priorityQueue->queue[i], INVALID_INDEX);
273         PriorityQueue_SiftDownNotify(priorityQueue, i, moved);
274         if (priorityQueue->queue[i] == moved)
275             PriorityQueue_SiftUpNotify(priorityQueue, i, moved);
276     }
277     else {
278         PriorityQueue_SiftDown(priorityQueue, i, moved);
279         if (priorityQueue->queue[i] == moved)
280             PriorityQueue_SiftUp(priorityQueue, i, moved);
281     }
282     return true;
283 }
284 
285 static void PriorityQueue_SiftUpComparable(PriorityQueue *priorityQueue,
286         int k, ElemType x)
287 {
288     while (k > 0)
289     {
290         int parent = (k - 1) >> 1;
291         ElemType e = priorityQueue->queue[parent];
292         if (x >= e) break;
293         priorityQueue->queue[k] = e;
294         k = parent;
295     }
296     priorityQueue->queue[k] = x;
297 }
298 
299 static void PriorityQueue_SiftUpComparableNotify(PriorityQueue *priorityQueue,
300         int k, ElemType x)
301 {
302     while (k > 0)
303     {
304         int parent = (k - 1) >> 1;
305         ElemType e = priorityQueue->queue[parent];
306         if (x >= e) break;
307         priorityQueue->queue[k] = e;
308         priorityQueue->notifier(e, k);
309         k = parent;
310     }
311     priorityQueue->queue[k] = x;
312     priorityQueue->notifier(x, k);
313 }
314 
315 static void PriorityQueue_SiftUpUsingComparator(PriorityQueue *priorityQueue,
316         int k, ElemType x)
317 {
318     while (k > 0)
319     {
320         int parent = (k - 1) >> 1;
321         ElemType e = priorityQueue->queue[parent];
322         if (priorityQueue->comparator(x, e) >= 0) break;
323         priorityQueue->queue[k] = e;
324         k = parent;
325     }
326     priorityQueue->queue[k] = x;
327 }
328 
329 static void PriorityQueue_SiftUpUsingComparatorNotify(PriorityQueue *priorityQueue,
330         int k, ElemType x)
331 {
332     while (k > 0)
333     {
334         int parent = (k - 1) >> 1;
335         ElemType e = priorityQueue->queue[parent];
336         if (priorityQueue->comparator(x, e) >= 0) break;
337         priorityQueue->queue[k] = e;
338         priorityQueue->notifier(e, k);
339         k = parent;
340     }
341     priorityQueue->queue[k] = x;
342     priorityQueue->notifier(x, k);
343 }
344 
345 /**
346  * Inserts item x at position k, maintaining heap invariant by
347  * promoting x up the tree until it is greater than or equal to
348  * its parent, or is the root.
349  *
350  * To simplify and speed up coercions and comparisons. the
351  * Comparable and Comparator versions are separated into different
352  * methods that are otherwise identical. (Similarly for siftDown.)
353  *
354  * @param k the position to fill
355  * @param x the item to insert
356  */
357 static void PriorityQueue_SiftUp(PriorityQueue *priorityQueue, int k, ElemType x)
358 {
359     if (priorityQueue->comparator != NULL)
360         PriorityQueue_SiftUpUsingComparator(priorityQueue, k, x);
361     else
362         PriorityQueue_SiftUpComparable(priorityQueue, k, x);
363 }
364 
365 static void PriorityQueue_SiftUpNotify(PriorityQueue *priorityQueue, int k, ElemType x)
366 {
367     if (priorityQueue->comparator != NULL)
368         PriorityQueue_SiftUpUsingComparatorNotify(priorityQueue, k, x);
369     else
370         PriorityQueue_SiftUpComparableNotify(priorityQueue, k, x);
371 }
372 
373 static void PriorityQueue_SiftDownComparable(PriorityQueue *priorityQueue,
374         int k, ElemType x) {
375     int half = priorityQueue->size >> 1;        // loop while a non-leaf
376     while (k < half) {
377         int child = (k << 1) + 1; // assume left child is least
378         ElemType c = priorityQueue->queue[child];
379         int right = child + 1;
380         if (right < priorityQueue->size &&
381             c > priorityQueue->queue[right])
382             c = priorityQueue->queue[child = right];
383         if (x <= c)
384             break;
385         priorityQueue->queue[k] = c;
386         k = child;
387     }
388     priorityQueue->queue[k] = x;
389 }
390 
391 static void PriorityQueue_SiftDownComparableNotify(PriorityQueue *priorityQueue,
392         int k, ElemType x) {
393     int half = priorityQueue->size >> 1;        // loop while a non-leaf
394     while (k < half) {
395         int child = (k << 1) + 1; // assume left child is least
396         ElemType c = priorityQueue->queue[child];
397         int right = child + 1;
398         if (right < priorityQueue->size &&
399             c > priorityQueue->queue[right])
400             c = priorityQueue->queue[child = right];
401         if (x <= c)
402             break;
403         priorityQueue->queue[k] = c;
404         priorityQueue->notifier(c, k);
405         k = child;
406     }
407     priorityQueue->queue[k] = x;
408     priorityQueue->notifier(x, k);
409 }
410 
411 static void PriorityQueue_SiftDownUsingComparator(PriorityQueue *priorityQueue,
412         int k, ElemType x)
413 {
414     int half = priorityQueue->size >> 1;
415     while (k < half) {
416         int child = (k << 1) + 1;
417         ElemType c = priorityQueue->queue[child];
418         int right = child + 1;
419         if (right < priorityQueue->size &&
420             priorityQueue->comparator(c, priorityQueue->queue[right]) > 0)
421             c = priorityQueue->queue[child = right];
422         if (priorityQueue->comparator(x, c) <= 0)
423             break;
424         priorityQueue->queue[k] = c;
425         k = child;
426     }
427     priorityQueue->queue[k] = x;
428 }
429 
430 static void PriorityQueue_SiftDownUsingComparatorNotify(PriorityQueue *priorityQueue,
431         int k, ElemType x)
432 {
433     int half = priorityQueue->size >> 1;
434     while (k < half) {
435         int child = (k << 1) + 1;
436         ElemType c = priorityQueue->queue[child];
437         int right = child + 1;
438         if (right < priorityQueue->size &&
439             priorityQueue->comparator(c, priorityQueue->queue[right]) > 0)
440             c = priorityQueue->queue[child = right];
441         if (priorityQueue->comparator(x, c) <= 0)
442             break;
443         priorityQueue->queue[k] = c;
444         priorityQueue->notifier(c, k);
445         k = child;
446     }
447     priorityQueue->queue[k] = x;
448     priorityQueue->notifier(x, k);
449 }
450 
451 /**
452  * Inserts item x at position k, maintaining heap invariant by
453  * demoting x down the tree repeatedly until it is less than or
454  * equal to its children or is a leaf.
455  *
456  * @param k the position to fill
457  * @param x the item to insert
458  */
459 static void PriorityQueue_SiftDown(PriorityQueue *priorityQueue, int k, ElemType x)
460 {
461     if (priorityQueue->comparator != NULL)
462         PriorityQueue_SiftDownUsingComparator(priorityQueue, k, x);
463     else
464         PriorityQueue_SiftDownComparable(priorityQueue, k, x);
465 }
466 
467 static void PriorityQueue_SiftDownNotify(PriorityQueue *priorityQueue, int k, ElemType x)
468 {
469     if (priorityQueue->comparator != NULL)
470         PriorityQueue_SiftDownUsingComparatorNotify(priorityQueue, k, x);
471     else
472         PriorityQueue_SiftDownComparableNotify(priorityQueue, k, x);
473 }
474 
475 /**
476  * Establishes the heap invariant (described above) in the entire tree,
477  * assuming nothing about the order of the elements prior to the call.
478  */
479 static void PriorityQueue_Heapify(PriorityQueue *priorityQueue)
480 {
481     for (int i = (priorityQueue->size >> 1) - 1; i >= 0; i--)
482         PriorityQueue_SiftDown(priorityQueue, i, priorityQueue->queue[i]);
483 }
484 
485 static void PriorityQueue_HeapifyNotify(PriorityQueue *priorityQueue)
486 {
487     for (int i = (priorityQueue->size >> 1) - 1; i >= 0; i--)
488         PriorityQueue_SiftDownNotify(priorityQueue, i, priorityQueue->queue[i]);
489 }

 

3. test_priority_queue.hpp

  1 #include "gtest.h"
  2 #include "../src/priority_queue.h"
  3 
  4 #define ARRAY_SIZE(arr) (int)(sizeof(arr)/sizeof(arr[0]))
  5 
  6 // 默认创建
  7 TEST(testPriorityQueue, create_default)
  8 {
  9     PriorityQueue _queue, *queue = &_queue;
 10     ConstructPriorityQueueDefault(queue);
 11 
 12     ASSERT_TRUE(PriorityQueue_IsEmpty(queue));
 13     ASSERT_TRUE(PriorityQueue_Size(queue) == 0);
 14 
 15     DestructPriorityQueue(queue);
 16 }
 17 
 18 // 使用数组创建
 19 TEST(testPriorityQueue, create_with_array)
 20 {
 21     const ElemType array[]     = { 1, 3, 8, 4, 6, 2, 0, 7, 9, 5};
 22     const ElemType sortedArr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 23     PriorityQueue _queue, *queue = &_queue;
 24     ConstructPriorityQueueArrayDefault(queue, array, ARRAY_SIZE(array));
 25 
 26     ASSERT_TRUE(PriorityQueue_Size(queue) == ARRAY_SIZE(array));
 27 
 28     for (int i = 0; i < ARRAY_SIZE(array); ++i)
 29     {
 30         ASSERT_TRUE(sortedArr[i] == PriorityQueue_Peek(queue));
 31         ASSERT_TRUE(sortedArr[i] == PriorityQueue_Poll(queue));
 32         ASSERT_TRUE(PriorityQueue_Size(queue) == ARRAY_SIZE(array) - i - 1);
 33     }
 34 
 35     ASSERT_TRUE(INVALID_ELEM == PriorityQueue_Peek(queue));
 36     ASSERT_TRUE(INVALID_ELEM == PriorityQueue_Poll(queue));
 37     ASSERT_TRUE(PriorityQueue_Size(queue) == 0);
 38 
 39     DestructPriorityQueue(queue);
 40 
 41     ASSERT_TRUE(PriorityQueue_IsEmpty(queue));
 42     ASSERT_TRUE(PriorityQueue_Size(queue) == 0);
 43 }
 44 
 45 TEST(testPriorityQueue, offer)
 46 {
 47     const ElemType array[]      = { 1, 3, 8, 4, 6, 2, 0, 7, 9, 5};
 48     const ElemType sortedTemp[] = { 1, 1, 1, 1, 1, 1, 0, 0, 0, 0};
 49     const ElemType sortedArr[]  = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 50     PriorityQueue _queue, *queue = &_queue;
 51     ConstructPriorityQueueDefault(queue);
 52 
 53     for (int i = 0; i < ARRAY_SIZE(array); ++i)
 54     {
 55         PriorityQueue_Offer(queue, array[i]);
 56         ASSERT_TRUE(PriorityQueue_Size(queue) == i + 1);
 57         ASSERT_TRUE(sortedTemp[i] == PriorityQueue_Peek(queue));
 58     }
 59 
 60     ASSERT_TRUE(PriorityQueue_Size(queue) == ARRAY_SIZE(array));
 61 
 62     for (int i = 0; i < ARRAY_SIZE(array); ++i)
 63     {
 64         ASSERT_TRUE(sortedArr[i] == PriorityQueue_Peek(queue));
 65         ASSERT_TRUE(sortedArr[i] == PriorityQueue_Poll(queue));
 66         ASSERT_TRUE(PriorityQueue_Size(queue) == ARRAY_SIZE(array) - i - 1);
 67     }
 68 
 69     ASSERT_TRUE(INVALID_ELEM == PriorityQueue_Peek(queue));
 70     ASSERT_TRUE(INVALID_ELEM == PriorityQueue_Poll(queue));
 71     ASSERT_TRUE(PriorityQueue_Size(queue) == 0);
 72 
 73     DestructPriorityQueue(queue);
 74 
 75     ASSERT_TRUE(PriorityQueue_IsEmpty(queue));
 76     ASSERT_TRUE(PriorityQueue_Size(queue) == 0);
 77 }
 78 
 79 TEST(testPriorityQueue, offer2)
 80 {
 81     const ElemType array[]      = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
 82     const ElemType sortedTemp[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
 83     const ElemType sortedArr[]  = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 84     PriorityQueue _queue, *queue = &_queue;
 85     ConstructPriorityQueueDefault(queue);
 86 
 87     for (int i = 0; i < ARRAY_SIZE(array); ++i)
 88     {
 89         PriorityQueue_Offer(queue, array[i]);
 90         ASSERT_TRUE(PriorityQueue_Size(queue) == i + 1);
 91         ASSERT_TRUE(sortedTemp[i] == PriorityQueue_Peek(queue));
 92     }
 93 
 94     ASSERT_TRUE(PriorityQueue_Size(queue) == ARRAY_SIZE(array));
 95 
 96     for (int i = 0; i < ARRAY_SIZE(array); ++i)
 97     {
 98         ASSERT_TRUE(sortedArr[i] == PriorityQueue_Peek(queue));
 99         ASSERT_TRUE(sortedArr[i] == PriorityQueue_Poll(queue));
100         ASSERT_TRUE(PriorityQueue_Size(queue) == ARRAY_SIZE(array) - i - 1);
101     }
102 
103     ASSERT_TRUE(INVALID_ELEM == PriorityQueue_Peek(queue));
104     ASSERT_TRUE(INVALID_ELEM == PriorityQueue_Poll(queue));
105     ASSERT_TRUE(PriorityQueue_Size(queue) == 0);
106 
107     DestructPriorityQueue(queue);
108 
109     ASSERT_TRUE(PriorityQueue_IsEmpty(queue));
110     ASSERT_TRUE(PriorityQueue_Size(queue) == 0);
111 }
112 
113 #define MAX(a, b) ((a) > (b) ? (a) : (b))
114 #define MIN(a, b) ((a) < (b) ? (a) : (b))
115 TEST(testPriorityQueue, large_array)
116 {
117     const ElemType array[1000] = { 193, 60, 342, 590, 648, 128, 496, 222, 687, 243, 449, 489, 482, 626, 187, 21, 626, 432, 151, 467, 1, 170, 495, 548, 448, 419, 264, 426, 310, 168, 630, 97, 398, 267, 527, 306, 684, 390, 621, 319, 34, 161, 198, 479, 456, 179, 54, 48, 285, 402, 54, 315, 412, 205, 571, 363, 197, 566, 87, 239, 471, 271, 314, 108, 595, 109, 102, 539, 102, 316, 638, 171, 272, 288, 138, 52, 360, 494, 382, 143, 128, 214, 516, 251, 632, 343, 355, 607, 667, 148, 594, 442, 510, 84, 432, 427, 168, 242, 609, 2, 135, 665, 295, 38, 396, 322, 92, 343, 689, 67, 106, 250, 289, 699, 675, 184, 351, 398, 216, 38, 9, 41, 249, 338, 457, 358, 75, 40, 577, 634, 407, 77, 473, 588, 471, 253, 299, 691, 477, 295, 3, 92, 671, 501, 311, 496, 569, 501, 379, 242, 65, 3, 369, 356, 485, 204, 232, 267, 34, 390, 301, 321, 14, 408, 527, 323, 315, 434, 331, 492, 426, 268, 413, 52, 695, 123, 288, 235, 168, 589, 414, 471, 569, 604, 589, 388, 384, 61, 301, 4, 223, 364, 591, 93, 545, 574, 79, 494, 332, 578, 264, 584, 130, 210, 180, 398, 403, 605, 409, 28, 655, 641, 300, 390, 603, 539, 624, 438, 72, 629, 7, 6, 509, 598, 327, 283, 293, 259, 589, 69, 606, 345, 518, 696, 478, 33, 317, 462, 583, 44, 634, 236, 321, 276, 538, 555, 126, 659, 276, 521, 396, 677, 79, 195, 185, 41, 237, 599, 157, 272, 489, 212, 215, 690, 38, 96, 601, 336, 97, 240, 336, 460, 682, 102, 544, 65, 338, 418, 51, 515, 71, 150, 665, 644, 698, 470, 168, 678, 676, 274, 103, 16, 272, 317, 134, 576, 511, 473, 189, 49, 27, 233, 491, 79, 14, 298, 457, 463, 44, 31, 70, 278, 633, 207, 230, 484, 667, 94, 414, 438, 2, 90, 433, 63, 540, 363, 366, 457, 443, 473, 361, 334, 189, 236, 685, 679, 225, 318, 203, 93, 422, 278, 120, 194, 531, 152, 430, 180, 14, 448, 117, 488, 513, 377, 119, 163, 65, 694, 270, 267, 579, 99, 342, 596, 632, 181, 142, 411, 161, 226, 399, 255, 145, 292, 627, 416, 434, 377, 553, 75, 630, 619, 156, 636, 456, 352, 229, 2, 54, 472, 63, 545, 213, 464, 662, 46, 381, 198, 265, 156, 663, 425, 313, 585, 646, 152, 175, 295, 236, 461, 371, 162, 369, 126, 7, 6, 486, 184, 542, 69, 568, 598, 486, 283, 279, 66, 279, 296, 334, 336, 184, 339, 132, 50, 150, 547, 685, 332, 554, 580, 442, 657, 333, 495, 147, 152, 162, 70, 675, 288, 171, 625, 10, 429, 353, 440, 561, 133, 60, 69, 340, 31, 315, 187, 476, 124, 535, 38, 629, 119, 420, 554, 401, 593, 52, 164, 296, 197, 171, 367, 526, 277, 350, 83, 422, 66, 109, 691, 59, 153, 373, 434, 62, 317, 385, 509, 359, 174, 154, 41, 654, 29, 467, 446, 38, 517, 285, 521, 329, 101, 374, 311, 169, 691, 310, 123, 562, 449, 221, 379, 551, 564, 290, 1, 54, 333, 407, 347, 259, 42, 469, 209, 280, 401, 688, 572, 276, 28, 195, 606, 553, 262, 96, 560, 318, 647, 381, 481, 267, 184, 372, 273, 461, 411, 637, 201, 428, 563, 505, 388, 571, 510, 644, 504, 453, 589, 436, 649, 7, 477, 333, 213, 96, 382, 188, 13, 438, 239, 247, 420, 519, 512, 553, 125, 472, 219, 373, 368, 181, 1, 101, 10, 348, 363, 643, 677, 647, 69, 315, 667, 372, 11, 329, 182, 4, 433, 622, 558, 642, 687, 350, 354, 406, 431, 551, 401, 379, 469, 503, 170, 54, 467, 383, 480, 615, 319, 515, 110, 420, 493, 597, 659, 441, 677, 254, 456, 228, 234, 171, 607, 156, 305, 634, 622, 84, 626, 510, 220, 386, 459, 382, 75, 263, 501, 534, 174, 53, 517, 158, 403, 216, 35, 536, 609, 273, 421, 618, 404, 166, 604, 186, 203, 546, 601, 166, 94, 74, 628, 215, 460, 426, 394, 344, 618, 277, 284, 591, 506, 242, 489, 558, 294, 477, 458, 348, 171, 431, 247, 385, 415, 196, 288, 292, 682, 483, 581, 588, 671, 296, 633, 668, 666, 487, 439, 140, 82, 242, 294, 499, 409, 236, 69, 63, 116, 387, 336, 253, 10, 29, 429, 433, 410, 114, 469, 252, 464, 453, 331, 204, 358, 360, 421, 459, 107, 269, 343, 70, 241, 287, 456, 355, 669, 678, 414, 697, 586, 343, 421, 525, 357, 536, 227, 330, 59, 588, 103, 496, 539, 345, 99, 381, 75, 424, 572, 681, 59, 611, 584, 180, 89, 666, 215, 541, 525, 275, 134, 163, 469, 627, 480, 289, 354, 40, 46, 57, 107, 592, 671, 479, 118, 110, 292, 264, 229, 676, 152, 16, 351, 347, 507, 152, 624, 646, 439, 585, 470, 170, 53, 587, 301, 62, 121, 422, 589, 156, 675, 548, 30, 248, 284, 32, 40, 163, 128, 626, 595, 687, 212, 238, 155, 106, 216, 242, 469, 693, 621, 294, 74, 142, 601, 126, 193, 451, 545, 261, 208, 26, 148, 169, 96, 32, 605, 168, 426, 82, 12, 410, 212, 235, 62, 457, 683, 53, 364, 50, 44, 403, 458, 649, 174, 619, 39, 65, 607, 421, 243, 670, 549, 307, 388, 45, 353, 517, 370, 111, 127, 269, 357, 471, 565, 390, 522, 541, 488, 560, 628, 419, 182, 300, 591, 498, 241, 143, 117, 4, 608, 454, 233, 301, 541, 275, 305, 400, 1, 66, 271, 358, 603, 72, 324, 573, 574, 246, 444, 595, 310, 618, 465, 309, 289, 256, 600, 313, 646, 562, 354, 118, 657, 454, 401, 416, 652, 255, 157, 109, 79, 576, 562, 172, 663, 375, 588, 237, 320, 395, 595, 439, 319, 273, 523, 457, 140, 506, 455, 300, 528, 578, 310, 336, 674, 3, 367, 581, 647, 307, 189, 125, 133, 559, 401, 610, 293, 108, 690, 592, 449, 130, 23, 1, 198};
118     const ElemType sortedArr[700] = { 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 6, 6, 7, 7, 7, 10, 12, 14, 14, 14, 16, 16, 23, 26, 28, 29, 29, 30, 31, 31, 32, 32, 34, 34, 35, 38, 38, 38, 38, 40, 40, 41, 41, 44, 46, 48, 50, 52, 53, 53, 53, 54, 54, 54, 54, 54, 59, 59, 59, 60, 60, 62, 63, 65, 65, 65, 65, 66, 66, 66, 69, 69, 69, 69, 70, 70, 71, 72, 72, 74, 74, 75, 75, 75, 75, 77, 79, 79, 83, 84, 84, 87, 92, 92, 93, 93, 94, 96, 96, 96, 97, 97, 99, 99, 101, 101, 102, 102, 102, 103, 106, 106, 107, 107, 109, 109, 109, 110, 110, 114, 116, 117, 118, 119, 119, 120, 121, 123, 123, 125, 125, 126, 130, 130, 132, 133, 133, 134, 140, 142, 142, 143, 145, 148, 148, 151, 152, 152, 152, 156, 156, 157, 157, 161, 161, 163, 163, 164, 166, 168, 168, 168, 168, 168, 169, 169, 170, 170, 171, 171, 171, 171, 171, 172, 174, 174, 174, 175, 179, 180, 180, 180, 181, 182, 182, 184, 184, 184, 184, 185, 186, 187, 187, 188, 189, 189, 193, 193, 195, 195, 197, 198, 198, 198, 204, 204, 210, 212, 212, 213, 213, 214, 215, 215, 216, 216, 219, 220, 221, 223, 225, 226, 228, 229, 233, 233, 235, 235, 236, 236, 236, 237, 237, 239, 239, 240, 241, 241, 242, 242, 247, 247, 250, 251, 252, 254, 255, 259, 259, 263, 264, 264, 265, 267, 267, 267, 267, 268, 269, 269, 272, 272, 272, 273, 273, 273, 274, 275, 275, 276, 276, 276, 277, 278, 278, 279, 279, 283, 284, 285, 287, 288, 288, 288, 288, 289, 289, 292, 292, 292, 293, 294, 294, 294, 295, 295, 296, 296, 296, 298, 300, 300, 301, 301, 301, 301, 305, 305, 306, 307, 310, 310, 310, 310, 311, 311, 314, 315, 315, 315, 315, 316, 317, 317, 318, 318, 319, 319, 319, 320, 321, 323, 329, 329, 332, 332, 333, 333, 333, 334, 334, 336, 336, 336, 336, 338, 339, 340, 342, 342, 343, 343, 343, 345, 345, 348, 348, 350, 350, 351, 351, 353, 354, 355, 355, 357, 358, 358, 358, 360, 360, 361, 363, 363, 363, 367, 367, 369, 369, 372, 372, 373, 374, 375, 377, 377, 379, 379, 381, 381, 381, 382, 382, 382, 383, 385, 385, 388, 388, 388, 390, 390, 390, 394, 395, 396, 398, 398, 398, 399, 401, 401, 401, 401, 401, 402, 403, 403, 407, 407, 408, 409, 409, 410, 410, 411, 411, 412, 414, 414, 415, 416, 418, 419, 420, 420, 420, 421, 421, 422, 422, 422, 425, 426, 426, 426, 426, 429, 429, 430, 432, 433, 433, 434, 434, 434, 438, 439, 439, 439, 440, 442, 442, 443, 446, 448, 448, 449, 449, 449, 454, 455, 456, 456, 456, 456, 457, 457, 457, 457, 457, 458, 459, 459, 460, 461, 462, 464, 465, 467, 467, 467, 469, 469, 469, 469, 470, 471, 471, 471, 471, 472, 472, 473, 473, 477, 477, 477, 479, 479, 480, 480, 482, 484, 486, 488, 488, 489, 489, 489, 494, 494, 495, 496, 496, 498, 501, 501, 503, 505, 506, 506, 509, 509, 510, 510, 512, 513, 515, 515, 516, 517, 517, 518, 519, 521, 521, 525, 525, 526, 527, 536, 536, 538, 539, 539, 539, 540, 541, 545, 545, 545, 546, 547, 548, 551, 551, 553, 553, 554, 554, 558, 561, 562, 562, 562, 564, 568, 569, 569, 571, 571, 572, 572, 574, 576, 578, 578, 579, 580, 584, 584, 585, 588, 588, 588, 589, 589, 589, 589, 590, 591, 591, 591, 592, 592, 593, 595, 595, 595, 595, 596, 598, 599, 601, 601, 601, 603, 603, 605, 605, 606, 607, 607, 608, 609, 609, 610, 611, 618, 618, 621, 621, 622, 622, 626, 626, 626, 627, 627, 628, 629, 629, 630, 630, 632, 632, 633, 634, 634, 637, 638, 641, 642, 643, 644, 646, 647, 647, 648, 649, 654, 655, 657, 657, 659, 659, 662, 663, 665, 667, 667, 667, 668, 669, 671, 671, 671, 675, 675, 676, 676, 677, 679, 682, 683, 684, 687, 689, 690, 691, 691, 691, 695, 697, 698};
119     const ElemType moveDataArr[300] = { 534, 624, 196, 364, 9, 441, 203, 560, 82, 460, 293, 44, 413, 606, 674, 227, 553, 414, 693, 566, 670, 230, 322, 597, 44, 615, 209, 542, 487, 451, 150, 261, 264, 201, 255, 158, 248, 528, 559, 685, 212, 685, 229, 354, 270, 289, 444, 586, 103, 587, 242, 353, 499, 652, 108, 11, 285, 541, 549, 373, 126, 82, 27, 666, 63, 41, 678, 558, 354, 126, 600, 501, 390, 189, 492, 433, 438, 555, 581, 79, 675, 541, 232, 678, 309, 427, 156, 94, 563, 271, 21, 134, 626, 454, 205, 330, 646, 396, 677, 598, 624, 696, 143, 234, 594, 300, 483, 89, 432, 313, 585, 347, 127, 344, 576, 431, 216, 67, 207, 215, 28, 495, 128, 170, 108, 290, 665, 544, 436, 581, 384, 321, 324, 565, 368, 203, 166, 386, 152, 619, 589, 242, 135, 507, 124, 560, 154, 469, 588, 522, 57, 517, 52, 633, 138, 63, 634, 256, 50, 336, 663, 343, 208, 504, 481, 111, 128, 476, 371, 152, 419, 327, 249, 607, 236, 253, 403, 461, 464, 299, 13, 453, 473, 62, 352, 42, 424, 140, 243, 681, 153, 687, 379, 147, 162, 646, 458, 421, 33, 421, 416, 535, 649, 69, 370, 155, 583, 295, 531, 331, 118, 283, 359, 262, 347, 46, 10, 277, 10, 682, 628, 428, 96, 574, 527, 496, 688, 644, 647, 493, 45, 70, 438, 194, 453, 491, 117, 687, 253, 197, 331, 181, 90, 604, 243, 463, 156, 51, 604, 222, 470, 280, 79, 162, 548, 619, 690, 523, 242, 366, 478, 431, 246, 699, 40, 573, 271, 618, 150, 364, 49, 313, 636, 625, 39, 511, 356, 400, 163, 486, 62, 387, 577, 128, 357, 677, 238, 307, 406, 666, 61, 317, 694, 38, 404, 485, 284, 510, 338, 52};
120     ElemType minData = 0x7FFFFFFF;
121     PriorityQueue _queue, *queue = &_queue;
122     ConstructPriorityQueueDefault(queue);
123 
124     for (int i = 0; i < ARRAY_SIZE(array); ++i)
125     {
126         PriorityQueue_Offer(queue, array[i]);
127         ASSERT_TRUE(PriorityQueue_Size(queue) == i + 1);
128         ASSERT_TRUE((minData = MIN(minData, array[i])) == PriorityQueue_Peek(queue));
129     }
130 
131     for (int i = 0; i < ARRAY_SIZE(moveDataArr); ++i)
132     {
133         PriorityQueue_Remove(queue, moveDataArr[i]);
134         ASSERT_TRUE(PriorityQueue_Size(queue) == ARRAY_SIZE(array) - i - 1);
135     }
136 
137     ASSERT_TRUE(PriorityQueue_Size(queue) == ARRAY_SIZE(sortedArr));
138 
139     for (int i = 0; i < ARRAY_SIZE(sortedArr); ++i)
140     {
141         ASSERT_TRUE(sortedArr[i] == PriorityQueue_Peek(queue));
142         ASSERT_TRUE(sortedArr[i] == PriorityQueue_Poll(queue));
143         ASSERT_TRUE(PriorityQueue_Size(queue) == ARRAY_SIZE(sortedArr) - i - 1);
144     }
145 
146     ASSERT_TRUE(INVALID_ELEM == PriorityQueue_Peek(queue));
147     ASSERT_TRUE(INVALID_ELEM == PriorityQueue_Poll(queue));
148     ASSERT_TRUE(PriorityQueue_Size(queue) == 0);
149 
150     DestructPriorityQueue(queue);
151 
152     ASSERT_TRUE(PriorityQueue_IsEmpty(queue));
153     ASSERT_TRUE(PriorityQueue_Size(queue) == 0);
154 }
155 
156 static int compare_int(const ElemType a, const ElemType b)
157 {
158     if (a > b) return -1;
159     if (a < b) return 1;
160     return 0;
161 }
162 
163 TEST(testPriorityQueue, large_array_using_comparator)
164 {
165     const ElemType array[1000] = { 193, 60, 342, 590, 648, 128, 496, 222, 687, 243, 449, 489, 482, 626, 187, 21, 626, 432, 151, 467, 1, 170, 495, 548, 448, 419, 264, 426, 310, 168, 630, 97, 398, 267, 527, 306, 684, 390, 621, 319, 34, 161, 198, 479, 456, 179, 54, 48, 285, 402, 54, 315, 412, 205, 571, 363, 197, 566, 87, 239, 471, 271, 314, 108, 595, 109, 102, 539, 102, 316, 638, 171, 272, 288, 138, 52, 360, 494, 382, 143, 128, 214, 516, 251, 632, 343, 355, 607, 667, 148, 594, 442, 510, 84, 432, 427, 168, 242, 609, 2, 135, 665, 295, 38, 396, 322, 92, 343, 689, 67, 106, 250, 289, 699, 675, 184, 351, 398, 216, 38, 9, 41, 249, 338, 457, 358, 75, 40, 577, 634, 407, 77, 473, 588, 471, 253, 299, 691, 477, 295, 3, 92, 671, 501, 311, 496, 569, 501, 379, 242, 65, 3, 369, 356, 485, 204, 232, 267, 34, 390, 301, 321, 14, 408, 527, 323, 315, 434, 331, 492, 426, 268, 413, 52, 695, 123, 288, 235, 168, 589, 414, 471, 569, 604, 589, 388, 384, 61, 301, 4, 223, 364, 591, 93, 545, 574, 79, 494, 332, 578, 264, 584, 130, 210, 180, 398, 403, 605, 409, 28, 655, 641, 300, 390, 603, 539, 624, 438, 72, 629, 7, 6, 509, 598, 327, 283, 293, 259, 589, 69, 606, 345, 518, 696, 478, 33, 317, 462, 583, 44, 634, 236, 321, 276, 538, 555, 126, 659, 276, 521, 396, 677, 79, 195, 185, 41, 237, 599, 157, 272, 489, 212, 215, 690, 38, 96, 601, 336, 97, 240, 336, 460, 682, 102, 544, 65, 338, 418, 51, 515, 71, 150, 665, 644, 698, 470, 168, 678, 676, 274, 103, 16, 272, 317, 134, 576, 511, 473, 189, 49, 27, 233, 491, 79, 14, 298, 457, 463, 44, 31, 70, 278, 633, 207, 230, 484, 667, 94, 414, 438, 2, 90, 433, 63, 540, 363, 366, 457, 443, 473, 361, 334, 189, 236, 685, 679, 225, 318, 203, 93, 422, 278, 120, 194, 531, 152, 430, 180, 14, 448, 117, 488, 513, 377, 119, 163, 65, 694, 270, 267, 579, 99, 342, 596, 632, 181, 142, 411, 161, 226, 399, 255, 145, 292, 627, 416, 434, 377, 553, 75, 630, 619, 156, 636, 456, 352, 229, 2, 54, 472, 63, 545, 213, 464, 662, 46, 381, 198, 265, 156, 663, 425, 313, 585, 646, 152, 175, 295, 236, 461, 371, 162, 369, 126, 7, 6, 486, 184, 542, 69, 568, 598, 486, 283, 279, 66, 279, 296, 334, 336, 184, 339, 132, 50, 150, 547, 685, 332, 554, 580, 442, 657, 333, 495, 147, 152, 162, 70, 675, 288, 171, 625, 10, 429, 353, 440, 561, 133, 60, 69, 340, 31, 315, 187, 476, 124, 535, 38, 629, 119, 420, 554, 401, 593, 52, 164, 296, 197, 171, 367, 526, 277, 350, 83, 422, 66, 109, 691, 59, 153, 373, 434, 62, 317, 385, 509, 359, 174, 154, 41, 654, 29, 467, 446, 38, 517, 285, 521, 329, 101, 374, 311, 169, 691, 310, 123, 562, 449, 221, 379, 551, 564, 290, 1, 54, 333, 407, 347, 259, 42, 469, 209, 280, 401, 688, 572, 276, 28, 195, 606, 553, 262, 96, 560, 318, 647, 381, 481, 267, 184, 372, 273, 461, 411, 637, 201, 428, 563, 505, 388, 571, 510, 644, 504, 453, 589, 436, 649, 7, 477, 333, 213, 96, 382, 188, 13, 438, 239, 247, 420, 519, 512, 553, 125, 472, 219, 373, 368, 181, 1, 101, 10, 348, 363, 643, 677, 647, 69, 315, 667, 372, 11, 329, 182, 4, 433, 622, 558, 642, 687, 350, 354, 406, 431, 551, 401, 379, 469, 503, 170, 54, 467, 383, 480, 615, 319, 515, 110, 420, 493, 597, 659, 441, 677, 254, 456, 228, 234, 171, 607, 156, 305, 634, 622, 84, 626, 510, 220, 386, 459, 382, 75, 263, 501, 534, 174, 53, 517, 158, 403, 216, 35, 536, 609, 273, 421, 618, 404, 166, 604, 186, 203, 546, 601, 166, 94, 74, 628, 215, 460, 426, 394, 344, 618, 277, 284, 591, 506, 242, 489, 558, 294, 477, 458, 348, 171, 431, 247, 385, 415, 196, 288, 292, 682, 483, 581, 588, 671, 296, 633, 668, 666, 487, 439, 140, 82, 242, 294, 499, 409, 236, 69, 63, 116, 387, 336, 253, 10, 29, 429, 433, 410, 114, 469, 252, 464, 453, 331, 204, 358, 360, 421, 459, 107, 269, 343, 70, 241, 287, 456, 355, 669, 678, 414, 697, 586, 343, 421, 525, 357, 536, 227, 330, 59, 588, 103, 496, 539, 345, 99, 381, 75, 424, 572, 681, 59, 611, 584, 180, 89, 666, 215, 541, 525, 275, 134, 163, 469, 627, 480, 289, 354, 40, 46, 57, 107, 592, 671, 479, 118, 110, 292, 264, 229, 676, 152, 16, 351, 347, 507, 152, 624, 646, 439, 585, 470, 170, 53, 587, 301, 62, 121, 422, 589, 156, 675, 548, 30, 248, 284, 32, 40, 163, 128, 626, 595, 687, 212, 238, 155, 106, 216, 242, 469, 693, 621, 294, 74, 142, 601, 126, 193, 451, 545, 261, 208, 26, 148, 169, 96, 32, 605, 168, 426, 82, 12, 410, 212, 235, 62, 457, 683, 53, 364, 50, 44, 403, 458, 649, 174, 619, 39, 65, 607, 421, 243, 670, 549, 307, 388, 45, 353, 517, 370, 111, 127, 269, 357, 471, 565, 390, 522, 541, 488, 560, 628, 419, 182, 300, 591, 498, 241, 143, 117, 4, 608, 454, 233, 301, 541, 275, 305, 400, 1, 66, 271, 358, 603, 72, 324, 573, 574, 246, 444, 595, 310, 618, 465, 309, 289, 256, 600, 313, 646, 562, 354, 118, 657, 454, 401, 416, 652, 255, 157, 109, 79, 576, 562, 172, 663, 375, 588, 237, 320, 395, 595, 439, 319, 273, 523, 457, 140, 506, 455, 300, 528, 578, 310, 336, 674, 3, 367, 581, 647, 307, 189, 125, 133, 559, 401, 610, 293, 108, 690, 592, 449, 130, 23, 1, 198};
166     const ElemType sortedArr[700] = { 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 6, 6, 7, 7, 7, 10, 12, 14, 14, 14, 16, 16, 23, 26, 28, 29, 29, 30, 31, 31, 32, 32, 34, 34, 35, 38, 38, 38, 38, 40, 40, 41, 41, 44, 46, 48, 50, 52, 53, 53, 53, 54, 54, 54, 54, 54, 59, 59, 59, 60, 60, 62, 63, 65, 65, 65, 65, 66, 66, 66, 69, 69, 69, 69, 70, 70, 71, 72, 72, 74, 74, 75, 75, 75, 75, 77, 79, 79, 83, 84, 84, 87, 92, 92, 93, 93, 94, 96, 96, 96, 97, 97, 99, 99, 101, 101, 102, 102, 102, 103, 106, 106, 107, 107, 109, 109, 109, 110, 110, 114, 116, 117, 118, 119, 119, 120, 121, 123, 123, 125, 125, 126, 130, 130, 132, 133, 133, 134, 140, 142, 142, 143, 145, 148, 148, 151, 152, 152, 152, 156, 156, 157, 157, 161, 161, 163, 163, 164, 166, 168, 168, 168, 168, 168, 169, 169, 170, 170, 171, 171, 171, 171, 171, 172, 174, 174, 174, 175, 179, 180, 180, 180, 181, 182, 182, 184, 184, 184, 184, 185, 186, 187, 187, 188, 189, 189, 193, 193, 195, 195, 197, 198, 198, 198, 204, 204, 210, 212, 212, 213, 213, 214, 215, 215, 216, 216, 219, 220, 221, 223, 225, 226, 228, 229, 233, 233, 235, 235, 236, 236, 236, 237, 237, 239, 239, 240, 241, 241, 242, 242, 247, 247, 250, 251, 252, 254, 255, 259, 259, 263, 264, 264, 265, 267, 267, 267, 267, 268, 269, 269, 272, 272, 272, 273, 273, 273, 274, 275, 275, 276, 276, 276, 277, 278, 278, 279, 279, 283, 284, 285, 287, 288, 288, 288, 288, 289, 289, 292, 292, 292, 293, 294, 294, 294, 295, 295, 296, 296, 296, 298, 300, 300, 301, 301, 301, 301, 305, 305, 306, 307, 310, 310, 310, 310, 311, 311, 314, 315, 315, 315, 315, 316, 317, 317, 318, 318, 319, 319, 319, 320, 321, 323, 329, 329, 332, 332, 333, 333, 333, 334, 334, 336, 336, 336, 336, 338, 339, 340, 342, 342, 343, 343, 343, 345, 345, 348, 348, 350, 350, 351, 351, 353, 354, 355, 355, 357, 358, 358, 358, 360, 360, 361, 363, 363, 363, 367, 367, 369, 369, 372, 372, 373, 374, 375, 377, 377, 379, 379, 381, 381, 381, 382, 382, 382, 383, 385, 385, 388, 388, 388, 390, 390, 390, 394, 395, 396, 398, 398, 398, 399, 401, 401, 401, 401, 401, 402, 403, 403, 407, 407, 408, 409, 409, 410, 410, 411, 411, 412, 414, 414, 415, 416, 418, 419, 420, 420, 420, 421, 421, 422, 422, 422, 425, 426, 426, 426, 426, 429, 429, 430, 432, 433, 433, 434, 434, 434, 438, 439, 439, 439, 440, 442, 442, 443, 446, 448, 448, 449, 449, 449, 454, 455, 456, 456, 456, 456, 457, 457, 457, 457, 457, 458, 459, 459, 460, 461, 462, 464, 465, 467, 467, 467, 469, 469, 469, 469, 470, 471, 471, 471, 471, 472, 472, 473, 473, 477, 477, 477, 479, 479, 480, 480, 482, 484, 486, 488, 488, 489, 489, 489, 494, 494, 495, 496, 496, 498, 501, 501, 503, 505, 506, 506, 509, 509, 510, 510, 512, 513, 515, 515, 516, 517, 517, 518, 519, 521, 521, 525, 525, 526, 527, 536, 536, 538, 539, 539, 539, 540, 541, 545, 545, 545, 546, 547, 548, 551, 551, 553, 553, 554, 554, 558, 561, 562, 562, 562, 564, 568, 569, 569, 571, 571, 572, 572, 574, 576, 578, 578, 579, 580, 584, 584, 585, 588, 588, 588, 589, 589, 589, 589, 590, 591, 591, 591, 592, 592, 593, 595, 595, 595, 595, 596, 598, 599, 601, 601, 601, 603, 603, 605, 605, 606, 607, 607, 608, 609, 609, 610, 611, 618, 618, 621, 621, 622, 622, 626, 626, 626, 627, 627, 628, 629, 629, 630, 630, 632, 632, 633, 634, 634, 637, 638, 641, 642, 643, 644, 646, 647, 647, 648, 649, 654, 655, 657, 657, 659, 659, 662, 663, 665, 667, 667, 667, 668, 669, 671, 671, 671, 675, 675, 676, 676, 677, 679, 682, 683, 684, 687, 689, 690, 691, 691, 691, 695, 697, 698};
167     const ElemType moveDataArr[300] = { 534, 624, 196, 364, 9, 441, 203, 560, 82, 460, 293, 44, 413, 606, 674, 227, 553, 414, 693, 566, 670, 230, 322, 597, 44, 615, 209, 542, 487, 451, 150, 261, 264, 201, 255, 158, 248, 528, 559, 685, 212, 685, 229, 354, 270, 289, 444, 586, 103, 587, 242, 353, 499, 652, 108, 11, 285, 541, 549, 373, 126, 82, 27, 666, 63, 41, 678, 558, 354, 126, 600, 501, 390, 189, 492, 433, 438, 555, 581, 79, 675, 541, 232, 678, 309, 427, 156, 94, 563, 271, 21, 134, 626, 454, 205, 330, 646, 396, 677, 598, 624, 696, 143, 234, 594, 300, 483, 89, 432, 313, 585, 347, 127, 344, 576, 431, 216, 67, 207, 215, 28, 495, 128, 170, 108, 290, 665, 544, 436, 581, 384, 321, 324, 565, 368, 203, 166, 386, 152, 619, 589, 242, 135, 507, 124, 560, 154, 469, 588, 522, 57, 517, 52, 633, 138, 63, 634, 256, 50, 336, 663, 343, 208, 504, 481, 111, 128, 476, 371, 152, 419, 327, 249, 607, 236, 253, 403, 461, 464, 299, 13, 453, 473, 62, 352, 42, 424, 140, 243, 681, 153, 687, 379, 147, 162, 646, 458, 421, 33, 421, 416, 535, 649, 69, 370, 155, 583, 295, 531, 331, 118, 283, 359, 262, 347, 46, 10, 277, 10, 682, 628, 428, 96, 574, 527, 496, 688, 644, 647, 493, 45, 70, 438, 194, 453, 491, 117, 687, 253, 197, 331, 181, 90, 604, 243, 463, 156, 51, 604, 222, 470, 280, 79, 162, 548, 619, 690, 523, 242, 366, 478, 431, 246, 699, 40, 573, 271, 618, 150, 364, 49, 313, 636, 625, 39, 511, 356, 400, 163, 486, 62, 387, 577, 128, 357, 677, 238, 307, 406, 666, 61, 317, 694, 38, 404, 485, 284, 510, 338, 52};
168     ElemType maxData = 0;
169     PriorityQueue _queue, *queue = &_queue;
170     ConstructPriorityQueueParams(queue, 1, compare_int, NULL);
171 
172     for (int i = 0; i < ARRAY_SIZE(array); ++i)
173     {
174         PriorityQueue_Offer(queue, array[i]);
175         ASSERT_TRUE(PriorityQueue_Size(queue) == i + 1);
176         ASSERT_TRUE((maxData = MAX(maxData, array[i])) == PriorityQueue_Peek(queue));
177     }
178 
179     for (int i = 0; i < ARRAY_SIZE(moveDataArr); ++i)
180     {
181         PriorityQueue_Remove(queue, moveDataArr[i]);
182         ASSERT_TRUE(PriorityQueue_Size(queue) == ARRAY_SIZE(array) - i - 1);
183     }
184 
185     ASSERT_TRUE(PriorityQueue_Size(queue) == ARRAY_SIZE(sortedArr));
186 
187     for (int i = 0; i < ARRAY_SIZE(sortedArr); ++i)
188     {
189         ASSERT_TRUE(sortedArr[ARRAY_SIZE(sortedArr) - i - 1] == PriorityQueue_Peek(queue));
190         ASSERT_TRUE(sortedArr[ARRAY_SIZE(sortedArr) - i - 1] == PriorityQueue_Poll(queue));
191         ASSERT_TRUE(PriorityQueue_Size(queue) == ARRAY_SIZE(sortedArr) - i - 1);
192     }
193 
194     ASSERT_TRUE(INVALID_ELEM == PriorityQueue_Peek(queue));
195     ASSERT_TRUE(INVALID_ELEM == PriorityQueue_Poll(queue));
196     ASSERT_TRUE(PriorityQueue_Size(queue) == 0);
197 
198     DestructPriorityQueue(queue);
199 
200     ASSERT_TRUE(PriorityQueue_IsEmpty(queue));
201     ASSERT_TRUE(PriorityQueue_Size(queue) == 0);
202 }
203 
204 typedef struct Node
205 {
206     int index;
207     int data;
208 } Node;
209 
210 static int compare_node(const ElemType a, const ElemType b)
211 {
212     const Node *nodeA = (Node*)a;
213     const Node *nodeB = (Node*)b;
214     if (nodeA->data < nodeB->data) return -1;
215     if (nodeA->data > nodeB->data) return 1;
216     return 0;
217 }
218 
219 static void notify_node(ElemType x, int index)
220 {
221     Node *node = (Node*)x;
222     node->index = index;
223 }
224 
225 TEST(testPriorityQueue, test_notifier)
226 {
227     Node nodes[] = { {0, 1}, {0, 3}, {0, 8}, {0, 4}, {0, 6}, {0, 2}, {0, 0}, {0, 7}, {0, 9}, {0, 5} };
228     PriorityQueue _queue, *queue = &_queue;
229     ConstructPriorityQueueParams(queue, 1, compare_node, notify_node);
230 
231     for (int i = 0; i < ARRAY_SIZE(nodes); ++i)
232     {
233         PriorityQueue_Offer(queue, (ElemType)(nodes + i));
234 
235         for (int j = 0; j <= i; ++j)
236             ASSERT_TRUE(nodes[j].index == PriorityQueue_IndexOf(queue, (ElemType)(nodes + j)));
237     }
238 
239     Node *n1 = (Node*)PriorityQueue_Poll(queue);
240     ASSERT_TRUE(n1->data == 0);
241     ASSERT_TRUE(n1->index == INVALID_INDEX);
242 
243     ASSERT_TRUE(nodes[0].index == 0);
244     ASSERT_TRUE(PriorityQueue_RemoveAt(queue, 0));
245     ASSERT_TRUE(nodes[0].index == INVALID_INDEX);
246 
247     DestructPriorityQueue(queue);
248 
249     for (int i = 0; i < ARRAY_SIZE(nodes); ++i)
250     {
251         ASSERT_TRUE(nodes[i].index == INVALID_INDEX);
252     }
253 }
254 
255 #if 0
256 TEST(testPriorityQueue, infinite)
257 {
258     PriorityQueue _queue, *queue = &_queue;
259     ConstructPriorityQueueDefault(queue);
260 
261     for (int i = 0; i <0x7FFFFFFF; ++i)
262     {
263         PriorityQueue_Offer(queue, i);
264 //        ASSERT_TRUE(PriorityQueue_Size(queue) == i + 1);
265     }
266 
267     PriorityQueue_Clear(queue);
268 
269     ASSERT_TRUE(INVALID_ELEM == PriorityQueue_Peek(queue));
270     ASSERT_TRUE(INVALID_ELEM == PriorityQueue_Poll(queue));
271     ASSERT_TRUE(PriorityQueue_Size(queue) == 0);
272 
273     DestructPriorityQueue(queue);
274 
275     ASSERT_TRUE(PriorityQueue_IsEmpty(queue));
276     ASSERT_TRUE(PriorityQueue_Size(queue) == 0);
277 }
278 #endif

 

posted on 2020-05-12 23:00  Jinglelove  阅读(534)  评论(0)    收藏  举报