2008-03-25 19:49:32 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2008-01-01 17:20:20 +00:00
|
|
|
* This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com>
|
2007-08-14 20:45:23 +00:00
|
|
|
*
|
2008-03-25 19:49:32 +00:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
2007-08-18 17:19:49 +00:00
|
|
|
*
|
2008-03-25 19:49:32 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
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
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
struct tr_ptrArray
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
|
|
|
void ** items;
|
|
|
|
int n_items;
|
|
|
|
int n_alloc;
|
|
|
|
};
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ptrArray*
|
2007-08-14 20:45:23 +00:00
|
|
|
tr_ptrArrayNew( void )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ptrArray * p;
|
2007-08-14 20:45:23 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
p = tr_new( tr_ptrArray, 1 );
|
2007-08-14 20:45:23 +00:00
|
|
|
p->n_items = 0;
|
|
|
|
p->n_alloc = GROW;
|
|
|
|
p->items = tr_new( void*, p->n_alloc );
|
|
|
|
|
|
|
|
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 );
|
|
|
|
out->n_items = in->n_items;
|
|
|
|
out->n_alloc = in->n_items;
|
|
|
|
out->items = tr_new( void*, out->n_alloc );
|
|
|
|
memcpy( out->items, in->items, out->n_items * sizeof(void*) );
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2007-08-14 20:45:23 +00:00
|
|
|
void
|
2007-09-30 23:55:49 +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;
|
|
|
|
|
2007-08-14 20:45:23 +00:00
|
|
|
assert( t != NULL );
|
|
|
|
assert( t->items != NULL );
|
2007-09-30 23:55:49 +00:00
|
|
|
assert( func != NULL );
|
|
|
|
|
|
|
|
for( i=0; i<t->n_items; ++i )
|
|
|
|
func( t->items[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_ptrArrayFree( tr_ptrArray * t, PtrArrayForeachFunc func )
|
|
|
|
{
|
|
|
|
assert( t != NULL );
|
|
|
|
assert( t->items != NULL );
|
|
|
|
|
|
|
|
if( func != NULL )
|
|
|
|
tr_ptrArrayForeach( t, func );
|
2007-08-14 20:45:23 +00:00
|
|
|
|
|
|
|
tr_free( t->items );
|
|
|
|
tr_free( t );
|
|
|
|
}
|
|
|
|
|
|
|
|
void**
|
2007-09-20 16:32:01 +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*
|
|
|
|
tr_ptrArrayNth( tr_ptrArray* t, int i )
|
|
|
|
{
|
|
|
|
assert( t != NULL );
|
|
|
|
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 );
|
|
|
|
|
|
|
|
return tr_ptrArrayNth( t, t->n_items-1 );
|
|
|
|
}
|
|
|
|
|
2007-08-14 20:45:23 +00:00
|
|
|
int
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ptrArraySize( const tr_ptrArray * t )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
|
|
|
return t->n_items;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ptrArrayEmpty( const tr_ptrArray * t )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
|
|
|
return t->n_items == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ptrArrayClear( tr_ptrArray * t )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
|
|
|
t->n_items = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ptrArrayInsert( tr_ptrArray * t, void * ptr, int pos )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
|
|
|
if( pos<0 || pos>t->n_items )
|
|
|
|
pos = t->n_items;
|
|
|
|
|
|
|
|
if( t->n_items >= t->n_alloc ) {
|
|
|
|
t->n_alloc = t->n_items + GROW;
|
|
|
|
t->items = tr_renew( void*, t->items, t->n_alloc );
|
|
|
|
}
|
|
|
|
|
|
|
|
memmove( t->items + pos + 1,
|
|
|
|
t->items + pos,
|
|
|
|
sizeof(void*) * (t->n_items - pos));
|
|
|
|
|
|
|
|
t->items[pos] = ptr;
|
|
|
|
t->n_items++;
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ptrArrayAppend( tr_ptrArray * t, void * ptr )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
|
|
|
return tr_ptrArrayInsert( t, ptr, -1 );
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-08-14 20:45:23 +00:00
|
|
|
void
|
2007-09-20 16:32:01 +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;
|
|
|
|
assert( end > begin );
|
|
|
|
assert( end <= t->n_items );
|
|
|
|
|
|
|
|
memmove( t->items + begin,
|
|
|
|
t->items + end,
|
|
|
|
sizeof(void*) * (t->n_items - end) );
|
|
|
|
|
|
|
|
t->n_items -= (end - begin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
int
|
2007-09-20 16:32:01 +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 )
|
|
|
|
{
|
|
|
|
int half = len / 2;
|
|
|
|
int middle = first + half;
|
2007-09-20 16:32:01 +00:00
|
|
|
const int c = compare( t->items[middle], ptr );
|
2007-08-14 20:45:23 +00:00
|
|
|
if( c < 0 ) {
|
|
|
|
first = middle + 1;
|
|
|
|
len = len - half - 1;
|
|
|
|
} else if (!c ) {
|
|
|
|
if( exact_match )
|
|
|
|
*exact_match = 1;
|
|
|
|
return middle;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
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,
|
|
|
|
int compare(const void*, const void*) )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for( i=0; i<t->n_items-2; ++i )
|
|
|
|
assert( compare( t->items[i], t->items[i+1] ) < 0 );
|
|
|
|
}
|
2007-08-14 20:45:23 +00:00
|
|
|
|
|
|
|
int
|
2007-09-20 16:32:01 +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 );
|
|
|
|
assertSortedAndUnique( t, compare );
|
|
|
|
return ret;
|
2007-08-14 20:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ptrArrayFindSorted( tr_ptrArray * t,
|
|
|
|
const void * ptr,
|
|
|
|
int compare(const void*,const void*) )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
|
|
|
int match;
|
|
|
|
const int pos = tr_ptrArrayLowerBound( t, ptr, compare, &match );
|
|
|
|
return match ? t->items[pos] : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ptrArrayRemoveSorted( tr_ptrArray * t,
|
|
|
|
void * ptr,
|
|
|
|
int compare(const void*,const void*) )
|
2007-08-14 20:45:23 +00:00
|
|
|
{
|
|
|
|
void * ret = NULL;
|
|
|
|
int match;
|
|
|
|
const int pos = tr_ptrArrayLowerBound( t, ptr, compare, &match );
|
|
|
|
if( match ) {
|
|
|
|
ret = t->items[pos];
|
|
|
|
tr_ptrArrayErase( t, pos, pos+1 );
|
|
|
|
}
|
2007-09-28 15:05:42 +00:00
|
|
|
assertSortedAndUnique( t, compare );
|
2007-08-14 20:45:23 +00:00
|
|
|
return ret;
|
|
|
|
}
|