File: hash.c

package info (click to toggle)
swish-e 1.1-1
  • links: PTS
  • area: main
  • in suites: hamm, potato, slink
  • size: 380 kB
  • ctags: 340
  • sloc: ansic: 4,540; makefile: 77; sh: 12
file content (241 lines) | stat: -rwxr-xr-x 5,125 bytes parent folder | download
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
** Copyright (C) 1995, Enterprise Integration Technologies Corp.        
** All Rights Reserved.
** Kevin Hughes, kevinh@eit.com 
** 3/11/94
**
** Added addStopList to support printing of common words
** G. Hill 4/7/97  ghill@library.berkeley.edu
*/

#include "swish.h"
#include "hash.h"

/* Hashes a string.
*/

unsigned hash(s)
     char *s;
{
        unsigned hashval;

        for (hashval = 0; *s != '\0'; s++)
                hashval = *s + 31 * hashval;
        return hashval % HASHSIZE;
}

/* Hashes a string for a larger hash table.
*/

unsigned bighash(s)
     char *s;
{
        unsigned hashval;

        for (hashval = 0; *s != '\0'; s++)
                hashval = *s + 31 * hashval;
        return hashval % BIGHASHSIZE;
}

/* Reads the internal list of default stopwords.
*/

void readdefaultstopwords()
{
        int i;

        for (i = 0; defaultstopwords[i] != NULL; i++)
                addstophash(defaultstopwords[i]);
}

/* Adds a stop word to the list of removed common words
*/
void addStopList(word)
     char * word;
{
  char* arrayWord;
  
  if (isstopword(word) )
    return;
  
  arrayWord = (char *) mystrdup (word);
  stopList[stopPos++] = arrayWord;
}


/* Adds a stop word to a hash table.
*/

void addstophash(word)
     char *word;
{
        unsigned hashval;
        struct swline *sp;

	if (isstopword(word))
		return;

        sp = (struct swline *) emalloc(sizeof(struct swline));
        sp->line = (char *) mystrdup(word);

        hashval = hash(word);
        sp->next = hashstoplist[hashval];
        hashstoplist[hashval] = sp;
}

/* Sees if a word is a stop word by looking it up in the hash table.
*/

int isstopword(word)
     char *word;
{
        unsigned hashval;
        struct swline *sp;

        hashval = hash(word);
        sp = hashstoplist[hashval];

        while (sp != NULL) {
                if (!strcmp(sp->line, word))
                        return 1;
                sp = sp->next;
        }
        return 0;
}

/* Adds a file number and its associated file location
** to a hash table.
*/

void addtofilehashlist(fileshort, filelong)
     int fileshort;
     long filelong;
{
        unsigned hashval;
        struct filenum *fp;
        char tmpstr[MAXSTRLEN];

        fp = (struct filenum *) emalloc(sizeof(struct filenum));
        fp->fileshort = fileshort;
        fp->filelong = filelong;

        sprintf(tmpstr, "%d", fileshort);
        hashval = bighash(tmpstr);
        fp->next = filehashlist[hashval];
        filehashlist[hashval] = fp;
}

/* Looks up a file number in the hash table and
** returns the file position of the associated file info.
*/

long getfilenum(filenum)
     int filenum;
{
        unsigned hashval;
        struct filenum *fp;
        char tmpstr[MAXSTRLEN];

        sprintf(tmpstr, "%d", filenum);
        hashval = bighash(tmpstr);
        fp = filehashlist[hashval];

        while (fp != NULL) {
                if (fp->fileshort == filenum)
                        return fp->filelong;
                fp = fp->next;
        }
        return 0;
}

/* Adds a file number and the number of indexed words
** to a hash table.
*/

void addtofwordtotals(filenum, ftotalwords)
     int filenum;
     int ftotalwords;
{
        unsigned hashval;
        struct fwordtotal *fp;
        char tmpstr[MAXSTRLEN];

        fp = (struct fwordtotal *) emalloc(sizeof(struct fwordtotal));
        fp->filenum = filenum;
        fp->totalwords = ftotalwords;
        fp->next = NULL;

        sprintf(tmpstr, "%d", filenum);
        hashval = bighash(tmpstr);
        fp->next = fwordtotals[hashval];
        fwordtotals[hashval] = fp;
}

/* Looks up a file number in the hash table and
** returns the total number of words indexed in it.
*/

int gettotalwords(filenum)
     int filenum;
{
        unsigned hashval;
        struct fwordtotal *fp;
        char tmpstr[MAXSTRLEN];

        sprintf(tmpstr, "%d", filenum);
        hashval = bighash(tmpstr);
        fp = fwordtotals[hashval];

        while (fp != NULL) {
                if (fp->filenum == filenum)
                        return fp->totalwords;
                fp = fp->next;
        }
        return 0;
}

/* Adds a file number to a hash table of results.
** If the entry's alrady there, add the ranks,
** else make a new entry.
*/

void mergeresulthashlist(filenum, rank, structure)
     int filenum;
     int rank;
     int structure;
{
        unsigned hashval;
        struct result *rp, *newrp;
        char word[MAXWORDLEN];

        sprintf(word, "%d", filenum);
	hashval = hash(word);

	rp = resulthashlist[hashval];
	while (rp != NULL) {
		if (rp->filenum == filenum) {
			rp->rank += rank;
			rp->structure |= structure;
			return;
		}
		rp = rp->next;
	}
	
        newrp = (struct result *) emalloc(sizeof(struct result));
        newrp->filenum = filenum;
        newrp->rank = rank;
	newrp->structure = structure;
        newrp->next = resulthashlist[hashval];
        resulthashlist[hashval] = newrp;
}

/* Initializes the result hash list.
*/

void initresulthashlist()
{
	int i;

	for (i = 0; i < HASHSIZE; i++)
		resulthashlist[i] = NULL;
}