1 /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
\r
2 * This file is part of clib library
\r
3 * Copyright (C) 2011 Avinash Dongre ( dongre.avinash@gmail.com )
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
6 * of this software and associated documentation files (the "Software"), to deal
\r
7 * in the Software without restriction, including without limitation the rights
\r
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
9 * copies of the Software, and to permit persons to whom the Software is
\r
10 * furnished to do so, subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in
\r
13 * all copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
22 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
\r
28 compare_int ( void *left, void *right ) {
\r
29 int *l = (int*)left;
\r
30 int *r = (int*)right;
\r
34 else if ( *l > *r )
\r
39 print_element ( void *ptr ) {
\r
40 printf ( "%d\n", *(int*)ptr);
\r
43 test_clib_heap_max() {
\r
44 int test[] = {4,1,3,2,16,9,10,14,8,7};
\r
46 int size = sizeof (test) /sizeof(test[0]);
\r
48 struct clib_heap* pHeap = new_clib_heap ( 8, compare_int, NULL);
\r
50 for ( index = 0; index < size; index++ ) {
\r
51 int v = test[index];
\r
52 insert_clib_heap ( pHeap, &v, sizeof(int));
\r
54 build_max_clib_heap( pHeap);
\r
55 printf ( "---------------------------------\n");
\r
56 for_each_clib_heap ( pHeap, print_element);
\r
57 printf ( "---------------------------------\n");
\r
58 while ( empty_clib_heap(pHeap) != clib_true ) {
\r
59 maxElem = extract_max_clib_heap ( pHeap );
\r
60 printf ( "MAX ELEMENT = %d\n", *(int*)maxElem);
\r
63 delete_clib_heap ( pHeap );
\r
67 test_clib_heap_min() {
\r
68 int test[] = {4,1,3,2,16,9,10,14,8,7};
\r
70 int size = sizeof (test) /sizeof(test[0]);
\r
72 struct clib_heap* pHeap = new_clib_heap ( 8, compare_int, NULL);
\r
74 for ( index = 0; index < size; index++ ) {
\r
75 int v = test[index];
\r
76 insert_clib_heap ( pHeap, &v, sizeof(int));
\r
78 build_min_clib_heap( pHeap);
\r
79 printf ( "---------------------------------\n");
\r
80 for_each_clib_heap ( pHeap, print_element);
\r
81 printf ( "---------------------------------\n");
\r
82 while ( empty_clib_heap(pHeap) != clib_true ) {
\r
83 maxElem = extract_min_clib_heap ( pHeap );
\r
84 printf ( "MIN ELEMENT = %d\n", *(int*)maxElem);
\r
87 delete_clib_heap ( pHeap );
\r
93 test_clib_heap_max();
\r
94 test_clib_heap_min();
\r