local-to-global.c 35.1 KB
Newer Older
inge's avatar
inge committed
1
2
3
/*
 * File: local_to_global.c
 *
Per Cederqvist's avatar
Per Cederqvist committed
4
 * Copyright 1996-1999, 2001-2002 Inge Wallin, Per Cederqvist
inge's avatar
inge committed
5
6
7
 */


David Byers's avatar
David Byers committed
8
9
10
11
12
13


#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

14
15
16
17
18
19
20
#include <assert.h>
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
#ifndef NULL
#  include <stdio.h>
#endif
21
#include <sys/types.h>
22
#include "timewrap.h"
23
24
25

#include "s-string.h"

inge's avatar
inge committed
26
#include "kom-types.h"
Per Cederqvist's avatar
Per Cederqvist committed
27
#include "local-to-global.h"
28
29
30
#include "log.h"
#include "ram-parse.h"
#include "ram-output.h"
31
32
33
34
35
#include "lyskomd.h"
#include "server/smalloc.h"

/* The test suite requires L2G_BLOCKSIZE to be 10, but that small
   blocks will result in far too much malloc-overhead.  A value of at
36
37
38
   least 100 is probably needed for reasonable performance.  The user
   can call l2g_set_block_size to set this to a reasonable value.  */
static int L2G_BLOCKSIZE = -1;
39
40
41
42
43
44
45
46
47
48


struct l2g_block_info {
    /* An index into key_block and value_block that indicates the
       first free spot.  This can also be thought of as the number of
       entries in key_block and value_block that are actually in use. */
    int first_free;

    /* Number of entries in the block that contain the value 0.  For
       purposes of calculating this value, data past the end of the
Per Cederqvist's avatar
Per Cederqvist committed
49
50
       block is counted as zeroes, so that this->first_free +
       this->zeroes is always at least L2G_BLOCKSIZE.  */
51
52
    int zeroes;

Per Cederqvist's avatar
Per Cederqvist committed
53
54
    /* First local text no in this block.  Note: this does not
       increase if the first entry of the block is removed. */
55
56
57
58
59
60
61
62
63
    Local_text_no start;

    /* NULL if this is a dense block, or a block of L2G_BLOCKSIZE
       Local_text_nos if this is a sparse block. */
    Local_text_no *key_block;

    /* A block of L2G_BLOCKSIZE Text_nos. */
    Text_no *value_block;
};
inge's avatar
inge committed
64
65


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
static long nr_constructs = 0;
static long nr_l2gs = 0;
static long nr_l2gs_peak = 0;
static long nr_destructs = 0;
static long nr_clears = 0;
static long nr_copies = 0;
static long nr_joins = 0;
static long nr_joined_blocks = 0;
static long nr_blocks = 0;
static long nr_blocks_peak = 0;
static long nr_blocks_sparse = 0;
static long nr_blocks_sparse_peak = 0;
static long sparse_skip_cost = 0;
static long nr_sparse_compactions = 0;
static long nr_sparsifications = 0;



inge's avatar
inge committed
84
85
86
87
88
/* ================================================================ */
/* ====                     Static functions                   ==== */
/* ================================================================ */


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
static inline int
is_dense(const struct l2g_block_info *binfo)
{
    return binfo->key_block == NULL;
}

static inline int
is_empty(const struct l2g_block_info *binfo)
{
    return binfo->zeroes == L2G_BLOCKSIZE;
}

static inline Local_text_no
key_value(const struct l2g_block_info *binfo,
	  int index)
{
    if (is_dense(binfo))
	return binfo->start + index;
    else
	return binfo->key_block[index];
}


static inline int
sparse_skip_deleted(const struct l2g_block_info *binfo,
		    int i)
{
    while (i < binfo->first_free && binfo->value_block[i] == 0)
117
118
    {
	++sparse_skip_cost;
119
	++i;
120
    }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

    return i;
}


/* Search for LNO in BINFO, and return the index for it.  BINFO must
   be a non-empty sparse block.  If LNO isn't present in the block
   this will return the index of next higher entry.  If LNO is larger
   than the last entry, the index past the largest entry will be
   returned.  This function never returns the index of a deleted entry. */
static inline int
sparse_locate_value(const struct l2g_block_info *binfo,
		    Local_text_no lno)
{
    int i;

    assert(!is_empty(binfo));
    assert(!is_dense(binfo));
    
140
141
142
143
    /* We could to this as a binary search instead, but since a block
       is fairly small, it isn't certain that it would be a gain.  If
       profiling shows that this is a hot spot, a binary search should
       be tried.  */
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
    for (i = 0; i < binfo->first_free && binfo->key_block[i] < lno; ++i)
	continue;

    /* Skip any lingering deleted entries. */
    i = sparse_skip_deleted(binfo, i);

    assert(i == binfo->first_free || binfo->key_block[i] >= lno);
    assert(i == binfo->first_free || binfo->value_block[i] != 0);

    return i;
}

static void
sparse_compact(struct l2g_block_info *binfo)
{
    int from, to;

    if (binfo->first_free + binfo->zeroes == L2G_BLOCKSIZE)
	return;

    assert(binfo->first_free + binfo->zeroes > L2G_BLOCKSIZE);

    for (from = to = 0; from < binfo->first_free; ++from)
	if (binfo->value_block[from] != 0)
	{
	    if (from != to)
	    {
		binfo->value_block[to] = binfo->value_block[from];
		binfo->key_block[to] = binfo->key_block[from];
	    }
	    ++to;
	}

    binfo->first_free = to;
    assert(binfo->first_free + binfo->zeroes == L2G_BLOCKSIZE);
179
    ++nr_sparse_compactions;
180
181
182
}


inge's avatar
inge committed
183
184
185
186
187
188
189
190
/*
 * Add a new block to the Local_to_global L2G.
 * Since we always add consecutively, we always start out by making 
 * a new block dense.
 *
 * Return a pointer to the newly created block info.
 */

