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 : testAdjacentFunctor.cxx
23 // Author : Eric Fayolle (EDF)
25 // Modified by : $LastChangedBy$
26 // Date : $LastChangedDate: 2007-01-08 19:01:14 +0100 (lun, 08 jan 2007) $
37 #include "DisplayPair.hxx"
38 #include "AdjacentFunctor.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));
55 std::vector<Type> vect(N);
59 std::generate(vect.begin(),vect.end(),myRand);
60 std::cout << "Vecteur généré aléatoirement :" << std::endl;
61 copy(vect.begin(),vect.end(),std::ostream_iterator<Type>(std::cout," "));
62 std::cout<< std::endl;
64 int valueToFind = myRand();
65 std::cout << "La valeur recherchée est : " << valueToFind << std::endl;
67 std::sort(vect.begin(),vect.end(),std::less< Type > ());
68 std::cout << "Vecteur trié par ordre croissant :" << std::endl;
69 copy(vect.begin(),vect.end(),std::ostream_iterator<Type>(std::cout," "));
70 std::cout<< std::endl;
72 std::vector<Type>::iterator it,it1,it2;
73 AdjacentFunctor<Type> ap(valueToFind);
74 AdjacentFunctor<Type> & vap(ap);
75 // Ne peut pas marcher à cause de la recopie du prédicat !!
76 it = std::find_if(vect.begin(),vect.end(),vap);
77 // On peut garder la valeur prédente de l'itérateur
78 // si l'itérateur n'est pas reverse
79 // for (it = vect.begin(); it!=vect.end(); ++it)
80 // if ( ap(*it) ) break;
82 if ( it == vect.end() ) {
83 std::cout << " Je n'ai pas trouvé la valeur " << valueToFind << std::endl;
85 if ( (*it) == valueToFind ) std::cout << " La valeur est trouvée *it :" << (*it) << std::endl;
88 // if ( ap.getBounds(min,max) ) {
90 std::cout << "La valeur : " << valueToFind << " est encadrée par (*it) :" << (*it) << " et valeur de *(it-1) :" << (*it2) << std::endl;
97 // if ( it == vect.end() ) {
98 // std::cout << " Je n'ai pas trouvé la valeur " << valueToFind << std::endl;
100 // if ( (*it) == valueToFind ) std::cout << " La valeur est trouvée *it :" << (*it) << std::endl;
103 // if ( ap.getBounds(min,max) ) {
105 // std::cout << "La valeur : " << valueToFind << " est encadrée par (*it) :" << (*it) << " et valeur de *(it-1) :" << (*it2) << std::endl;
111 // typedef std::map<Type,double> MapIntDouble;
112 // MapIntDouble myMap;
113 // MapIntDouble::const_iterator itM1,itM2;
114 // for(it=vect.begin(); it!=vect.end(); ++it) myMap[*it] = myRand();
116 // std::cout << "Clés de la Map :" << std::endl;
117 // for(itM1=myMap.begin();itM1!=myMap.end();++itM1)
118 // //std::cout << &((*itM1).first)
119 // std::cout << (*itM1).first << " ";
120 // std::cout<< std::endl;
122 // AdjacentFunctor<MapIntDouble::value_type::first_type> apMap(valueToFind);
123 // for (itM1 = myMap.begin(); itM1 != myMap.end(); ++itM1)
124 // if ( apMap(itM1->first) ) break;
126 // if ( itM1 == myMap.end() ) {
127 // std::cout << " Je n'est pas trouvé la valeur " << valueToFind << std::endl;
129 // if ( (itM1->first) == valueToFind ) std::cout << " La valeur est trouvée itM1->first :" << (itM1->first) << std::endl;
132 // if ( apMap.getBounds(min,max) ) {
134 // std::cout << "La valeur " << valueToFind << " est encadrée par (itM1->first) :" << (itM1->first) << " et valeur de (it-1)->first :" << (itM2->first) << std::endl;
140 // typedef std::map<std::pair<int,double>, double> MapIntDouble_Double;
141 // MapIntDouble_Double myMapP;
142 // MapIntDouble_Double::const_iterator itMP1,itMP2;
143 // for(it=vect.begin(); it!=vect.end(); ++it) myMapP[std::make_pair<int,double>(*it,myRand())] = myRand();
144 // //myMapP[std::make_pair<int,double>(*(vect.begin()),myRand())] = myRand();
146 // std::cout << "Clés de la MapP :" << std::endl;
147 // for(itMP1=myMapP.begin();itMP1!=myMapP.end();++itMP1)
148 // std::cout << (*itMP1).first << " " ;
149 // std::cout<< std::endl;
151 // //AdjacentPredicate<std::pair<int,double> > apMap(valueToFind);
152 // std::pair<int,double> valuePToFind=std::make_pair<int,double>(valueToFind,myRand());
153 // //std::pair<int,double> valuePToFind=myMapP.begin()->first;
154 // AdjacentFunctor< MapIntDouble_Double::key_type > apMapP(valuePToFind);
155 // for (itMP1 = myMapP.begin(); itMP1 != myMapP.end(); ++itMP1)
156 // if ( apMapP(itMP1->first) ) break;
158 // if ( itMP1 == myMapP.end() ) {
159 // std::cout << " Je n'est pas trouvé la valeur " << valuePToFind << std::endl;
161 // if ( (itMP1->first) == valuePToFind ) std::cout << " La valeur est trouvée itMP1->first :" << (itMP1->first) << std::endl;
163 // MapIntDouble_Double::key_type min,max;
164 // if ( apMapP.getBounds(min,max) ) {
165 // itMP2=itMP1;itMP2--;
166 // std::cout << "La valeur " << valuePToFind << " est encadrée par (itMP1->first) :" << (itMP1->first) << " et valeur de (it-1)->first :" << (itMP2->first) << std::endl;
172 std::cout<< std::endl;