~ubuntu-branches/ubuntu/trusty/rheolef/trusty

« back to all changes in this revision

Viewing changes to skit/ptst2/map_iterator_tst.cc

  • Committer: Package Import Robot
  • Author(s): Pierre Saramito
  • Date: 2012-04-06 09:12:21 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120406091221-m58me99p1nxqui49
Tags: 6.0-1
* New upstream release 6.0 (major changes):
  - massively distributed and parallel support
  - full FEM characteristic method (Lagrange-Gakerkin method) support
  - enhanced users documentation 
  - source code supports g++-4.7 (closes: #667356)
* debian/control: dependencies for MPI distributed solvers added
* debian/rules: build commands simplified
* debian/librheolef-dev.install: man1/* to man9/* added
* debian/changelog: package description rewritted (closes: #661689)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
///
2
 
/// This file is part of Rheolef.
3
 
///
4
 
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
5
 
///
6
 
/// Rheolef is free software; you can redistribute it and/or modify
7
 
/// it under the terms of the GNU General Public License as published by
8
 
/// the Free Software Foundation; either version 2 of the License, or
9
 
/// (at your option) any later version.
10
 
///
11
 
/// Rheolef is distributed in the hope that it will be useful,
12
 
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
/// GNU General Public License for more details.
15
 
///
16
 
/// You should have received a copy of the GNU General Public License
17
 
/// along with Rheolef; if not, write to the Free Software
18
 
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 
///
20
 
/// =========================================================================
21
 
//
22
 
// Tests de map::iterator vue de la classe polymorphic_array
23
 
//
24
 
//  => - conversion de map::iterator en void* et reciproquement
25
 
//     - insertion/suppression d'un element, eventuellement de type
26
 
//       derive different
27
 
//
28
 
// Author: Pierre.Saramito@imag.fr
29
 
//
30
 
// Date: 14 dec 2010
31
 
//
32
 
 
33
 
#include "geo_element_tst.h"
34
 
using namespace rheolef;
35
 
 
36
 
#include <map>
37
 
#include <iostream>
38
 
using namespace std;
39
 
 
40
 
template <class T>
41
 
void show (const map<size_t,T>& a) {
42
 
  for (typename map<size_t,T>::const_iterator iter = a.begin(), last = a.end(); iter != last; iter++) {
43
 
    cout << (*iter).first << " " << (*iter).second << endl;
44
 
  }
45
 
}
46
 
// transform a map::iterator into a void*
47
 
template <class Tk>
48
 
void*
49
 
to_pointer (typename map<size_t,Tk>::iterator iter_tk)
50
 
{
51
 
  return static_cast<void*>(iter_tk._M_node);
52
 
}
53
 
// transform a void* into a map::iterator
54
 
template <class Tk>
55
 
typename map<size_t,Tk>::iterator
56
 
to_iterator (void* ptr)
57
 
{
58
 
  typedef typename map<size_t,Tk>::iterator iterator;
59
 
  typedef typename iterator::_Link_type link_type;
60
 
  return iterator (static_cast<link_type>(ptr));
61
 
}
62
 
// ---------------------------------------------------------------------------
63
 
// print the data structure: convert void* into map::iterator
64
 
// ---------------------------------------------------------------------------
65
 
void
66
 
print (ostream& os, vector<pair<size_t,void*> >& table) {
67
 
  os << "geo " << table.size() << endl;
68
 
  for (size_t i = 0; i < table.size(); i++) {
69
 
    switch (table[i].first) {
70
 
      case 0 : {
71
 
        map<size_t,geo_element_t>::iterator iter = to_iterator<geo_element_t>(table[i].second);
72
 
        os << (*iter).second << endl;
73
 
        break;
74
 
      }
75
 
      case 1 : {
76
 
        map<size_t,geo_element_q>::iterator iter = to_iterator<geo_element_q>(table[i].second);
77
 
        os << (*iter).second << endl;
78
 
        break;
79
 
      }
80
 
      default :{
81
 
        cerr << "unexpected variant " << table[i].first <<  endl;
82
 
        exit (1);
83
 
      }
84
 
    }
85
 
  }
86
 
}
87
 
int main () {
88
 
  // ---------------------------------------------------------------------------
89
 
  // creation of the data structure: convert map::iterator into void*
90
 
  // ---------------------------------------------------------------------------
91
 
  // triangles:
92
 
  map<size_t,geo_element_t> t;
93
 
  t.insert (pair<size_t,geo_element_t>(0,geo_element_t(1,2,3)));
94
 
  t.insert (pair<size_t,geo_element_t>(1,geo_element_t(21,22,23))); // will be deleted
95
 
  t.insert (pair<size_t,geo_element_t>(2,geo_element_t(4,5,6)));
96
 
  t.insert (pair<size_t,geo_element_t>(4,geo_element_t(7,8,9)));
97
 
#ifdef TO_CLEAN
98
 
  cout << "t = " << endl; show (t);
99
 
#endif // TO_CLEAN
100
 
 
101
 
  // quadrangles:
102
 
  map<size_t,geo_element_q> q;
103
 
  //q.insert (pair<size_t,geo_element_q>(1,geo_element_q(11,12,13,14))); // will be created
104
 
  q.insert (pair<size_t,geo_element_q>(3,geo_element_q(15,16,17,18)));
105
 
#ifdef TO_CLEAN
106
 
  cout << "q = " << endl; show (q);
107
 
#endif // TO_CLEAN
108
 
 
109
 
  // test iterators
110
 
  cerr << "sizeof(void*) = "
111
 
       << sizeof(void*) << endl;
112
 
  cerr << "sizeof(map<size_t,geo_element_t>::iterator) = "
113
 
       << sizeof(map<size_t,geo_element_t>::iterator) << endl;
114
 
 
115
 
  // transform a map::iterator into a void*
116
 
  map<size_t,geo_element_t>::iterator it0 = t.begin();
117
 
  typedef map<size_t,geo_element_t>::iterator::_Link_type link_t_type;
118
 
  link_t_type link_t = static_cast<link_t_type>(it0._M_node);
119
 
 
120
 
  // typedef map<size_t,geo_element>::iterator::_Link_type link_type; // ERROR: pure virtual..
121
 
  typedef void* link_type; // ERROR: pure virtual..
122
 
  link_type link = static_cast<link_type>(link_t);
123
 
 
124
 
  // transform a void* into a map::iterator
125
 
  link_t_type link_t2 = static_cast<link_t_type>(link);
126
 
  map<size_t,geo_element_t>::iterator it2 (link_t2);
127
 
 
128
 
  // table of pointers
129
 
  vector<pair<size_t,void*> > table (5);
130
 
  map<size_t,geo_element_t>::iterator it = t.begin();
131
 
  map<size_t,geo_element_q>::iterator iq = q.begin();
132
 
  table[0] = make_pair(0, to_pointer<geo_element_t>(it++));
133
 
  table[1] = make_pair(0, to_pointer<geo_element_t>(it++));
134
 
  table[2] = make_pair(0, to_pointer<geo_element_t>(it++));
135
 
  table[3] = make_pair(1, to_pointer<geo_element_q>(iq++));
136
 
  table[4] = make_pair(0, to_pointer<geo_element_t>(it++));
137
 
 
138
 
#ifdef TO_CLEAN
139
 
  print (cout, table);
140
 
#endif // TO_CLEAN
141
 
 
142
 
// ---------------------------------------------------------------------------
143
 
// replace into the data structure: convert void* into map::iterator
144
 
// ---------------------------------------------------------------------------
145
 
  // replace an element: when variants are different
146
 
  // input: pos, value
147
 
  // - ptr2iter 
148
 
  // - save its index
149
 
  // - delete from orig map
150
 
  // - insert in dest map with index
151
 
warning_macro ("ici [0]");
152
 
  vector<pair<size_t,void*> >::iterator pos = table.begin();
153
 
warning_macro ("ici [1]");
154
 
  pos++; // points to index 1
155
 
  geo_element_q value (11,12,13,14);
156
 
  map<size_t,geo_element_t>::iterator iter_old = to_iterator<geo_element_t>((*pos).second);
157
 
  size_t index = (*iter_old).first;
158
 
  t.erase (iter_old);
159
 
warning_macro ("ici [2] index="<<index);
160
 
 
161
 
  pair<map<size_t,geo_element_q>::iterator, bool> status 
162
 
    = q.insert (pair<size_t,geo_element_q>(index,value));
163
 
 
164
 
  map<size_t,geo_element_q>::iterator iter_new = status.first;
165
 
 
166
 
  *pos = make_pair(1, to_pointer<geo_element_q>(iter_new));
167
 
warning_macro ("ici [3]");
168
 
  print (cout, table);
169
 
}