File: split.cc

package info (click to toggle)
aegis 4.24-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 37,640 kB
  • ctags: 12,410
  • sloc: cpp: 178,288; sh: 79,325; makefile: 34,810; yacc: 4,605; perl: 1,499; ansic: 492; awk: 325
file content (75 lines) | stat: -rw-r--r-- 2,396 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
//	aegis - project change supervisor
//	Copyright (C) 2004-2008 Peter Miller
//
//	This program is free software; you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation; either version 3 of the License, or
//	(at your option) any later version.
//
//	This program is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with this program. If not, see
//	<http://www.gnu.org/licenses/>.
//

#include <common/error.h> // for assert
#include <common/mem.h>
#include <common/symtab.h>


void
symtab_ty::split()
{
    //
    // double the modulus
    //
    // This is subtle.  If we only increase the modulus by one, the
    // load always hovers around 80%, so we have to do a split for
    // every insert.  I.e. the malloc burden os O(n) for the lifetime of
    // the symtab.  BUT if we double the modulus, the length of time
    // until the next split also doubles, making the probablity of a
    // split halve, and sigma(2**-n)=1, so the malloc burden becomes O(1)
    // for the lifetime of the symtab.
    //
    str_hash_ty new_hash_modulus = hash_modulus * 2;
    str_hash_ty new_hash_mask = new_hash_modulus - 1;
    row_t **new_hash_table = new row_t * [new_hash_modulus];

    //
    // now redistribute the list elements
    //
    for (str_hash_ty idx = 0; idx < hash_modulus; ++idx)
    {
	new_hash_table[idx] = 0;
	new_hash_table[idx + hash_modulus] = 0;

	row_t *p = hash_table[idx];
	while (p)
	{
	    row_t *p2 = p;
	    p = p2->overflow;
	    p2->overflow = 0;

	    //
	    // It is important to preserve the order of the links because
	    // they can be push-down stacks, and to simply add them to the
	    // head of the list will reverse the order of the stack!
	    //
	    assert((p2->key.get_hash() & hash_mask) == idx);
	    str_hash_ty idx2 = p2->key.get_hash() & new_hash_mask;
	    row_t **ipp = &new_hash_table[idx2];
	    for (; *ipp; ipp = &(*ipp)->overflow)
		;
	    *ipp = p2;
	}
    }
    delete [] hash_table;
    hash_table = new_hash_table;
    hash_modulus = new_hash_modulus;
    hash_mask = new_hash_mask;
}