00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00035 #include "aggregatefunctexpression.h"
00036
00037 AggregateFunctExpression::AggregateFunctExpression(EXP_TYPE _type,
00038 VarExpression *
00039 _var):OperandExpression
00040 (_type, et_operandaggregatefunct),
00041 var(_var),
00042 varstep(NULL),
00043 forexp(NULL),
00044 bit(NULL) {
00045 }
00046
00047 AggregateFunctExpression::AggregateFunctExpression(EXP_TYPE _type,
00048 VarStepExpression *
00049 _varstep):OperandExpression
00050 (_type, et_operandaggregatefunct),
00051 var(NULL),
00052 varstep(_varstep),
00053 forexp(NULL),
00054 bit(NULL) {
00055 }
00056
00057 AggregateFunctExpression::~AggregateFunctExpression() {
00058 delete var;
00059 delete varstep;
00060 delete forexp;
00061 delete bit;
00062 }
00063
00064 void AggregateFunctExpression::scopeCheck(vector < unsigned >&def_vars,
00065 vector < unsigned >&introduced_vars,
00066 vector < unsigned >&violating_vars) {
00067 if (var) {
00068 return var->scopeCheck(def_vars, introduced_vars, violating_vars);
00069 } else {
00070 return varstep->scopeCheck(def_vars, introduced_vars, violating_vars);
00071 }
00072 }
00073
00074 void AggregateFunctExpression::replaceVarId(unsigned old_id, unsigned new_id) {
00075 if (var) {
00076 var->replaceVarId(old_id, new_id);
00077 } else {
00078 varstep->replaceVarId(old_id, new_id);
00079 }
00080 }
00081
00082 void AggregateFunctExpression::extractFSAMap(FSAMap * fsamap,
00083 unsigned parent_var) {
00084 forexp->extractFSAMap(fsamap, parent_var);
00085 }
00086
00087 void AggregateFunctExpression::extractParVarMap(ParVarMap * parvarmap) {
00088 forexp->extractParVarMap(parvarmap);
00089 }
00090
00091 void AggregateFunctExpression::extractDependencies(vector <
00092 DependencySet * >*depset) {
00093
00094
00095 if (type != et_aggregatefunctcount) {
00096 forexp->extractDependencies(depset);
00097 }
00098 }
00099
00100 Expression *AggregateFunctExpression::placeSignOffs(vector <
00101 SignOffExpression *
00102 >&signoffs) {
00103 forexp = (ForExpression *) forexp->placeSignOffs(signoffs);
00104
00105 return this;
00106 }
00107
00108 void AggregateFunctExpression::rewriteAggregateFuncts() {
00109 if (var) {
00110 VarExpression *tmpvar =
00111 new VarExpression(VarName::getInstance()->getFreshVarname(),
00112 true);
00113 ForExpression *fexp =
00114 new ForExpression(tmpvar, var->clone(), tmpvar->clone());
00115 forexp = fexp;
00116 } else {
00117 VarExpression *tmpvar =
00118 new VarExpression(VarName::getInstance()->getFreshVarname(),
00119 true);
00120 ForExpression *fexp =
00121 new ForExpression(tmpvar, varstep->clone(), tmpvar->clone());
00122 forexp = fexp;
00123 }
00124 }
00125
00126 void AggregateFunctExpression::print(OutputStream & dos) const {
00127 switch (type) {
00128 case et_aggregatefunctsum:
00129 if (forexp) {
00130 dos << "fn:sum(" << (*forexp) << ")";
00131 } else if (varstep) {
00132 dos << "fn:sum(" << (*varstep) << ")";
00133 } else {
00134 dos << "fn:sum(" << (*var) << ")";
00135 }
00136 break;
00137 case et_aggregatefunctavg:
00138 if (forexp) {
00139 dos << "fn:avg(" << (*forexp) << ")";
00140 } else if (varstep) {
00141 dos << "fn:avg(" << (*varstep) << ")";
00142 } else {
00143 dos << "fn:avg(" << (*var) << ")";
00144 }
00145 break;
00146 case et_aggregatefunctmin:
00147 if (forexp) {
00148 dos << "fn:min(" << (*forexp) << ")";
00149 } else if (varstep) {
00150 dos << "fn:min(" << (*varstep) << ")";
00151 } else {
00152 dos << "fn:min(" << (*var) << ")";
00153 }
00154 break;
00155 case et_aggregatefunctmax:
00156 if (forexp) {
00157 dos << "fn:max(" << (*forexp) << ")";
00158 } else if (varstep) {
00159 dos << "fn:max(" << (*varstep) << ")";
00160 } else {
00161 dos << "fn:max(" << (*var) << ")";
00162 }
00163 break;
00164 case et_aggregatefunctcount:
00165 if (forexp) {
00166 dos << "fn:count(" << (*forexp) << ")";
00167 } else if (varstep) {
00168 dos << "fn:count(" << (*varstep) << ")";
00169 } else {
00170 dos << "fn:count(" << (*var) << ")";
00171 }
00172 break;
00173 case et_aggregatefunctstddevsamp:
00174 if (forexp) {
00175 dos << "fn:stddev_samp(" << (*forexp) << ")";
00176 } else if (varstep) {
00177 dos << "fn:stddev_samp(" << (*varstep) << ")";
00178 } else {
00179 dos << "fn:stddev_samp(" << (*var) << ")";
00180 }
00181 break;
00182 case et_aggregatefunctstddevpop:
00183 if (forexp) {
00184 dos << "fn:stddev_pop(" << (*forexp) << ")";
00185 } else if (varstep) {
00186 dos << "fn:stddev_pop(" << (*varstep) << ")";
00187 } else {
00188 dos << "fn:stddev_pop(" << (*var) << ")";
00189 }
00190 break;
00191 case et_aggregatefunctvarsamp:
00192 if (forexp) {
00193 dos << "fn:var_samp(" << (*forexp) << ")";
00194 } else if (varstep) {
00195 dos << "fn:var_samp(" << (*varstep) << ")";
00196 } else {
00197 dos << "fn:var_samp(" << (*var) << ")";
00198 }
00199 break;
00200 case et_aggregatefunctvarpop:
00201 if (forexp) {
00202 dos << "fn:var_pop(" << (*forexp) << ")";
00203 } else if (varstep) {
00204 dos << "fn:var_pop(" << (*varstep) << ")";
00205 } else {
00206 dos << "fn:var_pop(" << (*var) << ")";
00207 }
00208 break;
00209 case et_aggregatefunctmedian:
00210 if (forexp) {
00211 dos << "fn:median(" << (*forexp) << ")";
00212 } else if (varstep) {
00213 dos << "fn:median(" << (*varstep) << ")";
00214 } else {
00215 dos << "fn:median(" << (*var) << ")";
00216 }
00217 break;
00218 default:
00219 break;
00220 }
00221 }
00222
00223 void AggregateFunctExpression::init(BufferNode * root) {
00224 delete bit;
00225
00226 bit =
00227 var ? new BufferIterator(root, NULL) : new BufferIterator(root,
00228 varstep->
00229 getPath());
00230 forexp->init(root);
00231 }
00232
00233 void AggregateFunctExpression::prepareOperand(OutputStream & eos,
00234 Environment * env,
00235 unsigned modus) {
00236 eval(eos, env, EVAL_QUERY_SILENT);
00237 }
00238
00239 Value *AggregateFunctExpression::getNextValue() {
00240 if (initial) {
00241 initial = false;
00242 return &cur_val;
00243 } else {
00244 initial = true;
00245 return NULL;
00246 }
00247 }