-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.h
80 lines (67 loc) · 1.27 KB
/
helpers.h
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
#define _HELPERS_H
#ifndef _STRING_H
#include <string.h>
#endif
#ifndef _STDLIB_H
#include <stdlib.h>
#endif
#ifndef _TIME_H
#include <time.h>
#endif
#define TYPE_KANDR 1
#define TYPE_ALLMAN 2
char HelperMatch(char c)
{
switch (c) {
case ')':
return '(';
case ']':
return '[';
case '}':
return '{';
default:
return 0;
}
}
/*-*- String HashSet -*-*/
/*-*- 0. Init -*-*/
/*-*- 1. Insert -*-*/
/*-*- 2. Delete -*-*/
/*-*- 3. Exist -*-*/
/*-*- 每个方法都是就地操作 -*-*/
/*-*- 不处理冲突 -*-*/
#define SET_SIZE 100005
#define SET_PRIME 100003
struct StringSet
{
short Table[SET_SIZE];
};
unsigned HashFunction(char *str)
{
unsigned int hash = 0;
while (*str)
hash = (*str++) + (hash << 6) + (hash << 16) - hash;
return hash % SET_PRIME;
}
void Init(struct StringSet* S)
{
memset(S->Table, 0, sizeof(S->Table));
return;
}
void Insert(struct StringSet* S, char *str)
{
int Pos = HashFunction(str);
S->Table[Pos] = 1;
return;
}
void Delete(struct StringSet* S, char *str)
{
int Pos = HashFunction(str);
S->Table[Pos] = 0;
return;
}
int Exist(struct StringSet* S, char *str)
{
int Pos = HashFunction(str);
return (int)S->Table[Pos];
}