191
static struct l2g_block_info *
inge's avatar
inge committed
192
193
add_block(Local_to_global *l2g)
{
194
195
196
197
    struct l2g_block_info  * binfo;
#ifndef NDEBUG
    int i;
#endif
inge's avatar
inge committed
198
199
200

    /* Realloc the block pointer array. */
    l2g->num_blocks++;
201
202
    l2g->blocks = srealloc(l2g->blocks,
			   l2g->num_blocks * sizeof(struct l2g_block_info));
inge's avatar
inge committed
203
204
205
206
207

    /* Create a new block info and fill it in. */
    binfo = &(l2g->blocks[l2g->num_blocks - 1]);
    binfo->first_free  = 0;
    binfo->zeroes      = L2G_BLOCKSIZE;
208
#ifndef NDEBUG
209
    binfo->start       = 0xdeadbeef;
210
#endif
inge's avatar
inge committed
211
    binfo->key_block   = NULL;
212
213
214
215
    binfo->value_block = smalloc(L2G_BLOCKSIZE * sizeof(Text_no));
    
#ifndef NDEBUG
    for (i = 0; i < L2G_BLOCKSIZE; ++i)
216
	binfo->value_block[i] = 0xdeadbeef;
217
#endif
218
219
220
    if (++nr_blocks > nr_blocks_peak)
	nr_blocks_peak = nr_blocks;
	
inge's avatar
inge committed
221
222
223
    return binfo;
}

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* Insert a new block in the middle of an l2g structure.  This
   is only used by l2g_expensive_set, so it doesn't have to be
   optimally efficient.  */
static void
add_block_before(Local_to_global *l2g,
		 int position)
{
    struct l2g_block_info  * binfo;
    struct l2g_block_info  * new_blocks;
    int ix;

    /* Allocate a new block pointer array. */
    new_blocks = smalloc((l2g->num_blocks+1) * sizeof(struct l2g_block_info));

    /* Copy blocks before and after position. */
    for (ix = 0; ix < position; ix++)
	new_blocks[ix] = l2g->blocks[ix];
    for (ix = position; ix < l2g->num_blocks; ix++)
	new_blocks[ix+1] = l2g->blocks[ix];

    /* Discard the old block pointers. */
    sfree(l2g->blocks);
    l2g->blocks = new_blocks;
    l2g->num_blocks++;

    /* Initialize the new block. */
    binfo = &new_blocks[position];
    binfo->first_free  = 0;
    binfo->zeroes      = L2G_BLOCKSIZE;
#ifndef NDEBUG
    binfo->start       = 0xdeadbeef;
#endif
    binfo->key_block   = NULL;
    binfo->value_block = smalloc(L2G_BLOCKSIZE * sizeof(Text_no));
    
#ifndef NDEBUG
    for (ix = 0; ix < L2G_BLOCKSIZE; ++ix)
	binfo->value_block[ix] = 0xdeadbeef;
#endif
    if (++nr_blocks > nr_blocks_peak)
	nr_blocks_peak = nr_blocks;
}

inge's avatar
inge committed
267

268
269
270
271
272
273
274
/*
 * Delete a block from the Local_to_global L2G.  The pointer BINFO
 * points at the block, which is supposed to be empty.
 */

static void
delete_block(Local_to_global *l2g, 
275
	     struct l2g_block_info  *binfo)
276
{
277
278
    assert(is_empty(binfo));

279
280
281
282
    if (!is_dense(binfo))
	--nr_blocks_sparse;
    --nr_blocks;

283
284
    /* Remove the blocks from the Block Info */
    if (binfo->key_block != NULL)
285
286
	sfree(binfo->key_block);
    sfree(binfo->value_block);
287
288
289
290
291

    /* Compact the remaining blocks. */
    while (++binfo < l2g->blocks + l2g->num_blocks)
	*(binfo - 1) = *binfo;
    l2g->num_blocks--;
292
293
    l2g->blocks = srealloc(l2g->blocks,
			   l2g->num_blocks * sizeof(struct l2g_block_info));
294
295
296
}


inge's avatar
inge committed
297
298
299
300
/*
 * Make a sparse block from a dense one.
 */
static void
301
make_sparse(struct l2g_block_info *binfo)
inge's avatar
inge committed
302
303
304
305
{
    int  next;
    int  i;

306
    assert(is_dense(binfo));
307
308
309
    if (++nr_blocks_sparse > nr_blocks_sparse_peak)
	nr_blocks_sparse_peak = nr_blocks_sparse;
    ++nr_sparsifications;
310

inge's avatar
inge committed
311
    /* Allocate the room for the key block. */
312
313
314
    binfo->key_block = smalloc(L2G_BLOCKSIZE * sizeof(Local_text_no));
#ifndef NDEBUG
    for (i = 0; i < L2G_BLOCKSIZE; ++i)
David Byers's avatar
David Byers committed
315
	binfo->key_block[i] = (Local_text_no)0xdeadbeefUL;
316
#endif
inge's avatar
inge committed
317
318
319

    /* Compact the block. */
    next = 0;
320
321
322
    for (i = 0; i < binfo->first_free; ++i)
	if (binfo->value_block[i] != 0)
	{
inge's avatar
inge committed
323
324
325
326
327
328
329
330
331
332
333
334
	    binfo->key_block[next]   = binfo->start + i;
	    binfo->value_block[next] = binfo->value_block[i];
	    next++;
	}

    /* Set the rest of the fields. */
    binfo->first_free = next;
    binfo->zeroes = L2G_BLOCKSIZE - next;
}


/*
335
336
337
338
339
340
341
 * Find the block where the local text no LNO should be and return a
 * pointer to it.  Returning a pointer does not necessarily mean that
 * the text exists, so a search within the block must also be done
 * later.  If LNO is smaller than the smallest number in the structure
 * a pointer to the first block will be returned.  If LNO is larger
 * than the largest number in the structure a pointer to the last
 * block will be returned.
inge's avatar
inge committed
342
 * 
343
 * Return NULL if (and only if) the structure is empty.
inge's avatar
inge committed
344
345
 */

