@@ -25,9 +25,7 @@ typedef struct QueryItemWrap
2525 QueryItemType type ;
2626 int8 oper ;
2727 bool not ;
28- int operandsCount ,
29- operandsAllocated ;
30- struct QueryItemWrap * operands ;
28+ List * operands ;
3129 struct QueryItemWrap * parent ;
3230 int distance ,
3331 length ;
@@ -40,29 +38,12 @@ add_child(QueryItemWrap * parent)
4038{
4139 QueryItemWrap * result ;
4240
43- if (!parent )
44- {
45- result = (QueryItemWrap * ) palloc0 (sizeof (QueryItemWrap ));
46- }
47- else
41+ result = (QueryItemWrap * ) palloc0 (sizeof (QueryItemWrap ));
42+
43+ if (parent )
4844 {
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 ));
6545 result -> parent = parent ;
46+ parent -> operands = lappend (parent -> operands , result );
6647 }
6748 return result ;
6849}
@@ -129,21 +110,23 @@ make_query_item_wrap(QueryItem *item, QueryItemWrap * parent, bool not)
129110static int
130111calc_wraps (QueryItemWrap * wrap , int * num )
131112{
132- int i ,
133- notCount = 0 ,
113+ int notCount = 0 ,
134114 result ;
115+ ListCell * lc ;
135116
136- for ( i = 0 ; i < wrap -> operandsCount ; i ++ )
117+ foreach ( lc , wrap -> operands )
137118 {
138- if (wrap -> operands [i ].not )
119+ QueryItemWrap * item = (QueryItemWrap * ) lfirst (lc );
120+
121+ if (item -> not )
139122 notCount ++ ;
140123 }
141124
142125 if (wrap -> type == QI_OPR )
143126 {
144127 wrap -> num = (* num )++ ;
145128 if (wrap -> oper == OP_AND )
146- wrap -> sum = notCount + 1 - wrap -> operandsCount ;
129+ wrap -> sum = notCount + 1 - list_length ( wrap -> operands ) ;
147130 if (wrap -> oper == OP_OR )
148131 wrap -> sum = notCount ;
149132 }
@@ -153,8 +136,12 @@ calc_wraps(QueryItemWrap * wrap, int *num)
153136 }
154137
155138 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+ }
158145 return result ;
159146}
160147
@@ -167,22 +154,26 @@ check_allnegative(QueryItemWrap * wrap)
167154 }
168155 else if (wrap -> oper == OP_AND )
169156 {
170- int i ;
157+ ListCell * lc ;
171158
172- for ( i = 0 ; i < wrap -> operandsCount ; i ++ )
159+ foreach ( lc , wrap -> operands )
173160 {
174- if (!check_allnegative (& wrap -> operands [i ]))
161+ QueryItemWrap * item = (QueryItemWrap * ) lfirst (lc );
162+
163+ if (!check_allnegative (item ))
175164 return false;
176165 }
177166 return true;
178167 }
179168 else if (wrap -> oper == OP_OR )
180169 {
181- int i ;
170+ ListCell * lc ;
182171
183- for ( i = 0 ; i < wrap -> operandsCount ; i ++ )
172+ foreach ( lc , wrap -> operands )
184173 {
185- if (check_allnegative (& wrap -> operands [i ]))
174+ QueryItemWrap * item = (QueryItemWrap * ) lfirst (lc );
175+
176+ if (check_allnegative (item ))
186177 return true;
187178 }
188179 return false;
@@ -348,10 +339,14 @@ extract_wraps(QueryItemWrap * wrap, ExtractContext * context, int level)
348339 }
349340 else if (wrap -> type == QI_OPR )
350341 {
351- int i ;
342+ ListCell * lc ;
352343
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+ }
355350 }
356351}
357352
0 commit comments