summaryrefslogtreecommitdiff
path: root/utils/testhash.cc
blob: 31d67dfe63106b6ab16286687d8575ba384f99ab (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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "HashTable.h"
#include <stdio.h>
#include <string.h>

IvlDictionnary dict (16);

main ()
{
	char line [256];
	char *s;
	int i = 0;

	printf ("? to get help\n");

	for (;;) {
		s = gets (line);
		if (! s)
			break;
		if (strcmp (s, ".") == 0)
			break;
		
		/* help */
		if (strcmp (s, "?") == 0) {
			printf ("name      add name\n");
			printf ("-name     delete name\n");
			printf ("#         dump table\n");
#ifdef TUNE		
			printf ("%%         statistics\n");
#endif
			printf (">         rehash into smaller\n");
			printf ("<         rehash into larger\n");
			printf (".         quit\n");
			
			continue;
		}

#ifdef TUNE		
		/* stats */
		if (strcmp (s, "%") == 0) {
			dict.CollStats ();
			continue;
		}
#endif
		if (strcmp (s, "#") == 0) {
			IvlDictionnary dictbis (dict);
			IvlHashCellIter hi (dictbis);
			while (++hi)
				printf ("%s\n", (*hi)->GetKey());
			continue;
		}
		
		/* resizing */
		if (strcmp (s, ">") == 0) {
			int s;
			
			s = dict.GetSize() / 2;
			if (s < 1) {
				printf ("too small\n");
				continue;
			}
			printf ("rehashing into smaller table : size %d\n", s);
			dict.Resize (s);
			continue;
		}

		if (strcmp (s, "<") == 0) {
			int s;
			
			s  = dict.GetSize() * 2;
			printf ("rehashing into larger table : size %d\n", s);
			dict.Resize (s);
			continue;
		}
		
		/* delete */
		if (*s == '-') {
			int found;
			void* p = dict.Remove (++s, &found);
			if (! found)
				printf ("%s not found\n", s);
			else
				printf ("%s had info %d\n", s, (int) p);
			continue;
		}
		
		/* add */
//		BasicHashCell* h = dict.Add (s, &found);
//		int found;
//		if (found)
//			printf ("%s duplicate\n", s);
//		else
//			h->SetInfo ((void*) i++);
		dict[s] = (void*) i++;
	}
	
}

#if 0

static char strout[200];
static char *ptout;

static h_strlen (char *str)
{
	int i = 0;
	char *pt = str;
	
	while (*pt++) ;

	return (pt - str);
}

static decimal(int i)
{
	int tmp = 0;
	char buff[15];
	char *pbuff = buff;
	
	do {
		tmp = i % 10;
		*pbuff ++ = tmp + '0';
		i /= 10;
	} while (i) ;
	while (pbuff - buff) 
		*ptout++ = *--pbuff;
}

static hexa(int i)
{
	
	int tmp = 0;
	char buff[15];
	char *pbuff = buff;
	
	do {
		tmp = i % 16;
		*pbuff ++ = "0123456789abcdef"[tmp];
		i /= 16;
	} while (i) ;
	while (pbuff - buff) 
		*ptout++ = *--pbuff;
	
}

static h_write(int fd, char *buff, int n)
{
	write(fd, buff, n);

}

print(char *fmt, int a1, int a2, int a3, int a4, int a5, int a6)
{
	char *pt = fmt;
	char c;
	int arg[6];
	int i = 0;
	
	ptout = strout;
	arg[0] = a1;
	arg[1] = a2;
	arg[2] = a3;
	arg[3] = a4;
	arg[4] = a5;
	arg[5] = a6;

	
	for (c = *pt; c = *pt; pt++) {
		if(c != '%'){
			*ptout++ = c;
			continue;
		}
		switch (c = *++pt) {
			default:
				*ptout++ = c;
				break;
			case 'd':
				decimal((int)arg[i++]);
				break;
			case 'x':
				hexa((int)arg[i++]);
				break;
			case 'c':
				*ptout++ = arg[i++];
				break;
			case 's':{
				char *p;
				for (p = (char *)arg[i++]; *p; p++) 
					*ptout++ = *p;
			}
		}		
	}
	*ptout = 0;
	h_write(1, strout, h_strlen(strout));
}

#endif