David Byers's avatar
David Byers committed
346
static struct l2g_block_info *
347
find_block(const Local_to_global *l2g, Local_text_no lno)
inge's avatar
inge committed
348
{
David Byers's avatar
David Byers committed
349
    struct l2g_block_info  * binfo;
inge's avatar
inge committed
350

Per Cederqvist's avatar
Per Cederqvist committed
351
352
353
354
    /* If empty, the number can not be in here. */
    if (l2g->num_blocks == 0)
	return NULL;

inge's avatar
inge committed
355
356
357
358
    /* Let binfo point to the last block. */
    binfo = &(l2g->blocks[l2g->num_blocks - 1]);

    /* Find the block where lno *could* be. */
359
    /* FIXME (bug 152): Binary search? */
360
361
    while (binfo > l2g->blocks)
    {
inge's avatar
inge committed
362
363
364
365
366
	if (lno >= binfo->start)
	    return binfo;
	binfo--;
    }

Per Cederqvist's avatar
Per Cederqvist committed
367
    return binfo;
inge's avatar
inge committed
368
369
370
}


371
372
373
374
375
376
377
/* Find the first local text number higher than LNO and return it.
   Return 0 if LNO is higher than any local text number in the
   structure.  The block where the returned value was found is stored
   in BINFO_OUT, and the position within the block is stored in
   INDEX_OUT.  BINFO_OUT and INDEX_OUT are only filled in if the
   return value is non-zero.  */

inge's avatar
inge committed
378
static Local_text_no
379
380
find_block_index_key(const Local_to_global *l2g, Local_text_no lno,
		     const struct l2g_block_info **binfo_out,
inge's avatar
inge committed
381
382
		     int             *index_out)
{
383
    const struct l2g_block_info  * binfo;
inge's avatar
inge committed
384
385
386
387
388
389
390
    int               i;

    /* If the number is not to be found, return 0. */
    binfo = find_block(l2g, lno);
    if (binfo == NULL)
	return 0;

391
392
393
394
395
396
397
398
399
400
401
402
    /* If lno is lower than the first existing entry, return the first
       existing entry.  The search in the first block is performed the
       same way as when no entry was found after LNO in binfo. */
    if (lno >= binfo->start)
    {
	/* Check the found block.  See if there is another entry after
	   the entry for LNO.  If so, return it. */
	if (is_dense(binfo))
	{
	    for (i = lno - binfo->start + 1; i < binfo->first_free; ++i)
		if (binfo->value_block[i] != 0)
		{
inge's avatar
inge committed
403
404
405
406
407
		    *binfo_out = binfo;
		    *index_out = i;

		    return binfo->start + i;
		}
408
409
410
411
412
413
414
415
416
417
418
	}
	else
	{
	    i = sparse_locate_value(binfo, lno);
	    if (i < binfo->first_free && binfo->key_block[i] == lno)
		++i;
	    i = sparse_skip_deleted(binfo, i);
	    if (i < binfo->first_free)
	    {
		*binfo_out = binfo;
		*index_out = i;
inge's avatar
inge committed
419

420
		return binfo->key_block[i];
inge's avatar
inge committed
421
422
	    }
	}
Per Cederqvist's avatar
Per Cederqvist committed
423
	/*
Per Cederqvist's avatar
Per Cederqvist committed
424
425
	 * If the block didn't contain anything more, try the next
	 * block, if there is one.
Per Cederqvist's avatar
Per Cederqvist committed
426
	 */
inge's avatar
inge committed
427
428
429
430
	binfo++;
    }

    /*
Per Cederqvist's avatar
Per Cederqvist committed
431
     * We want the first existing entry in the block that binfo points
Per Cederqvist's avatar
Per Cederqvist committed
432
     * to.  However, binfo may point past the last block.
inge's avatar
inge committed
433
     */
Per Cederqvist's avatar
Per Cederqvist committed
434
435
436
437
438
    if (binfo >= l2g->blocks + l2g->num_blocks)
	return 0;

    for (i = 0; i < binfo->first_free; ++i)
	if (binfo->value_block[i] != 0)
439
	{
Per Cederqvist's avatar
Per Cederqvist committed
440
441
442
	    *binfo_out = binfo;
	    *index_out = i;
	    return key_value(binfo, i);
inge's avatar
inge committed
443
444
	}

Per Cederqvist's avatar
Per Cederqvist committed
445
    restart_kom("find_block_index_key found nothing\n");
inge's avatar
inge committed
446
447
}

448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/* Find the first local text number lower than LNO and return it.
   Return 0 if LNO is lower than any local text number in the
   structure.  The block where the returned value was found is stored
   in BINFO_OUT, and the position within the block is stored in
   INDEX_OUT.  BINFO_OUT and INDEX_OUT are only filled in if the
   return value is non-zero.  */

static Local_text_no
find_block_index_key_reverse(const Local_to_global        *l2g,
			     Local_text_no                 lno,
			     const struct l2g_block_info **binfo_out,
			     int                          *index_out)
{
    const struct l2g_block_info *binfo;
    int i;

    /* Adjust lno to the local number we are hunting for.  */
    if (lno > 0)
	--lno;

    /* If the number is not to be found, return 0. */
    binfo = find_block(l2g, lno);
    if (binfo == NULL)
	return 0;

    /* Check if not present in the first block. */
    if (lno < binfo->start)
	return 0;

    /* Check the found block and return the last entry found, if any. */
    if (is_dense(binfo))
    {
	i = lno - binfo->start;
	if (i >= binfo->first_free)
	    i = binfo->first_free - 1;
	for (; i >= 0; --i)
	    if (binfo->value_block[i] != 0)
	    {
		*binfo_out = binfo;
		*index_out = i;

		return binfo->start + i;
	    }
    }
    else
    {
	i = sparse_locate_value(binfo, lno);
	if (i >= binfo->first_free)
	    i = binfo->first_free - 1;
	while (i > 0 && (binfo->value_block[i] == 0
			 || binfo->key_block[i] > lno))
	{
	    ++sparse_skip_cost;
	    --i;
	}
	if (binfo->value_block[i] != 0 && binfo->key_block[i] <= lno)
	{
	    *binfo_out = binfo;
	    *index_out = i;

	    return binfo->key_block[i];
	}
    }
	
    /*
     * Try the previous block, if there is one.
     */
    if (binfo == l2g->blocks)
	return 0;

    binfo--;

    /*
     * We want the last existing entry in the block that binfo points
     * to.
     */
    for (i = binfo->first_free - 1; i >= 0; --i)
	if (binfo->value_block[i] != 0)
	{
	    *binfo_out = binfo;
	    *index_out = i;
	    return key_value(binfo, i);
	}

    restart_kom("find_block_index_key_reverse found nothing\n");
}

