3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #include <new_map_win.h>
21 #include <mapstorage.h>
23 bool NewMapWin::closeIfEscapeIsPressed(GdkEventKey* e)
25 if(e->keyval==GDK_Escape)
32 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")
34 set_default_size(200, 50);
36 signal_key_press_event().connect(sigc::mem_fun(*this, &NewMapWin::closeIfEscapeIsPressed));
38 Gtk::VBox * vbox=get_vbox();
41 table=new Gtk::Table(3, 2, false);
44 label->set_text("Name of new map:");
47 (*table).attach(*label,0,1,0,1,Gtk::SHRINK,Gtk::SHRINK,10,3);
48 (*table).attach(name,1,2,0,1,Gtk::SHRINK,Gtk::SHRINK,10,3);
51 label->set_text("Default value in the map:");
52 default_value.set_text("0");
54 (*table).attach(*label,0,1,1,2,Gtk::SHRINK,Gtk::SHRINK,10,3);
55 (*table).attach(default_value,1,2,1,2,Gtk::SHRINK,Gtk::SHRINK,10,3);
57 //node vs. edge map selector
58 Gtk::RadioButton::Group group = node.get_group();
59 edge.set_group(group);
63 (*table).attach(node,0,1,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3);
64 (*table).attach(edge,1,2,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3);
78 vbox->pack_start(*table);
81 add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
87 void NewMapWin::on_response(int response_id)
89 if(response_id==Gtk::RESPONSE_OK)
93 //get and formulate text
94 std::string def_val_str=default_value.get_text();
97 for(int i=0;i<(int)def_val_str.size() && only_nums;i++)
99 if( def_val_str[i]<'0' || def_val_str[i]>'9' )
104 std::string polishform;
108 def_val=atof(def_val_str.c_str());
112 polishform=string2Polishform(def_val_str,edge.get_active());
116 std::string mapname=name.get_text();
118 if(!mapname.empty()&&(!polishform.empty()||only_nums))
121 if(edge.get_active())
124 Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> (mytab.mapstorage->graph, def_val);
128 std::stack<double> polishstack;
130 for(EdgeIt k(mytab.mapstorage->graph); k!=INVALID; ++k)
132 for(int i=0;i<(int)polishform.size();i++)
136 switch(polishform[i])
142 op1=polishstack.top();
144 op2=polishstack.top();
148 //substitute variable
149 std::map< std::string,Graph::EdgeMap<double> * > ems=mytab.mapstorage->edgemap_storage;
150 bool itisvar=(ems.find(ch2var[ polishform[i] ])!=ems.end());
153 polishstack.push( (*(mytab.mapstorage->edgemap_storage[ ch2var[ polishform[i] ] ]))[k]);
157 polishstack.push(atof(ch2var[ polishform[i] ].c_str()));
165 switch(polishform[i])
180 std::cout << "How could we get here?" << std::endl;
183 polishstack.push(res);
185 }//foreach letter in polishform
186 (*emptr)[k]=polishstack.top();
190 //if addition was not successful addEdgeMap returns one.
191 //cause can be that there is already a map named like the new one
192 if(mytab.mapstorage->addEdgeMap(mapname, emptr, def_val))
197 //add it to the list of the displayable maps
198 //furthermore it is done by signals
199 //mytab.registerNewEdgeMap(mapname);
202 //gdc.changeEdgeText(mapname);
204 else //!edge.get_active()
207 Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> (mytab.mapstorage->graph, def_val);
211 std::stack<double> polishstack;
213 for(NodeIt k(mytab.mapstorage->graph); k!=INVALID; ++k)
215 for(int i=0;i<(int)polishform.size();i++)
219 switch(polishform[i])
225 op1=polishstack.top();
227 op2=polishstack.top();
231 std::map< std::string,Graph::NodeMap<double> * > nms=mytab.mapstorage->nodemap_storage;
232 bool itisvar=(nms.find(ch2var[ polishform[i] ])!=nms.end());
235 polishstack.push( (*(mytab.mapstorage->nodemap_storage[ ch2var[ polishform[i] ] ]))[k]);
239 polishstack.push(atof(ch2var[ polishform[i] ].c_str()));
247 switch(polishform[i])
262 std::cout << "How could we get here?" << std::endl;
265 polishstack.push(res);
268 (*emptr)[k]=polishstack.top();
271 //if addition was not successful addNodeMap returns one.
272 //cause can be that there is already a map named like the new one
273 if(mytab.mapstorage->addNodeMap(mapname,emptr, def_val))
278 //add it to the list of the displayable maps
279 //furthermore it is done by signals
280 //mytab.registerNewNodeMap(mapname);
283 //gdc.changeNodeText(mapname);
288 default_value.set_text("0");
298 std::string NewMapWin::string2Polishform(std::string rawcommand, bool itisedge)
300 bool valid_entry=true;
302 std::map<std::string, int> str2i;
306 std::string variable;
310 for(int i=0;(valid_entry&&(i<(int)rawcommand.size()));i++)
312 switch(rawcommand[i])
320 if(!variable.empty())
322 valid_entry=validVariable(variable, itisedge);
323 ch2var[index]=variable;
326 variable.erase(0,variable.size());
328 command+=rawcommand[i];
331 variable+=rawcommand[i];
336 if(!variable.empty()&&valid_entry)
338 valid_entry=validVariable(variable, itisedge);
339 ch2var[index]=variable;
342 variable.erase(0,variable.size());
347 unsigned int pr=10000;
349 unsigned int prev_change=pr;
350 unsigned int prev_br=pr;
352 std::string comm_nobr="";
353 std::vector<unsigned int> p;
357 //6 brackets embedded
358 //100 operation in a row from the same priority
360 for(int i=0;i<(int)command.size();i++)
362 bool put_in_string=true;
408 comm_nobr=comm_nobr+command[i];
412 tree_node * root=weightedString2Tree(comm_nobr, p, 0);
414 std::string polishform=postOrder(root);
423 void NewMapWin::deleteTree(NewMapWin::tree_node * node)
425 if(node->left_child!=NULL)
427 deleteTree(node->left_child);
429 if(node->right_child!=NULL)
431 deleteTree(node->right_child);
436 NewMapWin::tree_node * NewMapWin::weightedString2Tree(std::string to_tree, std::vector<unsigned int> & p, int offset)
438 unsigned int min=p[offset];
440 for(int i=0;i<(int)to_tree.size();i++)
448 tree_node * act_node=new tree_node;
449 act_node->ch=to_tree[minplace];
450 if(to_tree.size()>=3)
452 act_node->left_child=weightedString2Tree(to_tree.substr(0,minplace), p, offset);
453 act_node->right_child=weightedString2Tree(to_tree.substr(minplace+1,to_tree.size()-minplace-1), p, offset+minplace+1);
457 act_node->left_child=NULL;
458 act_node->right_child=NULL;
463 std::string NewMapWin::postOrder(tree_node * subtree)
465 std::string subtree_to_string;
466 if(subtree->left_child)
468 subtree_to_string=postOrder(subtree->left_child);
470 if(subtree->right_child)
472 subtree_to_string=subtree_to_string+postOrder(subtree->right_child);
474 subtree_to_string=subtree_to_string+subtree->ch;
475 return subtree_to_string;
478 bool NewMapWin::validVariable(std::string variable, bool itisedge)
484 cancel=(mytab.mapstorage->edgemap_storage.find(variable)==mytab.mapstorage->edgemap_storage.end());
488 cancel=(mytab.mapstorage->nodemap_storage.find(variable)==mytab.mapstorage->nodemap_storage.end());
495 for(int j=0;(!cancel)&&(j<(int)variable.size());j++)
497 if(((variable[j]<'0')||(variable[j]>'9'))&&(variable[j]!='.'))