source: trunk/libtransmission/ptrarray.h @ 13826

Last change on this file since 13826 was 13826, checked in by jordan, 8 years ago

(libT) #4632 'custom blocklist generates 2x # of rules': add a fix, which causes r13825's test to pass

  • Property svn:keywords set to Date Rev Author Id
File size: 4.4 KB
Line 
1/*
2 * This file Copyright (C) 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 13826 2013-01-21 21:14:14Z jordan $
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 <assert.h>
21
22#include "transmission.h"
23
24/**
25 * @addtogroup utils Utilities
26 * @{
27 */
28
29/**
30 * @brief simple pointer array that resizes itself dynamically.
31 */
32typedef struct tr_ptrArray
33{
34    void ** items;
35    int     n_items;
36    int     n_alloc;
37}
38tr_ptrArray;
39
40typedef int (*PtrArrayCompareFunc)(const void * a, const void * b);
41
42typedef void (*PtrArrayForeachFunc)(void *);
43
44#define TR_PTR_ARRAY_INIT_STATIC { NULL, 0, 0 }
45
46extern const tr_ptrArray TR_PTR_ARRAY_INIT;
47
48/** @brief Destructor to free a tr_ptrArray's internal memory */
49void tr_ptrArrayDestruct (tr_ptrArray*, PtrArrayForeachFunc func);
50
51/** @brief Iterate through each item in a tr_ptrArray */
52void tr_ptrArrayForeach (tr_ptrArray         * array,
53                         PtrArrayForeachFunc   func);
54
55/** @brief Return the nth item in a tr_ptrArray
56    @return the nth item in a tr_ptrArray */
57static inline void*
58tr_ptrArrayNth (tr_ptrArray * array, int i)
59{
60    assert (array);
61    assert (i >= 0);
62    assert (i < array->n_items);
63
64    return array->items[i];
65}
66
67/** @brief Remove the last item from the array and return it
68    @return the pointer that's been removed from the array
69    @see tr_ptrArrayBack () */
70void* tr_ptrArrayPop (tr_ptrArray * array);
71
72/** @brief Return the last item in a tr_ptrArray
73    @return the last item in a tr_ptrArray, or NULL if the array is empty
74    @see tr_ptrArrayPop () */
75static inline void* tr_ptrArrayBack (tr_ptrArray * array)
76{
77    return array->n_items > 0 ? tr_ptrArrayNth (array, array->n_items - 1)
78                              : NULL;
79}
80
81void tr_ptrArrayErase (tr_ptrArray * t, int begin, int end);
82
83static inline void tr_ptrArrayRemove (tr_ptrArray * t, int pos)
84{
85    tr_ptrArrayErase (t, pos, pos+1);
86}
87
88/** @brief Peek at the array pointer and its size, for easy iteration */
89void** tr_ptrArrayPeek (tr_ptrArray * array, int * size);
90
91static inline void  tr_ptrArrayClear (tr_ptrArray * a) { a->n_items = 0; }
92
93/** @brief Insert a pointer into the array at the specified position
94    @return the index of the stored pointer */
95int tr_ptrArrayInsert (tr_ptrArray * array, void * insertMe, int pos);
96
97/** @brief Append a pointer into the array */
98static inline int tr_ptrArrayAppend (tr_ptrArray * array, void * appendMe)
99{
100    return tr_ptrArrayInsert (array, appendMe, -1);
101}
102
103static inline void** tr_ptrArrayBase (const tr_ptrArray * a)
104{
105    return a->items;
106}
107
108/** @brief Return the number of items in the array
109    @return the number of items in the array */
110static inline int tr_ptrArraySize (const tr_ptrArray *  a)
111{
112    return a->n_items;
113}
114
115/** @brief Return True if the array has no pointers
116    @return True if the array has no pointers */
117static inline bool tr_ptrArrayEmpty (const tr_ptrArray * a)
118{
119    return tr_ptrArraySize (a) == 0;
120}
121
122int tr_ptrArrayLowerBound (const tr_ptrArray * array,
123                           const void * key,
124                           int compare (const void * arrayItem, const void * key),
125                           bool * exact_match);
126
127/** @brief Insert a pointer into the array at the position determined by the sort function
128    @return the index of the stored pointer */
129int tr_ptrArrayInsertSorted (tr_ptrArray * array,
130                             void        * value,
131                             int compare (const void*, const void*));
132
133/** @brief Remove a pointer from an array sorted by the specified sort function
134    @return the matching pointer, or NULL if no match was found */
135void* tr_ptrArrayRemoveSorted (tr_ptrArray * array,
136                               const void  * value,
137                               int compare (const void*, const void*));
138
139/** @brief Find a pointer from an array sorted by the specified sort function
140    @return the matching pointer, or NULL if no match was found */
141void* tr_ptrArrayFindSorted (tr_ptrArray * array,
142                             const void  * key,
143                             int compare (const void*, const void*));
144
145/* @} */
146#endif
Note: See TracBrowser for help on using the repository browser.