new_map_win.cc
branchgui
changeset 88 c397e85ec555
parent 85 0b2217328320
child 89 4042761b21e3
equal deleted inserted replaced
2:30ad22cef377 3:47d1bdbede66
    75   show();
    75   show();
    76 }
    76 }
    77 
    77 
    78 void NewMapWin::buttonPressed()
    78 void NewMapWin::buttonPressed()
    79 {
    79 {
    80   bool valid_double=true;
    80   double def_val=0;
    81   int point_num=0;
    81 
    82 
    82   //get and formulate text
    83   std::string def_val_str=default_value.get_text();
    83   std::string def_val_str=default_value.get_text();
    84   char * def_val_ch=new char [def_val_str.length()];
    84   std::string polishform=string2Polishform(def_val_str,edge.get_active());
    85   for(int i=0;i<(int)(def_val_str.length());i++)
    85 
    86     {
    86   //get name of text
    87       if(((def_val_str[i]<'0')||(def_val_str[i]>'9'))&&(def_val_str[i]!='.'))
       
    88 	{
       
    89 	  valid_double=false;
       
    90 	}
       
    91       else
       
    92 	{
       
    93 	  if(def_val_str[i]=='.')
       
    94 	    {
       
    95 	      point_num++;
       
    96 	    }
       
    97 	}
       
    98       def_val_ch[i]=def_val_str[i];
       
    99     }
       
   100   
       
   101   double def_val=atof(def_val_ch);
       
   102 
       
   103   std::string mapname=name.get_text();
    87   std::string mapname=name.get_text();
   104 
    88 
   105   if((point_num<=1)&&(valid_double)&&(!mapname.empty()))
    89   if(!mapname.empty()&&!polishform.empty())
   106     {
    90     {
   107       int abortion=0;
    91       int abortion=0;
   108       if(edge.get_active())
    92       if(edge.get_active())
   109 	{
    93 	{
   110 	  abortion=gdc.addNewEdgeMap(def_val,mapname);
    94 	  //create the new map
   111 	}
    95 	  Graph::EdgeMap<double> * emptr=new Graph::EdgeMap<double> (gdc.mapstorage.graph);
   112       else
    96 
   113 	{
    97 	  std::stack<double> polishstack;
   114 	  abortion=gdc.addNewNodeMap(def_val,mapname);
    98   
       
    99 	  for(EdgeIt k(gdc.mapstorage.graph); k!=INVALID; ++k)
       
   100 	    {
       
   101 	      for(int i=0;i<(int)polishform.size();i++)
       
   102 		{
       
   103 		  double op1, op2;
       
   104 		  bool operation=true;
       
   105 		  switch(polishform[i])
       
   106 		    {
       
   107 		    case '+':
       
   108 		    case '-':
       
   109 		    case '/':
       
   110 		    case '*':
       
   111 		      op1=polishstack.top();
       
   112 		      polishstack.pop();
       
   113 		      op2=polishstack.top();
       
   114 		      polishstack.pop();
       
   115 		      break;
       
   116 		    default:
       
   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());
       
   120 		      if(itisvar)
       
   121 			{
       
   122 			  polishstack.push( (*(gdc.mapstorage.edgemap_storage[ ch2var[ polishform[i] ] ]))[k]);
       
   123 			}
       
   124 		      else
       
   125 			{
       
   126 			  char * def_val_ch=new char [(int)(ch2var[ polishform[i] ].length())];
       
   127 			  for(int j=0;j<(int)(ch2var[ polishform[i] ].length());j++)
       
   128 			    {
       
   129 			      def_val_ch[j]=ch2var[ polishform[i] ][j];
       
   130 			    }
       
   131 			  polishstack.push(atof(def_val_ch));
       
   132 			}
       
   133 		      operation=false;
       
   134 		      break;
       
   135 		    }
       
   136 		  if(operation)
       
   137 		    {
       
   138 		      double res;
       
   139 		      switch(polishform[i])
       
   140 			{
       
   141 			case '+':
       
   142 			  res=op1+op2;
       
   143 			  break;
       
   144 			case '-':
       
   145 			  res=op2-op1;
       
   146 			  break;
       
   147 			case '/':
       
   148 			  res=op2/op1;
       
   149 			  break;
       
   150 			case '*':
       
   151 			  res=op1*op2;
       
   152 			  break;
       
   153 			default:
       
   154 			  std::cout << "How could we get here?" << std::endl;
       
   155 			  break;
       
   156 			}
       
   157 		      polishstack.push(res);
       
   158 		    }
       
   159 		}
       
   160 	      (*emptr)[k]=polishstack.top(); 
       
   161 	    }
       
   162 
       
   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))
       
   166 	    {
       
   167 	      abortion=1;
       
   168 	    }
       
   169 
       
   170 	  //add it to the list of the displayable maps
       
   171 	  gdc.mapwin.registerNewEdgeMap(mapname);
       
   172 
       
   173 	  //display it
       
   174 	  gdc.changeEdgeText(mapname);
       
   175 	}
       
   176       else //!edge.get_active()
       
   177 	{
       
   178 	  //create the new map
       
   179 	  Graph::NodeMap<double> * emptr=new Graph::NodeMap<double> (gdc.mapstorage.graph);
       
   180 
       
   181 	  std::stack<double> polishstack;
       
   182   
       
   183 	  for(NodeIt k(gdc.mapstorage.graph); k!=INVALID; ++k)
       
   184 	    {
       
   185 	      for(int i=0;i<(int)polishform.size();i++)
       
   186 		{
       
   187 		  double op1, op2;
       
   188 		  bool operation=true;
       
   189 		  switch(polishform[i])
       
   190 		    {
       
   191 		    case '+':
       
   192 		    case '-':
       
   193 		    case '/':
       
   194 		    case '*':
       
   195 		      op1=polishstack.top();
       
   196 		      polishstack.pop();
       
   197 		      op2=polishstack.top();
       
   198 		      polishstack.pop();
       
   199 		      break;
       
   200 		    default:
       
   201 		      std::map< std::string,Graph::NodeMap<double> * > nms=gdc.mapstorage.nodemap_storage;
       
   202 		      bool itisvar=(nms.find(ch2var[ polishform[i] ])!=nms.end());
       
   203 		      if(itisvar)
       
   204 			{
       
   205 			  polishstack.push( (*(gdc.mapstorage.nodemap_storage[ ch2var[ polishform[i] ] ]))[k]);
       
   206 			}
       
   207 		      else
       
   208 			{
       
   209 			  char * def_val_ch=new char [(int)(ch2var[ polishform[i] ].length())];
       
   210 			  for(int j=0;j<(int)(ch2var[ polishform[i] ].length());j++)
       
   211 			    {
       
   212 			      def_val_ch[j]=ch2var[ polishform[i] ][j];
       
   213 			    }
       
   214 			  polishstack.push(atof(def_val_ch));
       
   215 			}
       
   216 		      operation=false;
       
   217 		      break;
       
   218 		    }
       
   219 		  if(operation)
       
   220 		    {
       
   221 		      double res;
       
   222 		      switch(polishform[i])
       
   223 			{
       
   224 			case '+':
       
   225 			  res=op1+op2;
       
   226 			  break;
       
   227 			case '-':
       
   228 			  res=op2-op1;
       
   229 			  break;
       
   230 			case '/':
       
   231 			  res=op2/op1;
       
   232 			  break;
       
   233 			case '*':
       
   234 			  res=op1*op2;
       
   235 			  break;
       
   236 			default:
       
   237 			  std::cout << "How could we get here?" << std::endl;
       
   238 			  break;
       
   239 			}
       
   240 		      polishstack.push(res);
       
   241 		    }
       
   242 		}
       
   243 	      (*emptr)[k]=polishstack.top(); 
       
   244 	    }
       
   245 
       
   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))
       
   249 	    {
       
   250 	      abortion=1;
       
   251 	    }
       
   252 
       
   253 	  //add it to the list of the displayable maps
       
   254 	  gdc.mapwin.registerNewNodeMap(mapname);
       
   255 
       
   256 	  //display it
       
   257 	  gdc.changeNodeText(mapname);
   115 	}
   258 	}
   116       if(!abortion)
   259       if(!abortion)
   117 	{
   260 	{
   118 	  name.set_text("");
   261 	  name.set_text("");
   119 	  default_value.set_text("0");
   262 	  default_value.set_text("0");
   122 	  hide();
   265 	  hide();
   123 	}
   266 	}
   124     }
   267     }
   125 }
   268 }
   126 
   269 
       
   270 std::string NewMapWin::string2Polishform(std::string rawcommand, bool itisedge)
       
   271 {
       
   272   bool valid_entry=true;
       
   273 
       
   274   std::map<std::string, int> str2i;
       
   275 
       
   276   std::string command;
       
   277 
       
   278   std::string variable;
       
   279 
       
   280   char index='a';
       
   281 
       
   282   for(int i=0;(valid_entry&&(i<(int)rawcommand.size()));i++)
       
   283     {
       
   284       switch(rawcommand[i])
       
   285 	{
       
   286 	case '+':
       
   287 	case '-':
       
   288 	case '*':
       
   289 	case '/':
       
   290 	case ')':
       
   291 	case '(':
       
   292  	  if(!variable.empty())
       
   293 	    {
       
   294 	      valid_entry=validVariable(variable, itisedge);
       
   295 	      ch2var[index]=variable;
       
   296 	      command+=index;
       
   297 	      index++;
       
   298 	      variable.erase(0,variable.size());	  
       
   299 	    }
       
   300 	  command+=rawcommand[i];
       
   301 	  break;
       
   302 	default:
       
   303 	  variable+=rawcommand[i];
       
   304 	  break;
       
   305 	}
       
   306     }
       
   307 
       
   308   if(!variable.empty()&&valid_entry)
       
   309     {
       
   310       valid_entry=validVariable(variable, itisedge);
       
   311       ch2var[index]=variable;
       
   312       command+=index;
       
   313       index++;
       
   314       variable.erase(0,variable.size());	  
       
   315     }
       
   316 
       
   317   if(valid_entry)
       
   318     {
       
   319       unsigned int pr=10000;
       
   320       bool prevmult=false;
       
   321       unsigned int prev_change;
       
   322       unsigned int prev_br=10000;
       
   323       int counter=0;
       
   324       std::string comm_nobr="";
       
   325       std::vector<unsigned int> p;
       
   326       p.resize(counter+1);
       
   327       
       
   328       //limits
       
   329       //6 brackets embedded
       
   330       //100 operation in a row from the same priority
       
   331       
       
   332       for(int i=0;i<(int)command.size();i++)
       
   333 	{
       
   334 	  bool put_in_string=true;
       
   335 	  switch(command[i])
       
   336 	    {
       
   337 	    case '(':
       
   338 	      pr=prev_br+10000;
       
   339 	      prev_br=pr;
       
   340 	      prevmult=false;
       
   341 	      put_in_string=false;
       
   342 	      break;
       
   343 	    case ')':
       
   344 	      pr=prev_br-10000;
       
   345 	      prev_br=pr;
       
   346 	      prevmult=false;
       
   347 	      put_in_string=false;
       
   348 	      break;
       
   349 	    case '+':
       
   350 	    case '-':
       
   351 	      if(prevmult)
       
   352 		{
       
   353 		  pr=prev_change;
       
   354 		}
       
   355 	      p[counter]=pr;
       
   356 	      pr-=100;
       
   357 
       
   358 	      prevmult=false;
       
   359 	      break;
       
   360 	    case '/':
       
   361 	    case '*':
       
   362 	      if(!prevmult)
       
   363 		{
       
   364 		  prev_change=pr;
       
   365 		  pr+=200;
       
   366 		  pr-=1;
       
   367 		}
       
   368 	      p[counter]=pr;
       
   369 	      pr-=1;
       
   370 	      prevmult=true;
       
   371 	      break;
       
   372 	    default:
       
   373 	      p[counter]=65000;
       
   374 	      break;
       
   375 	    }
       
   376 	  if(put_in_string)
       
   377 	    {
       
   378 	      counter++;
       
   379 	      p.resize(counter+1);
       
   380 	      comm_nobr=comm_nobr+command[i];
       
   381 	    }
       
   382 	}
       
   383 
       
   384       tree_node * root=weightedString2Tree(comm_nobr, p, 0);
       
   385 
       
   386       std::string polishform=postOrder(root);
       
   387 
       
   388       return polishform;
       
   389     }
       
   390   return "";
       
   391 }
       
   392 
       
   393 NewMapWin::tree_node * NewMapWin::weightedString2Tree(std::string to_tree, std::vector<unsigned int> & p, int offset)
       
   394 {
       
   395   int min=p[offset];
       
   396   int minplace=0;
       
   397   for(int i=0;i<(int)to_tree.size();i++)
       
   398     {
       
   399       if(min>p[offset+i])
       
   400 	{
       
   401 	  min=p[offset+i];
       
   402 	  minplace=i;
       
   403 	}
       
   404     }
       
   405   tree_node * act_node=new tree_node;
       
   406   act_node->ch=to_tree[minplace];
       
   407   if(to_tree.size()>=3)
       
   408     {
       
   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);
       
   411     }
       
   412   else
       
   413     {
       
   414       act_node->left_child=NULL;
       
   415       act_node->right_child=NULL;
       
   416     }
       
   417   return act_node;
       
   418 }
       
   419 
       
   420 std::string NewMapWin::postOrder(tree_node * subtree)
       
   421 {
       
   422   std::string subtree_to_string;
       
   423   if(subtree->left_child)
       
   424     {
       
   425       subtree_to_string=postOrder(subtree->left_child);
       
   426     }
       
   427   if(subtree->right_child)
       
   428     {
       
   429       subtree_to_string=subtree_to_string+postOrder(subtree->right_child);
       
   430     }
       
   431   subtree_to_string=subtree_to_string+subtree->ch;
       
   432   return subtree_to_string;
       
   433 }
       
   434 
       
   435 bool NewMapWin::validVariable(std::string variable, bool itisedge)
       
   436 {
       
   437   bool cancel;
       
   438   //is it mapname?
       
   439   if(itisedge)
       
   440     {
       
   441       cancel=(gdc.mapstorage.edgemap_storage.find(variable)==gdc.mapstorage.edgemap_storage.end());
       
   442     }
       
   443   else
       
   444     {
       
   445       cancel=(gdc.mapstorage.nodemap_storage.find(variable)==gdc.mapstorage.nodemap_storage.end());
       
   446     }
       
   447   //maybe it is number
       
   448   int point_num=0;
       
   449   if(cancel)
       
   450     {
       
   451       cancel=false;
       
   452       for(int j=0;(!cancel)&&(j<(int)variable.size());j++)
       
   453 	{
       
   454 	  if(((variable[j]<'0')||(variable[j]>'9'))&&(variable[j]!='.'))
       
   455 	    {
       
   456 	      cancel=true;
       
   457 	    }
       
   458 	  else
       
   459 	    {
       
   460 	      if(variable[j]=='.')
       
   461 		{
       
   462 		  point_num++;
       
   463 		  if(point_num>1)
       
   464 		    {
       
   465 		      cancel=true;
       
   466 		    }
       
   467 		}
       
   468 	    }
       
   469 	}
       
   470     }
       
   471   if(cancel)
       
   472     {
       
   473       return false;
       
   474     }
       
   475   return true;
       
   476 }