copyediting: indentation cleanup

This commit is contained in:
Jordan Lee 2012-12-13 01:47:40 +00:00
parent e2f1d6744e
commit d74c7e93c8
1 changed files with 102 additions and 104 deletions

View File

@ -24,88 +24,78 @@ const tr_ptrArray TR_PTR_ARRAY_INIT = { NULL, 0, 0 };
void
tr_ptrArrayDestruct (tr_ptrArray * p, PtrArrayForeachFunc func)
{
assert (p);
assert (p->items || !p->n_items);
assert (p != NULL);
assert (p->items || !p->n_items);
if (func)
tr_ptrArrayForeach (p, func);
if (func)
tr_ptrArrayForeach (p, func);
tr_free (p->items);
memset (p, ~0, sizeof (tr_ptrArray));
tr_free (p->items);
}
void
tr_ptrArrayForeach (tr_ptrArray * t,
PtrArrayForeachFunc func)
tr_ptrArrayForeach (tr_ptrArray * t, PtrArrayForeachFunc func)
{
int i;
int i;
assert (t);
assert (t->items || !t->n_items);
assert (func);
assert (t);
assert (t->items || !t->n_items);
assert (func);
for (i = 0; i < t->n_items; ++i)
func (t->items[i]);
for (i=0; i<t->n_items; ++i)
func (t->items[i]);
}
void**
tr_ptrArrayPeek (tr_ptrArray * t,
int * size)
tr_ptrArrayPeek (tr_ptrArray * t, int * size)
{
*size = t->n_items;
return t->items;
*size = t->n_items;
return t->items;
}
int
tr_ptrArrayInsert (tr_ptrArray * t,
void * ptr,
int pos)
tr_ptrArrayInsert (tr_ptrArray * t, void * ptr, int pos)
{
if (t->n_items >= t->n_alloc)
if (t->n_items >= t->n_alloc)
{
t->n_alloc = MAX (FLOOR, t->n_alloc * 2);
t->items = tr_renew (void*, t->items, t->n_alloc);
t->n_alloc = MAX (FLOOR, t->n_alloc * 2);
t->items = tr_renew (void*, t->items, t->n_alloc);
}
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));
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));
t->items[pos] = ptr;
t->n_items++;
return pos;
t->items[pos] = ptr;
t->n_items++;
return pos;
}
void*
tr_ptrArrayPop (tr_ptrArray* t)
{
void * ret = NULL;
void * ret = NULL;
if (t->n_items)
ret = t->items[--t->n_items];
if (t->n_items)
ret = t->items[--t->n_items];
return ret;
return ret;
}
void
tr_ptrArrayErase (tr_ptrArray * t,
int begin,
int end)
tr_ptrArrayErase (tr_ptrArray * t, int begin, int end)
{
assert (begin >= 0);
if (end < 0) end = t->n_items;
assert (begin < end);
assert (end <= t->n_items);
if (end < 0)
end = t->n_items;
memmove (t->items + begin,
t->items + end,
sizeof (void*) * (t->n_items - end));
assert (begin >= 0);
assert (begin < end);
assert (end <= t->n_items);
t->n_items -= (end - begin);
memmove (t->items+begin, t->items+end, sizeof(void*)*(t->n_items-end));
t->n_items -= (end - begin);
}
/**
@ -118,50 +108,56 @@ tr_ptrArrayLowerBound (const tr_ptrArray * t,
int compare (const void *, const void *),
bool * exact_match)
{
int pos = -1;
bool match = false;
int pos = -1;
bool match = false;
if (t->n_items == 0)
if (t->n_items == 0)
{
pos = 0;
pos = 0;
}
else
else
{
int first = 0;
int last = t->n_items - 1;
int first = 0;
int last = t->n_items - 1;
for (;;)
for (;;)
{
const int half = (last - first) / 2;
const int c = compare (t->items[first + half], ptr);
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;
if (c < 0)
{
const int new_first = first + half + 1;
if (new_first > last)
{
pos = new_first;
break;
}
first = new_first;
first = new_first;
}
else if (c > 0) {
const int new_last = first + half - 1;
if (new_last < first) {
pos = first;
break;
else if (c > 0)
{
const int new_last = first + half - 1;
if (new_last < first)
{
pos = first;
break;
}
last = new_last;
last = new_last;
}
else {
match = true;
pos = first + half;
break;
else
{
match = true;
pos = first + half;
break;
}
}
}
if (exact_match)
*exact_match = match;
return pos;
if (exact_match != NULL)
*exact_match = match;
return pos;
}
static void
@ -169,38 +165,38 @@ assertSortedAndUnique (const tr_ptrArray * t UNUSED,
int compare (const void*, const void*) UNUSED)
{
#ifndef NDEBUG
int i;
int i;
for (i = 0; i < t->n_items - 2; ++i)
assert (compare (t->items[i], t->items[i + 1]) < 0);
for (i=0; i<t->n_items-2; ++i)
assert (compare (t->items[i], t->items[i+1]) < 0);
#endif
}
int
tr_ptrArrayInsertSorted (tr_ptrArray * t,
void * ptr,
void * ptr,
int compare (const void*, const void*))
{
int pos;
int ret;
int pos;
int ret;
assertSortedAndUnique (t, compare);
assertSortedAndUnique (t, compare);
pos = tr_ptrArrayLowerBound (t, ptr, compare, NULL);
ret = tr_ptrArrayInsert (t, ptr, pos);
pos = tr_ptrArrayLowerBound (t, ptr, compare, NULL);
ret = tr_ptrArrayInsert (t, ptr, pos);
assertSortedAndUnique (t, compare);
return ret;
assertSortedAndUnique (t, compare);
return ret;
}
void*
tr_ptrArrayFindSorted (tr_ptrArray * t,
const void * ptr,
const void * ptr,
int compare (const void*, const void*))
{
bool match = false;
const int pos = tr_ptrArrayLowerBound (t, ptr, compare, &match);
return match ? t->items[pos] : NULL;
bool match = false;
const int pos = tr_ptrArrayLowerBound (t, ptr, compare, &match);
return match ? t->items[pos] : NULL;
}
void*
@ -208,18 +204,20 @@ tr_ptrArrayRemoveSorted (tr_ptrArray * t,
const void * ptr,
int compare (const void*, const void*))
{
bool match;
void * ret = NULL;
const int pos = tr_ptrArrayLowerBound (t, ptr, compare, &match);
assertSortedAndUnique (t, compare);
bool match;
void * ret = NULL;
const int pos = tr_ptrArrayLowerBound (t, ptr, compare, &match);
if (match)
assertSortedAndUnique (t, compare);
if (match)
{
ret = t->items[pos];
assert (compare (ret, ptr) == 0);
tr_ptrArrayErase (t, pos, pos + 1);
ret = t->items[pos];
assert (compare (ret, ptr) == 0);
tr_ptrArrayErase (t, pos, pos + 1);
}
assertSortedAndUnique (t, compare);
assert ((ret == NULL) || (compare (ret, ptr) == 0));
return ret;
assertSortedAndUnique (t, compare);
assert ((ret == NULL) || (compare (ret, ptr) == 0));
return ret;
}