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