source: trunk/libtransmission/ptrarray.h @ 9868

Last change on this file since 9868 was 9868, checked in by charles, 12 years ago

happy new year!

  • Property svn:keywords set to Date Rev Author Id
File size: 3.8 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 9868 2010-01-04 21:00:47Z 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 */
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
68
69/** @brief Peek at the array pointer and its size, for easy iteration */
70void** tr_ptrArrayPeek( tr_ptrArray * array, int * size );
71
72static 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 */
76int tr_ptrArrayInsert( tr_ptrArray * array, void * insertMe, int pos );
77
78/** @brief Append a pointer into the array */
79static inline int tr_ptrArrayAppend( tr_ptrArray * array, void * appendMe )
80{
81    return tr_ptrArrayInsert( array, appendMe, -1 );
82}
83
84static 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 */
91static 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 */
98static 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 */
105int 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 */
111void* 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 */
117void* tr_ptrArrayFindSorted( tr_ptrArray * array,
118                             const void  * key,
119                             int compare(const void*, const void*) );
120
121/* @} */
122#endif
Note: See TracBrowser for help on using the repository browser.