2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2017-11-14 20:21:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
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
|
|
|
|
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <cstddef> // size_t
|
|
|
|
|
2009-01-02 20:19:10 +00:00
|
|
|
#include "transmission.h"
|
2021-12-15 21:25:42 +00:00
|
|
|
|
|
|
|
#include "tr-macros.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
|
|
|
*/
|
2021-10-06 14:26:07 +00:00
|
|
|
struct tr_ptrArray
|
2008-12-29 08:54:36 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
void** items;
|
|
|
|
int n_items;
|
|
|
|
int n_alloc;
|
2021-10-06 14:26:07 +00:00
|
|
|
};
|
2008-12-29 08:54:36 +00:00
|
|
|
|
2021-11-09 03:30:03 +00:00
|
|
|
using tr_voidptr_compare_func = int (*)(void const* lhs, void const* rhs);
|
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
using PtrArrayCompareFunc = tr_voidptr_compare_func;
|
2013-01-21 21:14:14 +00:00
|
|
|
|
2021-10-06 14:26:07 +00:00
|
|
|
using PtrArrayForeachFunc = void (*)(void*);
|
2008-09-23 19:11:04 +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 */
|
2021-12-15 21:25:42 +00:00
|
|
|
void* tr_ptrArrayNth(tr_ptrArray* array, int 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
|
2021-10-06 16:32:17 +00:00
|
|
|
@return the last item in a tr_ptrArray, or nullptr if the array is empty
|
2017-04-21 07:40:57 +00:00
|
|
|
@see tr_ptrArrayPop() */
|
2021-10-06 22:24:04 +00:00
|
|
|
constexpr void* tr_ptrArrayBack(tr_ptrArray* array)
|
2010-01-01 22:13:27 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
return array->n_items > 0 ? tr_ptrArrayNth(array, array->n_items - 1) : nullptr;
|
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
|
|
|
|
2021-10-06 22:24:04 +00:00
|
|
|
constexpr void tr_ptrArrayClear(tr_ptrArray* a)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2021-10-06 22:24:04 +00:00
|
|
|
constexpr 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 */
|
2021-10-06 22:24:04 +00:00
|
|
|
constexpr 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 */
|
2021-10-06 22:24:04 +00:00
|
|
|
constexpr 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
|
|
|
|
2019-02-10 11:05:16 +00:00
|
|
|
int tr_ptrArrayLowerBound(tr_ptrArray const* array, void const* key, tr_voidptr_compare_func compare, 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 */
|
2019-02-10 11:05:16 +00:00
|
|
|
int tr_ptrArrayInsertSorted(tr_ptrArray* array, void* value, tr_voidptr_compare_func compare);
|
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 */
|
2019-02-10 11:05:16 +00:00
|
|
|
void tr_ptrArrayRemoveSortedPointer(tr_ptrArray* t, void const* ptr, tr_voidptr_compare_func compare);
|
2010-01-01 22:13:27 +00:00
|
|
|
|
|
|
|
/** @brief Find a pointer from an array sorted by the specified sort function
|
2021-10-06 16:32:17 +00:00
|
|
|
@return the matching pointer, or nullptr if no match was found */
|
2019-02-10 11:05:16 +00:00
|
|
|
void* tr_ptrArrayFindSorted(tr_ptrArray* array, void const* key, tr_voidptr_compare_func compare);
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2021-11-09 03:30:03 +00:00
|
|
|
/** @brief similar to bsearch() but returns the index of the lower bound */
|
|
|
|
int tr_lowerBound(
|
|
|
|
void const* key,
|
|
|
|
void const* base,
|
|
|
|
size_t nmemb,
|
|
|
|
size_t size,
|
|
|
|
tr_voidptr_compare_func compar,
|
|
|
|
bool* exact_match) TR_GNUC_HOT TR_GNUC_NONNULL(1, 5, 6);
|
|
|
|
|
2009-05-29 19:17:12 +00:00
|
|
|
/* @} */
|