1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library 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 GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : testAdjacentPredicate.cxx
23 // Author : Eric Fayolle (EDF)
25 // Modified by : $LastChangedBy$
26 // Date : $LastChangedDate: 2007-01-08 19:01:14 +0100 (lun, 08 jan 2007) $
29 #include "AdjacentPredicate.hxx"
30 #include "DisplayPair.hxx"
40 #include <sys/types.h>
44 static const double MAXVALUE = 150.0;
45 MyRand() { srand(getpid()); }
46 int operator()() const {
47 return 1+(int) ( MAXVALUE *rand()/(RAND_MAX +1.0));
52 typedef double TagType;
53 typedef std::pair< TimeType , TagType > DataId;
55 template < typename DataType >
56 DataType processTimeInterval (DataId & dataId,
57 typename std::map<DataId, DataType>::const_iterator const & it1) {
64 std::vector<Type> vect(N);
68 std::generate(vect.begin(),vect.end(),myRand);
69 std::cout << "Vecteur généré aléatoirement :" << std::endl;
70 copy(vect.begin(),vect.end(),std::ostream_iterator<Type>(std::cout," "));
71 std::cout<< std::endl;
73 int valueToFind = myRand();
75 std::sort(vect.begin(),vect.end(),std::less< Type > ());
76 std::cout << "Vecteur trié par ordre croissant :" << std::endl;
77 copy(vect.begin(),vect.end(),std::ostream_iterator<Type>(std::cout," "));
78 std::cout<< std::endl;
80 std::vector<Type>::iterator it;
81 AdjacentPredicate<Type> ap(valueToFind);
82 it = std::adjacent_find(vect.begin(),vect.end(),ap);
83 if ( it == vect.end() )
84 std::cout << "Je n'est pas trouvé d'intervalle pour encadrer " << valueToFind << std::endl;
86 std::cout << "La valeur à trouver : " << valueToFind <<" est entre * it :" << *it << " et valeur de *(it+1) :" << *(it+1) << std::endl;
87 std::cout<< std::endl;
90 typedef std::map<int,double> MapIntDouble;
92 MapIntDouble::const_iterator itM1,itM2;
93 for(it=vect.begin(); it!=vect.end(); ++it) myMap[*it] = myRand();
95 std::cout << "Clés de la Map :" << std::endl;
96 for(itM1=myMap.begin();itM1!=myMap.end();++itM1)
97 //std::cout << &((*itM1).first)
98 std::cout << (*itM1).first << " ";
99 std::cout<< std::endl;
101 //AdjacentPredicate<std::pair<int,double> > apMap(valueToFind);
102 AdjacentPredicate< MapIntDouble::value_type > apMap(valueToFind);
103 itM1 = std::adjacent_find(myMap.begin(),myMap.end(),apMap);
105 if ( itM1 == myMap.end() )
106 std::cout << "Map : Je n'est pas trouvé d'intervalle pour encadrer " << valueToFind << std::endl;
108 std::cout << "Map : La valeur à trouver : " << valueToFind <<" est entre (*itM1).first :" << (*itM1).first << " et valeur de *(itM1+1).first :" << (*(itM2)).first << std::endl;
110 std::cout<< std::endl;
113 typedef std::map<std::pair<int,double>, double> MapPIntDouble_Double;
114 MapPIntDouble_Double myMapP;
115 MapPIntDouble_Double::const_iterator itMP1,itMP2;
116 // for(it=vect.begin(); it!=vect.end(); ++it) myMapP[std::make_pair<int,double>(*it,myRand())] = myRand();
117 myMapP[std::make_pair<int,double>(*(vect.begin()),myRand())] = myRand();
119 std::cout << "Clés de la MapP :" << std::endl;
120 for(itMP1=myMapP.begin();itMP1!=myMapP.end();++itMP1)
121 std::cout << (*itMP1).first << " " ;
122 std::cout<< std::endl;
124 //AdjacentPredicate<std::pair<int,double> > apMap(valueToFind);
125 // std::pair<int,double> valuePToFind=std::make_pair<int,double>(valueToFind,myRand());
126 std::pair<int,double> valuePToFind=myMapP.begin()->first;
127 AdjacentPredicate< MapPIntDouble_Double::value_type > apMapP(valuePToFind);
128 itMP1 = std::adjacent_find(myMapP.begin(),myMapP.end(),apMapP);
130 if ( itMP1 == myMapP.end() )
131 std::cout << "MapP : Je n'est pas trouvé d'intervalle pour encadrer " << valuePToFind << std::endl;
133 std::cout << "MapP : La valeur à trouver : " << valuePToFind <<" est entre (*itMP1).first :" << (*itMP1).first << " et valeur de *(itMP1+1).first :" << (*(itMP2)).first << std::endl;
136 std::cout << processTimeInterval<double>(valuePToFind,itMP1);
137 std::cout<< std::endl;
139 std::cout<< std::endl;