| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 1 | #ifndef HEADER_Vector |
| 2 | #define HEADER_Vector |
| 3 | /* |
| Hisham Muhammad | 84281bd | 2011-12-26 21:35:57 +0000 | [diff] [blame] | 4 | htop - Vector.h |
| Hisham Muhammad | 300caa0 | 2011-05-26 16:35:07 +0000 | [diff] [blame] | 5 | (C) 2004-2011 Hisham H. Muhammad |
| Daniel Lange | 94ad111 | 2021-09-22 11:33:00 +0200 | [diff] [blame] | 6 | Released under the GNU GPLv2+, see the COPYING file |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 7 | in the source distribution for its full text. |
| 8 | */ |
| 9 | |
| 10 | #include "Object.h" |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 11 | |
| Benny Baumann | 0f52629 | 2020-09-19 13:55:23 +0200 | [diff] [blame] | 12 | #include <stdbool.h> |
| 13 | |
| Hisham Muhammad | 7ca1081 | 2011-11-18 06:08:56 +0000 | [diff] [blame] | 14 | |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 15 | #ifndef DEFAULT_SIZE |
| Christian Göttsche | e9246ab | 2020-10-15 21:45:38 +0200 | [diff] [blame] | 16 | #define DEFAULT_SIZE (-1) |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 17 | #endif |
| 18 | |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 19 | typedef struct Vector_ { |
| Benny Baumann | 61e14d4 | 2020-10-31 23:28:02 +0100 | [diff] [blame] | 20 | Object** array; |
| Christian Göttsche | 08d85e6 | 2020-10-04 17:55:08 +0200 | [diff] [blame] | 21 | const ObjectClass* type; |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 22 | int arraySize; |
| 23 | int growthRate; |
| 24 | int items; |
| Charlie Vieth | 08166b2 | 2022-02-05 18:47:43 -0500 | [diff] [blame] | 25 | /* lowest index of a pending soft remove/delete operation, |
| 26 | used to speed up compaction */ |
| 27 | int dirty_index; |
| 28 | /* count of soft deletes, required for Vector_count to work in debug mode */ |
| 29 | int dirty_count; |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 30 | bool owner; |
| 31 | } Vector; |
| 32 | |
| Christian Göttsche | 08d85e6 | 2020-10-04 17:55:08 +0200 | [diff] [blame] | 33 | Vector* Vector_new(const ObjectClass* type, bool owner, int size); |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 34 | |
| Zev Weiss | 7b7822b | 2020-09-02 02:38:44 -0500 | [diff] [blame] | 35 | void Vector_delete(Vector* this); |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 36 | |
| Zev Weiss | 7b7822b | 2020-09-02 02:38:44 -0500 | [diff] [blame] | 37 | void Vector_prune(Vector* this); |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 38 | |
| Christian Göttsche | a3bb7cb | 2020-10-21 21:26:09 +0200 | [diff] [blame] | 39 | void Vector_quickSortCustomCompare(Vector* this, Object_Compare compare); |
| 40 | static inline void Vector_quickSort(Vector* this) { |
| 41 | Vector_quickSortCustomCompare(this, this->type->compare); |
| 42 | } |
| Hisham Muhammad | 7ca1081 | 2011-11-18 06:08:56 +0000 | [diff] [blame] | 43 | |
| Zev Weiss | 7b7822b | 2020-09-02 02:38:44 -0500 | [diff] [blame] | 44 | void Vector_insertionSort(Vector* this); |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 45 | |
| Zev Weiss | 7b7822b | 2020-09-02 02:38:44 -0500 | [diff] [blame] | 46 | void Vector_insert(Vector* this, int idx, void* data_); |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 47 | |
| Zev Weiss | 7b7822b | 2020-09-02 02:38:44 -0500 | [diff] [blame] | 48 | Object* Vector_take(Vector* this, int idx); |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 49 | |
| Zev Weiss | 7b7822b | 2020-09-02 02:38:44 -0500 | [diff] [blame] | 50 | Object* Vector_remove(Vector* this, int idx); |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 51 | |
| Charlie Vieth | 08166b2 | 2022-02-05 18:47:43 -0500 | [diff] [blame] | 52 | /* Vector_softRemove marks the item at index idx for deletion without |
| 53 | reclaiming any space. If owned, the item is immediately freed. |
| 54 | |
| 55 | Vector_compact must be called to reclaim space.*/ |
| 56 | Object* Vector_softRemove(Vector* this, int idx); |
| 57 | |
| 58 | /* Vector_compact reclaims space free'd up by Vector_softRemove, if any. */ |
| 59 | void Vector_compact(Vector* this); |
| 60 | |
| Zev Weiss | 7b7822b | 2020-09-02 02:38:44 -0500 | [diff] [blame] | 61 | void Vector_moveUp(Vector* this, int idx); |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 62 | |
| Zev Weiss | 7b7822b | 2020-09-02 02:38:44 -0500 | [diff] [blame] | 63 | void Vector_moveDown(Vector* this, int idx); |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 64 | |
| Zev Weiss | 7b7822b | 2020-09-02 02:38:44 -0500 | [diff] [blame] | 65 | void Vector_set(Vector* this, int idx, void* data_); |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 66 | |
| Christian Göttsche | d69585b | 2020-09-17 22:27:33 +0200 | [diff] [blame] | 67 | #ifndef NDEBUG |
| Hisham Muhammad | 7ca1081 | 2011-11-18 06:08:56 +0000 | [diff] [blame] | 68 | |
| Benny Baumann | a94fd87 | 2020-11-17 01:27:27 +0100 | [diff] [blame] | 69 | Object* Vector_get(const Vector* this, int idx); |
| Christian Göttsche | 846fe8a | 2020-10-15 20:41:35 +0200 | [diff] [blame] | 70 | int Vector_size(const Vector* this); |
| Charlie Vieth | 08166b2 | 2022-02-05 18:47:43 -0500 | [diff] [blame] | 71 | |
| 72 | /* Vector_countEquals returns true if the number of non-NULL items |
| 73 | in the Vector is equal to expectedCount. This is only for debugging |
| 74 | and consistency checks. */ |
| 75 | bool Vector_countEquals(const Vector* this, unsigned int expectedCount); |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 76 | |
| Christian Göttsche | d69585b | 2020-09-17 22:27:33 +0200 | [diff] [blame] | 77 | #else /* NDEBUG */ |
| Hisham Muhammad | 7ca1081 | 2011-11-18 06:08:56 +0000 | [diff] [blame] | 78 | |
| Christian Göttsche | 330d4fe | 2021-01-06 17:14:06 +0100 | [diff] [blame] | 79 | static inline Object* Vector_get(const Vector* this, int idx) { |
| Christian Göttsche | e9246ab | 2020-10-15 21:45:38 +0200 | [diff] [blame] | 80 | return this->array[idx]; |
| 81 | } |
| 82 | |
| 83 | static inline int Vector_size(const Vector* this) { |
| 84 | return this->items; |
| 85 | } |
| Hisham Muhammad | f37a050 | 2018-02-05 11:01:35 +0100 | [diff] [blame] | 86 | |
| Christian Göttsche | d69585b | 2020-09-17 22:27:33 +0200 | [diff] [blame] | 87 | #endif /* NDEBUG */ |
| Hisham Muhammad | f37a050 | 2018-02-05 11:01:35 +0100 | [diff] [blame] | 88 | |
| Christian Göttsche | d37d66b | 2021-03-12 16:46:55 +0100 | [diff] [blame] | 89 | static inline const ObjectClass* Vector_type(const Vector* this) { |
| Benny Baumann | 458749d | 2021-07-14 19:15:09 +0200 | [diff] [blame] | 90 | return this->type; |
| Christian Göttsche | d37d66b | 2021-03-12 16:46:55 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| Zev Weiss | 7b7822b | 2020-09-02 02:38:44 -0500 | [diff] [blame] | 93 | void Vector_add(Vector* this, void* data_); |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 94 | |
| Christian Göttsche | 846fe8a | 2020-10-15 20:41:35 +0200 | [diff] [blame] | 95 | int Vector_indexOf(const Vector* this, const void* search_, Object_Compare compare); |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 96 | |
| Nathan Scott | 728b04b | 2020-08-26 10:15:00 +1000 | [diff] [blame] | 97 | void Vector_splice(Vector* this, Vector* from); |
| 98 | |
| Hisham Muhammad | a853faa | 2006-05-30 13:45:40 +0000 | [diff] [blame] | 99 | #endif |