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 8637 2009-06-06 17:39:04Z 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 | tr_ptrArray * tr_ptrArrayNew( void ); |
---|
45 | |
---|
46 | tr_ptrArray * tr_ptrArrayDup( tr_ptrArray* ); |
---|
47 | |
---|
48 | void tr_ptrArrayForeach( tr_ptrArray * array, |
---|
49 | PtrArrayForeachFunc func ); |
---|
50 | |
---|
51 | void tr_ptrArrayFree( tr_ptrArray * array, |
---|
52 | PtrArrayForeachFunc func ); |
---|
53 | |
---|
54 | void* tr_ptrArrayNth( tr_ptrArray * array, |
---|
55 | int nth ); |
---|
56 | |
---|
57 | void* tr_ptrArrayBack( tr_ptrArray * array ); |
---|
58 | |
---|
59 | void** tr_ptrArrayPeek( tr_ptrArray * array, |
---|
60 | int * size ); |
---|
61 | |
---|
62 | static TR_INLINE void tr_ptrArrayClear( tr_ptrArray * a ) { a->n_items = 0; } |
---|
63 | |
---|
64 | int tr_ptrArrayInsert( tr_ptrArray * array, |
---|
65 | void * insertMe, |
---|
66 | int pos ); |
---|
67 | |
---|
68 | static TR_INLINE int tr_ptrArrayAppend( tr_ptrArray * array, void * appendMe ) |
---|
69 | { |
---|
70 | return tr_ptrArrayInsert( array, appendMe, -1 ); |
---|
71 | } |
---|
72 | |
---|
73 | void* tr_ptrArrayPop( tr_ptrArray * array ); |
---|
74 | |
---|
75 | void tr_ptrArrayErase( tr_ptrArray * array, |
---|
76 | int begin, |
---|
77 | int end ); |
---|
78 | |
---|
79 | static TR_INLINE void** tr_ptrArrayBase( const tr_ptrArray * a ) |
---|
80 | { |
---|
81 | return a->items; |
---|
82 | } |
---|
83 | |
---|
84 | static TR_INLINE int tr_ptrArraySize( const tr_ptrArray * a ) |
---|
85 | { |
---|
86 | return a->n_items; |
---|
87 | } |
---|
88 | |
---|
89 | static TR_INLINE tr_bool tr_ptrArrayEmpty( const tr_ptrArray * a ) |
---|
90 | { |
---|
91 | return tr_ptrArraySize(a) == 0; |
---|
92 | } |
---|
93 | |
---|
94 | int tr_ptrArrayInsertSorted( tr_ptrArray * array, |
---|
95 | void * value, |
---|
96 | int compare(const void*, const void*) ); |
---|
97 | |
---|
98 | void* tr_ptrArrayRemoveSorted( tr_ptrArray * array, |
---|
99 | void * value, |
---|
100 | int compare(const void*, const void*) ); |
---|
101 | |
---|
102 | void* tr_ptrArrayFindSorted( tr_ptrArray * array, |
---|
103 | const void * key, |
---|
104 | int compare(const void*, const void*) ); |
---|
105 | |
---|
106 | /* @} */ |
---|
107 | #endif |
---|