summaryrefslogtreecommitdiffstats
path: root/src/LYHash.c
blob: f419c7ea78342998f14833cab4954b0345bdd422 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * $LynxId: LYHash.c,v 1.39 2018/03/29 00:38:59 tom Exp $
 *
 * A hash table for the (fake) CSS support in Lynx-rp
 * (c) 1996 Rob Partington
 * rewritten 1997 by Klaus Weide.
 * rewritten 2018 -TD
 */
#include <LYHash.h>
#include <LYUtils.h>
#include <LYLeaks.h>
#include <LYStrings.h>
#include <LYGlobalDefs.h>

#ifdef USE_COLOR_STYLE

#undef HASH_TYPE

#define HASH_SIZE CSHASHSIZE
#define HASH_TYPE     int
#define HASH_OF(h, v) ((HASH_TYPE)((h) * 3 + UCH(v)) % HASH_SIZE)

static int count_bump;
static size_t limit;
static char *buffer;

static char *get_buffer(size_t need)
{
    if (++need > limit) {
	char *test = realloc(buffer, (limit = (1 + need) * 2));

	if (test == 0)
	    outofmem(__FILE__, "LYHash");
	buffer = test;
    }
    return buffer;
}

/*
 * This is the same algorithm as the private anchor_hash() in HTAnchor.c, but
 * with a different value for HASH_SIZE.
 */
static HASH_TYPE cs_hash(const char *string)
{
    HASH_TYPE hash = 0;
    HASH_TYPE best, n;
    bucket *data;
    const char *p;

    for (p = string; *p; p++)
	hash = HASH_OF(hash, *p);

    /*
     * The computed hash-code is only a starting point.  Check for collision.
     */
    best = hash;
    for (n = 0; n < HASH_SIZE; n++) {
	int nn = (n + hash) % HASH_SIZE;

	data = &hashStyles[nn];
	if (data->name == 0 || !strcmp(string, data->name)) {
	    best = nn;
	    hash = nn;
	    break;
	}
	++count_bump;
    }
    data = &hashStyles[best];
    if (data->name != 0) {
	if (strcmp(string, data->name)) {
	    CTRACE_STYLE((tfp, "cs_hash(%s) overwriting %d\n", string, data->name));
	    FREE(data->name);
	    StrAllocCopy(data->name, string);
	}
    } else {
	StrAllocCopy(data->name, string);
    }

    CTRACE_STYLE((tfp, "cs_hash(%s) = %d\n", string, hash));
    return hash;
}

int color_style_1(const char *string)
{
    int hash;

    if (dump_output_immediately) {
	hash = 0;
    } else {
	get_buffer(strlen(string));
	strcpy(buffer, string);
	LYLowerCase(buffer);
	hash = cs_hash(buffer);
    }
    return hash;
}

int color_style_3(const char *p, const char *q, const char *r)
{
    int hash;

    if (dump_output_immediately) {
	hash = 0;
    } else {
	get_buffer(strlen(p) + strlen(q) + strlen(r));
	strcpy(buffer, p);
	strcat(buffer, q);
	strcat(buffer, r);
	LYLowerCase(buffer);
	hash = cs_hash(buffer);
    }
    return hash;
}

void report_hashStyles(void)
{
    int i;
    int count_name = 0;
    int count_used = 0;

    for (i = 0; i < CSHASHSIZE; i++) {
	count_name += (hashStyles[i].name != 0);
	count_used += (hashStyles[i].used != 0);
    }
    CTRACE((tfp, "Style hash:\n"));
    CTRACE((tfp, "%5d names allocated\n", count_name));
    CTRACE((tfp, "%5d buckets used\n", count_used));
    CTRACE((tfp, "%5d hash collisions\n", count_bump));
}

void free_hashStyles(void)
{
    int i;

    for (i = 0; i < CSHASHSIZE; i++) {
	FREE(hashStyles[i].name);
	hashStyles[i].used = FALSE;
    }
    FREE(buffer);
    limit = 0;
    count_bump = 0;
}

#endif /* USE_COLOR_STYLE */