Salome HOME
CCAR: add trace file for DSC/CALCIUM calls
[modules/kernel.git] / src / DSC / DSC_User / Datastream / testAdjacentFunctor.cxx
1 //  Copyright (C) 2007-2008  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.
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 //  File   : testAdjacentFunctor.cxx
23 //  Author : Eric Fayolle (EDF)
24 //  Module : KERNEL
25 // Modified by : $LastChangedBy$
26 // Date        : $LastChangedDate: 2007-01-08 19:01:14 +0100 (lun, 08 jan 2007) $
27 // Id          : $Id$
28 //
29 #include <iostream>
30 #include <vector>
31 #include <map>
32 #include <algorithm>
33 #include <iterator>
34 #include <functional>
35 #include <utility>
36
37 #include "DisplayPair.hxx"
38 #include "AdjacentFunctor.hxx"
39
40 #include <stdlib.h>
41 #include <sys/types.h>
42 #include <unistd.h>
43
44 struct MyRand {
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));
49   }
50 };
51
52 int main() {
53   typedef int Type;
54   const   int N=20;
55   std::vector<Type> vect(N);
56   MyRand   myRand;
57
58   //TEST1
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;
63
64   int valueToFind = myRand();
65   std::cout << "La valeur recherchée est : " << valueToFind << std::endl;
66
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;
71
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;
81
82   if ( it == vect.end() ) {
83     std::cout << " Je n'ai pas trouvé la valeur " << valueToFind << std::endl;
84   } else {
85     if ( (*it) == valueToFind ) std::cout << " La valeur est trouvée *it :" << (*it) << std::endl;
86     else {
87       //      Type min,max;
88       //      if ( ap.getBounds(min,max) ) {
89       it2=it;it2--;
90       std::cout << "La valeur : " << valueToFind << " est encadrée par  (*it) :" << (*it) << " et valeur de *(it-1) :" << (*it2) << std::endl;
91     }
92   }
93   
94   
95
96          
97  //  if ( it == vect.end() ) {
98 //     std::cout << " Je n'ai pas trouvé la valeur " << valueToFind << std::endl;
99 //   } else {
100 //     if ( (*it) == valueToFind ) std::cout << " La valeur est trouvée *it :" << (*it) << std::endl;
101 //     else {
102 //       Type min,max;
103 //       if ( ap.getBounds(min,max) ) {
104 //         it2=it;it2--;
105 //      std::cout << "La valeur : " << valueToFind << " est encadrée par  (*it) :" << (*it) << " et valeur de *(it-1) :" << (*it2) << std::endl;
106 //       }
107 //     }
108 //   }
109
110   //TEST2
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();
115   
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;
121  
122 //   AdjacentFunctor<MapIntDouble::value_type::first_type> apMap(valueToFind);
123 //   for (itM1 = myMap.begin(); itM1 != myMap.end(); ++itM1) 
124 //      if ( apMap(itM1->first) ) break;
125          
126 //   if ( itM1 == myMap.end() ) {
127 //     std::cout << " Je n'est pas trouvé la valeur " << valueToFind << std::endl;
128 //   } else {
129 //     if ( (itM1->first) == valueToFind ) std::cout << " La valeur est trouvée itM1->first :" << (itM1->first) << std::endl;
130 //     else {
131 //       Type min,max;
132 //       if ( apMap.getBounds(min,max) ) {
133 //         itM2=itM1;itM2--;
134 //      std::cout << "La valeur " << valueToFind << " est encadrée par  (itM1->first) :" << (itM1->first) << " et valeur de (it-1)->first :" << (itM2->first) << std::endl;
135 //       }
136 //     }
137 //   }
138
139   // TEST3
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();
145   
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;
150  
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;
157
158 //   if ( itMP1 == myMapP.end() ) {
159 //     std::cout << " Je n'est pas trouvé la valeur " << valuePToFind << std::endl;
160 //   } else {
161 //     if ( (itMP1->first) == valuePToFind ) std::cout << " La valeur est trouvée itMP1->first :" << (itMP1->first) << std::endl;
162 //     else {
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;
167 //       }
168 //     }
169 //   }
170
171
172   std::cout<< std::endl;
173
174 };
175