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 @@ ...@@ -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 /* A BTreeItems struct is returned from calling .items(), .keys() or
* .values() on a BTree-based data structure, and is also the result of * .values() on a BTree-based data structure, and is also the result of
...@@ -188,6 +188,7 @@ BTreeItems_seek(BTreeItems *self, int i) ...@@ -188,6 +188,7 @@ BTreeItems_seek(BTreeItems *self, int i)
currentoffset = 0; currentoffset = 0;
} }
while (delta < 0) { /* move left */ while (delta < 0) { /* move left */
int status;
/* Want to move left -delta positions; the most we can move left in /* Want to move left -delta positions; the most we can move left in
* this bucket is currentoffset positions. * this bucket is currentoffset positions.
*/ */
...@@ -206,10 +207,11 @@ BTreeItems_seek(BTreeItems *self, int i) ...@@ -206,10 +207,11 @@ BTreeItems_seek(BTreeItems *self, int i)
PER_ALLOW_DEACTIVATION(currentbucket); PER_ALLOW_DEACTIVATION(currentbucket);
PER_ACCESSED(currentbucket); PER_ACCESSED(currentbucket);
if (currentbucket == self->firstbucket) goto no_match; if (currentbucket == self->firstbucket) goto no_match;
b = PreviousBucket(currentbucket, self->firstbucket, i); status = PreviousBucket(&currentbucket, self->firstbucket);
if (b == NULL) goto no_match; /* XXX this could be another error */ if (status == 0)
Py_DECREF(b); /* we didn't really want it incref'ed to begin with */ goto no_match;
currentbucket = b; else if (status < 0)
return -1;
PER_USE_OR_RETURN(currentbucket, -1); PER_USE_OR_RETURN(currentbucket, -1);
pseudoindex -= currentoffset + 1; pseudoindex -= currentoffset + 1;
delta += currentoffset + 1; delta += currentoffset + 1;
......
...@@ -260,42 +260,39 @@ IndexError(int i) ...@@ -260,42 +260,39 @@ IndexError(int i)
return NULL; return NULL;
} }
/* Returns a new reference to the bucket before current, in the bucket /* Search for the bucket immediately preceding *current, in the bucket chain
* chain starting at first. * starting at first. current, *current and first must not be NULL.
* 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). * 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 * static int
PreviousBucket(Bucket *current, Bucket *first, int i) PreviousBucket(Bucket **current, Bucket *first)
{ {
if (! first) return NULL; Bucket *trailing = NULL; /* first travels; trailing follows it */
if (first == current) int result = 0;
{
IndexError(i); assert(current && *current && first);
return NULL; if (first == *current)
} return 0;
while (1) do {
{ trailing = first;
Bucket *next; PER_USE_OR_RETURN(first, -1);
PER_USE_OR_RETURN(first, NULL); first = first->next;
next = first->next; PER_ALLOW_DEACTIVATION(trailing);
PER_ALLOW_DEACTIVATION(first); PER_ACCESSED(trailing);
PER_ACCESSED(first);
if (first == *current) {
if (next == current) *current = trailing;
{ result = 1;
Py_INCREF(first); break;
return first; }
} } while (first);
else if (next)
first=next; return result;
else
{
IndexError(i);
return NULL;
}
}
} }
static void * static void *
...@@ -372,7 +369,7 @@ static char BTree_module_documentation[] = ...@@ -372,7 +369,7 @@ static char BTree_module_documentation[] =
"\n" "\n"
MASTER_ID MASTER_ID
BTREEITEMSTEMPLATE_C 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 BTREETEMPLATE_C
BUCKETTEMPLATE_C BUCKETTEMPLATE_C
KEYMACROS_H 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