1 | /* |
---|
2 | * This file Copyright (C) Mnemosyne LLC |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2 (b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: ptrarray.h 13683 2012-12-22 20:35:19Z jordan $ |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef __TRANSMISSION__ |
---|
14 | #error only libtransmission should #include this header. |
---|
15 | #endif |
---|
16 | |
---|
17 | #ifndef _TR_PTR_ARRAY_H_ |
---|
18 | #define _TR_PTR_ARRAY_H_ |
---|
19 | |
---|
20 | #include <assert.h> |
---|
21 | |
---|
22 | #include "transmission.h" |
---|
23 | |
---|
24 | /** |
---|
25 | * @addtogroup utils Utilities |
---|
26 | * @{ |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | * @brief simple pointer array that resizes itself dynamically. |
---|
31 | */ |
---|
32 | typedef struct tr_ptrArray |
---|
33 | { |
---|
34 | void ** items; |
---|
35 | int n_items; |
---|
36 | int n_alloc; |
---|
37 | } |
---|
38 | tr_ptrArray; |
---|
39 | |
---|
40 | typedef void (*PtrArrayForeachFunc)(void *); |
---|
41 | |
---|
42 | #define TR_PTR_ARRAY_INIT_STATIC { NULL, 0, 0 } |
---|
43 | |
---|
44 | extern const tr_ptrArray TR_PTR_ARRAY_INIT; |
---|
45 | |
---|
46 | /** @brief Destructor to free a tr_ptrArray's internal memory */ |
---|
47 | void tr_ptrArrayDestruct (tr_ptrArray*, PtrArrayForeachFunc func); |
---|
48 | |
---|
49 | /** @brief Iterate through each item in a tr_ptrArray */ |
---|
50 | void tr_ptrArrayForeach (tr_ptrArray * array, |
---|
51 | PtrArrayForeachFunc func); |
---|
52 | |
---|
53 | /** @brief Return the nth item in a tr_ptrArray |
---|
54 | @return the nth item in a tr_ptrArray */ |
---|
55 | static inline void* |
---|
56 | tr_ptrArrayNth (tr_ptrArray * array, int i) |
---|
57 | { |
---|
58 | assert (array); |
---|
59 | assert (i >= 0); |
---|
60 | assert (i < array->n_items); |
---|
61 | |
---|
62 | return array->items[i]; |
---|
63 | } |
---|
64 | |
---|
65 | /** @brief Remove the last item from the array and return it |
---|
66 | @return the pointer that's been removed from the array |
---|
67 | @see tr_ptrArrayBack () */ |
---|
68 | void* tr_ptrArrayPop (tr_ptrArray * array); |
---|
69 | |
---|
70 | /** @brief Return the last item in a tr_ptrArray |
---|
71 | @return the last item in a tr_ptrArray, or NULL if the array is empty |
---|
72 | @see tr_ptrArrayPop () */ |
---|
73 | static inline void* tr_ptrArrayBack (tr_ptrArray * array) |
---|
74 | { |
---|
75 | return array->n_items > 0 ? tr_ptrArrayNth (array, array->n_items - 1) |
---|
76 | : NULL; |
---|
77 | } |
---|
78 | |
---|
79 | void tr_ptrArrayErase (tr_ptrArray * t, int begin, int end); |
---|
80 | |
---|
81 | static inline void tr_ptrArrayRemove (tr_ptrArray * t, int pos) |
---|
82 | { |
---|
83 | tr_ptrArrayErase (t, pos, pos+1); |
---|
84 | } |
---|
85 | |
---|
86 | /** @brief Peek at the array pointer and its size, for easy iteration */ |
---|
87 | void** tr_ptrArrayPeek (tr_ptrArray * array, int * size); |
---|
88 | |
---|
89 | static inline void tr_ptrArrayClear (tr_ptrArray * a) { a->n_items = 0; } |
---|
90 | |
---|
91 | /** @brief Insert a pointer into the array at the specified position |
---|
92 | @return the index of the stored pointer */ |
---|
93 | int tr_ptrArrayInsert (tr_ptrArray * array, void * insertMe, int pos); |
---|
94 | |
---|
95 | /** @brief Append a pointer into the array */ |
---|
96 | static inline int tr_ptrArrayAppend (tr_ptrArray * array, void * appendMe) |
---|
97 | { |
---|
98 | return tr_ptrArrayInsert (array, appendMe, -1); |
---|
99 | } |
---|
100 | |
---|
101 | static inline void** tr_ptrArrayBase (const tr_ptrArray * a) |
---|
102 | { |
---|
103 | return a->items; |
---|
104 | } |
---|
105 | |
---|
106 | /** @brief Return the number of items in the array |
---|
107 | @return the number of items in the array */ |
---|
108 | static inline int tr_ptrArraySize (const tr_ptrArray * a) |
---|
109 | { |
---|
110 | return a->n_items; |
---|
111 | } |
---|
112 | |
---|
113 | /** @brief Return True if the array has no pointers |
---|
114 | @return True if the array has no pointers */ |
---|
115 | static inline bool tr_ptrArrayEmpty (const tr_ptrArray * a) |
---|
116 | { |
---|
117 | return tr_ptrArraySize (a) == 0; |
---|
118 | } |
---|
119 | |
---|
120 | int tr_ptrArrayLowerBound (const tr_ptrArray * array, |
---|
121 | const void * key, |
---|
122 | int compare (const void * arrayItem, const void * key), |
---|
123 | bool * exact_match); |
---|
124 | |
---|
125 | /** @brief Insert a pointer into the array at the position determined by the sort function |
---|
126 | @return the index of the stored pointer */ |
---|
127 | int tr_ptrArrayInsertSorted (tr_ptrArray * array, |
---|
128 | void * value, |
---|
129 | int compare (const void*, const void*)); |
---|
130 | |
---|
131 | /** @brief Remove a pointer from an array sorted by the specified sort function |
---|
132 | @return the matching pointer, or NULL if no match was found */ |
---|
133 | void* tr_ptrArrayRemoveSorted (tr_ptrArray * array, |
---|
134 | const void * value, |
---|
135 | int compare (const void*, const void*)); |
---|
136 | |
---|
137 | /** @brief Find a pointer from an array sorted by the specified sort function |
---|
138 | @return the matching pointer, or NULL if no match was found */ |
---|
139 | void* tr_ptrArrayFindSorted (tr_ptrArray * array, |
---|
140 | const void * key, |
---|
141 | int compare (const void*, const void*)); |
---|
142 | |
---|
143 | /* @} */ |
---|
144 | #endif |
---|