535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
static void
join_range(Local_to_global *l2g,
	   struct l2g_block_info *first,
	   struct l2g_block_info *last)
{
    int next;
    int i;
    struct l2g_block_info *binfo;

    assert(first < last);
    assert(l2g->blocks <= first && first < l2g->blocks + l2g->num_blocks);
    assert(l2g->blocks <= last  && last  < l2g->blocks + l2g->num_blocks);

    if (is_dense(first))
	make_sparse(first);
    else
	sparse_compact(first);

553
    ++nr_joins;
554
555
556
    next = first->first_free;
    for (binfo = first + 1; binfo <= last; ++binfo)
    {
557
	++nr_joined_blocks;
558
559
560
561
562
563
564
565
	for (i = 0; i < binfo->first_free; ++i)
	    if (binfo->value_block[i] != 0)
	    {
		first->value_block[next] = binfo->value_block[i];
		first->key_block[next]  = key_value(binfo, i);
		next++;
	    }
	if (!is_dense(binfo))
566
567
	{
	    --nr_blocks_sparse;
568
	    sfree(binfo->key_block);
569
	}
570
571
572
573
574
	sfree(binfo->value_block);
    }

    while (binfo < l2g->blocks + l2g->num_blocks)
    {
575
	assert(binfo - (last - first) > first);
576
577
578
579
580
581
582
	*(binfo - (last - first)) = *binfo;
	++binfo;
    }

    assert(next <= L2G_BLOCKSIZE);
    first->first_free = next;
    first->zeroes = L2G_BLOCKSIZE - next;
583

584
    nr_blocks -= last - first;
585
586
587
    l2g->num_blocks -= last - first;
    l2g->blocks = srealloc(l2g->blocks,
			   l2g->num_blocks * sizeof(struct l2g_block_info));
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
}
    
    
static int
join_blocks(Local_to_global *l2g,
	    struct l2g_block_info *binfo)
{
    int zeroes;
    int gain;
    int best;
    int i;

    if (binfo->zeroes == 0)
	/* This block is full. */
	return 0;

    /* Note: on rare occasions we might be able to create one dense
       block from two (dense or sparse) blocks or one sparse block.
       We don't bother.  The gain would probably be small, and it
       would complicate the algorithm significantly.  */

    zeroes = L2G_BLOCKSIZE;
    gain = 0;
    best = 0;

    for (i = 0; zeroes > 0 && i <= binfo - l2g->blocks; ++i)
    {
	zeroes -= (L2G_BLOCKSIZE - (binfo-i)->zeroes);
	gain += (is_dense(binfo-i) ? 1 : 2);
	if (gain > 2 && zeroes >= 0)
	    best = i;
    }

    if (best > 0)
    {
	join_range(l2g, binfo - best, binfo);
	return 1;
    }

    zeroes = L2G_BLOCKSIZE;
    gain = 0;
    best = 0;
    for (i = 0; zeroes > 0 && i < l2g->num_blocks - (binfo - l2g->blocks); ++i)
    {
	zeroes -= (L2G_BLOCKSIZE - (binfo+i)->zeroes);
	gain += (is_dense(binfo+i) ? 1 : 2);
	if (gain > 2 && zeroes >= 0)
	    best = i;
    }

    if (best > 0)
    {
	join_range(l2g, binfo, binfo + best);
	return 1;
    }

    return 0;
}
inge's avatar
inge committed
646

inge's avatar
inge committed
647
648
649
650
651
/* ================================================================ */
/* ====                Outside accessible functions            ==== */
/* ================================================================ */


652
653
654
655
656
657
658
659
void
l2g_set_block_size(int sz)
{
    assert(L2G_BLOCKSIZE == -1);
    L2G_BLOCKSIZE = sz;
}


inge's avatar
inge committed
660
661
662
663
void
l2g_destruct(Local_to_global *l2g)
{
    l2g_clear(l2g);
664
665
666
667
668
669
670
    ++nr_destructs;
    --nr_l2gs;
#ifndef NDEBUG
    l2g->num_blocks = 0xdeadbeef;
    l2g->first_unused = 0xdeadbeef;
    l2g->blocks = (void*)0xdeadbeef;
#endif
inge's avatar
inge committed
671
672
673
674
675
676
}


void
l2g_init(Local_to_global *l2g)
{
677
678
679
    if (L2G_BLOCKSIZE == -1)
	L2G_BLOCKSIZE = 250;

inge's avatar
inge committed
680
681
    /* Initialize the main structure */
    l2g->num_blocks = 0;
682
    l2g->first_unused = 1;
inge's avatar
inge committed
683
    l2g->blocks = NULL;
684
685
686
    ++nr_constructs;
    if (++nr_l2gs > nr_l2gs_peak)
	nr_l2gs_peak = nr_l2gs;
inge's avatar
inge committed
687
688
689
690
691
692
}


void
l2g_clear(Local_to_global *l2g)
{
693
    struct l2g_block_info  * binfo;
inge's avatar
inge committed
694
695
696
    int               i;

    /* Free the block info structures. */
Per Cederqvist's avatar
Per Cederqvist committed
697
    binfo = l2g->blocks;
698
699
    for (i = 0; i < l2g->num_blocks; ++i)
    {
inge's avatar
inge committed
700
	if (binfo->key_block != NULL)
701
702
	{
	    --nr_blocks_sparse;
703
	    sfree(binfo->key_block);
704
	}
705
	sfree(binfo->value_block);
inge's avatar
inge committed
706
707
708

	binfo++;
    }
709
    nr_blocks -= l2g->num_blocks;
inge's avatar
inge committed
710

711
    /* Free the block pointers. */
Per Cederqvist's avatar
Per Cederqvist committed
712
    l2g->num_blocks = 0;
713
714
715
716
    l2g->first_unused = 1;
    if (l2g->blocks != NULL)
    {
	sfree(l2g->blocks);
717
718
	l2g->blocks = NULL;
    }
719
    ++nr_clears;
inge's avatar
inge committed
720
721
722
723
724
725
726
727
728
}


