/CS/migrated/tags/crystal/libs/csutil/csvector.cpp

# · C++ · 188 lines · 151 code · 16 blank · 21 comment · 43 complexity · e9cdb95cda72fa03b49ac1066ed21b36 MD5 · raw file

  1. /*
  2. Crystal Space Vector class
  3. Copyright (C) 1998,1999 by Andrew Zabolotny <bit@eltech.ru>
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free
  14. Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "sysdef.h"
  19. #include "csutil/csvector.h"
  20. csVector::csVector (int ilimit, int ithreshold)
  21. {
  22. root = (csSome *)malloc ((limit = ilimit) * sizeof (csSome));
  23. count = 0; threshold = ithreshold;
  24. }
  25. csVector::~csVector ()
  26. {
  27. //not much sense to call DeleteAll () since even for inherited classes
  28. //anyway will be called csVector::FreeItem which is empty.
  29. //DeleteAll ();
  30. if (root) free (root);
  31. }
  32. void csVector::DeleteAll ()
  33. {
  34. int idx;
  35. for (idx = count - 1; idx >= 0; idx--)
  36. if (!FreeItem (root [idx]))
  37. break;
  38. SetLength (idx + 1);
  39. while (idx >= 0)
  40. Delete (idx--);
  41. }
  42. void csVector::SetLength (int n)
  43. {
  44. count = n;
  45. if ((n > limit) || ((limit > threshold) && (n < limit - threshold)))
  46. {
  47. n = ((n + threshold - 1) / threshold) * threshold;
  48. root = n ? (csSome *)realloc (root, n * sizeof (csSome)) : NULL;
  49. limit = n;
  50. }
  51. }
  52. bool csVector::FreeItem (csSome Item)
  53. {
  54. (void)Item;
  55. return true;
  56. }
  57. bool csVector::Delete (int n)
  58. {
  59. if (n < count)
  60. {
  61. if (!FreeItem (root [n]))
  62. return false;
  63. int ncount = count - 1;
  64. memmove (&root [n], &root [n + 1], (ncount - n) * sizeof (csSome));
  65. SetLength (ncount);
  66. return true;
  67. }
  68. else
  69. return false;
  70. }
  71. bool csVector::Insert (int n, csSome Item)
  72. {
  73. if (n <= count)
  74. {
  75. SetLength (count + 1);
  76. memmove (&root [n + 1], &root [n], (count - n) * sizeof (csSome));
  77. root [n] = Item;
  78. return true;
  79. }
  80. else
  81. return false;
  82. }
  83. int csVector::Find (csSome which) const
  84. {
  85. for (int i = 0; i < Length (); i++)
  86. if (root [i] == which)
  87. return i;
  88. return -1;
  89. }
  90. int csVector::FindKey (csConstSome Key, int Mode) const
  91. {
  92. for (int i = 0; i < Length (); i++)
  93. if (CompareKey (root [i], Key, Mode) == 0)
  94. return i;
  95. return -1;
  96. }
  97. int csVector::FindSortedKey (csConstSome Key, int Mode) const
  98. {
  99. int l = 0, r = Length () - 1;
  100. while (l <= r)
  101. {
  102. int m = (l + r) / 2;
  103. int cmp = CompareKey (root [m], Key, Mode);
  104. if (cmp == 0)
  105. return m;
  106. else if (cmp < 0)
  107. l = m + 1;
  108. else
  109. r = m - 1;
  110. }
  111. return -1;
  112. }
  113. void csVector::QuickSort (int Left, int Right, int Mode)
  114. {
  115. recurse:
  116. int i = Left, j = Right;
  117. int x = (Left + Right) / 2;
  118. do
  119. {
  120. while ((i != x) && (Compare (Get (i), Get (x), Mode) < 0))
  121. i++;
  122. while ((j != x) && (Compare (Get (j), Get (x), Mode) > 0))
  123. j--;
  124. if (i < j)
  125. {
  126. Exchange (i, j);
  127. if (x == i)
  128. x = j;
  129. else if (x == j)
  130. x = i;
  131. }
  132. if (i <= j)
  133. {
  134. i++;
  135. if (j > Left)
  136. j--;
  137. }
  138. } while (i <= j);
  139. if (j - Left < Right - i)
  140. {
  141. if (Left < j)
  142. QuickSort (Left, j, Mode);
  143. if (i < Right)
  144. {
  145. Left = i;
  146. goto recurse;
  147. }
  148. }
  149. else
  150. {
  151. if (i < Right)
  152. QuickSort (i, Right, Mode);
  153. if (Left < j)
  154. {
  155. Right = j;
  156. goto recurse;
  157. }
  158. }
  159. }
  160. int csVector::Compare (csSome Item1, csSome Item2, int Mode) const
  161. {
  162. (void)Mode;
  163. return ((int)Item1 > (int)Item2) ? +1 : ((int)Item1 == (int)Item2) ? 0 : -1;
  164. }
  165. int csVector::CompareKey (csSome Item, csConstSome Key, int Mode) const
  166. {
  167. (void)Mode;
  168. return (Item > Key) ? +1 : (Item == Key) ? 0 : -1;
  169. }