t_c_heap.c 3.2 KB

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