As initial value of a new map expression with ()+-/* operators can be given. These operators work on numbers, or on maps. If maps are given, then the new value for a given graph element will be calculated using the value from the given maps that belong to that graph element.
1 #include <new_map_win.h>
3 bool NewMapWin::closeIfEscapeIsPressed(GdkEventKey* e)
5 if(e->keyval==GDK_Escape)
12 NewMapWin::NewMapWin(const std::string& title, GraphDisplayerCanvas & grdispc):gdc(grdispc),node("Create NodeMap"),edge("Create EdgeMap")
15 set_default_size(200, 50);
17 signal_key_press_event().connect(sigc::mem_fun(*this, &NewMapWin::closeIfEscapeIsPressed));
21 table=new Gtk::Table(3, 2, false);
24 label->set_text("Name of new map:");
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);
31 label->set_text("Default value in the map:");
32 default_value.set_text("0");
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);
37 //node vs. edge map selector
38 Gtk::RadioButton::Group group = node.get_group();
39 edge.set_group(group);
41 (*table).attach(node,0,1,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3);
42 (*table).attach(edge,1,2,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3);
44 vbox.pack_start(*table);
47 button=new Gtk::Button("OK");
49 button->signal_clicked().connect
51 sigc::mem_fun(*this, &NewMapWin::buttonPressed)
55 vbox.pack_start(*button);
63 void NewMapWin::showByPreChoose(bool itisedge)
78 void NewMapWin::buttonPressed()
82 //get and formulate text
83 std::string def_val_str=default_value.get_text();
84 std::string polishform=string2Polishform(def_val_str,edge.get_active());
87 std::string mapname=name.get_text();
89 if(!mapname.empty()&&!polishform.empty())
95 Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> (gdc.mapstorage.graph);
97 std::stack<double> polishstack;
99 for(EdgeIt k(gdc.mapstorage.graph); k!=INVALID; ++k)
101 for(int i=0;i<(int)polishform.size();i++)
105 switch(polishform[i])
111 op1=polishstack.top();
113 op2=polishstack.top();
117 //substitute variable
118 std::map< std::string,Graph::EdgeMap<double> * > ems=gdc.mapstorage.edgemap_storage;
119 bool itisvar=(ems.find(ch2var[ polishform[i] ])!=ems.end());
122 polishstack.push( (*(gdc.mapstorage.edgemap_storage[ ch2var[ polishform[i] ] ]))[k]);
126 char * def_val_ch=new char [(int)(ch2var[ polishform[i] ].length())];
127 for(int j=0;j<(int)(ch2var[ polishform[i] ].length());j++)
129 def_val_ch[j]=ch2var[ polishform[i] ][j];
131 polishstack.push(atof(def_val_ch));
139 switch(polishform[i])
154 std::cout << "How could we get here?" << std::endl;
157 polishstack.push(res);
160 (*emptr)[k]=polishstack.top();
163 //if addition was not successful addEdgeMap returns one.
164 //cause can be that there is already a map named like the new one
165 if(gdc.mapstorage.addEdgeMap(mapname, emptr, def_val))
170 //add it to the list of the displayable maps
171 gdc.mapwin.registerNewEdgeMap(mapname);
174 gdc.changeEdgeText(mapname);
176 else //!edge.get_active()
179 Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> (gdc.mapstorage.graph);
181 std::stack<double> polishstack;
183 for(NodeIt k(gdc.mapstorage.graph); k!=INVALID; ++k)
185 for(int i=0;i<(int)polishform.size();i++)
189 switch(polishform[i])
195 op1=polishstack.top();
197 op2=polishstack.top();
201 std::map< std::string,Graph::NodeMap<double> * > nms=gdc.mapstorage.nodemap_storage;
202 bool itisvar=(nms.find(ch2var[ polishform[i] ])!=nms.end());
205 polishstack.push( (*(gdc.mapstorage.nodemap_storage[ ch2var[ polishform[i] ] ]))[k]);
209 char * def_val_ch=new char [(int)(ch2var[ polishform[i] ].length())];
210 for(int j=0;j<(int)(ch2var[ polishform[i] ].length());j++)
212 def_val_ch[j]=ch2var[ polishform[i] ][j];
214 polishstack.push(atof(def_val_ch));
222 switch(polishform[i])
237 std::cout << "How could we get here?" << std::endl;
240 polishstack.push(res);
243 (*emptr)[k]=polishstack.top();
246 //if addition was not successful addNodeMap returns one.
247 //cause can be that there is already a map named like the new one
248 if(gdc.mapstorage.addNodeMap(mapname,emptr, def_val))
253 //add it to the list of the displayable maps
254 gdc.mapwin.registerNewNodeMap(mapname);
257 gdc.changeNodeText(mapname);
262 default_value.set_text("0");
270 std::string NewMapWin::string2Polishform(std::string rawcommand, bool itisedge)
272 bool valid_entry=true;
274 std::map<std::string, int> str2i;
278 std::string variable;
282 for(int i=0;(valid_entry&&(i<(int)rawcommand.size()));i++)
284 switch(rawcommand[i])
292 if(!variable.empty())
294 valid_entry=validVariable(variable, itisedge);
295 ch2var[index]=variable;
298 variable.erase(0,variable.size());
300 command+=rawcommand[i];
303 variable+=rawcommand[i];
308 if(!variable.empty()&&valid_entry)
310 valid_entry=validVariable(variable, itisedge);
311 ch2var[index]=variable;
314 variable.erase(0,variable.size());
319 unsigned int pr=10000;
321 unsigned int prev_change;
322 unsigned int prev_br=10000;
324 std::string comm_nobr="";
325 std::vector<unsigned int> p;
329 //6 brackets embedded
330 //100 operation in a row from the same priority
332 for(int i=0;i<(int)command.size();i++)
334 bool put_in_string=true;
380 comm_nobr=comm_nobr+command[i];
384 tree_node * root=weightedString2Tree(comm_nobr, p, 0);
386 std::string polishform=postOrder(root);
393 NewMapWin::tree_node * NewMapWin::weightedString2Tree(std::string to_tree, std::vector<unsigned int> & p, int offset)
397 for(int i=0;i<(int)to_tree.size();i++)
405 tree_node * act_node=new tree_node;
406 act_node->ch=to_tree[minplace];
407 if(to_tree.size()>=3)
409 act_node->left_child=weightedString2Tree(to_tree.substr(0,minplace), p, offset);
410 act_node->right_child=weightedString2Tree(to_tree.substr(minplace+1,to_tree.size()-minplace-1), p, offset+minplace+1);
414 act_node->left_child=NULL;
415 act_node->right_child=NULL;
420 std::string NewMapWin::postOrder(tree_node * subtree)
422 std::string subtree_to_string;
423 if(subtree->left_child)
425 subtree_to_string=postOrder(subtree->left_child);
427 if(subtree->right_child)
429 subtree_to_string=subtree_to_string+postOrder(subtree->right_child);
431 subtree_to_string=subtree_to_string+subtree->ch;
432 return subtree_to_string;
435 bool NewMapWin::validVariable(std::string variable, bool itisedge)
441 cancel=(gdc.mapstorage.edgemap_storage.find(variable)==gdc.mapstorage.edgemap_storage.end());
445 cancel=(gdc.mapstorage.nodemap_storage.find(variable)==gdc.mapstorage.nodemap_storage.end());
452 for(int j=0;(!cancel)&&(j<(int)variable.size());j++)
454 if(((variable[j]<'0')||(variable[j]>'9'))&&(variable[j]!='.'))