1 // Copyright (C) 2007-2021 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, or (at your option) any later version.
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 : testAdjacentFunctor.cxx
24 // Author : Eric Fayolle (EDF)
26 // Modified by : $LastChangedBy$
27 // Date : $LastChangedDate: 2007-01-08 19:01:14 +0100 (lun, 08 jan 2007) $
38 #include "DisplayPair.hxx"
39 #include "AdjacentFunctor.hxx"
42 #include <sys/types.h>
46 static const double MAXVALUE = 150.0;
47 MyRand() { srand(getpid()); }
48 int operator()() const {
49 return 1+(int) ( MAXVALUE *rand()/(RAND_MAX +1.0));
56 std::vector<Type> vect(N);
60 std::generate(vect.begin(),vect.end(),myRand);
61 std::cout << "Vecteur généré aléatoirement :" << std::endl;
62 copy(vect.begin(),vect.end(),std::ostream_iterator<Type>(std::cout," "));
63 std::cout<< std::endl;
65 int valueToFind = myRand();
66 std::cout << "La valeur recherchée est : " << valueToFind << std::endl;
68 std::sort(vect.begin(),vect.end(),std::less< Type > ());
69 std::cout << "Vecteur trié par ordre croissant :" << std::endl;
70 copy(vect.begin(),vect.end(),std::ostream_iterator<Type>(std::cout," "));
71 std::cout<< std::endl;
73 std::vector<Type>::iterator it,it1,it2;
74 AdjacentFunctor<Type> ap(valueToFind);
75 AdjacentFunctor<Type> & vap(ap);
76 // Ne peut pas marcher à cause de la recopie du prédicat !!
77 it = std::find_if(vect.begin(),vect.end(),vap);
78 // On peut garder la valeur prédente de l'itérateur
79 // si l'itérateur n'est pas reverse
80 // for (it = vect.begin(); it!=vect.end(); ++it)
81 // if ( ap(*it) ) break;
83 if ( it == vect.end() ) {
84 std::cout << " Je n'ai pas trouvé la valeur " << valueToFind << std::endl;
86 if ( (*it) == valueToFind ) std::cout << " La valeur est trouvée *it :" << (*it) << std::endl;
89 // if ( ap.getBounds(min,max) ) {
91 std::cout << "La valeur : " << valueToFind << " est encadrée par (*it) :" << (*it) << " et valeur de *(it-1) :" << (*it2) << std::endl;
98 // if ( it == vect.end() ) {
99 // std::cout << " Je n'ai pas trouvé la valeur " << valueToFind << std::endl;
101 // if ( (*it) == valueToFind ) std::cout << " La valeur est trouvée *it :" << (*it) << std::endl;
104 // if ( ap.getBounds(min,max) ) {
106 // std::cout << "La valeur : " << valueToFind << " est encadrée par (*it) :" << (*it) << " et valeur de *(it-1) :" << (*it2) << std::endl;
112 // typedef std::map<Type,double> MapIntDouble;
113 // MapIntDouble myMap;
114 // MapIntDouble::const_iterator itM1,itM2;
115 // for(it=vect.begin(); it!=vect.end(); ++it) myMap[*it] = myRand();
117 // std::cout << "Clés de la Map :" << std::endl;
118 // for(itM1=myMap.begin();itM1!=myMap.end();++itM1)
119 // //std::cout << &((*itM1).first)
120 // std::cout << (*itM1).first << " ";
121 // std::cout<< std::endl;
123 // AdjacentFunctor<MapIntDouble::value_type::first_type> apMap(valueToFind);
124 // for (itM1 = myMap.begin(); itM1 != myMap.end(); ++itM1)
125 // if ( apMap(itM1->first) ) break;
127 // if ( itM1 == myMap.end() ) {
128 // std::cout << " Je n'est pas trouvé la valeur " << valueToFind << std::endl;
130 // if ( (itM1->first) == valueToFind ) std::cout << " La valeur est trouvée itM1->first :" << (itM1->first) << std::endl;
133 // if ( apMap.getBounds(min,max) ) {
135 // std::cout << "La valeur " << valueToFind << " est encadrée par (itM1->first) :" << (itM1->first) << " et valeur de (it-1)->first :" << (itM2->first) << std::endl;
141 // typedef std::map<std::pair<int,double>, double> MapIntDouble_Double;
142 // MapIntDouble_Double myMapP;
143 // MapIntDouble_Double::const_iterator itMP1,itMP2;
144 // for(it=vect.begin(); it!=vect.end(); ++it) myMapP[std::make_pair<int,double>(*it,myRand())] = myRand();
145 // //myMapP[std::make_pair<int,double>(*(vect.begin()),myRand())] = myRand();
147 // std::cout << "Clés de la MapP :" << std::endl;
148 // for(itMP1=myMapP.begin();itMP1!=myMapP.end();++itMP1)
149 // std::cout << (*itMP1).first << " " ;
150 // std::cout<< std::endl;
152 // //AdjacentPredicate<std::pair<int,double> > apMap(valueToFind);
153 // std::pair<int,double> valuePToFind=std::make_pair<int,double>(valueToFind,myRand());
154 // //std::pair<int,double> valuePToFind=myMapP.begin()->first;
155 // AdjacentFunctor< MapIntDouble_Double::key_type > apMapP(valuePToFind);
156 // for (itMP1 = myMapP.begin(); itMP1 != myMapP.end(); ++itMP1)
157 // if ( apMapP(itMP1->first) ) break;
159 // if ( itMP1 == myMapP.end() ) {
160 // std::cout << " Je n'est pas trouvé la valeur " << valuePToFind << std::endl;
162 // if ( (itMP1->first) == valuePToFind ) std::cout << " La valeur est trouvée itMP1->first :" << (itMP1->first) << std::endl;
164 // MapIntDouble_Double::key_type min,max;
165 // if ( apMapP.getBounds(min,max) ) {
166 // itMP2=itMP1;itMP2--;
167 // std::cout << "La valeur " << valuePToFind << " est encadrée par (itMP1->first) :" << (itMP1->first) << " et valeur de (it-1)->first :" << (itMP2->first) << std::endl;
173 std::cout<< std::endl;