@@ -25,9 +25,7 @@ typedef struct QueryItemWrap
25
25
QueryItemType type ;
26
26
int8 oper ;
27
27
bool not ;
28
- int operandsCount ,
29
- operandsAllocated ;
30
- struct QueryItemWrap * operands ;
28
+ List * operands ;
31
29
struct QueryItemWrap * parent ;
32
30
int distance ,
33
31
length ;
@@ -40,29 +38,12 @@ add_child(QueryItemWrap * parent)
40
38
{
41
39
QueryItemWrap * result ;
42
40
43
- if (!parent )
44
- {
45
- result = (QueryItemWrap * ) palloc0 (sizeof (QueryItemWrap ));
46
- }
47
- else
41
+ result = (QueryItemWrap * ) palloc0 (sizeof (QueryItemWrap ));
42
+
43
+ if (parent )
48
44
{
49
- parent -> operandsCount ++ ;
50
- while (parent -> operandsCount > parent -> operandsAllocated )
51
- {
52
- if (parent -> operandsAllocated > 0 )
53
- {
54
- parent -> operandsAllocated *= 2 ;
55
- parent -> operands = (QueryItemWrap * ) repalloc (parent -> operands , parent -> operandsAllocated * sizeof (* parent -> operands ));
56
- }
57
- else
58
- {
59
- parent -> operandsAllocated = 4 ;
60
- parent -> operands = (QueryItemWrap * ) palloc (parent -> operandsAllocated * sizeof (* parent -> operands ));
61
- }
62
- }
63
- result = & parent -> operands [parent -> operandsCount - 1 ];
64
- memset (result , 0 , sizeof (* result ));
65
45
result -> parent = parent ;
46
+ parent -> operands = lappend (parent -> operands , result );
66
47
}
67
48
return result ;
68
49
}
@@ -129,21 +110,23 @@ make_query_item_wrap(QueryItem *item, QueryItemWrap * parent, bool not)
129
110
static int
130
111
calc_wraps (QueryItemWrap * wrap , int * num )
131
112
{
132
- int i ,
133
- notCount = 0 ,
113
+ int notCount = 0 ,
134
114
result ;
115
+ ListCell * lc ;
135
116
136
- for ( i = 0 ; i < wrap -> operandsCount ; i ++ )
117
+ foreach ( lc , wrap -> operands )
137
118
{
138
- if (wrap -> operands [i ].not )
119
+ QueryItemWrap * item = (QueryItemWrap * ) lfirst (lc );
120
+
121
+ if (item -> not )
139
122
notCount ++ ;
140
123
}
141
124
142
125
if (wrap -> type == QI_OPR )
143
126
{
144
127
wrap -> num = (* num )++ ;
145
128
if (wrap -> oper == OP_AND )
146
- wrap -> sum = notCount + 1 - wrap -> operandsCount ;
129
+ wrap -> sum = notCount + 1 - list_length ( wrap -> operands ) ;
147
130
if (wrap -> oper == OP_OR )
148
131
wrap -> sum = notCount ;
149
132
}
@@ -153,8 +136,12 @@ calc_wraps(QueryItemWrap * wrap, int *num)
153
136
}
154
137
155
138
result = 0 ;
156
- for (i = 0 ; i < wrap -> operandsCount ; i ++ )
157
- result += calc_wraps (& wrap -> operands [i ], num );
139
+ foreach (lc , wrap -> operands )
140
+ {
141
+ QueryItemWrap * item = (QueryItemWrap * ) lfirst (lc );
142
+
143
+ result += calc_wraps (item , num );
144
+ }
158
145
return result ;
159
146
}
160
147
@@ -167,22 +154,26 @@ check_allnegative(QueryItemWrap * wrap)
167
154
}
168
155
else if (wrap -> oper == OP_AND )
169
156
{
170
- int i ;
157
+ ListCell * lc ;
171
158
172
- for ( i = 0 ; i < wrap -> operandsCount ; i ++ )
159
+ foreach ( lc , wrap -> operands )
173
160
{
174
- if (!check_allnegative (& wrap -> operands [i ]))
161
+ QueryItemWrap * item = (QueryItemWrap * ) lfirst (lc );
162
+
163
+ if (!check_allnegative (item ))
175
164
return false;
176
165
}
177
166
return true;
178
167
}
179
168
else if (wrap -> oper == OP_OR )
180
169
{
181
- int i ;
170
+ ListCell * lc ;
182
171
183
- for ( i = 0 ; i < wrap -> operandsCount ; i ++ )
172
+ foreach ( lc , wrap -> operands )
184
173
{
185
- if (check_allnegative (& wrap -> operands [i ]))
174
+ QueryItemWrap * item = (QueryItemWrap * ) lfirst (lc );
175
+
176
+ if (check_allnegative (item ))
186
177
return true;
187
178
}
188
179
return false;
@@ -348,10 +339,14 @@ extract_wraps(QueryItemWrap * wrap, ExtractContext * context, int level)
348
339
}
349
340
else if (wrap -> type == QI_OPR )
350
341
{
351
- int i ;
342
+ ListCell * lc ;
352
343
353
- for (i = 0 ; i < wrap -> operandsCount ; i ++ )
354
- extract_wraps (& wrap -> operands [i ], context , level + 1 );
344
+ foreach (lc , wrap -> operands )
345
+ {
346
+ QueryItemWrap * item = (QueryItemWrap * ) lfirst (lc );
347
+
348
+ extract_wraps (item , context , level + 1 );
349
+ }
355
350
}
356
351
}
357
352
0 commit comments