Vector.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef HEADER_Vector
  2. #define HEADER_Vector
  3. /*
  4. htop - Vector.h
  5. (C) 2004-2011 Hisham H. Muhammad
  6. Released under the GNU GPLv2+, see the COPYING file
  7. in the source distribution for its full text.
  8. */
  9. #include "Object.h"
  10. #include <stdbool.h>
  11. #ifndef DEFAULT_SIZE
  12. #define DEFAULT_SIZE (-1)
  13. #endif
  14. typedef struct Vector_ {
  15. Object** array;
  16. const ObjectClass* type;
  17. int arraySize;
  18. int growthRate;
  19. int items;
  20. /* lowest index of a pending soft remove/delete operation,
  21. used to speed up compaction */
  22. int dirty_index;
  23. /* count of soft deletes, required for Vector_count to work in debug mode */
  24. int dirty_count;
  25. bool owner;
  26. } Vector;
  27. Vector* Vector_new(const ObjectClass* type, bool owner, int size);
  28. void Vector_delete(Vector* this);
  29. void Vector_prune(Vector* this);
  30. void Vector_quickSortCustomCompare(Vector* this, Object_Compare compare);
  31. static inline void Vector_quickSort(Vector* this) {
  32. Vector_quickSortCustomCompare(this, this->type->compare);
  33. }
  34. void Vector_insertionSort(Vector* this);
  35. void Vector_insert(Vector* this, int idx, void* data_);
  36. Object* Vector_take(Vector* this, int idx);
  37. Object* Vector_remove(Vector* this, int idx);
  38. /* Vector_softRemove marks the item at index idx for deletion without
  39. reclaiming any space. If owned, the item is immediately freed.
  40. Vector_compact must be called to reclaim space.*/
  41. Object* Vector_softRemove(Vector* this, int idx);
  42. /* Vector_compact reclaims space free'd up by Vector_softRemove, if any. */
  43. void Vector_compact(Vector* this);
  44. void Vector_moveUp(Vector* this, int idx);
  45. void Vector_moveDown(Vector* this, int idx);
  46. void Vector_set(Vector* this, int idx, void* data_);
  47. #ifndef NDEBUG
  48. Object* Vector_get(const Vector* this, int idx);
  49. int Vector_size(const Vector* this);
  50. /* Vector_countEquals returns true if the number of non-NULL items
  51. in the Vector is equal to expectedCount. This is only for debugging
  52. and consistency checks. */
  53. bool Vector_countEquals(const Vector* this, unsigned int expectedCount);
  54. #else /* NDEBUG */
  55. static inline Object* Vector_get(const Vector* this, int idx) {
  56. return this->array[idx];
  57. }
  58. static inline int Vector_size(const Vector* this) {
  59. return this->items;
  60. }
  61. #endif /* NDEBUG */
  62. static inline const ObjectClass* Vector_type(const Vector* this) {
  63. return this->type;
  64. }
  65. void Vector_add(Vector* this, void* data_);
  66. int Vector_indexOf(const Vector* this, const void* search_, Object_Compare compare);
  67. void Vector_splice(Vector* this, Vector* from);
  68. #endif