1 // Copyright (C) 2007-2012 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
23 // File : testAdjacentPredicate.cxx
24 // Author : Eric Fayolle (EDF)
26 // Modified by : $LastChangedBy$
27 // Date : $LastChangedDate: 2007-01-08 19:01:14 +0100 (lun, 08 jan 2007) $
30 #include "AdjacentPredicate.hxx"
31 #include "DisplayPair.hxx"
41 #include <sys/types.h>
45 static const double MAXVALUE = 150.0;
46 MyRand() { srand(getpid()); }
47 int operator()() const {
48 return 1+(int) ( MAXVALUE *rand()/(RAND_MAX +1.0));
53 typedef double TagType;
54 typedef std::pair< TimeType , TagType > DataId;
56 template < typename DataType >
57 DataType processTimeInterval (DataId & dataId,
58 typename std::map<DataId, DataType>::const_iterator const & it1) {
65 std::vector<Type> vect(N);
69 std::generate(vect.begin(),vect.end(),myRand);
70 std::cout << "Vecteur généré aléatoirement :" << std::endl;
71 copy(vect.begin(),vect.end(),std::ostream_iterator<Type>(std::cout," "));
72 std::cout<< std::endl;
74 int valueToFind = myRand();
76 std::sort(vect.begin(),vect.end(),std::less< Type > ());
77 std::cout << "Vecteur trié par ordre croissant :" << std::endl;
78 copy(vect.begin(),vect.end(),std::ostream_iterator<Type>(std::cout," "));
79 std::cout<< std::endl;
81 std::vector<Type>::iterator it;
82 AdjacentPredicate<Type> ap(valueToFind);
83 it = std::adjacent_find(vect.begin(),vect.end(),ap);
84 if ( it == vect.end() )
85 std::cout << "Je n'est pas trouvé d'intervalle pour encadrer " << valueToFind << std::endl;
87 std::cout << "La valeur à trouver : " << valueToFind <<" est entre * it :" << *it << " et valeur de *(it+1) :" << *(it+1) << std::endl;
88 std::cout<< std::endl;
91 typedef std::map<int,double> MapIntDouble;
93 MapIntDouble::const_iterator itM1,itM2;
94 for(it=vect.begin(); it!=vect.end(); ++it) myMap[*it] = myRand();
96 std::cout << "Clés de la Map :" << std::endl;
97 for(itM1=myMap.begin();itM1!=myMap.end();++itM1)
98 //std::cout << &((*itM1).first)
99 std::cout << (*itM1).first << " ";
100 std::cout<< std::endl;
102 //AdjacentPredicate<std::pair<int,double> > apMap(valueToFind);
103 AdjacentPredicate< MapIntDouble::value_type > apMap(valueToFind);
104 itM1 = std::adjacent_find(myMap.begin(),myMap.end(),apMap);
106 if ( itM1 == myMap.end() )
107 std::cout << "Map : Je n'est pas trouvé d'intervalle pour encadrer " << valueToFind << std::endl;
109 std::cout << "Map : La valeur à trouver : " << valueToFind <<" est entre (*itM1).first :" << (*itM1).first << " et valeur de *(itM1+1).first :" << (*(itM2)).first << std::endl;
111 std::cout<< std::endl;
114 typedef std::map<std::pair<int,double>, double> MapPIntDouble_Double;
115 MapPIntDouble_Double myMapP;
116 MapPIntDouble_Double::const_iterator itMP1,itMP2;
117 // for(it=vect.begin(); it!=vect.end(); ++it) myMapP[std::make_pair<int,double>(*it,myRand())] = myRand();
118 myMapP[std::make_pair<int,double>(*(vect.begin()),myRand())] = myRand();
120 std::cout << "Clés de la MapP :" << std::endl;
121 for(itMP1=myMapP.begin();itMP1!=myMapP.end();++itMP1)
122 std::cout << (*itMP1).first << " " ;
123 std::cout<< std::endl;
125 //AdjacentPredicate<std::pair<int,double> > apMap(valueToFind);
126 // std::pair<int,double> valuePToFind=std::make_pair<int,double>(valueToFind,myRand());
127 std::pair<int,double> valuePToFind=myMapP.begin()->first;
128 AdjacentPredicate< MapPIntDouble_Double::value_type > apMapP(valuePToFind);
129 itMP1 = std::adjacent_find(myMapP.begin(),myMapP.end(),apMapP);
131 if ( itMP1 == myMapP.end() )
132 std::cout << "MapP : Je n'est pas trouvé d'intervalle pour encadrer " << valuePToFind << std::endl;
134 std::cout << "MapP : La valeur à trouver : " << valuePToFind <<" est entre (*itMP1).first :" << (*itMP1).first << " et valeur de *(itMP1+1).first :" << (*(itMP2)).first << std::endl;
137 std::cout << processTimeInterval<double>(valuePToFind,itMP1);
138 std::cout<< std::endl;
140 std::cout<< std::endl;