fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
constmap.c
Go to the documentation of this file.
1#include "constmap.h"
2#include "alloc.h"
3#include "case.h"
4
14static constmap_hash hash(char *s,int len)
15{
16 unsigned char ch;
18 h = 5381;
19 while (len > 0) {
20 ch = *s++ - 'A';
21 if (ch <= 'Z' - 'A') ch += 'a' - 'A';
22 h = ((h << 5) + h) ^ ch;
23 --len;
24 }
25 return h;
26}
27
28char *constmap(struct constmap *cm,char *s,int len)
29{
31 int pos;
32 h = hash(s,len);
33 pos = cm->first[h & cm->mask];
34 while (pos != -1) {
35 if (h == cm->hash[pos])
36 if (len == cm->inputlen[pos])
37 if (!case_diffb(cm->input[pos],len,s))
38 return cm->input[pos] + cm->inputlen[pos] + 1;
39 pos = cm->next[pos];
40 }
41 return 0;
42}
43
44int constmap_init(struct constmap *cm,char *s,int len,int flagcolon)
45{
46 int i;
47 int j;
48 int k;
49 int pos;
51
52 cm->num = 0;
53 for (j = 0; j < len; ++j) if (!s[j]) ++cm->num;
54
55 h = 64;
56 while (h && (h < cm->num)) h += h;
57 cm->mask = h - 1;
58
59 cm->first = (int *) alloc(sizeof(int) * h);
60 if (cm->first) {
61 cm->input = (char **) alloc(sizeof(char *) * cm->num);
62 if (cm->input) {
63 cm->inputlen = (int *) alloc(sizeof(int) * cm->num);
64 if (cm->inputlen) {
65 cm->hash = (constmap_hash *) alloc(sizeof(constmap_hash) * cm->num);
66 if (cm->hash) {
67 cm->next = (int *) alloc(sizeof(int) * cm->num);
68 if (cm->next) {
69 for (h = 0; h <= cm->mask; ++h)
70 cm->first[h] = -1;
71 pos = 0;
72 i = 0;
73 for (j = 0; j < len; ++j)
74 if (!s[j]) {
75 k = j - i;
76 if (flagcolon) {
77 for (k = i; k < j; ++k)
78 if (s[k] == ':') break;
79 if (k >= j) { i = j + 1; continue; }
80 k -= i;
81 }
82 cm->input[pos] = s + i;
83 cm->inputlen[pos] = k;
84 h = hash(s + i,k);
85 cm->hash[pos] = h;
86 h &= cm->mask;
87 cm->next[pos] = cm->first[h];
88 cm->first[h] = pos;
89 ++pos;
90 i = j + 1;
91 }
92 return 1;
93 }
94 alloc_free(cm->hash);
95 }
97 }
98 alloc_free(cm->input);
99 }
100 alloc_free(cm->first);
101 }
102 return 0;
103}
104
105int constmap_init_char(struct constmap *cm,char *s,int len,int flagcolon,char flagchar)
106{
107 int i;
108 int j;
109 int k;
110 int pos;
112
113 if (!flagchar || flagchar == 0 || flagchar == '\0') {
114 flagchar = ':';
115 }
116
117 cm->num = 0;
118 for (j = 0; j < len; ++j) if (!s[j]) ++cm->num;
119
120 h = 64;
121 while (h && (h < cm->num)) h += h;
122 cm->mask = h - 1;
123
124 cm->first = (int *) alloc(sizeof(int) * h);
125 if (cm->first) {
126 cm->input = (char **) alloc(sizeof(char *) * cm->num);
127 if (cm->input) {
128 cm->inputlen = (int *) alloc(sizeof(int) * cm->num);
129 if (cm->inputlen) {
130 cm->hash = (constmap_hash *) alloc(sizeof(constmap_hash) * cm->num);
131 if (cm->hash) {
132 cm->next = (int *) alloc(sizeof(int) * cm->num);
133 if (cm->next) {
134 for (h = 0; h <= cm->mask; ++h)
135 cm->first[h] = -1;
136 pos = 0;
137 i = 0;
138 for (j = 0; j < len; ++j) {
139 if (!s[j]) {
140 k = j - i;
141 if (flagcolon) {
142 for (k = i; k < j; ++k)
143 if (s[k] == flagchar) break;
144 if (k >= j) { i = j + 1; continue; }
145 k -= i;
146 }
147 cm->input[pos] = s + i;
148 cm->inputlen[pos] = k;
149 h = hash(s + i,k);
150 cm->hash[pos] = h;
151 h &= cm->mask;
152 cm->next[pos] = cm->first[h];
153 cm->first[h] = pos;
154 ++pos;
155 i = j + 1;
156 }
157 }
158 return 1;
159 }
160 alloc_free(cm->hash);
161 }
162 alloc_free(cm->inputlen);
163 }
164 alloc_free(cm->input);
165 }
166 alloc_free(cm->first);
167 }
168 return 0;
169}
170
171void constmap_free(struct constmap *cm)
172{
173 alloc_free(cm->next);
174 alloc_free(cm->hash);
175 alloc_free(cm->inputlen);
176 alloc_free(cm->input);
177 alloc_free(cm->first);
178}
void constmap_free(struct constmap *cm)
Definition: constmap.c:171
int constmap_init_char(struct constmap *cm, char *s, int len, int flagcolon, char flagchar)
Definition: constmap.c:105
int constmap_init(struct constmap *cm, char *s, int len, int flagcolon)
Definition: constmap.c:44
int case_diffb(char *, unsigned int, char *)
Definition: case.c:10
void alloc_free(void *)
Definition: alloc.c:45
void * alloc(unsigned int)
Definition: alloc.c:27
unsigned long constmap_hash
Definition: constmap.h:4
int * first
Definition: constmap.h:10
constmap_hash * hash
Definition: constmap.h:9
constmap_hash mask
Definition: constmap.h:8
char ** input
Definition: constmap.h:12
int * next
Definition: constmap.h:11
int * inputlen
Definition: constmap.h:13
int num
Definition: constmap.h:7