Commit 33f7fd31 authored by Per Cederqvist's avatar Per Cederqvist
Browse files

Bug fixes.

(l2g_next_key): Implemented.
parent e5c38211
......@@ -11,7 +11,7 @@
#include <malloc.h>
#include "kom-types.h"
#include "local_to_global.h"
#include "local-to-global.h"
/* ================================================================ */
......@@ -95,6 +95,10 @@ find_block(Local_to_global *l2g, Local_text_no lno)
{
L2g_block_info * binfo;
/* If empty, the number can not be in here. */
if (l2g->num_blocks == 0)
return NULL;
/* Let binfo point to the last block. */
binfo = &(l2g->blocks[l2g->num_blocks - 1]);
......@@ -117,7 +121,7 @@ find_block(Local_to_global *l2g, Local_text_no lno)
binfo--;
}
return l2g->blocks;
return binfo;
}
......@@ -135,9 +139,6 @@ l2g_destruct(Local_to_global *l2g)
{
l2g_clear(l2g);
/* Free the only data block in existance. */
free(l2g->blocks->value_block);
/* Free the block pointers */
free(l2g->blocks);
}
......@@ -153,9 +154,6 @@ l2g_init(Local_to_global *l2g)
/* Initialize the main structure */
l2g->num_blocks = 0;
l2g->blocks = NULL;
/* Initialize the first and, so far only, block info. */
add_block(l2g);
}
......@@ -170,8 +168,8 @@ l2g_clear(Local_to_global *l2g)
int i;
/* Free the block info structures. */
binfo = l2g->blocks + 1;
for (i = 1; i < l2g->num_blocks; ++i) {
binfo = l2g->blocks;
for (i = 0; i < l2g->num_blocks; ++i) {
if (binfo->key_block != NULL)
free(binfo->key_block);
free(binfo->value_block);
......@@ -180,18 +178,9 @@ l2g_clear(Local_to_global *l2g)
}
/* Free the block pointers except the first one. */
l2g->blocks = realloc(l2g->blocks, sizeof(L2g_block_info));
l2g->num_blocks = 1;
/* Make the first block into a dense block and reset it. */
binfo = l2g->blocks;
binfo->first_free = 0;
binfo->zeroes = L2G_BLOCKSIZE;
binfo->start = 0;
if (binfo->key_block != NULL) {
free(binfo->key_block);
binfo->key_block = NULL;
}
l2g->num_blocks = 0;
free(l2g->blocks);
l2g->blocks = NULL;
}
......@@ -242,19 +231,21 @@ l2g_append(Local_to_global *l2g,
L2g_block_info * binfo;
int i;
/* Don't add anything if tno == 0. */
if (tno == 0)
return;
/* Let binfo point to the last block. */
binfo = &(l2g->blocks[l2g->num_blocks - 1]);
/* Correct fix? */
if (binfo->first_free == 0) {
if (l2g->num_blocks == 0) {
/* If totally empty, add the first block and set binfo to it. */
binfo = add_block(l2g);
binfo->start = lno;
} else
} else {
/* else let binfo point to the last block. */
binfo = &(l2g->blocks[l2g->num_blocks - 1]);
}
if (binfo->first_free == L2G_BLOCKSIZE) {
/* If the last block is full, create a new dense block. */
/* If the last block is full, create a new block. */
binfo = add_block(l2g);
} else if (binfo->key_block == NULL
......@@ -270,10 +261,9 @@ l2g_append(Local_to_global *l2g,
} else {
binfo = add_block(l2g);
}
}
/* Enter the new value in the block. */
/* Enter the new value into the block. */
if (binfo->first_free == 0) {
binfo->start = lno;
}
......@@ -319,6 +309,7 @@ l2g_delete(Local_to_global *l2g, Local_text_no lno)
binfo->value_block[lno - binfo->start] = 0;
binfo->zeroes++;
} else {
/* A sparse block */
/* FIXME: Binary search */
for (i = 0; i < binfo->first_free && binfo->key_block[i] <= lno; ++i) {
if (binfo->key_block[i] == lno) {
......@@ -365,12 +356,70 @@ l2g_lookup(Local_to_global *l2g, Local_text_no lno)
/*
*
* Return the next local text number which is not
*/
Local_text_no
l2g_next_key(Local_to_global *l2g, Local_text_no lno)
{
/* FIXME: NYI */
L2g_block_info * binfo;
int i;
binfo = find_block(l2g, lno);
if (binfo == NULL)
return 0;
/* If lno is lower than the first text, treat this as a special case. */
if (lno < binfo->start) {
/*
* Kid of ugly. This pointer is decremented here to point one
* item before the first block, but it is incremented again
* below. This way we can use the code below without having
* to copy it here.
*/
--binfo;
} else {
/* Check the found block */
if (binfo->key_block == NULL) {
/* dense block */
for (i = lno - binfo->start + 1; i < binfo->first_free; ++i) {
if (binfo->value_block[i] != 0)
return binfo->start + i;
}
} else {
/* sparse block */
/* FIXME: binary search */
for (i = 0; i < binfo->first_free; ++i) {
if (binfo->key_block[i] > lno)
return binfo->key_block[i];
}
}
}
/*
* If the block didn't contain anything more, try next block
* until there are no more blocks. Blocks can be completely
* empty due to removals of entries.
*/
while (++binfo <= l2g->blocks + l2g->num_blocks) {
/* If the block contains anything at all, get the item. */
if (binfo->zeroes < L2G_BLOCKSIZE){
if (binfo->key_block == NULL) {
/* dense block */
for (i = 0; i < binfo->first_free; ++i) {
if (binfo->value_block[i] != 0)
return binfo->start + i;
}
} else {
/* sparse block */
return binfo->key_block[0];
}
}
}
return 0;
}
......
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