source: trunk/libtransmission/ptrarray.h @ 11599

Last change on this file since 11599 was 11599, checked in by charles, 11 years ago

(trunk) Join the 21st century and use only 1 space at the end sentences. This commit is nearly as important as the semi-annual ones that remove trailing spaces from the ends of lines of code... :)

  • Property svn:keywords set to Date Rev Author Id
File size: 4.1 KB
Line 
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 11599 2010-12-27 19:18:17Z 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 */
30typedef struct tr_ptrArray
31{
32    void ** items;
33    int     n_items;
34    int     n_alloc;
35}
36tr_ptrArray;
37
38typedef void ( *PtrArrayForeachFunc )( void * );
39
40extern const tr_ptrArray TR_PTR_ARRAY_INIT;
41
42/** @brief Destructor to free a tr_ptrArray's internal memory */
43void tr_ptrArrayDestruct( tr_ptrArray*, PtrArrayForeachFunc func );
44
45/** @brief Iterate through each item in a tr_ptrArray */
46void 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 */
51void*         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() */
57void* 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() */
62static 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
68void tr_ptrArrayErase( tr_ptrArray * t, int begin, int end );
69
70
71
72/** @brief Peek at the array pointer and its size, for easy iteration */
73void** tr_ptrArrayPeek( tr_ptrArray * array, int * size );
74
75static inline void  tr_ptrArrayClear( tr_ptrArray * a ) { a->n_items = 0; }
76
77/** @brief Insert a pointer into the array at the specified position
78    @return the index of the stored pointer */
79int tr_ptrArrayInsert( tr_ptrArray * array, void * insertMe, int pos );
80
81/** @brief Append a pointer into the array */
82static inline int tr_ptrArrayAppend( tr_ptrArray * array, void * appendMe )
83{
84    return tr_ptrArrayInsert( array, appendMe, -1 );
85}
86
87static inline void** tr_ptrArrayBase( const tr_ptrArray * a )
88{
89    return a->items;
90}
91
92/** @brief Return the number of items in the array
93    @return the number of items in the array */
94static inline int tr_ptrArraySize( const tr_ptrArray *  a )
95{
96    return a->n_items;
97}
98
99/** @brief Return True if the array has no pointers
100    @return True if the array has no pointers */
101static inline tr_bool tr_ptrArrayEmpty( const tr_ptrArray * a )
102{
103    return tr_ptrArraySize(a) == 0;
104}
105
106int tr_ptrArrayLowerBound( const tr_ptrArray * array,
107                           const void * key,
108                           int compare( const void * arrayItem, const void * key ),
109                           tr_bool * exact_match );
110
111/** @brief Insert a pointer into the array at the position determined by the sort function
112    @return the index of the stored pointer */
113int tr_ptrArrayInsertSorted( tr_ptrArray * array,
114                             void        * value,
115                             int compare(const void*, const void*) );
116
117/** @brief Remove a pointer from an array sorted by the specified sort function
118    @return the matching pointer, or NULL if no match was found */
119void* tr_ptrArrayRemoveSorted( tr_ptrArray * array,
120                               const void  * value,
121                               int compare(const void*, const void*) );
122
123/** @brief Find a pointer from an array sorted by the specified sort function
124    @return the matching pointer, or NULL if no match was found */
125void* tr_ptrArrayFindSorted( tr_ptrArray * array,
126                             const void  * key,
127                             int compare(const void*, const void*) );
128
129/* @} */
130#endif
Note: See TracBrowser for help on using the repository browser.