/*
 * Copy everything from one l2g structure (FROM) into another (TO).
 * TO has to be an initialized structure.
 */

void
729
l2g_copy(Local_to_global *dest, const Local_to_global *src)
inge's avatar
inge committed
730
{
731
732
    const struct l2g_block_info *binfo;
    int i;
inge's avatar
inge committed
733

734
    /* FIXME (bug 153): More efficient code */
735
    l2g_clear(dest);
inge's avatar
inge committed
736

737
738
739
740
741
    for (binfo = src->blocks; binfo < src->blocks + src->num_blocks; ++binfo)
	for (i = 0; i < binfo->first_free; ++i)
	    l2g_append(dest, key_value(binfo, i), binfo->value_block[i]);
    assert(src->first_unused >= dest->first_unused);
    dest->first_unused = src->first_unused;
742
    ++nr_copies;
inge's avatar
inge committed
743
744
745
}


746
747
typedef Local_text_no Local_text_no_iter;

inge's avatar
inge committed
748
749
750
751
752
void
l2g_append(Local_to_global *l2g, 
	   Local_text_no    lno,
	   Text_no          tno)
{
753
    struct l2g_block_info  * binfo;
754
    Local_text_no_iter ix;
inge's avatar
inge committed
755

756
757
    if (lno < l2g->first_unused)
    {
David Byers's avatar
David Byers committed
758
	kom_log("l2g_append: won't add %lu<%lu> when first_unused=%lu\n",
759
760
761
762
763
764
765
	    (unsigned long)tno, (unsigned long)lno,
	    (unsigned long)l2g->first_unused);
	return;
    }

    l2g->first_unused = lno + 1;
	    
Per Cederqvist's avatar
Per Cederqvist committed
766
    /* Don't add anything if tno == 0. */
inge's avatar
inge committed
767
768
769
    if (tno == 0)
	return;

770
771
    if (l2g->num_blocks == 0)
    {
Per Cederqvist's avatar
Per Cederqvist committed
772
773
	/* If totally empty, add the first block and set binfo to it. */
	binfo = add_block(l2g);
inge's avatar
inge committed
774
	binfo->start = lno;
775
776
777
    }
    else
    {
Per Cederqvist's avatar
Per Cederqvist committed
778
779
780
	/* else let binfo point to the last block. */
	binfo = &(l2g->blocks[l2g->num_blocks - 1]);
    }
inge's avatar
inge committed
781

782
783
784
785
786
    /* Try to make room for the new data in the last block (pointed to
       by binfo).  If that fails, allocate a new block and let binfo
       point to it. */
    if (is_dense(binfo))
    {
787
	if (lno - binfo->start >= (Local_text_no)L2G_BLOCKSIZE)
788
789
790
791
	{
	    /* The last block is a dense block, and this text does not
	       fit in it.  Or is there anything we can do to make it fit?  */

Per Cederqvist's avatar
Per Cederqvist committed
792
	    if (binfo->zeroes >= L2G_BLOCKSIZE / 2)
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
		/* Arbitrary limit: make the block sparse if at least
		   half of it contains zeroes.  (Remeber that any
		   unused entries past binfo->first_free are counted
		   in binfo->zeroes). */
		make_sparse(binfo);
	    else
		/* There is no way to make room in the last block, so
		   allocate a new one.  (There *could* be ways to make
		   this entry fit into the last block: if the next to
		   last block is sparse and has room for an entry, we
		   might be able to slide the last block, and fit in
		   the new entry.  However, we never move data between
		   blocks.  That would be too complex and error-prone,
		   and the gain would be very small.) */
		binfo = add_block(l2g);
inge's avatar
inge committed
808
809
	}
    }
810
811
812
813
814
815
816
    else
    {
	/* The last block was sparse. */
	sparse_compact(binfo);
	if (binfo->first_free == L2G_BLOCKSIZE)
	    binfo = add_block(l2g);
    }
inge's avatar
inge committed
817

818
819
    /* Enter the new value into the last block. */
    if (binfo->first_free == 0)
inge's avatar
inge committed
820
821
	binfo->start = lno;

822
823
    if (is_dense(binfo))
    {
824
825
	for (ix = binfo->first_free; ix < lno - binfo->start; ++ix)
	    binfo->value_block[ix] = 0;
inge's avatar
inge committed
826
827
	binfo->value_block[lno - binfo->start] = tno;
	binfo->first_free = lno - binfo->start + 1;
828
829
830
    }
    else
    {
inge's avatar
inge committed
831
832
833
834
835
836
837
838
	binfo->key_block  [binfo->first_free] = lno;
	binfo->value_block[binfo->first_free] = tno;
	binfo->first_free++;
    }
    
    binfo->zeroes--;
}

839

