Commit 7ac182ab authored by Jim Fulton's avatar Jim Fulton

Factored out grow method.

parent fbaeeb41
...@@ -134,6 +134,41 @@ bucket_getitem(Bucket *self, PyObject *key) ...@@ -134,6 +134,41 @@ bucket_getitem(Bucket *self, PyObject *key)
return _bucket_get(self, key, 0); return _bucket_get(self, key, 0);
} }
static int
Bucket_grow(Bucket *self, int noval)
{
KEY_TYPE *keys;
VALUE_TYPE *values;
if (self->size)
{
UNLESS (keys=PyRealloc(self->keys, sizeof(KEY_TYPE)*self->size*2))
return -1;
UNLESS (noval)
{
UNLESS (values=PyRealloc(self->values,
sizeof(VALUE_TYPE)*self->size*2))
return -1;
self->values=values;
}
self->keys=keys;
self->size*=2;
}
else
{
UNLESS (self->keys=PyMalloc(sizeof(KEY_TYPE)*MIN_BUCKET_ALLOC))
return -1;
UNLESS (noval ||
(self->values=PyMalloc(sizeof(VALUE_TYPE)*MIN_BUCKET_ALLOC))
)
return -1;
self->size=MIN_BUCKET_ALLOC;
}
return 0;
}
/* /*
** _bucket_set ** _bucket_set
** **
...@@ -153,8 +188,6 @@ _bucket_set(Bucket *self, PyObject *keyarg, PyObject *v, int unique, int noval) ...@@ -153,8 +188,6 @@ _bucket_set(Bucket *self, PyObject *keyarg, PyObject *v, int unique, int noval)
{ {
int min, max, i, l, cmp, copied=1; int min, max, i, l, cmp, copied=1;
KEY_TYPE key; KEY_TYPE key;
KEY_TYPE *keys;
VALUE_TYPE *values;
COPY_KEY_FROM_ARG(key, keyarg, &copied); COPY_KEY_FROM_ARG(key, keyarg, &copied);
UNLESS(copied) return -1; UNLESS(copied) return -1;
...@@ -225,34 +258,8 @@ _bucket_set(Bucket *self, PyObject *keyarg, PyObject *v, int unique, int noval) ...@@ -225,34 +258,8 @@ _bucket_set(Bucket *self, PyObject *keyarg, PyObject *v, int unique, int noval)
goto err; goto err;
} }
if (self->len==self->size) if (self->len==self->size && Bucket_grow(self, noval) < 0) goto err;
{
if (self->size)
{
UNLESS (keys=PyRealloc(self->keys, sizeof(KEY_TYPE)*self->size*2))
goto err;
UNLESS (noval)
{
UNLESS (values=PyRealloc(self->values,
sizeof(VALUE_TYPE)*self->size*2))
goto err;
self->values=values;
}
self->keys=keys;
self->size*=2;
}
else
{
UNLESS (self->keys=PyMalloc(sizeof(KEY_TYPE)*MIN_BUCKET_ALLOC))
goto err;
UNLESS (noval ||
(self->values=PyMalloc(sizeof(VALUE_TYPE)*MIN_BUCKET_ALLOC))
)
goto err;
self->size=MIN_BUCKET_ALLOC;
}
}
if (max != i) i++; if (max != i) i++;
if (self->len > i) if (self->len > i)
......
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