1 | /* |
---|
2 | * This file Copyright (C) 2008-2009 Charles Kerr <charles@transmissionbt.com> |
---|
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 9052 2009-09-07 06:23:15Z 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 | * A 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 | void tr_ptrArrayDestruct( tr_ptrArray*, PtrArrayForeachFunc func ); |
---|
43 | |
---|
44 | void tr_ptrArrayForeach( tr_ptrArray * array, |
---|
45 | PtrArrayForeachFunc func ); |
---|
46 | |
---|
47 | void* tr_ptrArrayNth( tr_ptrArray * array, |
---|
48 | int nth ); |
---|
49 | |
---|
50 | void* tr_ptrArrayBack( tr_ptrArray * array ); |
---|
51 | |
---|
52 | void** tr_ptrArrayPeek( tr_ptrArray * array, |
---|
53 | int * size ); |
---|
54 | |
---|
55 | static TR_INLINE void tr_ptrArrayClear( tr_ptrArray * a ) { a->n_items = 0; } |
---|
56 | |
---|
57 | int tr_ptrArrayInsert( tr_ptrArray * array, |
---|
58 | void * insertMe, |
---|
59 | int pos ); |
---|
60 | |
---|
61 | static TR_INLINE int tr_ptrArrayAppend( tr_ptrArray * array, void * appendMe ) |
---|
62 | { |
---|
63 | return tr_ptrArrayInsert( array, appendMe, -1 ); |
---|
64 | } |
---|
65 | |
---|
66 | void* tr_ptrArrayPop( tr_ptrArray * array ); |
---|
67 | |
---|
68 | void tr_ptrArrayErase( tr_ptrArray * array, |
---|
69 | int begin, |
---|
70 | int end ); |
---|
71 | |
---|
72 | static TR_INLINE void** tr_ptrArrayBase( const tr_ptrArray * a ) |
---|
73 | { |
---|
74 | return a->items; |
---|
75 | } |
---|
76 | |
---|
77 | static TR_INLINE int tr_ptrArraySize( const tr_ptrArray * a ) |
---|
78 | { |
---|
79 | return a->n_items; |
---|
80 | } |
---|
81 | |
---|
82 | static TR_INLINE tr_bool tr_ptrArrayEmpty( const tr_ptrArray * a ) |
---|
83 | { |
---|
84 | return tr_ptrArraySize(a) == 0; |
---|
85 | } |
---|
86 | |
---|
87 | int tr_ptrArrayInsertSorted( tr_ptrArray * array, |
---|
88 | void * value, |
---|
89 | int compare(const void*, const void*) ); |
---|
90 | |
---|
91 | void* tr_ptrArrayRemoveSorted( tr_ptrArray * array, |
---|
92 | void * value, |
---|
93 | int compare(const void*, const void*) ); |
---|
94 | |
---|
95 | void* tr_ptrArrayFindSorted( tr_ptrArray * array, |
---|
96 | const void * key, |
---|
97 | int compare(const void*, const void*) ); |
---|
98 | |
---|
99 | /* @} */ |
---|
100 | #endif |
---|