840
841
842
843
844
845
846
void
l2g_expensive_set(Local_to_global *l2g, 
		  Local_text_no    lno,
		  Text_no          tno)
{
    struct l2g_block_info  * binfo;
    Local_text_no_iter ix;
847
    int block_no;
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863

    if (lno >= l2g->first_unused)
    {
	l2g_append(l2g, lno, tno);
	return;
    }

    if (tno == 0)
    {
	l2g_delete(l2g, lno);
	return;
    }

    binfo = find_block(l2g, lno);
    if (binfo == NULL)
    {
864
865
	assert(l2g->num_blocks == 0);

866
	/* Append a new block. */
867
868
869
870
871
872
873
	binfo = add_block(l2g);
	assert(binfo->first_free == 0);
	assert(binfo->key_block == NULL);
	binfo->start = lno;
	binfo->value_block[0] = tno;
	binfo->first_free = 1;
	binfo->zeroes--;
874
875
876
877
878
879
    }
    else if (lno < binfo->start)
    {
	assert(binfo == l2g->blocks);

	/* Prepend a new block. */
880
881
882
883
884
885
	add_block_before(l2g, 0);
	binfo = l2g->blocks;
	binfo->start = lno;
	binfo->value_block[0] = tno;
	binfo->first_free = 1;
	binfo->zeroes--;
886
887
888
889
890
891
    }
    else if (is_dense(binfo))
    {
	assert(binfo->start <= lno);
	if (lno < binfo->start + L2G_BLOCKSIZE)
	{
892
	    /* The number can be entered into this block.  */
893
	    while (binfo->start + binfo->first_free <= lno)
894
	    {
895
		binfo->value_block[binfo->first_free++] = 0;
896
897
		assert(binfo->first_free <= L2G_BLOCKSIZE);
	    }
898
899
	    
	    assert(lno < binfo->start + binfo->first_free);
900
	    assert(binfo->first_free <= L2G_BLOCKSIZE);
901
902
903
904
905
906
907
908
909

	    if (binfo->value_block[lno - binfo->start] == 0)
		binfo->zeroes--;
	    binfo->value_block[lno - binfo->start] = tno;
	}
	else
	{
	    /* There is no room within this block.  Insert a new block
	       after binfo.  */
910
911
912
913
914
915
916
	    block_no = 1 + binfo - l2g->blocks;
	    add_block_before(l2g, block_no);
	    binfo = l2g->blocks + block_no;
	    binfo->start = lno;
	    binfo->value_block[0] = tno;
	    binfo->first_free = 1;
	    binfo->zeroes--;
917
918
919
920
921
	}
    }
    else
    {
	ix = sparse_locate_value(binfo, lno);
922
923
	if (ix < (Local_text_no_iter)binfo->first_free
	    && binfo->key_block[ix] == lno)
924
	{
925
	    /* We found the value in this sparse block.  */
926
927
928
929
930
931
932
	    assert(binfo->value_block[ix] != 0);
	    binfo->value_block[ix] = tno;
	}
	else
	{
	    if (binfo->zeroes == 0)
	    {
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
		/* Split the sparse block to make room. */
		block_no = 1 + binfo - l2g->blocks;
		add_block_before(l2g, block_no);
		binfo = l2g->blocks + block_no;

		assert(binfo[-1].first_free == L2G_BLOCKSIZE);

		if (lno > binfo[-1].key_block[L2G_BLOCKSIZE-1])
		{
		    /* Insert the mapping into the new block.  */
		    binfo->start = lno;
		    binfo->value_block[0] = tno;
		    binfo->first_free = 1;
		    binfo->zeroes--;
		    return;
		}
		else
		{
		    /* Move the last mapping from the block into the new block,
		       and enter the new mapping into the old block.  */

		    /* Set up the new block.  */
		    binfo->start = binfo[-1].key_block[L2G_BLOCKSIZE-1];
		    binfo->value_block[0] = binfo[-1].value_block[
			L2G_BLOCKSIZE-1];
		    binfo->first_free = 1;
		    binfo->zeroes--;

		    /* Fix the old block.  */
		    binfo--;
		    binfo->zeroes++;
		    binfo->first_free--;
		    /* The old block is now compact.  Let the code below
		       enter the new mapping into it.  */
		}
		assert(binfo->zeroes == 1);
969
970
971
972
973
	    }
	    else
		sparse_compact(binfo);

	    /* There is room for a new mapping in this block. */
974
	    assert(binfo->zeroes > 0);
975
976
977
978
979
980
981
982
983
984
985
986
987
	    for (ix = binfo->first_free; ix > 0; --ix)
	    {
		assert(binfo->key_block[ix-1] != lno);
		if (binfo->key_block[ix-1] > lno)
		{
		    binfo->key_block[ix] = binfo->key_block[ix-1];
		    binfo->value_block[ix] = binfo->value_block[ix-1];
		}
		else
		    break;
	    }

	    assert(ix == 0 || binfo->key_block[ix-1] < lno);
988
989
	    assert(ix == (Local_text_no_iter)binfo->first_free
		   || binfo->key_block[ix+1] > lno);
990
991
992
993
994
995
996
997
998

	    binfo->key_block[ix] = lno;
	    binfo->value_block[ix] = tno;
	    binfo->first_free++;
	    binfo->zeroes--;
	}
    }
}

inge's avatar
inge committed
999
1000
1001
1002
1003
1004

/*
 * Delete the local text LNO from the structure L2G.
 */

void
1005
1006
l2g_delete(Local_to_global *l2g,
	   Local_text_no lno)
inge's avatar
inge committed
1007
{
1008
1009
1010
    struct l2g_block_info *binfo;
    int i;
    
inge's avatar
inge committed
1011
    /* Find block where LNO might be and return if not there. */
David Byers's avatar
David Byers committed
1012
    binfo = find_block(l2g, lno);
inge's avatar
inge committed
1013
1014
1015
1016
    if (binfo == NULL)
	return;

    /* Go through the block where it might be and delete LNO */
1017
1018
    if (is_dense(binfo))
    {
inge's avatar
inge committed
1019
	if (binfo->start <= lno && lno < binfo->start + L2G_BLOCKSIZE)
1020
1021
1022
	    if (binfo->value_block[lno - binfo->start] != 0)
	    {
		binfo->value_block[lno - binfo->start] = 0;
inge's avatar
inge committed
1023
1024
		binfo->zeroes++;
	    }
1025
1026
1027
1028
1029
1030
1031
1032
1033
    }
    else
    {
	i = sparse_locate_value(binfo, lno);
	if (i < binfo->first_free && binfo->key_block[i] == lno)
	{
	    assert(binfo->value_block[i] != 0); /* See sparse_locate_value. */
	    binfo->value_block[i] = 0;
	    binfo->zeroes++;
inge's avatar
inge committed
1034
1035
	}
    }
1036

1037
1038
1039
    /* Delete the block if it became empty. */
    if (is_empty(binfo))
    {
1040
	delete_block(l2g, binfo);
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
	return;
    }

    /* Check if we can save space by joining adjoining blocks. */
    if (!join_blocks(l2g, binfo))
	/* Could not join.  Compact this block if it is a sparse block
           with more than half of the entries zeroes.  */
	if (!is_dense(binfo)
	    && binfo->zeroes + binfo->first_free/2 > L2G_BLOCKSIZE)
	{
	    sparse_compact(binfo);
	}
inge's avatar
inge committed
1053
1054
1055
1056
}


