Vector.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. htop - Vector.c
  3. (C) 2004-2011 Hisham H. Muhammad
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "config.h" // IWYU pragma: keep
  8. #include "Vector.h"
  9. #include <assert.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "XUtils.h"
  13. Vector* Vector_new(const ObjectClass* type, bool owner, int size) {
  14. Vector* this;
  15. if (size == DEFAULT_SIZE) {
  16. size = 10;
  17. }
  18. assert(size > 0);
  19. this = xMalloc(sizeof(Vector));
  20. *this = (Vector) {
  21. .growthRate = size,
  22. .array = xCalloc(size, sizeof(Object*)),
  23. .arraySize = size,
  24. .items = 0,
  25. .type = type,
  26. .owner = owner,
  27. .dirty_index = -1,
  28. .dirty_count = 0,
  29. };
  30. return this;
  31. }
  32. void Vector_delete(Vector* this) {
  33. if (this->owner) {
  34. for (int i = 0; i < this->items; i++) {
  35. if (this->array[i]) {
  36. Object_delete(this->array[i]);
  37. }
  38. }
  39. }
  40. free(this->array);
  41. free(this);
  42. }
  43. static inline bool Vector_isDirty(const Vector* this) {
  44. if (this->dirty_count > 0) {
  45. assert(0 <= this->dirty_index && this->dirty_index < this->items);
  46. assert(this->dirty_count <= this->items);
  47. return true;
  48. }
  49. assert(this->dirty_index == -1);
  50. return false;
  51. }
  52. #ifndef NDEBUG
  53. static bool Vector_isConsistent(const Vector* this) {
  54. assert(this->items <= this->arraySize);
  55. assert(!Vector_isDirty(this));
  56. return true;
  57. }
  58. bool Vector_countEquals(const Vector* this, unsigned int expectedCount) {
  59. unsigned int n = 0;
  60. for (int i = 0; i < this->items; i++) {
  61. if (this->array[i]) {
  62. n++;
  63. }
  64. }
  65. return n == expectedCount;
  66. }
  67. Object* Vector_get(const Vector* this, int idx) {
  68. assert(idx >= 0 && idx < this->items);
  69. assert(this->array[idx]);
  70. assert(Object_isA(this->array[idx], this->type));
  71. return this->array[idx];
  72. }
  73. int Vector_size(const Vector* this) {
  74. assert(Vector_isConsistent(this));
  75. return this->items;
  76. }
  77. #endif /* NDEBUG */
  78. void Vector_prune(Vector* this) {
  79. assert(Vector_isConsistent(this));
  80. if (this->owner) {
  81. for (int i = 0; i < this->items; i++) {
  82. if (this->array[i]) {
  83. Object_delete(this->array[i]);
  84. }
  85. }
  86. }
  87. this->items = 0;
  88. this->dirty_index = -1;
  89. this->dirty_count = 0;
  90. memset(this->array, '\0', this->arraySize * sizeof(Object*));
  91. }
  92. //static int comparisons = 0;
  93. static void swap(Object** array, int indexA, int indexB) {
  94. assert(indexA >= 0);
  95. assert(indexB >= 0);
  96. Object* tmp = array[indexA];
  97. array[indexA] = array[indexB];
  98. array[indexB] = tmp;
  99. }
  100. static int partition(Object** array, int left, int right, int pivotIndex, Object_Compare compare) {
  101. const Object* pivotValue = array[pivotIndex];
  102. swap(array, pivotIndex, right);
  103. int storeIndex = left;
  104. for (int i = left; i < right; i++) {
  105. //comparisons++;
  106. if (compare(array[i], pivotValue) <= 0) {
  107. swap(array, i, storeIndex);
  108. storeIndex++;
  109. }
  110. }
  111. swap(array, storeIndex, right);
  112. return storeIndex;
  113. }
  114. static void quickSort(Object** array, int left, int right, Object_Compare compare) {
  115. if (left >= right)
  116. return;
  117. int pivotIndex = (left + right) / 2;
  118. int pivotNewIndex = partition(array, left, right, pivotIndex, compare);
  119. quickSort(array, left, pivotNewIndex - 1, compare);
  120. quickSort(array, pivotNewIndex + 1, right, compare);
  121. }
  122. // If I were to use only one sorting algorithm for both cases, it would probably be this one:
  123. /*
  124. static void combSort(Object** array, int left, int right, Object_Compare compare) {
  125. int gap = right - left;
  126. bool swapped = true;
  127. while ((gap > 1) || swapped) {
  128. if (gap > 1) {
  129. gap = (int)((double)gap / 1.247330950103979);
  130. }
  131. swapped = false;
  132. for (int i = left; gap + i <= right; i++) {
  133. comparisons++;
  134. if (compare(array[i], array[i+gap]) > 0) {
  135. swap(array, i, i+gap);
  136. swapped = true;
  137. }
  138. }
  139. }
  140. }
  141. */
  142. static void insertionSort(Object** array, int left, int right, Object_Compare compare) {
  143. for (int i = left + 1; i <= right; i++) {
  144. Object* t = array[i];
  145. int j = i - 1;
  146. while (j >= left) {
  147. //comparisons++;
  148. if (compare(array[j], t) <= 0)
  149. break;
  150. array[j + 1] = array[j];
  151. j--;
  152. }
  153. array[j + 1] = t;
  154. }
  155. }
  156. void Vector_quickSortCustomCompare(Vector* this, Object_Compare compare) {
  157. assert(compare);
  158. assert(Vector_isConsistent(this));
  159. quickSort(this->array, 0, this->items - 1, compare);
  160. assert(Vector_isConsistent(this));
  161. }
  162. void Vector_insertionSort(Vector* this) {
  163. assert(this->type->compare);
  164. assert(Vector_isConsistent(this));
  165. insertionSort(this->array, 0, this->items - 1, this->type->compare);
  166. assert(Vector_isConsistent(this));
  167. }
  168. static void Vector_resizeIfNecessary(Vector* this, int newSize) {
  169. assert(newSize >= 0);
  170. if (newSize > this->arraySize) {
  171. assert(Vector_isConsistent(this));
  172. int oldSize = this->arraySize;
  173. this->arraySize = newSize + this->growthRate;
  174. this->array = (Object**)xReallocArrayZero(this->array, oldSize, this->arraySize, sizeof(Object*));
  175. }
  176. assert(Vector_isConsistent(this));
  177. }
  178. void Vector_insert(Vector* this, int idx, void* data_) {
  179. Object* data = data_;
  180. assert(idx >= 0);
  181. assert(Object_isA(data, this->type));
  182. assert(Vector_isConsistent(this));
  183. if (idx > this->items) {
  184. idx = this->items;
  185. }
  186. Vector_resizeIfNecessary(this, this->items + 1);
  187. //assert(this->array[this->items] == NULL);
  188. if (idx < this->items) {
  189. memmove(&this->array[idx + 1], &this->array[idx], (this->items - idx) * sizeof(this->array[0]));
  190. }
  191. this->array[idx] = data;
  192. this->items++;
  193. assert(Vector_isConsistent(this));
  194. }
  195. Object* Vector_take(Vector* this, int idx) {
  196. assert(idx >= 0 && idx < this->items);
  197. assert(Vector_isConsistent(this));
  198. Object* removed = this->array[idx];
  199. assert(removed);
  200. this->items--;
  201. if (idx < this->items) {
  202. memmove(&this->array[idx], &this->array[idx + 1], (this->items - idx) * sizeof(this->array[0]));
  203. }
  204. this->array[this->items] = NULL;
  205. assert(Vector_isConsistent(this));
  206. return removed;
  207. }
  208. Object* Vector_remove(Vector* this, int idx) {
  209. Object* removed = Vector_take(this, idx);
  210. if (this->owner) {
  211. Object_delete(removed);
  212. return NULL;
  213. } else {
  214. return removed;
  215. }
  216. }
  217. Object* Vector_softRemove(Vector* this, int idx) {
  218. assert(idx >= 0 && idx < this->items);
  219. Object* removed = this->array[idx];
  220. assert(removed);
  221. if (removed) {
  222. this->array[idx] = NULL;
  223. this->dirty_count++;
  224. if (this->dirty_index < 0 || idx < this->dirty_index) {
  225. this->dirty_index = idx;
  226. }
  227. if (this->owner) {
  228. Object_delete(removed);
  229. return NULL;
  230. }
  231. }
  232. return removed;
  233. }
  234. void Vector_compact(Vector* this) {
  235. if (!Vector_isDirty(this)) {
  236. return;
  237. }
  238. const int size = this->items;
  239. assert(0 <= this->dirty_index && this->dirty_index < size);
  240. assert(this->array[this->dirty_index] == NULL);
  241. int idx = this->dirty_index;
  242. // one deletion: use memmove, which should be faster
  243. if (this->dirty_count == 1) {
  244. memmove(&this->array[idx], &this->array[idx + 1], (this->items - idx - 1) * sizeof(this->array[0]));
  245. this->array[this->items - 1] = NULL;
  246. } else {
  247. // multiple deletions
  248. for (int i = idx + 1; i < size; i++) {
  249. if (this->array[i]) {
  250. this->array[idx++] = this->array[i];
  251. }
  252. }
  253. // idx is now at the end of the vector and on the first index which should be set to NULL
  254. memset(&this->array[idx], '\0', (size - idx) * sizeof(this->array[0]));
  255. }
  256. this->items -= this->dirty_count;
  257. this->dirty_index = -1;
  258. this->dirty_count = 0;
  259. assert(Vector_isConsistent(this));
  260. }
  261. void Vector_moveUp(Vector* this, int idx) {
  262. assert(idx >= 0 && idx < this->items);
  263. assert(Vector_isConsistent(this));
  264. if (idx == 0)
  265. return;
  266. Object* temp = this->array[idx];
  267. this->array[idx] = this->array[idx - 1];
  268. this->array[idx - 1] = temp;
  269. }
  270. void Vector_moveDown(Vector* this, int idx) {
  271. assert(idx >= 0 && idx < this->items);
  272. assert(Vector_isConsistent(this));
  273. if (idx == this->items - 1)
  274. return;
  275. Object* temp = this->array[idx];
  276. this->array[idx] = this->array[idx + 1];
  277. this->array[idx + 1] = temp;
  278. }
  279. void Vector_set(Vector* this, int idx, void* data_) {
  280. Object* data = data_;
  281. assert(idx >= 0);
  282. assert(Object_isA(data, this->type));
  283. assert(Vector_isConsistent(this));
  284. Vector_resizeIfNecessary(this, idx + 1);
  285. if (idx >= this->items) {
  286. this->items = idx + 1;
  287. } else {
  288. if (this->owner) {
  289. Object* removed = this->array[idx];
  290. if (removed != NULL) {
  291. Object_delete(removed);
  292. }
  293. }
  294. }
  295. this->array[idx] = data;
  296. assert(Vector_isConsistent(this));
  297. }
  298. /*
  299. static void Vector_merge(Vector* this, Vector* v2) {
  300. int i;
  301. assert(Vector_isConsistent(this));
  302. for (i = 0; i < v2->items; i++)
  303. Vector_add(this, v2->array[i]);
  304. v2->items = 0;
  305. Vector_delete(v2);
  306. assert(Vector_isConsistent(this));
  307. }
  308. */
  309. void Vector_add(Vector* this, void* data_) {
  310. Object* data = data_;
  311. assert(Object_isA(data, this->type));
  312. assert(Vector_isConsistent(this));
  313. int i = this->items;
  314. Vector_set(this, this->items, data);
  315. assert(this->items == i + 1); (void)(i);
  316. assert(Vector_isConsistent(this));
  317. }
  318. int Vector_indexOf(const Vector* this, const void* search_, Object_Compare compare) {
  319. const Object* search = search_;
  320. assert(Object_isA(search, this->type));
  321. assert(compare);
  322. assert(Vector_isConsistent(this));
  323. for (int i = 0; i < this->items; i++) {
  324. const Object* o = this->array[i];
  325. assert(o);
  326. if (compare(search, o) == 0) {
  327. return i;
  328. }
  329. }
  330. return -1;
  331. }
  332. void Vector_splice(Vector* this, Vector* from) {
  333. assert(Vector_isConsistent(this));
  334. assert(Vector_isConsistent(from));
  335. assert(!this->owner);
  336. int olditems = this->items;
  337. Vector_resizeIfNecessary(this, this->items + from->items);
  338. this->items += from->items;
  339. for (int j = 0; j < from->items; j++) {
  340. this->array[olditems + j] = from->array[j];
  341. }
  342. }