hegyi@1593: #include hegyi@1593: hegyi@1593: bool NewMapWin::closeIfEscapeIsPressed(GdkEventKey* e) hegyi@1593: { hegyi@1593: if(e->keyval==GDK_Escape) hegyi@1593: { hegyi@1593: hide(); hegyi@1593: } hegyi@1593: return true; hegyi@1593: } hegyi@1593: hegyi@1849: 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") hegyi@1593: { hegyi@1593: set_default_size(200, 50); hegyi@1593: hegyi@1593: signal_key_press_event().connect(sigc::mem_fun(*this, &NewMapWin::closeIfEscapeIsPressed)); hegyi@1593: hegyi@1823: Gtk::VBox * vbox=get_vbox(); hegyi@1593: hegyi@1593: //entries hegyi@1593: table=new Gtk::Table(3, 2, false); hegyi@1593: hegyi@1593: label=new Gtk::Label; hegyi@1593: label->set_text("Name of new map:"); hegyi@1593: name.set_text(""); hegyi@1593: hegyi@1593: (*table).attach(*label,0,1,0,1,Gtk::SHRINK,Gtk::SHRINK,10,3); hegyi@1593: (*table).attach(name,1,2,0,1,Gtk::SHRINK,Gtk::SHRINK,10,3); hegyi@1593: hegyi@1593: label=new Gtk::Label; hegyi@1593: label->set_text("Default value in the map:"); hegyi@1593: default_value.set_text("0"); hegyi@1593: hegyi@1593: (*table).attach(*label,0,1,1,2,Gtk::SHRINK,Gtk::SHRINK,10,3); hegyi@1593: (*table).attach(default_value,1,2,1,2,Gtk::SHRINK,Gtk::SHRINK,10,3); hegyi@1593: hegyi@1593: //node vs. edge map selector hegyi@1593: Gtk::RadioButton::Group group = node.get_group(); hegyi@1593: edge.set_group(group); hegyi@1823: hegyi@1823: if(edgenode) hegyi@1823: { hegyi@1823: (*table).attach(node,0,1,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3); hegyi@1823: (*table).attach(edge,1,2,2,3,Gtk::SHRINK,Gtk::SHRINK,10,3); hegyi@1823: } hegyi@1823: else hegyi@1823: { hegyi@1823: if(itisedge) hegyi@1823: { hegyi@1823: edge.set_active(); hegyi@1823: } hegyi@1823: else hegyi@1823: { hegyi@1823: node.set_active(); hegyi@1823: } hegyi@1823: } hegyi@1593: hegyi@1823: vbox->pack_start(*table); hegyi@1593: hegyi@1593: //OK button hegyi@1823: add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK); hegyi@1593: hegyi@1593: show_all_children(); hegyi@1593: hegyi@1593: } hegyi@1593: hegyi@1823: void NewMapWin::on_response(int response_id) hegyi@1737: { hegyi@1823: if(response_id==Gtk::RESPONSE_OK) hegyi@1737: { hegyi@1823: double def_val=0; hegyi@1737: hegyi@1823: //get and formulate text hegyi@1823: std::string def_val_str=default_value.get_text(); hegyi@1823: std::string polishform=string2Polishform(def_val_str,edge.get_active()); hegyi@1593: hegyi@1823: //get name of text hegyi@1823: std::string mapname=name.get_text(); hegyi@1593: hegyi@1823: if(!mapname.empty()&&!polishform.empty()) hegyi@1823: { hegyi@1823: int abortion=0; hegyi@1823: if(edge.get_active()) hegyi@1823: { hegyi@1823: //create the new map hegyi@1849: Graph::EdgeMap * emptr=new Graph::EdgeMap (mytab.mapstorage.graph); hegyi@1593: hegyi@1823: std::stack polishstack; hegyi@1814: hegyi@1849: for(EdgeIt k(mytab.mapstorage.graph); k!=INVALID; ++k) hegyi@1814: { hegyi@1823: for(int i=0;i<(int)polishform.size();i++) hegyi@1814: { hegyi@1823: double op1, op2; hegyi@1823: bool operation=true; hegyi@1814: switch(polishform[i]) hegyi@1814: { hegyi@1814: case '+': hegyi@1814: case '-': hegyi@1814: case '/': hegyi@1814: case '*': hegyi@1823: op1=polishstack.top(); hegyi@1823: polishstack.pop(); hegyi@1823: op2=polishstack.top(); hegyi@1823: polishstack.pop(); hegyi@1814: break; hegyi@1814: default: hegyi@1823: //substitute variable hegyi@1849: std::map< std::string,Graph::EdgeMap * > ems=mytab.mapstorage.edgemap_storage; hegyi@1823: bool itisvar=(ems.find(ch2var[ polishform[i] ])!=ems.end()); hegyi@1823: if(itisvar) hegyi@1823: { hegyi@1849: polishstack.push( (*(mytab.mapstorage.edgemap_storage[ ch2var[ polishform[i] ] ]))[k]); hegyi@1823: } hegyi@1823: else hegyi@1823: { hegyi@1823: char * def_val_ch=new char [(int)(ch2var[ polishform[i] ].length())]; hegyi@1823: for(int j=0;j<(int)(ch2var[ polishform[i] ].length());j++) hegyi@1823: { hegyi@1823: def_val_ch[j]=ch2var[ polishform[i] ][j]; hegyi@1823: } hegyi@1823: polishstack.push(atof(def_val_ch)); hegyi@1823: } hegyi@1823: operation=false; hegyi@1814: break; hegyi@1814: } hegyi@1823: if(operation) hegyi@1823: { hegyi@1823: double res; hegyi@1823: switch(polishform[i]) hegyi@1823: { hegyi@1823: case '+': hegyi@1823: res=op1+op2; hegyi@1823: break; hegyi@1823: case '-': hegyi@1823: res=op2-op1; hegyi@1823: break; hegyi@1823: case '/': hegyi@1823: res=op2/op1; hegyi@1823: break; hegyi@1823: case '*': hegyi@1823: res=op1*op2; hegyi@1823: break; hegyi@1823: default: hegyi@1823: std::cout << "How could we get here?" << std::endl; hegyi@1823: break; hegyi@1823: } hegyi@1823: polishstack.push(res); hegyi@1823: } hegyi@1814: } hegyi@1823: (*emptr)[k]=polishstack.top(); hegyi@1814: } hegyi@1823: hegyi@1823: //if addition was not successful addEdgeMap returns one. hegyi@1823: //cause can be that there is already a map named like the new one hegyi@1849: if(mytab.mapstorage.addEdgeMap(mapname, emptr, def_val)) hegyi@1823: { hegyi@1823: abortion=1; hegyi@1823: } hegyi@1823: hegyi@1823: //add it to the list of the displayable maps hegyi@1878: //furthermore it is done by signals hegyi@1878: //mytab.registerNewEdgeMap(mapname); hegyi@1823: hegyi@1823: //display it hegyi@1837: //gdc.changeEdgeText(mapname); hegyi@1814: } hegyi@1823: else //!edge.get_active() hegyi@1823: { hegyi@1823: //create the new map hegyi@1849: Graph::NodeMap * emptr=new Graph::NodeMap (mytab.mapstorage.graph); hegyi@1814: hegyi@1823: std::stack polishstack; hegyi@1814: hegyi@1849: for(NodeIt k(mytab.mapstorage.graph); k!=INVALID; ++k) hegyi@1814: { hegyi@1823: for(int i=0;i<(int)polishform.size();i++) hegyi@1814: { hegyi@1823: double op1, op2; hegyi@1823: bool operation=true; hegyi@1814: switch(polishform[i]) hegyi@1814: { hegyi@1814: case '+': hegyi@1814: case '-': hegyi@1814: case '/': hegyi@1814: case '*': hegyi@1823: op1=polishstack.top(); hegyi@1823: polishstack.pop(); hegyi@1823: op2=polishstack.top(); hegyi@1823: polishstack.pop(); hegyi@1814: break; hegyi@1814: default: hegyi@1849: std::map< std::string,Graph::NodeMap * > nms=mytab.mapstorage.nodemap_storage; hegyi@1823: bool itisvar=(nms.find(ch2var[ polishform[i] ])!=nms.end()); hegyi@1823: if(itisvar) hegyi@1823: { hegyi@1849: polishstack.push( (*(mytab.mapstorage.nodemap_storage[ ch2var[ polishform[i] ] ]))[k]); hegyi@1823: } hegyi@1823: else hegyi@1823: { hegyi@1823: char * def_val_ch=new char [(int)(ch2var[ polishform[i] ].length())]; hegyi@1823: for(int j=0;j<(int)(ch2var[ polishform[i] ].length());j++) hegyi@1823: { hegyi@1823: def_val_ch[j]=ch2var[ polishform[i] ][j]; hegyi@1823: } hegyi@1823: polishstack.push(atof(def_val_ch)); hegyi@1823: } hegyi@1823: operation=false; hegyi@1814: break; hegyi@1814: } hegyi@1823: if(operation) hegyi@1823: { hegyi@1823: double res; hegyi@1823: switch(polishform[i]) hegyi@1823: { hegyi@1823: case '+': hegyi@1823: res=op1+op2; hegyi@1823: break; hegyi@1823: case '-': hegyi@1823: res=op2-op1; hegyi@1823: break; hegyi@1823: case '/': hegyi@1823: res=op2/op1; hegyi@1823: break; hegyi@1823: case '*': hegyi@1823: res=op1*op2; hegyi@1823: break; hegyi@1823: default: hegyi@1823: std::cout << "How could we get here?" << std::endl; hegyi@1823: break; hegyi@1823: } hegyi@1823: polishstack.push(res); hegyi@1823: } hegyi@1814: } hegyi@1823: (*emptr)[k]=polishstack.top(); hegyi@1814: } hegyi@1823: hegyi@1823: //if addition was not successful addNodeMap returns one. hegyi@1823: //cause can be that there is already a map named like the new one hegyi@1849: if(mytab.mapstorage.addNodeMap(mapname,emptr, def_val)) hegyi@1823: { hegyi@1823: abortion=1; hegyi@1823: } hegyi@1823: hegyi@1823: //add it to the list of the displayable maps hegyi@1878: //furthermore it is done by signals hegyi@1878: //mytab.registerNewNodeMap(mapname); hegyi@1823: hegyi@1823: //display it hegyi@1823: //gdc.changeNodeText(mapname); hegyi@1814: } hegyi@1823: if(!abortion) hegyi@1814: { hegyi@1823: name.set_text(""); hegyi@1823: default_value.set_text("0"); hegyi@1823: edge.show(); hegyi@1823: node.show(); hegyi@1823: hide(); hegyi@1814: } hegyi@1597: } hegyi@1593: } hegyi@1593: } hegyi@1593: hegyi@1823: hegyi@1814: std::string NewMapWin::string2Polishform(std::string rawcommand, bool itisedge) hegyi@1814: { hegyi@1814: bool valid_entry=true; hegyi@1814: hegyi@1814: std::map str2i; hegyi@1814: hegyi@1814: std::string command; hegyi@1814: hegyi@1814: std::string variable; hegyi@1814: hegyi@1814: char index='a'; hegyi@1814: hegyi@1814: for(int i=0;(valid_entry&&(i<(int)rawcommand.size()));i++) hegyi@1814: { hegyi@1814: switch(rawcommand[i]) hegyi@1814: { hegyi@1814: case '+': hegyi@1814: case '-': hegyi@1814: case '*': hegyi@1814: case '/': hegyi@1814: case ')': hegyi@1814: case '(': hegyi@1814: if(!variable.empty()) hegyi@1814: { hegyi@1814: valid_entry=validVariable(variable, itisedge); hegyi@1814: ch2var[index]=variable; hegyi@1814: command+=index; hegyi@1814: index++; hegyi@1814: variable.erase(0,variable.size()); hegyi@1814: } hegyi@1814: command+=rawcommand[i]; hegyi@1814: break; hegyi@1814: default: hegyi@1814: variable+=rawcommand[i]; hegyi@1814: break; hegyi@1814: } hegyi@1814: } hegyi@1814: hegyi@1814: if(!variable.empty()&&valid_entry) hegyi@1814: { hegyi@1814: valid_entry=validVariable(variable, itisedge); hegyi@1814: ch2var[index]=variable; hegyi@1814: command+=index; hegyi@1814: index++; hegyi@1814: variable.erase(0,variable.size()); hegyi@1814: } hegyi@1814: hegyi@1814: if(valid_entry) hegyi@1814: { hegyi@1814: unsigned int pr=10000; hegyi@1814: bool prevmult=false; hegyi@1819: unsigned int prev_change=pr; hegyi@1819: unsigned int prev_br=pr; hegyi@1814: int counter=0; hegyi@1814: std::string comm_nobr=""; hegyi@1814: std::vector p; hegyi@1814: p.resize(counter+1); hegyi@1814: hegyi@1814: //limits hegyi@1814: //6 brackets embedded hegyi@1814: //100 operation in a row from the same priority hegyi@1814: hegyi@1814: for(int i=0;i<(int)command.size();i++) hegyi@1814: { hegyi@1814: bool put_in_string=true; hegyi@1814: switch(command[i]) hegyi@1814: { hegyi@1814: case '(': hegyi@1814: pr=prev_br+10000; hegyi@1814: prev_br=pr; hegyi@1814: prevmult=false; hegyi@1814: put_in_string=false; hegyi@1814: break; hegyi@1814: case ')': hegyi@1814: pr=prev_br-10000; hegyi@1814: prev_br=pr; hegyi@1814: prevmult=false; hegyi@1814: put_in_string=false; hegyi@1814: break; hegyi@1814: case '+': hegyi@1814: case '-': hegyi@1814: if(prevmult) hegyi@1814: { hegyi@1814: pr=prev_change; hegyi@1814: } hegyi@1814: p[counter]=pr; hegyi@1814: pr-=100; hegyi@1814: hegyi@1814: prevmult=false; hegyi@1814: break; hegyi@1814: case '/': hegyi@1814: case '*': hegyi@1814: if(!prevmult) hegyi@1814: { hegyi@1814: prev_change=pr; hegyi@1814: pr+=200; hegyi@1814: pr-=1; hegyi@1814: } hegyi@1814: p[counter]=pr; hegyi@1814: pr-=1; hegyi@1814: prevmult=true; hegyi@1814: break; hegyi@1814: default: hegyi@1814: p[counter]=65000; hegyi@1814: break; hegyi@1814: } hegyi@1814: if(put_in_string) hegyi@1814: { hegyi@1814: counter++; hegyi@1814: p.resize(counter+1); hegyi@1814: comm_nobr=comm_nobr+command[i]; hegyi@1814: } hegyi@1814: } hegyi@1814: hegyi@1814: tree_node * root=weightedString2Tree(comm_nobr, p, 0); hegyi@1814: hegyi@1814: std::string polishform=postOrder(root); hegyi@1814: hegyi@1814: return polishform; hegyi@1814: } hegyi@1814: return ""; hegyi@1814: } hegyi@1814: hegyi@1814: NewMapWin::tree_node * NewMapWin::weightedString2Tree(std::string to_tree, std::vector & p, int offset) hegyi@1814: { hegyi@1819: unsigned int min=p[offset]; hegyi@1814: int minplace=0; hegyi@1814: for(int i=0;i<(int)to_tree.size();i++) hegyi@1814: { hegyi@1814: if(min>p[offset+i]) hegyi@1814: { hegyi@1814: min=p[offset+i]; hegyi@1814: minplace=i; hegyi@1814: } hegyi@1814: } hegyi@1814: tree_node * act_node=new tree_node; hegyi@1814: act_node->ch=to_tree[minplace]; hegyi@1814: if(to_tree.size()>=3) hegyi@1814: { hegyi@1814: act_node->left_child=weightedString2Tree(to_tree.substr(0,minplace), p, offset); hegyi@1814: act_node->right_child=weightedString2Tree(to_tree.substr(minplace+1,to_tree.size()-minplace-1), p, offset+minplace+1); hegyi@1814: } hegyi@1814: else hegyi@1814: { hegyi@1814: act_node->left_child=NULL; hegyi@1814: act_node->right_child=NULL; hegyi@1814: } hegyi@1814: return act_node; hegyi@1814: } hegyi@1814: hegyi@1814: std::string NewMapWin::postOrder(tree_node * subtree) hegyi@1814: { hegyi@1814: std::string subtree_to_string; hegyi@1814: if(subtree->left_child) hegyi@1814: { hegyi@1814: subtree_to_string=postOrder(subtree->left_child); hegyi@1814: } hegyi@1814: if(subtree->right_child) hegyi@1814: { hegyi@1814: subtree_to_string=subtree_to_string+postOrder(subtree->right_child); hegyi@1814: } hegyi@1814: subtree_to_string=subtree_to_string+subtree->ch; hegyi@1814: return subtree_to_string; hegyi@1814: } hegyi@1814: hegyi@1814: bool NewMapWin::validVariable(std::string variable, bool itisedge) hegyi@1814: { hegyi@1814: bool cancel; hegyi@1814: //is it mapname? hegyi@1814: if(itisedge) hegyi@1814: { hegyi@1849: cancel=(mytab.mapstorage.edgemap_storage.find(variable)==mytab.mapstorage.edgemap_storage.end()); hegyi@1814: } hegyi@1814: else hegyi@1814: { hegyi@1849: cancel=(mytab.mapstorage.nodemap_storage.find(variable)==mytab.mapstorage.nodemap_storage.end()); hegyi@1814: } hegyi@1814: //maybe it is number hegyi@1814: int point_num=0; hegyi@1814: if(cancel) hegyi@1814: { hegyi@1814: cancel=false; hegyi@1814: for(int j=0;(!cancel)&&(j<(int)variable.size());j++) hegyi@1814: { hegyi@1814: if(((variable[j]<'0')||(variable[j]>'9'))&&(variable[j]!='.')) hegyi@1814: { hegyi@1814: cancel=true; hegyi@1814: } hegyi@1814: else hegyi@1814: { hegyi@1814: if(variable[j]=='.') hegyi@1814: { hegyi@1814: point_num++; hegyi@1814: if(point_num>1) hegyi@1814: { hegyi@1814: cancel=true; hegyi@1814: } hegyi@1814: } hegyi@1814: } hegyi@1814: } hegyi@1814: } hegyi@1814: if(cancel) hegyi@1814: { hegyi@1814: return false; hegyi@1814: } hegyi@1814: return true; hegyi@1814: }