Text_no
1057
1058
l2g_lookup(const Local_to_global *l2g,
	   Local_text_no lno)
inge's avatar
inge committed
1059
{
1060
    const struct l2g_block_info  * binfo;
inge's avatar
inge committed
1061
1062
1063
1064
1065
1066
    int               i;

    binfo = find_block(l2g, lno);
    if (binfo == NULL)
	return 0;

1067
1068
1069
    if (is_dense(binfo))
    {
	if (binfo->start <= lno && lno < binfo->start + binfo->first_free)
inge's avatar
inge committed
1070
	    return binfo->value_block[lno - binfo->start];
1071
1072
1073
1074
1075
1076
    }
    else
    {
	i = sparse_locate_value(binfo, lno);
	if (i < binfo->first_free && binfo->key_block[i] == lno)
	    return binfo->value_block[i];
inge's avatar
inge committed
1077
1078
1079
1080
1081
1082
1083
    }

    return 0;
}


Local_text_no
1084
1085
l2g_next_key(const Local_to_global *l2g,
	     Local_text_no lno)
inge's avatar
inge committed
1086
{
1087
    const struct l2g_block_info  * binfo;
Per Cederqvist's avatar
Per Cederqvist committed
1088
1089
    int               i;

inge's avatar
inge committed
1090
    return find_block_index_key(l2g, lno, &binfo, &i);
inge's avatar
inge committed
1091
1092
1093
}


1094
1095
1096
1097
1098
Local_text_no
l2g_first_appendable_key(const Local_to_global *l2g)
{
    return l2g->first_unused;
}
inge's avatar
inge committed
1099

1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115

void
l2g_set_first_appendable_key(Local_to_global *l2g,
			     Local_text_no    key)
{
    if (key < l2g->first_unused)
    {
    	kom_log("l2g_append: won't decrease first_unused from %lu to %lu\n",
		(unsigned long)l2g->first_unused, (unsigned long)key);
	return;
    }

    l2g->first_unused = key;
}


inge's avatar
inge committed
1116
void
1117
1118
l2g_delete_global_in_sorted(Local_to_global *l2g,
			    Text_no tno)
inge's avatar
inge committed
1119
{
1120
1121
1122
    /* This implementation performs a linear search through the list,
       but since we know that both the key and the data are sorted in
       ascending order we could do a binary search instead.
1123
1124
       FIXME (bug 154): profile the code and se if it is worthwhile to
       implement something fancier.  */
1125
1126
1127
1128
1129
1130

    L2g_iterator iter;

    for (l2gi_searchall(&iter, l2g); !iter.search_ended; l2gi_next(&iter))
	if (iter.tno == tno)
	{
1131
1132
1133
	    l2g_delete(l2g, iter.lno);
	    return;
	}
inge's avatar
inge committed
1134
1135
1136
}


1137
1138
1139
void
l2g_dump(FILE *file,
	 const Local_to_global *l2g)
inge's avatar
inge committed
1140
{
1141
1142
    struct l2g_block_info  * binfo;
    int               i, j;
inge's avatar
inge committed
1143

1144
1145
    fprintf(file, "Number of blocks: %d\n", l2g->num_blocks);
    fprintf(file, "First unused: %lu\n", l2g->first_unused);
inge's avatar
inge committed
1146

1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
    binfo = l2g->blocks;
    for (i = 0; i < l2g->num_blocks; ++i) {
	fprintf(file, "%d: %d %d %d (%s) [", i,
		binfo->first_free,
		binfo->zeroes,
		(int) binfo->start,
		(is_dense(binfo) ? "dense" : "sparse"));

	if (is_dense(binfo)) {
	    for (j = 0; j < binfo->first_free; ++j) {
		fprintf(file, "%d ", (int) binfo->value_block[j]);
	    }
	} else {
	    for (j = 0; j < binfo->first_free; ++j) {
		fprintf(file, "%d:%d ", 
			(int) binfo->key_block[j],
			(int) binfo->value_block[j]);
	    }
	}
	fprintf(file, "]\n");

	binfo++;
    }
inge's avatar
inge committed
1170
1171
1172
}


1173
1174
1175
1176
Success
l2g_read(FILE *fp, Local_to_global *l2g)
{
    int            c;
1177
    Local_text_no  lno = 0;
1178
    Text_no        tno;
1179
1180
1181
    Local_text_no  first_unused;

    l2g_clear(l2g);
1182
1183
1184

    /* Read past the start marker */
    fskipwhite(fp);
1185
1186
    if ( (c = getc(fp)) == EOF || c != '[')
    {
David Byers's avatar
David Byers committed
1187
1188
	kom_log("l2g_read() failed to find ``['' marker at pos %lu.\n",
                (unsigned long) ftell(fp));
1189
	return FAILURE;
1190
1191
    }

1192
1193
    first_unused = fparse_long(fp);

1194
    /* Read numbers until the EOL (end-of-l2g :-) ) marker is read. */
1195
1196
    while (1)
    {
1197
	if ((c = getc(fp)) == EOF)
1198
	{
David Byers's avatar
David Byers committed
1199
	    kom_log("l2g_read(): unexpected EOF at pos %lu.\n",
1200
1201
1202
		(unsigned long) ftell(fp));
	    return FAILURE;
	}
1203

1204
1205
1206
1207
1208
1209
	switch (c)
	{
	case ' ':
	    lno = fparse_long(fp);
	    if (lno == 0)
	    {
David Byers's avatar
David Byers committed
1210
		kom_log("l2g_read(): got local number 0 at pos %lu.\n",
1211
1212
1213
1214
1215
1216
1217
		    (unsigned long) ftell(fp));
		return FAILURE;
	    }
	    break;
	case ',':
	    if (lno == 0)
	    {
David Byers's avatar
David Byers committed
1218
		kom_log("l2g_read(): missing local number at pos %lu.\n",
1219
1220
1221
1222
1223
1224
1225
1226
		    (unsigned long)ftell(fp));
		return FAILURE;
	    }
	    ++lno;
	    /*FALLTHROUGH*/
	case ':':
	    if (lno == 0)
	    {
David Byers's avatar
David Byers committed
1227
		kom_log("l2g_read(): missing local number at pos %lu.\n",
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
		    (unsigned long)ftell(fp));
		return FAILURE;
	    }
	    tno = fparse_long(fp);
	    l2g_append(l2g, lno, tno);
	    break;
	case ']':
	    assert(first_unused >= l2g->first_unused);
	    l2g->first_unused = first_unused;
	    return OK;
	default:
David Byers's avatar
David Byers committed
1239
	    kom_log("l2g_read(): unexpected character ``%c'' at pos %lu.\n",
1240
1241
1242
		c, (unsigned long) ftell(fp));
	    return FAILURE;
	}
1243
    }
inge's avatar
inge committed
1244
1245
}

