[42] | 1 | #include <new_map_win.h> |
---|
| 2 | |
---|
| 3 | bool NewMapWin::closeIfEscapeIsPressed(GdkEventKey* e) |
---|
| 4 | { |
---|
| 5 | if(e->keyval==GDK_Escape) |
---|
| 6 | { |
---|
| 7 | hide(); |
---|
| 8 | } |
---|
| 9 | return true; |
---|
| 10 | } |
---|
| 11 | |
---|
[96] | 12 | NewMapWin::NewMapWin(const std::string& title, NoteBookTab & mw, bool itisedge, bool edgenode):Gtk::Dialog(title, true, true),mytab(mw),node("Create NodeMap"),edge("Create EdgeMap") |
---|
[42] | 13 | { |
---|
| 14 | set_default_size(200, 50); |
---|
| 15 | |
---|
| 16 | signal_key_press_event().connect(sigc::mem_fun(*this, &NewMapWin::closeIfEscapeIsPressed)); |
---|
| 17 | |
---|
[90] | 18 | Gtk::VBox * vbox=get_vbox(); |
---|
[42] | 19 | |
---|
| 20 | //entries |
---|
| 21 | table=new Gtk::Table(3, 2, false); |
---|
| 22 | |
---|
| 23 | label=new Gtk::Label; |
---|
| 24 | label->set_text("Name of new map:"); |
---|
| 25 | name.set_text(""); |
---|
| 26 | |
---|
| 27 | (*table).attach(*label,0,1,0,1,Gtk::SHRINK,Gtk::SHRINK,10,3); |
---|
| 28 | (*table).attach(name,1,2,0,1,Gtk::SHRINK,Gtk::SHRINK,10,3); |
---|
| 29 | |
---|
| 30 | label=new Gtk::Label; |
---|
| 31 | label->set_text("Default value in the map:"); |
---|
| 32 | default_value.set_text("0"); |
---|
| 33 | |
---|
| 34 | (*table).attach(*label,0,1,1,2,Gtk::SHRINK,Gtk::SHRINK,10,3); |
---|
| 35 | (*table).attach(default_value,1,2,1,2,Gtk::SHRINK,Gtk::SHRINK,10,3); |
---|
| 36 | |
---|
| 37 | //node vs. edge map selector |
---|
| 38 | Gtk::RadioButton::Group group = node.get_group(); |
---|
| 39 | edge.set_group(group); |
---|
[90] | 40 | |
---|
| 41 | if(edgenode) |
---|
| 42 | { |
---|
| 43 | (*table).attach(node,0,1,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3); |
---|
| 44 | (*table).attach(edge,1,2,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3); |
---|
| 45 | } |
---|
| 46 | else |
---|
| 47 | { |
---|
| 48 | if(itisedge) |
---|
| 49 | { |
---|
| 50 | edge.set_active(); |
---|
| 51 | } |
---|
| 52 | else |
---|
| 53 | { |
---|
| 54 | node.set_active(); |
---|
| 55 | } |
---|
| 56 | } |
---|
[42] | 57 | |
---|
[90] | 58 | vbox->pack_start(*table); |
---|
[42] | 59 | |
---|
| 60 | //OK button |
---|
[90] | 61 | add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK); |
---|
[42] | 62 | |
---|
| 63 | show_all_children(); |
---|
| 64 | |
---|
| 65 | } |
---|
| 66 | |
---|
[90] | 67 | void NewMapWin::on_response(int response_id) |
---|
[85] | 68 | { |
---|
[90] | 69 | if(response_id==Gtk::RESPONSE_OK) |
---|
[85] | 70 | { |
---|
[90] | 71 | double def_val=0; |
---|
[85] | 72 | |
---|
[90] | 73 | //get and formulate text |
---|
| 74 | std::string def_val_str=default_value.get_text(); |
---|
| 75 | std::string polishform=string2Polishform(def_val_str,edge.get_active()); |
---|
[42] | 76 | |
---|
[90] | 77 | //get name of text |
---|
| 78 | std::string mapname=name.get_text(); |
---|
[42] | 79 | |
---|
[90] | 80 | if(!mapname.empty()&&!polishform.empty()) |
---|
| 81 | { |
---|
| 82 | int abortion=0; |
---|
| 83 | if(edge.get_active()) |
---|
| 84 | { |
---|
| 85 | //create the new map |
---|
[96] | 86 | Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> (mytab.mapstorage.graph); |
---|
[42] | 87 | |
---|
[90] | 88 | std::stack<double> polishstack; |
---|
[88] | 89 | |
---|
[96] | 90 | for(EdgeIt k(mytab.mapstorage.graph); k!=INVALID; ++k) |
---|
[88] | 91 | { |
---|
[90] | 92 | for(int i=0;i<(int)polishform.size();i++) |
---|
[88] | 93 | { |
---|
[90] | 94 | double op1, op2; |
---|
| 95 | bool operation=true; |
---|
[88] | 96 | switch(polishform[i]) |
---|
| 97 | { |
---|
| 98 | case '+': |
---|
| 99 | case '-': |
---|
| 100 | case '/': |
---|
| 101 | case '*': |
---|
[90] | 102 | op1=polishstack.top(); |
---|
| 103 | polishstack.pop(); |
---|
| 104 | op2=polishstack.top(); |
---|
| 105 | polishstack.pop(); |
---|
[88] | 106 | break; |
---|
| 107 | default: |
---|
[90] | 108 | //substitute variable |
---|
[96] | 109 | std::map< std::string,Graph::EdgeMap<double> * > ems=mytab.mapstorage.edgemap_storage; |
---|
[90] | 110 | bool itisvar=(ems.find(ch2var[ polishform[i] ])!=ems.end()); |
---|
| 111 | if(itisvar) |
---|
| 112 | { |
---|
[96] | 113 | polishstack.push( (*(mytab.mapstorage.edgemap_storage[ ch2var[ polishform[i] ] ]))[k]); |
---|
[90] | 114 | } |
---|
| 115 | else |
---|
| 116 | { |
---|
| 117 | char * def_val_ch=new char [(int)(ch2var[ polishform[i] ].length())]; |
---|
| 118 | for(int j=0;j<(int)(ch2var[ polishform[i] ].length());j++) |
---|
| 119 | { |
---|
| 120 | def_val_ch[j]=ch2var[ polishform[i] ][j]; |
---|
| 121 | } |
---|
| 122 | polishstack.push(atof(def_val_ch)); |
---|
| 123 | } |
---|
| 124 | operation=false; |
---|
[88] | 125 | break; |
---|
| 126 | } |
---|
[90] | 127 | if(operation) |
---|
| 128 | { |
---|
| 129 | double res; |
---|
| 130 | switch(polishform[i]) |
---|
| 131 | { |
---|
| 132 | case '+': |
---|
| 133 | res=op1+op2; |
---|
| 134 | break; |
---|
| 135 | case '-': |
---|
| 136 | res=op2-op1; |
---|
| 137 | break; |
---|
| 138 | case '/': |
---|
| 139 | res=op2/op1; |
---|
| 140 | break; |
---|
| 141 | case '*': |
---|
| 142 | res=op1*op2; |
---|
| 143 | break; |
---|
| 144 | default: |
---|
| 145 | std::cout << "How could we get here?" << std::endl; |
---|
| 146 | break; |
---|
| 147 | } |
---|
| 148 | polishstack.push(res); |
---|
| 149 | } |
---|
[88] | 150 | } |
---|
[90] | 151 | (*emptr)[k]=polishstack.top(); |
---|
[88] | 152 | } |
---|
[90] | 153 | |
---|
| 154 | //if addition was not successful addEdgeMap returns one. |
---|
| 155 | //cause can be that there is already a map named like the new one |
---|
[96] | 156 | if(mytab.mapstorage.addEdgeMap(mapname, emptr, def_val)) |
---|
[90] | 157 | { |
---|
| 158 | abortion=1; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | //add it to the list of the displayable maps |
---|
[96] | 162 | mytab.registerNewEdgeMap(mapname); |
---|
[90] | 163 | |
---|
| 164 | //display it |
---|
[94] | 165 | //gdc.changeEdgeText(mapname); |
---|
[88] | 166 | } |
---|
[90] | 167 | else //!edge.get_active() |
---|
| 168 | { |
---|
| 169 | //create the new map |
---|
[96] | 170 | Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> (mytab.mapstorage.graph); |
---|
[88] | 171 | |
---|
[90] | 172 | std::stack<double> polishstack; |
---|
[88] | 173 | |
---|
[96] | 174 | for(NodeIt k(mytab.mapstorage.graph); k!=INVALID; ++k) |
---|
[88] | 175 | { |
---|
[90] | 176 | for(int i=0;i<(int)polishform.size();i++) |
---|
[88] | 177 | { |
---|
[90] | 178 | double op1, op2; |
---|
| 179 | bool operation=true; |
---|
[88] | 180 | switch(polishform[i]) |
---|
| 181 | { |
---|
| 182 | case '+': |
---|
| 183 | case '-': |
---|
| 184 | case '/': |
---|
| 185 | case '*': |
---|
[90] | 186 | op1=polishstack.top(); |
---|
| 187 | polishstack.pop(); |
---|
| 188 | op2=polishstack.top(); |
---|
| 189 | polishstack.pop(); |
---|
[88] | 190 | break; |
---|
| 191 | default: |
---|
[96] | 192 | std::map< std::string,Graph::NodeMap<double> * > nms=mytab.mapstorage.nodemap_storage; |
---|
[90] | 193 | bool itisvar=(nms.find(ch2var[ polishform[i] ])!=nms.end()); |
---|
| 194 | if(itisvar) |
---|
| 195 | { |
---|
[96] | 196 | polishstack.push( (*(mytab.mapstorage.nodemap_storage[ ch2var[ polishform[i] ] ]))[k]); |
---|
[90] | 197 | } |
---|
| 198 | else |
---|
| 199 | { |
---|
| 200 | char * def_val_ch=new char [(int)(ch2var[ polishform[i] ].length())]; |
---|
| 201 | for(int j=0;j<(int)(ch2var[ polishform[i] ].length());j++) |
---|
| 202 | { |
---|
| 203 | def_val_ch[j]=ch2var[ polishform[i] ][j]; |
---|
| 204 | } |
---|
| 205 | polishstack.push(atof(def_val_ch)); |
---|
| 206 | } |
---|
| 207 | operation=false; |
---|
[88] | 208 | break; |
---|
| 209 | } |
---|
[90] | 210 | if(operation) |
---|
| 211 | { |
---|
| 212 | double res; |
---|
| 213 | switch(polishform[i]) |
---|
| 214 | { |
---|
| 215 | case '+': |
---|
| 216 | res=op1+op2; |
---|
| 217 | break; |
---|
| 218 | case '-': |
---|
| 219 | res=op2-op1; |
---|
| 220 | break; |
---|
| 221 | case '/': |
---|
| 222 | res=op2/op1; |
---|
| 223 | break; |
---|
| 224 | case '*': |
---|
| 225 | res=op1*op2; |
---|
| 226 | break; |
---|
| 227 | default: |
---|
| 228 | std::cout << "How could we get here?" << std::endl; |
---|
| 229 | break; |
---|
| 230 | } |
---|
| 231 | polishstack.push(res); |
---|
| 232 | } |
---|
[88] | 233 | } |
---|
[90] | 234 | (*emptr)[k]=polishstack.top(); |
---|
[88] | 235 | } |
---|
[90] | 236 | |
---|
| 237 | //if addition was not successful addNodeMap returns one. |
---|
| 238 | //cause can be that there is already a map named like the new one |
---|
[96] | 239 | if(mytab.mapstorage.addNodeMap(mapname,emptr, def_val)) |
---|
[90] | 240 | { |
---|
| 241 | abortion=1; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | //add it to the list of the displayable maps |
---|
[96] | 245 | mytab.registerNewNodeMap(mapname); |
---|
[90] | 246 | |
---|
| 247 | //display it |
---|
| 248 | //gdc.changeNodeText(mapname); |
---|
[88] | 249 | } |
---|
[90] | 250 | if(!abortion) |
---|
[88] | 251 | { |
---|
[90] | 252 | name.set_text(""); |
---|
| 253 | default_value.set_text("0"); |
---|
| 254 | edge.show(); |
---|
| 255 | node.show(); |
---|
| 256 | hide(); |
---|
[88] | 257 | } |
---|
[46] | 258 | } |
---|
[42] | 259 | } |
---|
| 260 | } |
---|
| 261 | |
---|
[90] | 262 | |
---|
[88] | 263 | std::string NewMapWin::string2Polishform(std::string rawcommand, bool itisedge) |
---|
| 264 | { |
---|
| 265 | bool valid_entry=true; |
---|
| 266 | |
---|
| 267 | std::map<std::string, int> str2i; |
---|
| 268 | |
---|
| 269 | std::string command; |
---|
| 270 | |
---|
| 271 | std::string variable; |
---|
| 272 | |
---|
| 273 | char index='a'; |
---|
| 274 | |
---|
| 275 | for(int i=0;(valid_entry&&(i<(int)rawcommand.size()));i++) |
---|
| 276 | { |
---|
| 277 | switch(rawcommand[i]) |
---|
| 278 | { |
---|
| 279 | case '+': |
---|
| 280 | case '-': |
---|
| 281 | case '*': |
---|
| 282 | case '/': |
---|
| 283 | case ')': |
---|
| 284 | case '(': |
---|
| 285 | if(!variable.empty()) |
---|
| 286 | { |
---|
| 287 | valid_entry=validVariable(variable, itisedge); |
---|
| 288 | ch2var[index]=variable; |
---|
| 289 | command+=index; |
---|
| 290 | index++; |
---|
| 291 | variable.erase(0,variable.size()); |
---|
| 292 | } |
---|
| 293 | command+=rawcommand[i]; |
---|
| 294 | break; |
---|
| 295 | default: |
---|
| 296 | variable+=rawcommand[i]; |
---|
| 297 | break; |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | if(!variable.empty()&&valid_entry) |
---|
| 302 | { |
---|
| 303 | valid_entry=validVariable(variable, itisedge); |
---|
| 304 | ch2var[index]=variable; |
---|
| 305 | command+=index; |
---|
| 306 | index++; |
---|
| 307 | variable.erase(0,variable.size()); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | if(valid_entry) |
---|
| 311 | { |
---|
| 312 | unsigned int pr=10000; |
---|
| 313 | bool prevmult=false; |
---|
[89] | 314 | unsigned int prev_change=pr; |
---|
| 315 | unsigned int prev_br=pr; |
---|
[88] | 316 | int counter=0; |
---|
| 317 | std::string comm_nobr=""; |
---|
| 318 | std::vector<unsigned int> p; |
---|
| 319 | p.resize(counter+1); |
---|
| 320 | |
---|
| 321 | //limits |
---|
| 322 | //6 brackets embedded |
---|
| 323 | //100 operation in a row from the same priority |
---|
| 324 | |
---|
| 325 | for(int i=0;i<(int)command.size();i++) |
---|
| 326 | { |
---|
| 327 | bool put_in_string=true; |
---|
| 328 | switch(command[i]) |
---|
| 329 | { |
---|
| 330 | case '(': |
---|
| 331 | pr=prev_br+10000; |
---|
| 332 | prev_br=pr; |
---|
| 333 | prevmult=false; |
---|
| 334 | put_in_string=false; |
---|
| 335 | break; |
---|
| 336 | case ')': |
---|
| 337 | pr=prev_br-10000; |
---|
| 338 | prev_br=pr; |
---|
| 339 | prevmult=false; |
---|
| 340 | put_in_string=false; |
---|
| 341 | break; |
---|
| 342 | case '+': |
---|
| 343 | case '-': |
---|
| 344 | if(prevmult) |
---|
| 345 | { |
---|
| 346 | pr=prev_change; |
---|
| 347 | } |
---|
| 348 | p[counter]=pr; |
---|
| 349 | pr-=100; |
---|
| 350 | |
---|
| 351 | prevmult=false; |
---|
| 352 | break; |
---|
| 353 | case '/': |
---|
| 354 | case '*': |
---|
| 355 | if(!prevmult) |
---|
| 356 | { |
---|
| 357 | prev_change=pr; |
---|
| 358 | pr+=200; |
---|
| 359 | pr-=1; |
---|
| 360 | } |
---|
| 361 | p[counter]=pr; |
---|
| 362 | pr-=1; |
---|
| 363 | prevmult=true; |
---|
| 364 | break; |
---|
| 365 | default: |
---|
| 366 | p[counter]=65000; |
---|
| 367 | break; |
---|
| 368 | } |
---|
| 369 | if(put_in_string) |
---|
| 370 | { |
---|
| 371 | counter++; |
---|
| 372 | p.resize(counter+1); |
---|
| 373 | comm_nobr=comm_nobr+command[i]; |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | tree_node * root=weightedString2Tree(comm_nobr, p, 0); |
---|
| 378 | |
---|
| 379 | std::string polishform=postOrder(root); |
---|
| 380 | |
---|
| 381 | return polishform; |
---|
| 382 | } |
---|
| 383 | return ""; |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | NewMapWin::tree_node * NewMapWin::weightedString2Tree(std::string to_tree, std::vector<unsigned int> & p, int offset) |
---|
| 387 | { |
---|
[89] | 388 | unsigned int min=p[offset]; |
---|
[88] | 389 | int minplace=0; |
---|
| 390 | for(int i=0;i<(int)to_tree.size();i++) |
---|
| 391 | { |
---|
| 392 | if(min>p[offset+i]) |
---|
| 393 | { |
---|
| 394 | min=p[offset+i]; |
---|
| 395 | minplace=i; |
---|
| 396 | } |
---|
| 397 | } |
---|
| 398 | tree_node * act_node=new tree_node; |
---|
| 399 | act_node->ch=to_tree[minplace]; |
---|
| 400 | if(to_tree.size()>=3) |
---|
| 401 | { |
---|
| 402 | act_node->left_child=weightedString2Tree(to_tree.substr(0,minplace), p, offset); |
---|
| 403 | act_node->right_child=weightedString2Tree(to_tree.substr(minplace+1,to_tree.size()-minplace-1), p, offset+minplace+1); |
---|
| 404 | } |
---|
| 405 | else |
---|
| 406 | { |
---|
| 407 | act_node->left_child=NULL; |
---|
| 408 | act_node->right_child=NULL; |
---|
| 409 | } |
---|
| 410 | return act_node; |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | std::string NewMapWin::postOrder(tree_node * subtree) |
---|
| 414 | { |
---|
| 415 | std::string subtree_to_string; |
---|
| 416 | if(subtree->left_child) |
---|
| 417 | { |
---|
| 418 | subtree_to_string=postOrder(subtree->left_child); |
---|
| 419 | } |
---|
| 420 | if(subtree->right_child) |
---|
| 421 | { |
---|
| 422 | subtree_to_string=subtree_to_string+postOrder(subtree->right_child); |
---|
| 423 | } |
---|
| 424 | subtree_to_string=subtree_to_string+subtree->ch; |
---|
| 425 | return subtree_to_string; |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | bool NewMapWin::validVariable(std::string variable, bool itisedge) |
---|
| 429 | { |
---|
| 430 | bool cancel; |
---|
| 431 | //is it mapname? |
---|
| 432 | if(itisedge) |
---|
| 433 | { |
---|
[96] | 434 | cancel=(mytab.mapstorage.edgemap_storage.find(variable)==mytab.mapstorage.edgemap_storage.end()); |
---|
[88] | 435 | } |
---|
| 436 | else |
---|
| 437 | { |
---|
[96] | 438 | cancel=(mytab.mapstorage.nodemap_storage.find(variable)==mytab.mapstorage.nodemap_storage.end()); |
---|
[88] | 439 | } |
---|
| 440 | //maybe it is number |
---|
| 441 | int point_num=0; |
---|
| 442 | if(cancel) |
---|
| 443 | { |
---|
| 444 | cancel=false; |
---|
| 445 | for(int j=0;(!cancel)&&(j<(int)variable.size());j++) |
---|
| 446 | { |
---|
| 447 | if(((variable[j]<'0')||(variable[j]>'9'))&&(variable[j]!='.')) |
---|
| 448 | { |
---|
| 449 | cancel=true; |
---|
| 450 | } |
---|
| 451 | else |
---|
| 452 | { |
---|
| 453 | if(variable[j]=='.') |
---|
| 454 | { |
---|
| 455 | point_num++; |
---|
| 456 | if(point_num>1) |
---|
| 457 | { |
---|
| 458 | cancel=true; |
---|
| 459 | } |
---|
| 460 | } |
---|
| 461 | } |
---|
| 462 | } |
---|
| 463 | } |
---|
| 464 | if(cancel) |
---|
| 465 | { |
---|
| 466 | return false; |
---|
| 467 | } |
---|
| 468 | return true; |
---|
| 469 | } |
---|