1 | /* |
---|
2 | * This file Copyright (C) 2008-2010 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 9891 2010-01-06 00:18:33Z charles $ |
---|
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 "transmission.h" |
---|
21 | |
---|
22 | /** |
---|
23 | * @addtogroup utils Utilities |
---|
24 | * @{ |
---|
25 | */ |
---|
26 | |
---|
27 | /** |
---|
28 | * @brief simple pointer array that resizes itself dynamically. |
---|
29 | */ |
---|
30 | typedef struct tr_ptrArray |
---|
31 | { |
---|
32 | void ** items; |
---|
33 | int n_items; |
---|
34 | int n_alloc; |
---|
35 | } |
---|
36 | tr_ptrArray; |
---|
37 | |
---|
38 | typedef void ( *PtrArrayForeachFunc )( void * ); |
---|
39 | |
---|
40 | extern const tr_ptrArray TR_PTR_ARRAY_INIT; |
---|
41 | |
---|
42 | /** @brief Destructor to free a tr_ptrArray's internal memory */ |
---|
43 | void tr_ptrArrayDestruct( tr_ptrArray*, PtrArrayForeachFunc func ); |
---|
44 | |
---|
45 | /** @brief Iterate through each item in a tr_ptrArray */ |
---|
46 | void tr_ptrArrayForeach( tr_ptrArray * array, |
---|
47 | PtrArrayForeachFunc func ); |
---|
48 | |
---|
49 | /** @brief Return the nth item in a tr_ptrArray |
---|
50 | @return the nth item in a tr_ptrArray */ |
---|
51 | void* tr_ptrArrayNth( tr_ptrArray * array, |
---|
52 | int nth ); |
---|
53 | |
---|
54 | /** @brief Remove the last item from the array and return it |
---|
55 | @return the pointer that's been removed from the array |
---|
56 | @see tr_ptrArrayBack() */ |
---|
57 | void* tr_ptrArrayPop( tr_ptrArray * array ); |
---|
58 | |
---|
59 | /** @brief Return the last item in a tr_ptrArray |
---|
60 | @return the last item in a tr_ptrArray, or NULL if the array is empty |
---|
61 | @see tr_ptrArrayPop() */ |
---|
62 | static inline void* tr_ptrArrayBack( tr_ptrArray * array ) |
---|
63 | { |
---|
64 | return array->n_items > 0 ? tr_ptrArrayNth( array, array->n_items - 1 ) |
---|
65 | : NULL; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | /** @brief Peek at the array pointer and its size, for easy iteration */ |
---|
70 | void** tr_ptrArrayPeek( tr_ptrArray * array, int * size ); |
---|
71 | |
---|
72 | static inline void tr_ptrArrayClear( tr_ptrArray * a ) { a->n_items = 0; } |
---|
73 | |
---|
74 | /** @brief Insert a pointer into the array at the specified position |
---|
75 | @return the index of the stored pointer */ |
---|
76 | int tr_ptrArrayInsert( tr_ptrArray * array, void * insertMe, int pos ); |
---|
77 | |
---|
78 | /** @brief Append a pointer into the array */ |
---|
79 | static inline int tr_ptrArrayAppend( tr_ptrArray * array, void * appendMe ) |
---|
80 | { |
---|
81 | return tr_ptrArrayInsert( array, appendMe, -1 ); |
---|
82 | } |
---|
83 | |
---|
84 | static inline void** tr_ptrArrayBase( const tr_ptrArray * a ) |
---|
85 | { |
---|
86 | return a->items; |
---|
87 | } |
---|
88 | |
---|
89 | /** @brief Return the number of items in the array |
---|
90 | @return the number of items in the array */ |
---|
91 | static inline int tr_ptrArraySize( const tr_ptrArray * a ) |
---|
92 | { |
---|
93 | return a->n_items; |
---|
94 | } |
---|
95 | |
---|
96 | /** @brief Return True if the array has no pointers |
---|
97 | @return True if the array has no pointers */ |
---|
98 | static inline tr_bool tr_ptrArrayEmpty( const tr_ptrArray * a ) |
---|
99 | { |
---|
100 | return tr_ptrArraySize(a) == 0; |
---|
101 | } |
---|
102 | |
---|
103 | /** @brief Insert a pointer into the array at the position determined by the sort function |
---|
104 | @return the index of the stored pointer */ |
---|
105 | int tr_ptrArrayInsertSorted( tr_ptrArray * array, |
---|
106 | void * value, |
---|
107 | int compare(const void*, const void*) ); |
---|
108 | |
---|
109 | /** @brief Remove a pointer from an array sorted by the specified sort function |
---|
110 | @return the matching pointer, or NULL if no match was found */ |
---|
111 | void* tr_ptrArrayRemoveSorted( tr_ptrArray * array, |
---|
112 | void * value, |
---|
113 | int compare(const void*, const void*) ); |
---|
114 | |
---|
115 | /** @brief Find a pointer from an array sorted by the specified sort function |
---|
116 | @return the matching pointer, or NULL if no match was found */ |
---|
117 | void* tr_ptrArrayFindSorted( tr_ptrArray * array, |
---|
118 | const void * key, |
---|
119 | int compare(const void*, const void*) ); |
---|
120 | |
---|
121 | /* @} */ |
---|
122 | #endif |
---|