1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
/* Write the unsigned long value ``l'' to ``fp''. */
static void
put_ulong(unsigned long l,
	  FILE *fp)
{
    static char buf[sizeof(unsigned long) * 3 + 1];
    char       *cp;

    if (l < 10)
	putc("0123456789"[l], fp);
    else
    {
	cp = buf + sizeof(buf);
	while (l > 0)
	{
	    *--cp = (l % 10) + '0';
	    l /= 10;
	}
	fwrite(cp, buf + sizeof(buf) - cp, 1, fp);
    }
}
inge's avatar
inge committed
1267
1268

void
1269
l2g_write(FILE *fp, const Local_to_global *l2g)
inge's avatar
inge committed
1270
{
1271
1272
    const struct l2g_block_info *binfo;
    int ix;
1273
1274
1275
    Local_text_no key;
    Text_no val;
    Local_text_no current = 0;
1276
1277

    putc('[', fp);
1278
    put_ulong(l2g->first_unused, fp);
1279

1280
1281
    for (binfo = l2g->blocks; binfo < l2g->blocks + l2g->num_blocks; ++binfo)
	for (ix = 0; ix < binfo->first_free; ++ix)
1282
	    if ((val = binfo->value_block[ix]) != 0)
1283
	    {
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
		key = key_value(binfo, ix);
		if (key > current + 3 || current == 0)
		{
		    putc(' ', fp);
		    put_ulong(key, fp);
		    putc(':', fp);
		    put_ulong(val, fp);
		    current = key;
		}
		else
		{
		    while (++current < key)
		    {
			putc(',', fp);
			putc('0', fp);
		    }
		    putc(',', fp);
		    put_ulong(val, fp);
		}
1303
	    }
1304
1305

    putc(']', fp);
inge's avatar
inge committed
1306
}
inge's avatar
inge committed
1307
1308
1309
1310
1311
1312
1313
1314


/* ================================================================ */
/* ===                     Iterator code                        === */
/* ================================================================ */


void
1315
1316
l2gi_searchall (L2g_iterator *l2gi,
		const Local_to_global *l2g)
inge's avatar
inge committed
1317
{
1318
    l2gi_searchsome(l2gi, l2g, 1, 0);
inge's avatar
inge committed
1319
1320
1321
1322
}


void
1323
1324
1325
l2gi_searchsome(L2g_iterator *l2gi,
		const Local_to_global *l2g, 
		Local_text_no begin, 
inge's avatar
inge committed
1326
1327
		Local_text_no end)
{
1328
    const struct l2g_block_info  * binfo;
inge's avatar
inge committed
1329
1330
    int               index;

1331
1332
    if (begin < 1)
    {
David Byers's avatar
David Byers committed
1333
	kom_log("l2gi_searchsome(%lu, %lu) called: min is 1\n",
1334
1335
1336
1337
	    (unsigned long)begin, (unsigned long)end);
	begin = 1;
    }

inge's avatar
inge committed
1338
    l2gi->l2g = l2g;
1339
    l2gi->beginval = begin;
inge's avatar
inge committed
1340
    l2gi->endval   = end;
1341
    l2gi->search_ended = 1;
inge's avatar
inge committed
1342

1343
    if (end != 0 && begin >= end)
inge's avatar
inge committed
1344
1345
	return;

1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
    if (find_block_index_key(l2g, begin-1, &binfo, &index) == 0)
	return;

    l2gi->binfo    = binfo;
    l2gi->arrindex = index;

    assert(binfo != NULL);
    
    l2gi->lno = key_value(binfo, index);
    l2gi->tno = binfo->value_block[index];
1356
1357
1358
1359
1360

    if (end == 0)
	l2gi->search_ended = 0;
    else
	l2gi->search_ended = l2gi->lno >= end;
inge's avatar
inge committed
1361
1362
1363
}


Per Cederqvist's avatar
Per Cederqvist committed
1364
1365
void
l2gi_next(L2g_iterator *l2gi)
inge's avatar
inge committed
1366
{
1367
1368
    const Local_to_global *l2g;
    const struct l2g_block_info *binfo;
inge's avatar
inge committed
1369
1370
1371
1372
1373
1374
1375
1376
    int                arrindex;

    l2g      = l2gi->l2g;
    arrindex = l2gi->arrindex + 1;
    for (binfo = l2gi->binfo;
	 binfo < l2g->blocks + l2g->num_blocks;
	 ++binfo, arrindex = 0)
    {
1377
1378
1379
1380
1381
1382
1383
	for (; arrindex < binfo->first_free; ++arrindex)
	{
	    if (l2gi->endval != 0
		&& key_value(binfo, arrindex) >= l2gi->endval)
	    {
		l2gi->search_ended = 1;
		return;
inge's avatar
inge committed
1384
1385
	    }

1386
1387
1388
1389
	    if (binfo->value_block[arrindex] != 0)
	    {
		l2gi->binfo    = binfo;
		l2gi->arrindex = arrindex;
inge's avatar
inge committed
1390

1391
1392
		l2gi->lno = key_value(binfo, arrindex);
		l2gi->tno = binfo->value_block[arrindex