blob: b7b39031e71798452c3c2bd7505eb355e649d39c [file] [log] [blame]
Hisham Muhammada853faa2006-05-30 13:45:40 +00001#ifndef HEADER_Vector
2#define HEADER_Vector
3/*
Hisham Muhammad84281bd2011-12-26 21:35:57 +00004htop - Vector.h
Hisham Muhammad300caa02011-05-26 16:35:07 +00005(C) 2004-2011 Hisham H. Muhammad
Daniel Lange94ad1112021-09-22 11:33:00 +02006Released under the GNU GPLv2+, see the COPYING file
Hisham Muhammada853faa2006-05-30 13:45:40 +00007in the source distribution for its full text.
8*/
9
10#include "Object.h"
Hisham Muhammada853faa2006-05-30 13:45:40 +000011
Benny Baumann0f526292020-09-19 13:55:23 +020012#include <stdbool.h>
13
Hisham Muhammad7ca10812011-11-18 06:08:56 +000014
Hisham Muhammada853faa2006-05-30 13:45:40 +000015#ifndef DEFAULT_SIZE
Christian Göttschee9246ab2020-10-15 21:45:38 +020016#define DEFAULT_SIZE (-1)
Hisham Muhammada853faa2006-05-30 13:45:40 +000017#endif
18
Hisham Muhammada853faa2006-05-30 13:45:40 +000019typedef struct Vector_ {
Benny Baumann61e14d42020-10-31 23:28:02 +010020 Object** array;
Christian Göttsche08d85e62020-10-04 17:55:08 +020021 const ObjectClass* type;
Hisham Muhammada853faa2006-05-30 13:45:40 +000022 int arraySize;
23 int growthRate;
24 int items;
Charlie Vieth08166b22022-02-05 18:47:43 -050025 /* 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 Muhammada853faa2006-05-30 13:45:40 +000030 bool owner;
31} Vector;
32
Christian Göttsche08d85e62020-10-04 17:55:08 +020033Vector* Vector_new(const ObjectClass* type, bool owner, int size);
Hisham Muhammada853faa2006-05-30 13:45:40 +000034
Zev Weiss7b7822b2020-09-02 02:38:44 -050035void Vector_delete(Vector* this);
Hisham Muhammada853faa2006-05-30 13:45:40 +000036
Zev Weiss7b7822b2020-09-02 02:38:44 -050037void Vector_prune(Vector* this);
Hisham Muhammada853faa2006-05-30 13:45:40 +000038
Christian Göttschea3bb7cb2020-10-21 21:26:09 +020039void Vector_quickSortCustomCompare(Vector* this, Object_Compare compare);
40static inline void Vector_quickSort(Vector* this) {
41 Vector_quickSortCustomCompare(this, this->type->compare);
42}
Hisham Muhammad7ca10812011-11-18 06:08:56 +000043
Zev Weiss7b7822b2020-09-02 02:38:44 -050044void Vector_insertionSort(Vector* this);
Hisham Muhammada853faa2006-05-30 13:45:40 +000045
Zev Weiss7b7822b2020-09-02 02:38:44 -050046void Vector_insert(Vector* this, int idx, void* data_);
Hisham Muhammada853faa2006-05-30 13:45:40 +000047
Zev Weiss7b7822b2020-09-02 02:38:44 -050048Object* Vector_take(Vector* this, int idx);
Hisham Muhammada853faa2006-05-30 13:45:40 +000049
Zev Weiss7b7822b2020-09-02 02:38:44 -050050Object* Vector_remove(Vector* this, int idx);
Hisham Muhammada853faa2006-05-30 13:45:40 +000051
Charlie Vieth08166b22022-02-05 18:47:43 -050052/* 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.*/
56Object* Vector_softRemove(Vector* this, int idx);
57
58/* Vector_compact reclaims space free'd up by Vector_softRemove, if any. */
59void Vector_compact(Vector* this);
60
Zev Weiss7b7822b2020-09-02 02:38:44 -050061void Vector_moveUp(Vector* this, int idx);
Hisham Muhammada853faa2006-05-30 13:45:40 +000062
Zev Weiss7b7822b2020-09-02 02:38:44 -050063void Vector_moveDown(Vector* this, int idx);
Hisham Muhammada853faa2006-05-30 13:45:40 +000064
Zev Weiss7b7822b2020-09-02 02:38:44 -050065void Vector_set(Vector* this, int idx, void* data_);
Hisham Muhammada853faa2006-05-30 13:45:40 +000066
Christian Göttsched69585b2020-09-17 22:27:33 +020067#ifndef NDEBUG
Hisham Muhammad7ca10812011-11-18 06:08:56 +000068
Benny Baumanna94fd872020-11-17 01:27:27 +010069Object* Vector_get(const Vector* this, int idx);
Christian Göttsche846fe8a2020-10-15 20:41:35 +020070int Vector_size(const Vector* this);
Charlie Vieth08166b22022-02-05 18:47:43 -050071
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. */
75bool Vector_countEquals(const Vector* this, unsigned int expectedCount);
Hisham Muhammada853faa2006-05-30 13:45:40 +000076
Christian Göttsched69585b2020-09-17 22:27:33 +020077#else /* NDEBUG */
Hisham Muhammad7ca10812011-11-18 06:08:56 +000078
Christian Göttsche330d4fe2021-01-06 17:14:06 +010079static inline Object* Vector_get(const Vector* this, int idx) {
Christian Göttschee9246ab2020-10-15 21:45:38 +020080 return this->array[idx];
81}
82
83static inline int Vector_size(const Vector* this) {
84 return this->items;
85}
Hisham Muhammadf37a0502018-02-05 11:01:35 +010086
Christian Göttsched69585b2020-09-17 22:27:33 +020087#endif /* NDEBUG */
Hisham Muhammadf37a0502018-02-05 11:01:35 +010088
Christian Göttsched37d66b2021-03-12 16:46:55 +010089static inline const ObjectClass* Vector_type(const Vector* this) {
Benny Baumann458749d2021-07-14 19:15:09 +020090 return this->type;
Christian Göttsched37d66b2021-03-12 16:46:55 +010091}
92
Zev Weiss7b7822b2020-09-02 02:38:44 -050093void Vector_add(Vector* this, void* data_);
Hisham Muhammada853faa2006-05-30 13:45:40 +000094
Christian Göttsche846fe8a2020-10-15 20:41:35 +020095int Vector_indexOf(const Vector* this, const void* search_, Object_Compare compare);
Hisham Muhammada853faa2006-05-30 13:45:40 +000096
Nathan Scott728b04b2020-08-26 10:15:00 +100097void Vector_splice(Vector* this, Vector* from);
98
Hisham Muhammada853faa2006-05-30 13:45:40 +000099#endif