1 | /****************************************************************************** |
---|
2 | * $Id: ptrarray.h 7582 2009-01-02 20:19:10Z charles $ |
---|
3 | * |
---|
4 | * This file Copyright (C) 2007-2008 Charles Kerr <charles@transmissionbt.com> |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #ifndef __TRANSMISSION__ |
---|
26 | #error only libtransmission should #include this header. |
---|
27 | #endif |
---|
28 | |
---|
29 | #ifndef _TR_PTR_ARRAY_H_ |
---|
30 | #define _TR_PTR_ARRAY_H_ |
---|
31 | |
---|
32 | #include "transmission.h" |
---|
33 | |
---|
34 | /** |
---|
35 | * A simple pointer array that resizes itself dynamically. |
---|
36 | */ |
---|
37 | typedef struct tr_ptrArray |
---|
38 | { |
---|
39 | void ** items; |
---|
40 | int n_items; |
---|
41 | int n_alloc; |
---|
42 | } |
---|
43 | tr_ptrArray; |
---|
44 | |
---|
45 | #define TR_PTR_ARRAY_DATA( A ) ((A)->items) |
---|
46 | #define TR_PTR_ARRAY_LENGTH( A ) ((A)->n_items) |
---|
47 | |
---|
48 | typedef void ( *PtrArrayForeachFunc )( void * ); |
---|
49 | |
---|
50 | extern const tr_ptrArray TR_PTR_ARRAY_INIT; |
---|
51 | |
---|
52 | void tr_ptrArrayDestruct( tr_ptrArray*, PtrArrayForeachFunc func ); |
---|
53 | |
---|
54 | tr_ptrArray * tr_ptrArrayNew( void ); |
---|
55 | |
---|
56 | tr_ptrArray * tr_ptrArrayDup( tr_ptrArray* ); |
---|
57 | |
---|
58 | void tr_ptrArrayForeach( tr_ptrArray * array, |
---|
59 | PtrArrayForeachFunc func ); |
---|
60 | |
---|
61 | void tr_ptrArrayFree( tr_ptrArray * array, |
---|
62 | PtrArrayForeachFunc func ); |
---|
63 | |
---|
64 | void* tr_ptrArrayNth( tr_ptrArray * array, |
---|
65 | int nth ); |
---|
66 | |
---|
67 | void* tr_ptrArrayBack( tr_ptrArray * array ); |
---|
68 | |
---|
69 | void** tr_ptrArrayPeek( tr_ptrArray * array, |
---|
70 | int * size ); |
---|
71 | |
---|
72 | static inline void tr_ptrArrayClear( tr_ptrArray * a ) { a->n_items = 0; } |
---|
73 | |
---|
74 | int tr_ptrArrayInsert( tr_ptrArray * array, |
---|
75 | void * insertMe, |
---|
76 | int pos ); |
---|
77 | |
---|
78 | static inline int tr_ptrArrayAppend( tr_ptrArray * array, void * appendMe ) |
---|
79 | { |
---|
80 | return tr_ptrArrayInsert( array, appendMe, -1 ); |
---|
81 | } |
---|
82 | |
---|
83 | void* tr_ptrArrayPop( tr_ptrArray * array ); |
---|
84 | |
---|
85 | void tr_ptrArrayErase( tr_ptrArray * array, |
---|
86 | int begin, |
---|
87 | int end ); |
---|
88 | |
---|
89 | static inline int tr_ptrArraySize( const tr_ptrArray * a ) |
---|
90 | { |
---|
91 | return a->n_items; |
---|
92 | } |
---|
93 | |
---|
94 | static inline tr_bool tr_ptrArrayEmpty( const tr_ptrArray * a ) |
---|
95 | { |
---|
96 | return tr_ptrArraySize(a) == 0; |
---|
97 | } |
---|
98 | |
---|
99 | int tr_ptrArrayInsertSorted( tr_ptrArray * array, |
---|
100 | void * value, |
---|
101 | int compare(const void*, const void*) ); |
---|
102 | |
---|
103 | void* tr_ptrArrayRemoveSorted( tr_ptrArray * array, |
---|
104 | void * value, |
---|
105 | int compare(const void*, const void*) ); |
---|
106 | |
---|
107 | void* tr_ptrArrayFindSorted( tr_ptrArray * array, |
---|
108 | const void * key, |
---|
109 | int compare(const void*, const void*) ); |
---|
110 | |
---|
111 | #endif |
---|