2 * This file Copyright (C) 2008-2014 Mnemosyne LLC
4 * It may be used under the GNU GPL versions 2 or 3
5 * or any future license endorsed by Mnemosyne LLC.
11 #ifndef __TRANSMISSION__
12 #error only libtransmission should #include this header.
15 #include "transmission.h"
16 #include "tr-assert.h"
19 * @addtogroup utils Utilities
24 * @brief simple pointer array that resizes itself dynamically.
26 typedef struct tr_ptrArray
34 typedef tr_voidptr_compare_func PtrArrayCompareFunc;
36 typedef void (* PtrArrayForeachFunc)(void*);
38 #define TR_PTR_ARRAY_INIT_STATIC { NULL, 0, 0 }
40 extern tr_ptrArray const TR_PTR_ARRAY_INIT;
42 /** @brief Destructor to free a tr_ptrArray's internal memory */
43 void tr_ptrArrayDestruct(tr_ptrArray*, PtrArrayForeachFunc func);
45 /** @brief Iterate through each item in a tr_ptrArray */
46 void tr_ptrArrayForeach(tr_ptrArray* array, PtrArrayForeachFunc func);
48 /** @brief Return the nth item in a tr_ptrArray
49 @return the nth item in a tr_ptrArray */
50 static inline void* tr_ptrArrayNth(tr_ptrArray* array, int i)
52 TR_ASSERT(array != NULL);
54 TR_ASSERT(i < array->n_items);
56 return array->items[i];
59 /** @brief Remove the last item from the array and return it
60 @return the pointer that's been removed from the array
61 @see tr_ptrArrayBack() */
62 void* tr_ptrArrayPop(tr_ptrArray* array);
64 /** @brief Return the last item in a tr_ptrArray
65 @return the last item in a tr_ptrArray, or NULL if the array is empty
66 @see tr_ptrArrayPop() */
67 static inline void* tr_ptrArrayBack(tr_ptrArray* array)
69 return array->n_items > 0 ? tr_ptrArrayNth(array, array->n_items - 1) : NULL;
72 void tr_ptrArrayErase(tr_ptrArray* t, int begin, int end);
74 static inline void tr_ptrArrayRemove(tr_ptrArray* t, int pos)
76 tr_ptrArrayErase(t, pos, pos + 1);
79 /** @brief Peek at the array pointer and its size, for easy iteration */
80 void** tr_ptrArrayPeek(tr_ptrArray* array, int* size);
82 static inline void tr_ptrArrayClear(tr_ptrArray* a)
87 /** @brief Insert a pointer into the array at the specified position
88 @return the index of the stored pointer */
89 int tr_ptrArrayInsert(tr_ptrArray* array, void* insertMe, int pos);
91 /** @brief Append a pointer into the array */
92 static inline int tr_ptrArrayAppend(tr_ptrArray* array, void* appendMe)
94 return tr_ptrArrayInsert(array, appendMe, -1);
97 static inline void** tr_ptrArrayBase(tr_ptrArray const* a)
102 /** @brief Return the number of items in the array
103 @return the number of items in the array */
104 static inline int tr_ptrArraySize(tr_ptrArray const* a)
109 /** @brief Return True if the array has no pointers
110 @return True if the array has no pointers */
111 static inline bool tr_ptrArrayEmpty(tr_ptrArray const* a)
113 return tr_ptrArraySize(a) == 0;
116 int tr_ptrArrayLowerBound(tr_ptrArray const* array, void const* key, tr_voidptr_compare_func compare, bool* exact_match);
118 /** @brief Insert a pointer into the array at the position determined by the sort function
119 @return the index of the stored pointer */
120 int tr_ptrArrayInsertSorted(tr_ptrArray* array, void* value, tr_voidptr_compare_func compare);
122 /** @brief Remove this specific pointer from a sorted ptrarray */
123 void tr_ptrArrayRemoveSortedPointer(tr_ptrArray* t, void const* ptr, tr_voidptr_compare_func compare);
125 /** @brief Find a pointer from an array sorted by the specified sort function
126 @return the matching pointer, or NULL if no match was found */
127 void* tr_ptrArrayFindSorted(tr_ptrArray* array, void const* key, tr_voidptr_compare_func compare);