2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright 2008-2022 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// 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
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring> /* memmove */
|
2007-08-14 20:45:23 +00:00
|
|
|
|
|
|
|
#include "ptrarray.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2020-08-18 11:09:30 +00:00
|
|
|
#include "tr-macros.h"
|
2007-08-14 20:45:23 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2022-01-17 18:39:50 +00:00
|
|
|
static auto constexpr Floor = int{ 32 };
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_ptrArrayDestruct(tr_ptrArray* p, PtrArrayForeachFunc func)
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(p != nullptr);
|
|
|
|
TR_ASSERT(p->items != nullptr || p->n_items == 0);
|
2008-12-29 08:54:36 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (func != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_ptrArrayForeach(p, func);
|
|
|
|
}
|
2008-12-29 08:54:36 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(p->items);
|
2008-12-29 08:54:36 +00:00
|
|
|
}
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_ptrArrayForeach(tr_ptrArray* t, PtrArrayForeachFunc func)
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(t != nullptr);
|
|
|
|
TR_ASSERT(t->items != nullptr || t->n_items == 0);
|
|
|
|
TR_ASSERT(func != nullptr);
|
2007-09-30 23:55:49 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 0; i < t->n_items; ++i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
func(t->items[i]);
|
|
|
|
}
|
2007-09-30 23:55:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void** tr_ptrArrayPeek(tr_ptrArray* t, int* size)
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
*size = t->n_items;
|
|
|
|
return t->items;
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
int tr_ptrArrayInsert(tr_ptrArray* t, void* ptr, int pos)
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (t->n_items >= t->n_alloc)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2022-01-17 18:39:50 +00:00
|
|
|
t->n_alloc = std::max(Floor, t->n_alloc * 2);
|
2017-04-19 12:04:45 +00:00
|
|
|
t->items = tr_renew(void*, t->items, t->n_alloc);
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (pos < 0 || pos > t->n_items)
|
|
|
|
{
|
|
|
|
pos = t->n_items;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memmove(t->items + pos + 1, t->items + pos, sizeof(void*) * (t->n_items - pos));
|
|
|
|
}
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
t->items[pos] = ptr;
|
|
|
|
t->n_items++;
|
|
|
|
return pos;
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_ptrArrayErase(tr_ptrArray* t, int begin, int end)
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (end < 0)
|
|
|
|
{
|
|
|
|
end = t->n_items;
|
|
|
|
}
|
2012-12-13 01:47:40 +00:00
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(begin >= 0);
|
|
|
|
TR_ASSERT(begin < end);
|
|
|
|
TR_ASSERT(end <= t->n_items);
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
memmove(t->items + begin, t->items + end, sizeof(void*) * (t->n_items - end));
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2017-04-30 16:30:03 +00:00
|
|
|
t->n_items -= end - begin;
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2019-02-10 11:05:16 +00:00
|
|
|
int tr_ptrArrayLowerBound(tr_ptrArray const* t, void const* ptr, tr_voidptr_compare_func compare, bool* exact_match)
|
2011-06-24 18:25:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int pos = -1;
|
|
|
|
bool match = false;
|
2011-06-24 18:25:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (t->n_items == 0)
|
2011-06-24 18:25:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
pos = 0;
|
2011-06-24 18:25:56 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2011-06-24 18:25:56 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int first = 0;
|
|
|
|
int last = t->n_items - 1;
|
2011-06-24 18:25:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (;;)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const half = (last - first) / 2;
|
|
|
|
int const c = compare(t->items[first + half], ptr);
|
2012-12-13 01:47:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (c < 0)
|
2012-12-13 01:47:40 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const new_first = first + half + 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (new_first > last)
|
2012-12-13 01:47:40 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
pos = new_first;
|
|
|
|
break;
|
2011-06-24 18:25:56 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
first = new_first;
|
2011-06-24 18:25:56 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (c > 0)
|
2012-12-13 01:47:40 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
int const new_last = first + half - 1;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (new_last < first)
|
2012-12-13 01:47:40 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
pos = first;
|
|
|
|
break;
|
2011-06-24 18:25:56 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
last = new_last;
|
2011-06-24 18:25:56 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-13 01:47:40 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
match = true;
|
|
|
|
pos = first + half;
|
|
|
|
break;
|
2011-06-24 18:25:56 +00:00
|
|
|
}
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (exact_match != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
*exact_match = match;
|
|
|
|
}
|
2012-12-13 01:47:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return pos;
|
2007-09-28 15:05:42 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 07:24:12 +00:00
|
|
|
#ifndef TR_ENABLE_ASSERTS
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
#define assertArrayIsSortedAndUnique(array, compare) /* no-op */
|
|
|
|
#define assertIndexIsSortedAndUnique(array, pos, compare) /* no-op */
|
|
|
|
|
2013-01-13 22:09:18 +00:00
|
|
|
#else
|
|
|
|
|
2019-02-10 11:05:16 +00:00
|
|
|
static void assertArrayIsSortedAndUnique(tr_ptrArray const* t, tr_voidptr_compare_func compare)
|
2007-09-28 15:05:42 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (t->items == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-09-10 23:50:46 +00:00
|
|
|
TR_ASSERT(t->n_items == 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = 0; i < t->n_items - 2; ++i)
|
|
|
|
{
|
|
|
|
TR_ASSERT(compare(t->items[i], t->items[i + 1]) < 0);
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-06-24 18:25:56 +00:00
|
|
|
}
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2019-02-10 11:05:16 +00:00
|
|
|
static void assertIndexIsSortedAndUnique(tr_ptrArray const* t, int pos, tr_voidptr_compare_func compare)
|
2013-01-13 22:09:18 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (pos > 0)
|
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(compare(t->items[pos - 1], t->items[pos]) < 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-13 22:09:18 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (pos + 1 < t->n_items)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(compare(t->items[pos], t->items[pos + 1]) < 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-13 22:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2019-02-10 11:05:16 +00:00
|
|
|
int tr_ptrArrayInsertSorted(tr_ptrArray* t, void* ptr, tr_voidptr_compare_func compare)
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
assertArrayIsSortedAndUnique(t, compare);
|
2011-06-24 18:25:56 +00:00
|
|
|
|
2021-10-26 18:02:07 +00:00
|
|
|
int const pos = tr_ptrArrayLowerBound(t, ptr, compare, nullptr);
|
|
|
|
int const ret = tr_ptrArrayInsert(t, ptr, pos);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assertIndexIsSortedAndUnique(t, ret, compare);
|
|
|
|
return ret;
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 11:05:16 +00:00
|
|
|
void* tr_ptrArrayFindSorted(tr_ptrArray* t, void const* ptr, tr_voidptr_compare_func compare)
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool match = false;
|
2017-04-20 16:02:19 +00:00
|
|
|
int const pos = tr_ptrArrayLowerBound(t, ptr, compare, &match);
|
2021-09-15 00:18:09 +00:00
|
|
|
return match ? t->items[pos] : nullptr;
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 11:05:16 +00:00
|
|
|
static void* tr_ptrArrayRemoveSortedValue(tr_ptrArray* t, void const* ptr, tr_voidptr_compare_func compare)
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
void* ret = nullptr;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
assertArrayIsSortedAndUnique(t, compare);
|
2013-01-13 22:09:18 +00:00
|
|
|
|
2021-10-26 18:02:07 +00:00
|
|
|
bool match = false;
|
|
|
|
int const pos = tr_ptrArrayLowerBound(t, ptr, compare, &match);
|
2012-12-13 01:47:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (match)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = t->items[pos];
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(compare(ret, ptr) == 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ptrArrayErase(t, pos, pos + 1);
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
2012-12-13 01:47:40 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(ret == nullptr || compare(ret, ptr) == 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
2013-08-17 17:20:31 +00:00
|
|
|
|
2019-02-10 11:05:16 +00:00
|
|
|
void tr_ptrArrayRemoveSortedPointer(tr_ptrArray* t, void const* ptr, tr_voidptr_compare_func compare)
|
2013-08-17 17:20:31 +00:00
|
|
|
{
|
2021-10-10 16:52:26 +00:00
|
|
|
[[maybe_unused]] void const* removed = tr_ptrArrayRemoveSortedValue(t, ptr, compare);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(removed != nullptr);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(removed == ptr);
|
2021-09-15 00:18:09 +00:00
|
|
|
TR_ASSERT(tr_ptrArrayFindSorted(t, ptr, compare) == nullptr);
|
2013-08-17 17:20:31 +00:00
|
|
|
}
|
2021-12-15 21:25:42 +00:00
|
|
|
|
|
|
|
void* tr_ptrArrayNth(tr_ptrArray* array, int i)
|
|
|
|
{
|
|
|
|
TR_ASSERT(array != nullptr);
|
|
|
|
TR_ASSERT(i >= 0);
|
|
|
|
TR_ASSERT(i < array->n_items);
|
|
|
|
|
|
|
|
return array->items[i];
|
|
|
|
}
|