2008-07-22 23:28:28 +00:00
|
|
|
/*
|
2008-12-16 00:20:44 +00:00
|
|
|
* This file Copyright (C) 2008 Charles Kerr <charles@transmissionbt.com>
|
2007-08-14 20:45:23 +00:00
|
|
|
*
|
2008-07-22 23:28:28 +00:00
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* 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>
|
2007-08-14 20:45:23 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h> /* memmove */
|
|
|
|
|
|
|
|
#include "ptrarray.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#define GROW 32
|
|
|
|
|
2008-12-29 08:54:36 +00:00
|
|
|
const tr_ptrArray TR_PTR_ARRAY_INIT = { NULL, 0, 0 };
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_ptrArrayDestruct( tr_ptrArray * p, PtrArrayForeachFunc func )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2008-12-29 08:54:36 +00:00
|
|
|
assert( p );
|
|
|
|
assert( p->items || !p->n_items );
|
|
|
|
|
|
|
|
if( func )
|
|
|
|
tr_ptrArrayForeach( p, func );
|
|
|
|
|
|
|
|
tr_free( p->items );
|
|
|
|
|
|
|
|
memset( p, ~0, sizeof( tr_ptrArray ) );
|
|
|
|
}
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ptrArray*
|
2007-08-14 20:45:23 +00:00
|
|
|
tr_ptrArrayNew( void )
|
|
|
|
{
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArray * p = tr_new( tr_ptrArray, 1 );
|
|
|
|
*p = TR_PTR_ARRAY_INIT;
|
2007-08-14 20:45:23 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2007-10-02 14:35:02 +00:00
|
|
|
tr_ptrArray*
|
|
|
|
tr_ptrArrayDup( tr_ptrArray* in )
|
|
|
|
{
|
|
|
|
tr_ptrArray * out;
|
|
|
|
|
|
|
|
out = tr_new( tr_ptrArray, 1 );
|
2008-06-07 21:26:41 +00:00
|
|
|
out->n_items = out->n_alloc = in->n_items;
|
2008-09-23 19:11:04 +00:00
|
|
|
out->items = tr_memdup( in->items, out->n_items * sizeof( void* ) );
|
2007-10-02 14:35:02 +00:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2007-08-14 20:45:23 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ptrArrayForeach( tr_ptrArray * t,
|
|
|
|
PtrArrayForeachFunc func )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2007-09-30 23:55:49 +00:00
|
|
|
int i;
|
|
|
|
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( t );
|
2008-10-25 02:15:37 +00:00
|
|
|
assert( t->items || !t->n_items );
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( func );
|
2007-09-30 23:55:49 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
for( i = 0; i < t->n_items; ++i )
|
2007-09-30 23:55:49 +00:00
|
|
|
func( t->items[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ptrArrayFree( tr_ptrArray * t,
|
|
|
|
PtrArrayForeachFunc func )
|
2007-09-30 23:55:49 +00:00
|
|
|
{
|
2008-12-29 08:54:36 +00:00
|
|
|
tr_ptrArrayDestruct( t, func );
|
2007-08-14 20:45:23 +00:00
|
|
|
tr_free( t );
|
|
|
|
}
|
|
|
|
|
|
|
|
void**
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ptrArrayPeek( tr_ptrArray * t,
|
|
|
|
int * size )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
|
|
|
*size = t->n_items;
|
|
|
|
return t->items;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ptrArrayNth( tr_ptrArray* t,
|
|
|
|
int i )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2008-08-01 16:43:22 +00:00
|
|
|
assert( t );
|
2007-09-20 16:32:01 +00:00
|
|
|
assert( i >= 0 );
|
|
|
|
assert( i < t->n_items );
|
|
|
|
|
|
|
|
return t->items[i];
|
|
|
|
}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
void*
|
|
|
|
tr_ptrArrayBack( tr_ptrArray* t )
|
|
|
|
{
|
|
|
|
assert( t->n_items > 0 );
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
return tr_ptrArrayNth( t, t->n_items - 1 );
|
2008-01-31 02:24:43 +00:00
|
|
|
}
|
|
|
|
|
2007-08-14 20:45:23 +00:00
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ptrArrayInsert( tr_ptrArray * t,
|
|
|
|
void * ptr,
|
|
|
|
int pos )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
if( pos < 0 || pos > t->n_items )
|
2007-08-14 20:45:23 +00:00
|
|
|
pos = t->n_items;
|
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
if( t->n_items >= t->n_alloc )
|
|
|
|
{
|
2007-08-14 20:45:23 +00:00
|
|
|
t->n_alloc = t->n_items + GROW;
|
|
|
|
t->items = tr_renew( void*, t->items, t->n_alloc );
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove( t->items + pos + 1,
|
2008-09-23 19:11:04 +00:00
|
|
|
t->items + pos,
|
|
|
|
sizeof( void* ) * ( t->n_items - pos ) );
|
2007-08-14 20:45:23 +00:00
|
|
|
|
|
|
|
t->items[pos] = ptr;
|
|
|
|
t->n_items++;
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
void*
|
|
|
|
tr_ptrArrayPop( tr_ptrArray* t )
|
|
|
|
{
|
|
|
|
void * ret = NULL;
|
|
|
|
|
|
|
|
if( t->n_items )
|
|
|
|
ret = t->items[--t->n_items];
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-01-31 02:24:43 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-08-14 20:45:23 +00:00
|
|
|
void
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ptrArrayErase( tr_ptrArray * t,
|
|
|
|
int begin,
|
|
|
|
int end )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
|
|
|
assert( begin >= 0 );
|
|
|
|
if( end < 0 ) end = t->n_items;
|
2008-09-04 14:42:32 +00:00
|
|
|
assert( end - begin > 0 );
|
2007-08-14 20:45:23 +00:00
|
|
|
assert( end <= t->n_items );
|
|
|
|
|
|
|
|
memmove( t->items + begin,
|
2008-09-23 19:11:04 +00:00
|
|
|
t->items + end,
|
|
|
|
sizeof( void* ) * ( t->n_items - end ) );
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2008-09-23 19:11:04 +00:00
|
|
|
t->n_items -= ( end - begin );
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2008-08-11 19:05:02 +00:00
|
|
|
static int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ptrArrayLowerBound( const tr_ptrArray * t,
|
|
|
|
const void * ptr,
|
|
|
|
int compare( const void *,
|
|
|
|
const void * ),
|
|
|
|
int * exact_match )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
|
|
|
int len = t->n_items;
|
|
|
|
int first = 0;
|
|
|
|
|
|
|
|
while( len > 0 )
|
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int half = len / 2;
|
|
|
|
int middle = first + half;
|
2007-09-20 16:32:01 +00:00
|
|
|
const int c = compare( t->items[middle], ptr );
|
2008-09-23 19:11:04 +00:00
|
|
|
if( c < 0 )
|
|
|
|
{
|
2007-08-14 20:45:23 +00:00
|
|
|
first = middle + 1;
|
|
|
|
len = len - half - 1;
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else if( !c )
|
|
|
|
{
|
2007-08-14 20:45:23 +00:00
|
|
|
if( exact_match )
|
|
|
|
*exact_match = 1;
|
|
|
|
return middle;
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-08-14 20:45:23 +00:00
|
|
|
len = half;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( exact_match )
|
|
|
|
*exact_match = 0;
|
|
|
|
|
|
|
|
return first;
|
2007-09-28 15:05:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
assertSortedAndUnique( const tr_ptrArray * t,
|
2008-09-23 19:11:04 +00:00
|
|
|
int compare(const void*, const void*) )
|
2007-09-28 15:05:42 +00:00
|
|
|
{
|
|
|
|
int i;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
for( i = 0; i < t->n_items - 2; ++i )
|
|
|
|
assert( compare( t->items[i], t->items[i + 1] ) < 0 );
|
2007-09-28 15:05:42 +00:00
|
|
|
}
|
2007-08-14 20:45:23 +00:00
|
|
|
|
|
|
|
int
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ptrArrayInsertSorted( tr_ptrArray * t,
|
|
|
|
void * ptr,
|
|
|
|
int compare(const void*, const void*) )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
|
|
|
const int pos = tr_ptrArrayLowerBound( t, ptr, compare, NULL );
|
2007-09-28 15:05:42 +00:00
|
|
|
const int ret = tr_ptrArrayInsert( t, ptr, pos );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-09-28 15:05:42 +00:00
|
|
|
assertSortedAndUnique( t, compare );
|
|
|
|
return ret;
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ptrArrayFindSorted( tr_ptrArray * t,
|
|
|
|
const void * ptr,
|
|
|
|
int compare(const void*, const void*) )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
int match;
|
2007-08-14 20:45:23 +00:00
|
|
|
const int pos = tr_ptrArrayLowerBound( t, ptr, compare, &match );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2007-08-14 20:45:23 +00:00
|
|
|
return match ? t->items[pos] : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ptrArrayRemoveSorted( tr_ptrArray * t,
|
|
|
|
void * ptr,
|
|
|
|
int compare(const void*, const void*) )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
2008-09-23 19:11:04 +00:00
|
|
|
void * ret = NULL;
|
|
|
|
int match;
|
2007-08-14 20:45:23 +00:00
|
|
|
const int pos = tr_ptrArrayLowerBound( t, ptr, compare, &match );
|
2008-09-23 19:11:04 +00:00
|
|
|
|
|
|
|
if( match )
|
|
|
|
{
|
2007-08-14 20:45:23 +00:00
|
|
|
ret = t->items[pos];
|
2008-09-23 19:11:04 +00:00
|
|
|
tr_ptrArrayErase( t, pos, pos + 1 );
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
2007-09-28 15:05:42 +00:00
|
|
|
assertSortedAndUnique( t, compare );
|
2007-08-14 20:45:23 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|