Salome HOME
a1bed1e600f52b4eb2c1fb6191727f135ed91ce1
[modules/kernel.git] / src / DSC / DSC_User / Datastream / testAdjacentFunctor.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : testAdjacentFunctor.cxx
24 //  Author : Eric Fayolle (EDF)
25 //  Module : KERNEL
26 // Modified by : $LastChangedBy$
27 // Date        : $LastChangedDate: 2007-01-08 19:01:14 +0100 (lun, 08 jan 2007) $
28 // Id          : $Id$
29 //
30 #include <iostream>
31 #include <vector>
32 #include <map>
33 #include <algorithm>
34 #include <iterator>
35 #include <functional>
36 #include <utility>
37
38 #include "DisplayPair.hxx"
39 #include "AdjacentFunctor.hxx"
40
41 #include <stdlib.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44
45 struct MyRand {
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));
50   }
51 };
52
53 int main() {
54   typedef int Type;
55   const   int N=20;
56   std::vector<Type> vect(N);
57   MyRand   myRand;
58
59   //TEST1
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;
64
65   int valueToFind = myRand();
66   std::cout << "La valeur recherchée est : " << valueToFind << std::endl;
67
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;
72
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;
82
83   if ( it == vect.end() ) {
84     std::cout << " Je n'ai pas trouvé la valeur " << valueToFind << std::endl;
85   } else {
86     if ( (*it) == valueToFind ) std::cout << " La valeur est trouvée *it :" << (*it) << std::endl;
87     else {
88       //      Type min,max;
89       //      if ( ap.getBounds(min,max) ) {
90       it2=it;it2--;
91       std::cout << "La valeur : " << valueToFind << " est encadrée par  (*it) :" << (*it) << " et valeur de *(it-1) :" << (*it2) << std::endl;
92     }
93   }
94   
95   
96
97          
98  //  if ( it == vect.end() ) {
99 //     std::cout << " Je n'ai pas trouvé la valeur " << valueToFind << std::endl;
100 //   } else {
101 //     if ( (*it) == valueToFind ) std::cout << " La valeur est trouvée *it :" << (*it) << std::endl;
102 //     else {
103 //       Type min,max;
104 //       if ( ap.getBounds(min,max) ) {
105 //         it2=it;it2--;
106 //      std::cout << "La valeur : " << valueToFind << " est encadrée par  (*it) :" << (*it) << " et valeur de *(it-1) :" << (*it2) << std::endl;
107 //       }
108 //     }
109 //   }
110
111   //TEST2
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();
116   
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;
122  
123 //   AdjacentFunctor<MapIntDouble::value_type::first_type> apMap(valueToFind);
124 //   for (itM1 = myMap.begin(); itM1 != myMap.end(); ++itM1) 
125 //      if ( apMap(itM1->first) ) break;
126          
127 //   if ( itM1 == myMap.end() ) {
128 //     std::cout << " Je n'est pas trouvé la valeur " << valueToFind << std::endl;
129 //   } else {
130 //     if ( (itM1->first) == valueToFind ) std::cout << " La valeur est trouvée itM1->first :" << (itM1->first) << std::endl;
131 //     else {
132 //       Type min,max;
133 //       if ( apMap.getBounds(min,max) ) {
134 //         itM2=itM1;itM2--;
135 //      std::cout << "La valeur " << valueToFind << " est encadrée par  (itM1->first) :" << (itM1->first) << " et valeur de (it-1)->first :" << (itM2->first) << std::endl;
136 //       }
137 //     }
138 //   }
139
140   // TEST3
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();
146   
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;
151  
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;
158
159 //   if ( itMP1 == myMapP.end() ) {
160 //     std::cout << " Je n'est pas trouvé la valeur " << valuePToFind << std::endl;
161 //   } else {
162 //     if ( (itMP1->first) == valuePToFind ) std::cout << " La valeur est trouvée itMP1->first :" << (itMP1->first) << std::endl;
163 //     else {
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;
168 //       }
169 //     }
170 //   }
171
172
173   std::cout<< std::endl;
174
175 };
176