Commit 9a217f76 authored by Tim Peters's avatar Tim Peters

PreviousBucket(): Changed the signature. Before, a NULL return could

mean any of no error, an expected index error, or an unexpected error;
naturally, callers conflated those.  The return value now distinguishes
among the cases.

BTreeItems_seek():  Use the new form of PreviousBucket().
parent d6937996
......@@ -12,7 +12,7 @@
****************************************************************************/
#define BTREEITEMSTEMPLATE_C "$Id: BTreeItemsTemplate.c,v 1.14 2002/06/17 23:39:56 tim_one Exp $\n"
#define BTREEITEMSTEMPLATE_C "$Id: BTreeItemsTemplate.c,v 1.15 2002/06/18 02:26:19 tim_one Exp $\n"
/* A BTreeItems struct is returned from calling .items(), .keys() or
* .values() on a BTree-based data structure, and is also the result of
......@@ -188,6 +188,7 @@ BTreeItems_seek(BTreeItems *self, int i)
currentoffset = 0;
}
while (delta < 0) { /* move left */
int status;
/* Want to move left -delta positions; the most we can move left in
* this bucket is currentoffset positions.
*/
......@@ -206,10 +207,11 @@ BTreeItems_seek(BTreeItems *self, int i)
PER_ALLOW_DEACTIVATION(currentbucket);
PER_ACCESSED(currentbucket);
if (currentbucket == self->firstbucket) goto no_match;
b = PreviousBucket(currentbucket, self->firstbucket, i);
if (b == NULL) goto no_match; /* XXX this could be another error */
Py_DECREF(b); /* we didn't really want it incref'ed to begin with */
currentbucket = b;
status = PreviousBucket(&currentbucket, self->firstbucket);
if (status == 0)
goto no_match;
else if (status < 0)
return -1;
PER_USE_OR_RETURN(currentbucket, -1);
pseudoindex -= currentoffset + 1;
delta += currentoffset + 1;
......
......@@ -260,42 +260,39 @@ IndexError(int i)
return NULL;
}
/* Returns a new reference to the bucket before current, in the bucket
* chain starting at first.
* Returns NULL on error. IndexError(i) may or may not be set then (XXX I
* don't know what the intent is, that's just what it does; should be redone).
/* Search for the bucket immediately preceding *current, in the bucket chain
* starting at first. current, *current and first must not be NULL.
*
* Return:
* 1 *current holds the correct bucket; this is a borrowed reference
* 0 no such bucket exists; *current unaltered
* -1 error; *current unaltered
*/
static Bucket *
PreviousBucket(Bucket *current, Bucket *first, int i)
static int
PreviousBucket(Bucket **current, Bucket *first)
{
if (! first) return NULL;
if (first == current)
{
IndexError(i);
return NULL;
Bucket *trailing = NULL; /* first travels; trailing follows it */
int result = 0;
assert(current && *current && first);
if (first == *current)
return 0;
do {
trailing = first;
PER_USE_OR_RETURN(first, -1);
first = first->next;
PER_ALLOW_DEACTIVATION(trailing);
PER_ACCESSED(trailing);
if (first == *current) {
*current = trailing;
result = 1;
break;
}
} while (first);
while (1)
{
Bucket *next;
PER_USE_OR_RETURN(first, NULL);
next = first->next;
PER_ALLOW_DEACTIVATION(first);
PER_ACCESSED(first);
if (next == current)
{
Py_INCREF(first);
return first;
}
else if (next)
first=next;
else
{
IndexError(i);
return NULL;
}
}
return result;
}
static void *
......@@ -372,7 +369,7 @@ static char BTree_module_documentation[] =
"\n"
MASTER_ID
BTREEITEMSTEMPLATE_C
"$Id: BTreeModuleTemplate.c,v 1.33 2002/06/17 23:39:56 tim_one Exp $\n"
"$Id: BTreeModuleTemplate.c,v 1.34 2002/06/18 02:26:19 tim_one Exp $\n"
BTREETEMPLATE_C
BUCKETTEMPLATE_C
KEYMACROS_H
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment