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
|
|
|
*/
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2008-11-24 20:17:36 +00:00
|
|
|
#ifndef __TRANSMISSION__
|
2017-04-19 12:04:45 +00:00
|
|
|
#error only libtransmission should #include this header.
|
2008-11-24 20:17:36 +00:00
|
|
|
#endif
|
|
|
|
|
2016-03-29 16:37:21 +00:00
|
|
|
#pragma once
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2009-01-02 20:19:10 +00:00
|
|
|
#include "transmission.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2009-01-02 20:19:10 +00:00
|
|
|
|
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
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
void** items;
|
|
|
|
int n_items;
|
|
|
|
int n_alloc;
|
2008-12-29 08:54:36 +00:00
|
|
|
}
|
|
|
|
tr_ptrArray;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
typedef int (* PtrArrayCompareFunc)(void const* a, void const* b);
|
2013-01-21 21:14:14 +00:00
|
|
|
|
2017-04-19 12:04:45 +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 }
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
extern tr_ptrArray const TR_PTR_ARRAY_INIT;
|
2008-12-29 08:54:36 +00:00
|
|
|
|
2010-01-01 22:13:27 +00:00
|
|
|
/** @brief Destructor to free a tr_ptrArray's internal memory */
|
2017-04-19 12:04:45 +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 */
|
2017-04-19 12:04:45 +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 */
|
2017-04-19 12:04:45 +00:00
|
|
|
static inline void* tr_ptrArrayNth(tr_ptrArray* array, int i)
|
2011-03-14 14:09:41 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(array != NULL);
|
|
|
|
TR_ASSERT(i >= 0);
|
|
|
|
TR_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
|
2017-04-21 07:40:57 +00:00
|
|
|
@see tr_ptrArrayBack() */
|
2017-04-19 12:04:45 +00:00
|
|
|
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
|
2017-04-21 07:40:57 +00:00
|
|
|
@see tr_ptrArrayPop() */
|
2017-04-19 12:04:45 +00:00
|
|
|
static inline void* tr_ptrArrayBack(tr_ptrArray* array)
|
2010-01-01 22:13:27 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return array->n_items > 0 ? tr_ptrArrayNth(array, array->n_items - 1) : NULL;
|
2010-01-01 22:13:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_ptrArrayErase(tr_ptrArray* t, int begin, int end);
|
2010-06-19 14:25:11 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static inline void tr_ptrArrayRemove(tr_ptrArray* t, int pos)
|
2011-03-13 00:18:11 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +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 */
|
2017-04-19 12:04:45 +00:00
|
|
|
void** tr_ptrArrayPeek(tr_ptrArray* array, int* size);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +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 */
|
2017-04-19 12:04:45 +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 */
|
2017-04-19 12:04:45 +00:00
|
|
|
static inline int tr_ptrArrayAppend(tr_ptrArray* array, void* appendMe)
|
2009-01-02 20:19:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_ptrArrayInsert(array, appendMe, -1);
|
2009-01-02 20:19:10 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static inline void** tr_ptrArrayBase(tr_ptrArray const* 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 */
|
2017-04-20 16:02:19 +00:00
|
|
|
static inline int tr_ptrArraySize(tr_ptrArray const* 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 */
|
2017-04-20 16:02:19 +00:00
|
|
|
static inline bool tr_ptrArrayEmpty(tr_ptrArray const* a)
|
2009-01-02 20:19:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_ptrArraySize(a) == 0;
|
2009-01-02 20:19:10 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_ptrArrayLowerBound(tr_ptrArray const* array, void const* key, int (* compare)(void const* arrayItem, void const* 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 */
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_ptrArrayInsertSorted(tr_ptrArray* array, void* value, int (* compare)(void const*, void const*));
|
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 */
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_ptrArrayRemoveSortedPointer(tr_ptrArray* t, void const* ptr, int (* compare)(void const*, void const*));
|
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 */
|
2017-04-20 16:02:19 +00:00
|
|
|
void* tr_ptrArrayFindSorted(tr_ptrArray* array, void const* key, int (* compare)(void const*, void const*));
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/* @} */
|