2008-07-22 23:28:28 +00:00
|
|
|
/*
|
2011-01-19 13:48:47 +00:00
|
|
|
* This file Copyright (C) Mnemosyne LLC
|
2007-08-14 20:45:23 +00:00
|
|
|
*
|
2010-12-27 19:18:17 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
2012-12-05 17:29:46 +00:00
|
|
|
* Transmission project are granted a special exemption to clause 2 (b)
|
2008-09-23 19:11:04 +00:00
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
2008-07-22 23:28:28 +00:00
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
2007-08-18 17:19:49 +00:00
|
|
|
*
|
2008-07-22 23:28:28 +00:00
|
|
|
* $Id$
|
|
|
|
*/
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2012-12-05 17:29:46 +00:00
|
|
|
#include <stdlib.h> /* tr_renew () -> realloc () */
|
2007-08-14 20:45:23 +00:00
|
|
|
#include <string.h> /* memmove */
|
|
|
|
|
|
|
|
#include "ptrarray.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2010-02-15 16:44:02 +00:00
|
|
|
#define FLOOR 32
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2012-12-22 20:35:19 +00:00
|
|
|
const tr_ptrArray TR_PTR_ARRAY_INIT = TR_PTR_ARRAY_INIT_STATIC;
|
2008-12-29 08:54:36 +00:00
|
|
|
|
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ptrArrayDestruct (tr_ptrArray * p, PtrArrayForeachFunc func)
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2012-12-13 01:47:40 +00:00
|
|
|
assert (p != NULL);
|
|
|
|
assert (p->items || !p->n_items);
|
2008-12-29 08:54:36 +00:00
|
|
|
|
2012-12-13 01:47:40 +00:00
|
|
|
if (func)
|
|
|
|
tr_ptrArrayForeach (p, func);
|
2008-12-29 08:54:36 +00:00
|
|
|
|
2012-12-13 01:47:40 +00:00
|
|
|
tr_free (p->items);
|
2008-12-29 08:54:36 +00:00
|
|
|
}
|
2007-08-14 20:45:23 +00:00
|
|
|
|
|
|
|
void
|
2012-12-13 01:47:40 +00:00
|
|
|
tr_ptrArrayForeach (tr_ptrArray * t, PtrArrayForeachFunc func)
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2012-12-13 01:47:40 +00:00
|
|
|
int i;
|
2007-09-30 23:55:49 +00:00
|
|
|
|
2012-12-13 01:47:40 +00:00
|
|
|
assert (t);
|
|
|
|
assert (t->items || !t->n_items);
|
|
|
|
assert (func);
|
2007-09-30 23:55:49 +00:00
|
|
|
|
2012-12-13 01:47:40 +00:00
|
|
|
for (i=0; i<t->n_items; ++i)
|
|
|
|
func (t->items[i]);
|
2007-09-30 23:55:49 +00:00
|
|
|
}
|
|
|
|
|
2007-08-14 20:45:23 +00:00
|
|
|
void**
|
2012-12-13 01:47:40 +00:00
|
|
|
tr_ptrArrayPeek (tr_ptrArray * t, int * size)
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2012-12-13 01:47:40 +00:00
|
|
|
*size = t->n_items;
|
|
|
|
return t->items;
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-13 01:47:40 +00:00
|
|
|
tr_ptrArrayInsert (tr_ptrArray * t, void * ptr, int pos)
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2012-12-13 01:47:40 +00:00
|
|
|
if (t->n_items >= t->n_alloc)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-13 01:47:40 +00:00
|
|
|
t->n_alloc = MAX (FLOOR, t->n_alloc * 2);
|
|
|
|
t->items = tr_renew (void*, t->items, t->n_alloc);
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
|
2012-12-13 01:47:40 +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
|
|
|
|
2012-12-13 01:47:40 +00:00
|
|
|
t->items[pos] = ptr;
|
|
|
|
t->n_items++;
|
|
|
|
return pos;
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
void*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ptrArrayPop (tr_ptrArray* t)
|
2008-01-31 02:24:43 +00:00
|
|
|
{
|
2012-12-13 01:47:40 +00:00
|
|
|
void * ret = NULL;
|
2008-01-31 02:24:43 +00:00
|
|
|
|
2012-12-13 01:47:40 +00:00
|
|
|
if (t->n_items)
|
|
|
|
ret = t->items[--t->n_items];
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-13 01:47:40 +00:00
|
|
|
return ret;
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
|
|
|
|
2010-06-19 14:25:11 +00:00
|
|
|
void
|
2012-12-13 01:47:40 +00:00
|
|
|
tr_ptrArrayErase (tr_ptrArray * t, int begin, int end)
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2012-12-13 01:47:40 +00:00
|
|
|
if (end < 0)
|
|
|
|
end = t->n_items;
|
|
|
|
|
|
|
|
assert (begin >= 0);
|
|
|
|
assert (begin < end);
|
|
|
|
assert (end <= t->n_items);
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2012-12-13 01:47:40 +00:00
|
|
|
memmove (t->items+begin, t->items+end, sizeof(void*)*(t->n_items-end));
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2012-12-13 01:47:40 +00:00
|
|
|
t->n_items -= (end - begin);
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2011-06-24 18:25:56 +00:00
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ptrArrayLowerBound (const tr_ptrArray * t,
|
2011-06-24 18:25:56 +00:00
|
|
|
const void * ptr,
|
2012-12-05 17:29:46 +00:00
|
|
|
int compare (const void *, const void *),
|
|
|
|
bool * exact_match)
|
2011-06-24 18:25:56 +00:00
|
|
|
{
|
2012-12-13 01:47:40 +00:00
|
|
|
int pos = -1;
|
|
|
|
bool match = false;
|
2011-06-24 18:25:56 +00:00
|
|
|
|
2012-12-13 01:47:40 +00:00
|
|
|
if (t->n_items == 0)
|
2011-06-24 18:25:56 +00:00
|
|
|
{
|
2012-12-13 01:47:40 +00:00
|
|
|
pos = 0;
|
2011-06-24 18:25:56 +00:00
|
|
|
}
|
2012-12-13 01:47:40 +00:00
|
|
|
else
|
2011-06-24 18:25:56 +00:00
|
|
|
{
|
2012-12-13 01:47:40 +00:00
|
|
|
int first = 0;
|
|
|
|
int last = t->n_items - 1;
|
2011-06-24 18:25:56 +00:00
|
|
|
|
2012-12-13 01:47:40 +00:00
|
|
|
for (;;)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-13 01:47:40 +00:00
|
|
|
const int half = (last - first) / 2;
|
|
|
|
const int c = compare (t->items[first + half], ptr);
|
|
|
|
|
|
|
|
if (c < 0)
|
|
|
|
{
|
|
|
|
const int new_first = first + half + 1;
|
|
|
|
if (new_first > last)
|
|
|
|
{
|
|
|
|
pos = new_first;
|
|
|
|
break;
|
2011-06-24 18:25:56 +00:00
|
|
|
}
|
2012-12-13 01:47:40 +00:00
|
|
|
first = new_first;
|
2011-06-24 18:25:56 +00:00
|
|
|
}
|
2012-12-13 01:47:40 +00:00
|
|
|
else if (c > 0)
|
|
|
|
{
|
|
|
|
const int new_last = first + half - 1;
|
|
|
|
if (new_last < first)
|
|
|
|
{
|
|
|
|
pos = first;
|
|
|
|
break;
|
2011-06-24 18:25:56 +00:00
|
|
|
}
|
2012-12-13 01:47:40 +00:00
|
|
|
last = new_last;
|
2011-06-24 18:25:56 +00:00
|
|
|
}
|
2012-12-13 01:47:40 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
match = true;
|
|
|
|
pos = first + half;
|
|
|
|
break;
|
2011-06-24 18:25:56 +00:00
|
|
|
}
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-13 01:47:40 +00:00
|
|
|
if (exact_match != NULL)
|
|
|
|
*exact_match = match;
|
|
|
|
|
|
|
|
return pos;
|
2007-09-28 15:05:42 +00:00
|
|
|
}
|
|
|
|
|
2013-01-13 22:09:18 +00:00
|
|
|
#ifdef NDEBUG
|
|
|
|
#define assertArrayIsSortedAndUnique(array,compare) /* no-op */
|
|
|
|
#define assertIndexIsSortedAndUnique(array,pos,compare) /* no-op */
|
|
|
|
#else
|
|
|
|
|
2007-09-28 15:05:42 +00:00
|
|
|
static void
|
2013-01-13 22:09:18 +00:00
|
|
|
assertArrayIsSortedAndUnique (const tr_ptrArray * t,
|
|
|
|
int compare (const void*, const void*))
|
2007-09-28 15:05:42 +00:00
|
|
|
{
|
2012-12-13 01:47:40 +00:00
|
|
|
int i;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-13 01:47:40 +00:00
|
|
|
for (i=0; i<t->n_items-2; ++i)
|
|
|
|
assert (compare (t->items[i], t->items[i+1]) < 0);
|
2011-06-24 18:25:56 +00:00
|
|
|
}
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2013-01-13 22:09:18 +00:00
|
|
|
static void
|
|
|
|
assertIndexIsSortedAndUnique (const tr_ptrArray * t,
|
|
|
|
int pos,
|
|
|
|
int compare (const void*, const void*))
|
|
|
|
{
|
|
|
|
if (pos > 0)
|
|
|
|
assert (compare (t->items[pos-1], t->items[pos]) < 0);
|
|
|
|
|
|
|
|
if ((pos + 1) < t->n_items)
|
|
|
|
assert (compare (t->items[pos], t->items[pos+1]) < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2007-08-14 20:45:23 +00:00
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ptrArrayInsertSorted (tr_ptrArray * t,
|
2012-12-13 01:47:40 +00:00
|
|
|
void * ptr,
|
2012-12-05 17:29:46 +00:00
|
|
|
int compare (const void*, const void*))
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2012-12-13 01:47:40 +00:00
|
|
|
int pos;
|
|
|
|
int ret;
|
2013-01-13 22:09:18 +00:00
|
|
|
assertArrayIsSortedAndUnique (t, compare);
|
2011-06-24 18:25:56 +00:00
|
|
|
|
2012-12-13 01:47:40 +00:00
|
|
|
pos = tr_ptrArrayLowerBound (t, ptr, compare, NULL);
|
|
|
|
ret = tr_ptrArrayInsert (t, ptr, pos);
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2013-01-13 22:09:18 +00:00
|
|
|
assertIndexIsSortedAndUnique (t, ret, compare);
|
2012-12-13 01:47:40 +00:00
|
|
|
return ret;
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_ptrArrayFindSorted (tr_ptrArray * t,
|
2012-12-13 01:47:40 +00:00
|
|
|
const void * ptr,
|
2012-12-05 17:29:46 +00:00
|
|
|
int compare (const void*, const void*))
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2012-12-13 01:47:40 +00:00
|
|
|
bool match = false;
|
|
|
|
const int pos = tr_ptrArrayLowerBound (t, ptr, compare, &match);
|
|
|
|
return match ? t->items[pos] : NULL;
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
|
2013-08-17 17:20:31 +00:00
|
|
|
static void*
|
|
|
|
tr_ptrArrayRemoveSortedValue (tr_ptrArray * t,
|
|
|
|
const void * ptr,
|
|
|
|
int compare (const void*, const void*))
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2013-01-13 22:09:18 +00:00
|
|
|
int pos;
|
2012-12-13 01:47:40 +00:00
|
|
|
bool match;
|
|
|
|
void * ret = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2013-01-13 22:09:18 +00:00
|
|
|
assertArrayIsSortedAndUnique (t, compare);
|
|
|
|
|
|
|
|
pos = tr_ptrArrayLowerBound (t, ptr, compare, &match);
|
2012-12-13 01:47:40 +00:00
|
|
|
|
|
|
|
if (match)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-13 01:47:40 +00:00
|
|
|
ret = t->items[pos];
|
|
|
|
assert (compare (ret, ptr) == 0);
|
|
|
|
tr_ptrArrayErase (t, pos, pos + 1);
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
2012-12-13 01:47:40 +00:00
|
|
|
|
|
|
|
assert ((ret == NULL) || (compare (ret, ptr) == 0));
|
|
|
|
return ret;
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
2013-08-17 17:20:31 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
tr_ptrArrayRemoveSortedPointer (tr_ptrArray * t,
|
|
|
|
const void * ptr,
|
|
|
|
int compare (const void*, const void*))
|
|
|
|
{
|
|
|
|
#ifdef NDEBUG
|
|
|
|
tr_ptrArrayRemoveSortedValue (t, ptr, compare);
|
|
|
|
#else
|
|
|
|
void * removed = tr_ptrArrayRemoveSortedValue (t, ptr, compare);
|
|
|
|
assert (removed != NULL);
|
|
|
|
assert (removed == ptr);
|
|
|
|
assert (tr_ptrArrayFindSorted (t, ptr, compare) == NULL);
|
|
|
|
#endif
|
|
|
|
}
|