1.1 --- a/gui/new_map_win.cc Thu Nov 17 10:46:38 2005 +0000
1.2 +++ b/gui/new_map_win.cc Thu Nov 17 15:34:18 2005 +0000
1.3 @@ -77,41 +77,184 @@
1.4
1.5 void NewMapWin::buttonPressed()
1.6 {
1.7 - bool valid_double=true;
1.8 - int point_num=0;
1.9 + double def_val=0;
1.10
1.11 + //get and formulate text
1.12 std::string def_val_str=default_value.get_text();
1.13 - char * def_val_ch=new char [def_val_str.length()];
1.14 - for(int i=0;i<(int)(def_val_str.length());i++)
1.15 - {
1.16 - if(((def_val_str[i]<'0')||(def_val_str[i]>'9'))&&(def_val_str[i]!='.'))
1.17 - {
1.18 - valid_double=false;
1.19 - }
1.20 - else
1.21 - {
1.22 - if(def_val_str[i]=='.')
1.23 - {
1.24 - point_num++;
1.25 - }
1.26 - }
1.27 - def_val_ch[i]=def_val_str[i];
1.28 - }
1.29 -
1.30 - double def_val=atof(def_val_ch);
1.31 + std::string polishform=string2Polishform(def_val_str,edge.get_active());
1.32
1.33 + //get name of text
1.34 std::string mapname=name.get_text();
1.35
1.36 - if((point_num<=1)&&(valid_double)&&(!mapname.empty()))
1.37 + if(!mapname.empty()&&!polishform.empty())
1.38 {
1.39 int abortion=0;
1.40 if(edge.get_active())
1.41 {
1.42 - abortion=gdc.addNewEdgeMap(def_val,mapname);
1.43 + //create the new map
1.44 + Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> (gdc.mapstorage.graph);
1.45 +
1.46 + std::stack<double> polishstack;
1.47 +
1.48 + for(EdgeIt k(gdc.mapstorage.graph); k!=INVALID; ++k)
1.49 + {
1.50 + for(int i=0;i<(int)polishform.size();i++)
1.51 + {
1.52 + double op1, op2;
1.53 + bool operation=true;
1.54 + switch(polishform[i])
1.55 + {
1.56 + case '+':
1.57 + case '-':
1.58 + case '/':
1.59 + case '*':
1.60 + op1=polishstack.top();
1.61 + polishstack.pop();
1.62 + op2=polishstack.top();
1.63 + polishstack.pop();
1.64 + break;
1.65 + default:
1.66 + //substitute variable
1.67 + std::map< std::string,Graph::EdgeMap<double> * > ems=gdc.mapstorage.edgemap_storage;
1.68 + bool itisvar=(ems.find(ch2var[ polishform[i] ])!=ems.end());
1.69 + if(itisvar)
1.70 + {
1.71 + polishstack.push( (*(gdc.mapstorage.edgemap_storage[ ch2var[ polishform[i] ] ]))[k]);
1.72 + }
1.73 + else
1.74 + {
1.75 + char * def_val_ch=new char [(int)(ch2var[ polishform[i] ].length())];
1.76 + for(int j=0;j<(int)(ch2var[ polishform[i] ].length());j++)
1.77 + {
1.78 + def_val_ch[j]=ch2var[ polishform[i] ][j];
1.79 + }
1.80 + polishstack.push(atof(def_val_ch));
1.81 + }
1.82 + operation=false;
1.83 + break;
1.84 + }
1.85 + if(operation)
1.86 + {
1.87 + double res;
1.88 + switch(polishform[i])
1.89 + {
1.90 + case '+':
1.91 + res=op1+op2;
1.92 + break;
1.93 + case '-':
1.94 + res=op2-op1;
1.95 + break;
1.96 + case '/':
1.97 + res=op2/op1;
1.98 + break;
1.99 + case '*':
1.100 + res=op1*op2;
1.101 + break;
1.102 + default:
1.103 + std::cout << "How could we get here?" << std::endl;
1.104 + break;
1.105 + }
1.106 + polishstack.push(res);
1.107 + }
1.108 + }
1.109 + (*emptr)[k]=polishstack.top();
1.110 + }
1.111 +
1.112 + //if addition was not successful addEdgeMap returns one.
1.113 + //cause can be that there is already a map named like the new one
1.114 + if(gdc.mapstorage.addEdgeMap(mapname, emptr, def_val))
1.115 + {
1.116 + abortion=1;
1.117 + }
1.118 +
1.119 + //add it to the list of the displayable maps
1.120 + gdc.mapwin.registerNewEdgeMap(mapname);
1.121 +
1.122 + //display it
1.123 + gdc.changeEdgeText(mapname);
1.124 }
1.125 - else
1.126 + else //!edge.get_active()
1.127 {
1.128 - abortion=gdc.addNewNodeMap(def_val,mapname);
1.129 + //create the new map
1.130 + Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> (gdc.mapstorage.graph);
1.131 +
1.132 + std::stack<double> polishstack;
1.133 +
1.134 + for(NodeIt k(gdc.mapstorage.graph); k!=INVALID; ++k)
1.135 + {
1.136 + for(int i=0;i<(int)polishform.size();i++)
1.137 + {
1.138 + double op1, op2;
1.139 + bool operation=true;
1.140 + switch(polishform[i])
1.141 + {
1.142 + case '+':
1.143 + case '-':
1.144 + case '/':
1.145 + case '*':
1.146 + op1=polishstack.top();
1.147 + polishstack.pop();
1.148 + op2=polishstack.top();
1.149 + polishstack.pop();
1.150 + break;
1.151 + default:
1.152 + std::map< std::string,Graph::NodeMap<double> * > nms=gdc.mapstorage.nodemap_storage;
1.153 + bool itisvar=(nms.find(ch2var[ polishform[i] ])!=nms.end());
1.154 + if(itisvar)
1.155 + {
1.156 + polishstack.push( (*(gdc.mapstorage.nodemap_storage[ ch2var[ polishform[i] ] ]))[k]);
1.157 + }
1.158 + else
1.159 + {
1.160 + char * def_val_ch=new char [(int)(ch2var[ polishform[i] ].length())];
1.161 + for(int j=0;j<(int)(ch2var[ polishform[i] ].length());j++)
1.162 + {
1.163 + def_val_ch[j]=ch2var[ polishform[i] ][j];
1.164 + }
1.165 + polishstack.push(atof(def_val_ch));
1.166 + }
1.167 + operation=false;
1.168 + break;
1.169 + }
1.170 + if(operation)
1.171 + {
1.172 + double res;
1.173 + switch(polishform[i])
1.174 + {
1.175 + case '+':
1.176 + res=op1+op2;
1.177 + break;
1.178 + case '-':
1.179 + res=op2-op1;
1.180 + break;
1.181 + case '/':
1.182 + res=op2/op1;
1.183 + break;
1.184 + case '*':
1.185 + res=op1*op2;
1.186 + break;
1.187 + default:
1.188 + std::cout << "How could we get here?" << std::endl;
1.189 + break;
1.190 + }
1.191 + polishstack.push(res);
1.192 + }
1.193 + }
1.194 + (*emptr)[k]=polishstack.top();
1.195 + }
1.196 +
1.197 + //if addition was not successful addNodeMap returns one.
1.198 + //cause can be that there is already a map named like the new one
1.199 + if(gdc.mapstorage.addNodeMap(mapname,emptr, def_val))
1.200 + {
1.201 + abortion=1;
1.202 + }
1.203 +
1.204 + //add it to the list of the displayable maps
1.205 + gdc.mapwin.registerNewNodeMap(mapname);
1.206 +
1.207 + //display it
1.208 + gdc.changeNodeText(mapname);
1.209 }
1.210 if(!abortion)
1.211 {
1.212 @@ -124,3 +267,210 @@
1.213 }
1.214 }
1.215
1.216 +std::string NewMapWin::string2Polishform(std::string rawcommand, bool itisedge)
1.217 +{
1.218 + bool valid_entry=true;
1.219 +
1.220 + std::map<std::string, int> str2i;
1.221 +
1.222 + std::string command;
1.223 +
1.224 + std::string variable;
1.225 +
1.226 + char index='a';
1.227 +
1.228 + for(int i=0;(valid_entry&&(i<(int)rawcommand.size()));i++)
1.229 + {
1.230 + switch(rawcommand[i])
1.231 + {
1.232 + case '+':
1.233 + case '-':
1.234 + case '*':
1.235 + case '/':
1.236 + case ')':
1.237 + case '(':
1.238 + if(!variable.empty())
1.239 + {
1.240 + valid_entry=validVariable(variable, itisedge);
1.241 + ch2var[index]=variable;
1.242 + command+=index;
1.243 + index++;
1.244 + variable.erase(0,variable.size());
1.245 + }
1.246 + command+=rawcommand[i];
1.247 + break;
1.248 + default:
1.249 + variable+=rawcommand[i];
1.250 + break;
1.251 + }
1.252 + }
1.253 +
1.254 + if(!variable.empty()&&valid_entry)
1.255 + {
1.256 + valid_entry=validVariable(variable, itisedge);
1.257 + ch2var[index]=variable;
1.258 + command+=index;
1.259 + index++;
1.260 + variable.erase(0,variable.size());
1.261 + }
1.262 +
1.263 + if(valid_entry)
1.264 + {
1.265 + unsigned int pr=10000;
1.266 + bool prevmult=false;
1.267 + unsigned int prev_change;
1.268 + unsigned int prev_br=10000;
1.269 + int counter=0;
1.270 + std::string comm_nobr="";
1.271 + std::vector<unsigned int> p;
1.272 + p.resize(counter+1);
1.273 +
1.274 + //limits
1.275 + //6 brackets embedded
1.276 + //100 operation in a row from the same priority
1.277 +
1.278 + for(int i=0;i<(int)command.size();i++)
1.279 + {
1.280 + bool put_in_string=true;
1.281 + switch(command[i])
1.282 + {
1.283 + case '(':
1.284 + pr=prev_br+10000;
1.285 + prev_br=pr;
1.286 + prevmult=false;
1.287 + put_in_string=false;
1.288 + break;
1.289 + case ')':
1.290 + pr=prev_br-10000;
1.291 + prev_br=pr;
1.292 + prevmult=false;
1.293 + put_in_string=false;
1.294 + break;
1.295 + case '+':
1.296 + case '-':
1.297 + if(prevmult)
1.298 + {
1.299 + pr=prev_change;
1.300 + }
1.301 + p[counter]=pr;
1.302 + pr-=100;
1.303 +
1.304 + prevmult=false;
1.305 + break;
1.306 + case '/':
1.307 + case '*':
1.308 + if(!prevmult)
1.309 + {
1.310 + prev_change=pr;
1.311 + pr+=200;
1.312 + pr-=1;
1.313 + }
1.314 + p[counter]=pr;
1.315 + pr-=1;
1.316 + prevmult=true;
1.317 + break;
1.318 + default:
1.319 + p[counter]=65000;
1.320 + break;
1.321 + }
1.322 + if(put_in_string)
1.323 + {
1.324 + counter++;
1.325 + p.resize(counter+1);
1.326 + comm_nobr=comm_nobr+command[i];
1.327 + }
1.328 + }
1.329 +
1.330 + tree_node * root=weightedString2Tree(comm_nobr, p, 0);
1.331 +
1.332 + std::string polishform=postOrder(root);
1.333 +
1.334 + return polishform;
1.335 + }
1.336 + return "";
1.337 +}
1.338 +
1.339 +NewMapWin::tree_node * NewMapWin::weightedString2Tree(std::string to_tree, std::vector<unsigned int> & p, int offset)
1.340 +{
1.341 + int min=p[offset];
1.342 + int minplace=0;
1.343 + for(int i=0;i<(int)to_tree.size();i++)
1.344 + {
1.345 + if(min>p[offset+i])
1.346 + {
1.347 + min=p[offset+i];
1.348 + minplace=i;
1.349 + }
1.350 + }
1.351 + tree_node * act_node=new tree_node;
1.352 + act_node->ch=to_tree[minplace];
1.353 + if(to_tree.size()>=3)
1.354 + {
1.355 + act_node->left_child=weightedString2Tree(to_tree.substr(0,minplace), p, offset);
1.356 + act_node->right_child=weightedString2Tree(to_tree.substr(minplace+1,to_tree.size()-minplace-1), p, offset+minplace+1);
1.357 + }
1.358 + else
1.359 + {
1.360 + act_node->left_child=NULL;
1.361 + act_node->right_child=NULL;
1.362 + }
1.363 + return act_node;
1.364 +}
1.365 +
1.366 +std::string NewMapWin::postOrder(tree_node * subtree)
1.367 +{
1.368 + std::string subtree_to_string;
1.369 + if(subtree->left_child)
1.370 + {
1.371 + subtree_to_string=postOrder(subtree->left_child);
1.372 + }
1.373 + if(subtree->right_child)
1.374 + {
1.375 + subtree_to_string=subtree_to_string+postOrder(subtree->right_child);
1.376 + }
1.377 + subtree_to_string=subtree_to_string+subtree->ch;
1.378 + return subtree_to_string;
1.379 +}
1.380 +
1.381 +bool NewMapWin::validVariable(std::string variable, bool itisedge)
1.382 +{
1.383 + bool cancel;
1.384 + //is it mapname?
1.385 + if(itisedge)
1.386 + {
1.387 + cancel=(gdc.mapstorage.edgemap_storage.find(variable)==gdc.mapstorage.edgemap_storage.end());
1.388 + }
1.389 + else
1.390 + {
1.391 + cancel=(gdc.mapstorage.nodemap_storage.find(variable)==gdc.mapstorage.nodemap_storage.end());
1.392 + }
1.393 + //maybe it is number
1.394 + int point_num=0;
1.395 + if(cancel)
1.396 + {
1.397 + cancel=false;
1.398 + for(int j=0;(!cancel)&&(j<(int)variable.size());j++)
1.399 + {
1.400 + if(((variable[j]<'0')||(variable[j]>'9'))&&(variable[j]!='.'))
1.401 + {
1.402 + cancel=true;
1.403 + }
1.404 + else
1.405 + {
1.406 + if(variable[j]=='.')
1.407 + {
1.408 + point_num++;
1.409 + if(point_num>1)
1.410 + {
1.411 + cancel=true;
1.412 + }
1.413 + }
1.414 + }
1.415 + }
1.416 + }
1.417 + if(cancel)
1.418 + {
1.419 + return false;
1.420 + }
1.421 + return true;
1.422 +}