-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression_match.h
270 lines (235 loc) · 8.32 KB
/
expression_match.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "api.h"
#include "hashTable.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
// #define TOKEN_MAX_LEN 1000 // (Jun): this is the max length of ONE token?
// (Anthony): yes. I just guess that it'll less than 1000
//(YuKai): Stack is good.
// #define min(x, y) x>y?y:x
// Mark1: Functions declarations
// Main functions
int expressionMatch(char *expr, int *ans_arr, HashTable *hashTables[]);
bool compute(char *expr, int lenExpr, int mailId, HashTable *hashTables[]);
// Tool functions
int operatorLevel(char);
void myStrcpy(char *dest, char *source, int len);
// Stack
struct Charstack;
typedef struct CharStack CharStack;
void charStack_init(CharStack *);
bool charStack_empty(CharStack *);
void charStack_push(CharStack *, char);
char charStack_pop(CharStack *);
char charStack_top(CharStack *);
void charStack_print(CharStack *);
// Mark2: Functions definitions
// Stack
typedef struct CharStack {
// start from 0 so size will be initialize -1
char *arr;
int size, capa;
} CharStack;
void charStack_init(CharStack *cs) {
cs->size = -1;
cs->capa = 1;
cs->arr = malloc(sizeof(char)*cs->capa);
}
bool charStack_empty(CharStack *cs) {
return cs->size <= -1;
}
void charStack_push(CharStack *cs, char charPushed) {
if (cs->size+1 >= cs->capa) {
cs->capa *= 2;
cs->arr = realloc(cs->arr, sizeof(char)*cs->capa);
}
cs->size++;
cs->arr[cs->size] = charPushed;
}
char charStack_pop(CharStack *cs) {
if (!charStack_empty(cs)) {
cs->size--;
return cs->arr[cs->size+1];
}
else {
return -1;
}
}
char charStack_top(CharStack *cs) {
if (!charStack_empty(cs)) {
return cs->arr[cs->size];
}
else {
return -1;
}
}
void charStack_print(CharStack *cs) {
for (int i = 0; i <= cs->size; i++) {
if (cs->arr[i] == 0) {
printf("0 ");
}
else if (cs->arr[i] == 1) {
printf("1 ");
}
else {
printf("%c ", cs->arr[i]);
}
}
printf(" size = %d", cs->size);
printf("\n");
}
// Stack end
int expressionMatch(char *expr, int *ans_arr, HashTable *hashTables[]) {
int ans_len = 0;
int lenExpr = strlen(expr);
for (int i = 0; i < n_mails; i++) {
if (compute(expr, lenExpr, i, hashTables)) {
ans_arr[ans_len++] = i;
}
}
return ans_len;
}
bool compute(char *expr, int lenExpr, int mailId, HashTable *hashTables[]) {
// () -> NOT -> AND -> OR
CharStack boolStack, operStack;
charStack_init(&boolStack);
charStack_init(&operStack);
for (int i = 0; i < lenExpr; i++) {
// printf("\n");
// charStack_print(&boolStack);
// charStack_print(&operStack);
if (expr[i] == '!') {
// printf("found operation %c\n", expr[i]);
charStack_push(&operStack, '!');
}
else if (expr[i] == '(') {
// printf("found operation %c\n", expr[i]);
charStack_push(&operStack, '(');
}
else if (expr[i] == ')') {
// printf("found operation %c at %d\n", expr[i], i);
while (charStack_top(&operStack) != '(') {
char topOperator = charStack_pop(&operStack);
if (topOperator == '|') {
char bool1 = charStack_pop(&boolStack);
char bool2 = charStack_pop(&boolStack);
charStack_push(&boolStack, bool1 | bool2);
}
else if (topOperator == '&') {
char bool1 = charStack_pop(&boolStack);
char bool2 = charStack_pop(&boolStack);
charStack_push(&boolStack, bool1 & bool2);
}
else if (topOperator == '!') {
char bool1 = charStack_pop(&boolStack);
charStack_push(&boolStack, !bool1);
}
}
charStack_pop(&operStack); // Pop the '('
}
else if (expr[i] == '|') {
// printf("found operation %c\n", expr[i]);
charStack_push(&operStack, '|');
}
else if (expr[i] == '&') {
// printf("found operation %c\n", expr[i]);
charStack_push(&operStack, '&');
}
else { // reach a token
int tokenLen = 0;
while (i+tokenLen < lenExpr && !isDelimiter(expr[i+tokenLen])) {
tokenLen++;
}
// (Jun): I changed findToken() into using hashTable
// char charPushed = findToken(token, lenToken, mailId)?1:0;
char charPushed = hashTable_findToken_inputString(hashTables[mailId], expr+i, tokenLen)?1:0;
i += tokenLen - 1;
// 1736
// Henry
// Mumbai Champs was one of the nine teams played in the defunct Indian Cricket League (ICL). The team is based in Mumbai, India and its captain is former New Zealand batsman Nathan Astle.[1][2] The squad announced for the inaugural tournament comprises four international cricketers. Nathan Astle, Johan Van der Wath, Tino Best, Michael Kasprowicz are amongst the high-profile names playing for the Mumbai Champs.[3] Coach - Sandeep Patil I read the paragraph on http://wikipedia.org
// Mumbai Champs
// Victoria
// !((proboard)&((c19)&(plo))|(aira)|(!((tolbooth)|(459e)|(!tino)&(!mdrx)|(522052n)))&(!20022007))
// if (mailId == 1736)
// printf("found string %s(hashed:%d) => %d\n", token, hashString(token, lenToken), charPushed?1:0);
charStack_push(&boolStack, charPushed);
}
while (operStack.size > 0) { // size larger than 1
char topOperator = charStack_pop(&operStack);
char preOperator = charStack_pop(&operStack);
int preLevel = operatorLevel(preOperator);
int topLevel = operatorLevel(topOperator);
if (preLevel >= topLevel && preOperator != '(') {
if (preOperator == '|') {
char bool1 = charStack_pop(&boolStack);
char bool2 = charStack_pop(&boolStack);
charStack_push(&boolStack, bool1 | bool2);
}
else if (preOperator == '&') {
char bool1 = charStack_pop(&boolStack);
char bool2 = charStack_pop(&boolStack);
charStack_push(&boolStack, bool1 & bool2);
}
else if (preOperator == '!') {
char bool1 = charStack_pop(&boolStack);
charStack_push(&boolStack, !bool1);
}
charStack_push(&operStack, topOperator);
} else {
charStack_push(&operStack, preOperator);
charStack_push(&operStack, topOperator);
break;
}
}
}
while (!charStack_empty(&operStack)) {
// printf("\n");
// charStack_print(&boolStack);
// charStack_print(&operStack);
char topOperator = charStack_pop(&operStack);
if (topOperator == '|') {
char bool1 = charStack_pop(&boolStack);
char bool2 = charStack_pop(&boolStack);
charStack_push(&boolStack, bool1 | bool2);
}
else if (topOperator == '&') {
char bool1 = charStack_pop(&boolStack);
char bool2 = charStack_pop(&boolStack);
charStack_push(&boolStack, bool1 & bool2);
}
else if (topOperator == '!') {
char bool1 = charStack_pop(&boolStack);
charStack_push(&boolStack, !bool1);
}
}
// printf("\n");
// charStack_print(&boolStack);
// charStack_print(&operStack);
bool returnAns = charStack_top(&boolStack) == 1;
free(boolStack.arr);
free(operStack.arr);
return returnAns;
}
int operatorLevel(char someOperator) {
if (someOperator == '|') {
return 0;
}
if (someOperator == '&') {
return 1;
}
if (someOperator == '!') {
return 2;
}
if (someOperator == '(') {
return 3;
}
return -1;
}
void myStrcpy(char *dest, char *source, int len) {
// printf("copying len=%d\n", len);
for (int i = 0; i < len; i++) {
dest[i] = source[i];
}
dest[len] = '\0';
}