2
/// This file is part of Rheolef.
4
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
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.
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.
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
20
/// =========================================================================
22
// Tests de map::iterator vue de la classe polymorphic_array
24
// => - conversion de map::iterator en void* et reciproquement
25
// - insertion/suppression d'un element, eventuellement de type
28
// Author: Pierre.Saramito@imag.fr
33
#include "geo_element_tst.h"
34
using namespace rheolef;
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;
46
// transform a map::iterator into a void*
49
to_pointer (typename map<size_t,Tk>::iterator iter_tk)
51
return static_cast<void*>(iter_tk._M_node);
53
// transform a void* into a map::iterator
55
typename map<size_t,Tk>::iterator
56
to_iterator (void* ptr)
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));
62
// ---------------------------------------------------------------------------
63
// print the data structure: convert void* into map::iterator
64
// ---------------------------------------------------------------------------
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) {
71
map<size_t,geo_element_t>::iterator iter = to_iterator<geo_element_t>(table[i].second);
72
os << (*iter).second << endl;
76
map<size_t,geo_element_q>::iterator iter = to_iterator<geo_element_q>(table[i].second);
77
os << (*iter).second << endl;
81
cerr << "unexpected variant " << table[i].first << endl;
88
// ---------------------------------------------------------------------------
89
// creation of the data structure: convert map::iterator into void*
90
// ---------------------------------------------------------------------------
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)));
98
cout << "t = " << endl; show (t);
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)));
106
cout << "q = " << endl; show (q);
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;
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);
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);
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);
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++));
142
// ---------------------------------------------------------------------------
143
// replace into the data structure: convert void* into map::iterator
144
// ---------------------------------------------------------------------------
145
// replace an element: when variants are different
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;
159
warning_macro ("ici [2] index="<<index);
161
pair<map<size_t,geo_element_q>::iterator, bool> status
162
= q.insert (pair<size_t,geo_element_q>(index,value));
164
map<size_t,geo_element_q>::iterator iter_new = status.first;
166
*pos = make_pair(1, to_pointer<geo_element_q>(iter_new));
167
warning_macro ("ici [3]");