2009-06-06 17:39:04 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2008-2014 Mnemosyne LLC
|
2007-08-18 17:05:51 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2008-03-25 19:49:32 +00:00
|
|
|
*
|
2009-06-06 17:39:04 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2009-06-06 17:39:04 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2008-11-24 20:17:36 +00:00
|
|
|
#endif
|
|
|
|
|
2008-03-25 19:49:32 +00:00
|
|
|
#ifndef _TR_PTR_ARRAY_H_
|
|
|
|
#define _TR_PTR_ARRAY_H_
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2011-03-14 14:09:41 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2009-01-02 20:19:10 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/**
|
|
|
|
* @addtogroup utils Utilities
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2007-08-14 20:45:23 +00:00
|
|
|
/**
|
2010-01-06 00:18:33 +00:00
|
|
|
* @brief simple pointer array that resizes itself dynamically.
|
2007-08-14 20:45:23 +00:00
|
|
|
*/
|
2008-12-29 08:54:36 +00:00
|
|
|
typedef struct tr_ptrArray
|
|
|
|
{
|
|
|
|
void ** items;
|
|
|
|
int n_items;
|
|
|
|
int n_alloc;
|
|
|
|
}
|
|
|
|
tr_ptrArray;
|
|
|
|
|
2013-01-21 21:14:14 +00:00
|
|
|
typedef int (*PtrArrayCompareFunc)(const void * a, const void * b);
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
typedef void (*PtrArrayForeachFunc)(void *);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
#define TR_PTR_ARRAY_INIT_STATIC { NULL, 0, 0 }
|
|
|
|
|
2008-12-29 08:54:36 +00:00
|
|
|
extern const tr_ptrArray TR_PTR_ARRAY_INIT;
|
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief Destructor to free a tr_ptrArray's internal memory */
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_ptrArrayDestruct (tr_ptrArray*, PtrArrayForeachFunc func);
|
2008-12-29 08:54:36 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief Iterate through each item in a tr_ptrArray */
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_ptrArrayForeach (tr_ptrArray * array,
|
|
|
|
PtrArrayForeachFunc func);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief Return the nth item in a tr_ptrArray
|
|
|
|
@return the nth item in a tr_ptrArray */
|
2011-03-14 14:09:41 +00:00
|
|
|
static inline void*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ptrArrayNth (tr_ptrArray * array, int i)
|
2011-03-14 14:09:41 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
assert (array);
|
|
|
|
assert (i >= 0);
|
|
|
|
assert (i < array->n_items);
|
2011-03-14 14:09:41 +00:00
|
|
|
|
|
|
|
return array->items[i];
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief Remove the last item from the array and return it
|
|
|
|
@return the pointer that's been removed from the array
|
2012-12-05 17:29:46 +00:00
|
|
|
@see tr_ptrArrayBack () */
|
|
|
|
void* tr_ptrArrayPop (tr_ptrArray * array);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief Return the last item in a tr_ptrArray
|
|
|
|
@return the last item in a tr_ptrArray, or NULL if the array is empty
|
2012-12-05 17:29:46 +00:00
|
|
|
@see tr_ptrArrayPop () */
|
|
|
|
static inline void* tr_ptrArrayBack (tr_ptrArray * array)
|
2010-01-01 22:13:27 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return array->n_items > 0 ? tr_ptrArrayNth (array, array->n_items - 1)
|
2010-01-01 22:13:27 +00:00
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
void tr_ptrArrayErase (tr_ptrArray * t, int begin, int end);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
static inline void tr_ptrArrayRemove (tr_ptrArray * t, int pos)
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ptrArrayErase (t, pos, pos+1);
|
2011-03-13 00:18:11 +00:00
|
|
|
}
|
2010-01-01 22:13:27 +00:00
|
|
|
|
|
|
|
/** @brief Peek at the array pointer and its size, for easy iteration */
|
2012-12-05 17:29:46 +00:00
|
|
|
void** tr_ptrArrayPeek (tr_ptrArray * array, int * size);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
static inline void tr_ptrArrayClear (tr_ptrArray * a) { a->n_items = 0; }
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief Insert a pointer into the array at the specified position
|
|
|
|
@return the index of the stored pointer */
|
2012-12-05 17:29:46 +00:00
|
|
|
int tr_ptrArrayInsert (tr_ptrArray * array, void * insertMe, int pos);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief Append a pointer into the array */
|
2012-12-05 17:29:46 +00:00
|
|
|
static inline int tr_ptrArrayAppend (tr_ptrArray * array, void * appendMe)
|
2009-01-02 20:19:10 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return tr_ptrArrayInsert (array, appendMe, -1);
|
2009-01-02 20:19:10 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
static inline void** tr_ptrArrayBase (const tr_ptrArray * a)
|
2009-01-04 16:59:15 +00:00
|
|
|
{
|
|
|
|
return a->items;
|
|
|
|
}
|
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief Return the number of items in the array
|
|
|
|
@return the number of items in the array */
|
2012-12-05 17:29:46 +00:00
|
|
|
static inline int tr_ptrArraySize (const tr_ptrArray * a)
|
2009-01-02 20:19:10 +00:00
|
|
|
{
|
|
|
|
return a->n_items;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief Return True if the array has no pointers
|
|
|
|
@return True if the array has no pointers */
|
2012-12-05 17:29:46 +00:00
|
|
|
static inline bool tr_ptrArrayEmpty (const tr_ptrArray * a)
|
2009-01-02 20:19:10 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return tr_ptrArraySize (a) == 0;
|
2009-01-02 20:19:10 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
int tr_ptrArrayLowerBound (const tr_ptrArray * array,
|
2010-06-19 14:25:11 +00:00
|
|
|
const void * key,
|
2012-12-05 17:29:46 +00:00
|
|
|
int compare (const void * arrayItem, const void * key),
|
|
|
|
bool * exact_match);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief Insert a pointer into the array at the position determined by the sort function
|
|
|
|
@return the index of the stored pointer */
|
2012-12-05 17:29:46 +00:00
|
|
|
int tr_ptrArrayInsertSorted (tr_ptrArray * array,
|
2010-01-01 22:13:27 +00:00
|
|
|
void * value,
|
2012-12-05 17:29:46 +00:00
|
|
|
int compare (const void*, const void*));
|
2010-01-01 22:13:27 +00:00
|
|
|
|
2013-08-17 17:20:31 +00:00
|
|
|
/** @brief Remove this specific pointer from a sorted ptrarray */
|
|
|
|
void tr_ptrArrayRemoveSortedPointer (tr_ptrArray * t,
|
|
|
|
const void * ptr,
|
|
|
|
int compare (const void*, const void*));
|
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
|
|
|
|
/** @brief Find a pointer from an array sorted by the specified sort function
|
|
|
|
@return the matching pointer, or NULL if no match was found */
|
2012-12-05 17:29:46 +00:00
|
|
|
void* tr_ptrArrayFindSorted (tr_ptrArray * array,
|
2010-01-01 22:13:27 +00:00
|
|
|
const void * key,
|
2012-12-05 17:29:46 +00:00
|
|
|
int compare (const void*, const void*));
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/* @} */
|
2007-08-14 20:45:23 +00:00
|
|
|
#endif
|