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