sortlist.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //***************************************************************************
  2. //
  3. // SortList.cpp -- Sort List class
  4. //
  5. // MechCommander II
  6. //
  7. //---------------------------------------------------------------------------//
  8. // Copyright (C) Microsoft Corporation. All rights reserved. //
  9. //===========================================================================//
  10. //--------------
  11. // Include Files
  12. #ifndef SORTLIST_H
  13. #include "sortlist.h"
  14. #endif
  15. #ifndef ERR_H
  16. #include "err.h"
  17. #endif
  18. #ifndef HEAP_H
  19. #include "heap.h"
  20. #endif
  21. #include <stdlib.h>
  22. #include <gameos.hpp>
  23. //***************************************************************************
  24. // Class SortList
  25. //***************************************************************************
  26. long SortList::init (long _numItems) {
  27. //-------------------------
  28. // Create the sort list...
  29. numItems = _numItems;
  30. list = (SortListNode*)systemHeap->Malloc(sizeof(SortListNode) * numItems);
  31. if (!list)
  32. Fatal(0, " Unable to init sortList ");
  33. return(list == NULL);
  34. }
  35. //---------------------------------------------------------------------------
  36. void SortList::clear (bool setToMin) {
  37. for (long i = 0; i < numItems; i++)
  38. list[i].id = i;
  39. if (setToMin)
  40. for (i = 0; i < numItems; i++)
  41. list[i].value = float(-3.4E38);
  42. else
  43. for (i = 0; i < numItems; i++)
  44. list[i].value = float(3.4E38);
  45. }
  46. //---------------------------------------------------------------------------
  47. int cdecl descendingCompare (const void* arg1, const void* arg2 ) {
  48. float value1 = ((SortListNode*)arg1)->value;
  49. float value2 = ((SortListNode*)arg2)->value;
  50. if (value1 > value2)
  51. return(-1);
  52. else if (value1 < value2)
  53. return(1);
  54. else
  55. return(0);
  56. }
  57. //---------------------------------------------------------------------------
  58. int cdecl ascendingCompare (const void* arg1, const void* arg2 ) {
  59. float value1 = ((SortListNode*)arg1)->value;
  60. float value2 = ((SortListNode*)arg2)->value;
  61. if (value1 > value2)
  62. return(1);
  63. else if (value1 < value2)
  64. return(-1);
  65. else
  66. return(0);
  67. }
  68. //---------------------------------------------------------------------------
  69. void SortList::sort (bool descendingOrder) {
  70. //------------------------------------------------------------------
  71. // For now, just use ANSI C's built-in qsort (ugly, but functional).
  72. if (descendingOrder)
  73. qsort((void*)list, (size_t)numItems, sizeof(SortListNode), descendingCompare);
  74. else
  75. qsort((void*)list, (size_t)numItems, sizeof(SortListNode), ascendingCompare);
  76. }
  77. //---------------------------------------------------------------------------
  78. void SortList::destroy (void) {
  79. systemHeap->Free(list);
  80. list = NULL;
  81. }
  82. //***************************************************************************