Salome HOME
merge from branch BR_V5_DEV
[modules/kernel.git] / src / DSC / DSC_User / Datastream / testAdjacentPredicate.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   : testAdjacentPredicate.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 "AdjacentPredicate.hxx"
30 #include "DisplayPair.hxx"
31
32 #include <iostream>
33
34 #include <vector>
35 #include <map>
36 #include <algorithm>
37 #include <iterator>
38
39 #include <stdlib.h>
40 #include <sys/types.h>
41 #include <unistd.h>
42
43 struct MyRand {
44   static const double MAXVALUE = 150.0;
45   MyRand() { srand(getpid()); }
46   int operator()() const {
47         return 1+(int) ( MAXVALUE *rand()/(RAND_MAX +1.0));
48   }
49 };
50
51 typedef int TimeType;
52 typedef double TagType;
53 typedef std::pair< TimeType , TagType >     DataId;
54
55 template < typename DataType > 
56   DataType processTimeInterval (DataId & dataId, 
57                                 typename std::map<DataId, DataType>::const_iterator const & it1) {
58      return (*it1).second;
59 };
60
61 int main() {
62   typedef int Type;
63   const   int N=20;
64   std::vector<Type> vect(N);
65   MyRand   myRand;
66
67   //TEST1
68   std::generate(vect.begin(),vect.end(),myRand);
69   std::cout << "Vecteur généré aléatoirement :" << std::endl;
70   copy(vect.begin(),vect.end(),std::ostream_iterator<Type>(std::cout," "));
71   std::cout<< std::endl;
72
73   int valueToFind = myRand();
74
75   std::sort(vect.begin(),vect.end(),std::less< Type > ());
76   std::cout << "Vecteur trié par ordre croissant :" << std::endl;
77   copy(vect.begin(),vect.end(),std::ostream_iterator<Type>(std::cout," "));
78   std::cout<< std::endl;
79
80   std::vector<Type>::iterator it;
81   AdjacentPredicate<Type> ap(valueToFind);
82   it = std::adjacent_find(vect.begin(),vect.end(),ap);
83   if ( it == vect.end() )
84     std::cout << "Je n'est pas trouvé d'intervalle pour encadrer " << valueToFind << std::endl;
85   else
86     std::cout << "La valeur à trouver : " << valueToFind <<" est entre * it :" << *it << " et valeur de *(it+1) :" << *(it+1) << std::endl;
87   std::cout<< std::endl;
88   
89   //TEST2
90   typedef std::map<int,double> MapIntDouble;
91   MapIntDouble myMap;
92   MapIntDouble::const_iterator itM1,itM2;
93   for(it=vect.begin(); it!=vect.end(); ++it) myMap[*it] = myRand();
94   
95   std::cout << "Clés de la Map :" << std::endl;
96   for(itM1=myMap.begin();itM1!=myMap.end();++itM1)
97     //std::cout << &((*itM1).first) 
98     std::cout << (*itM1).first << " ";
99   std::cout<< std::endl;
100  
101   //AdjacentPredicate<std::pair<int,double> > apMap(valueToFind);
102   AdjacentPredicate< MapIntDouble::value_type  > apMap(valueToFind);
103   itM1 = std::adjacent_find(myMap.begin(),myMap.end(),apMap);
104   itM2=itM1;itM2++;
105   if ( itM1 == myMap.end() )
106     std::cout << "Map : Je n'est pas trouvé d'intervalle pour encadrer " << valueToFind << std::endl;
107   else {
108     std::cout << "Map : La valeur à trouver : " << valueToFind <<" est entre (*itM1).first :" << (*itM1).first << " et valeur de *(itM1+1).first :" << (*(itM2)).first << std::endl;
109   }
110   std::cout<< std::endl;
111
112   // TEST3
113   typedef std::map<std::pair<int,double>, double> MapPIntDouble_Double;
114   MapPIntDouble_Double myMapP;
115   MapPIntDouble_Double::const_iterator itMP1,itMP2;
116   //  for(it=vect.begin(); it!=vect.end(); ++it) myMapP[std::make_pair<int,double>(*it,myRand())] = myRand();
117   myMapP[std::make_pair<int,double>(*(vect.begin()),myRand())] = myRand();
118   
119   std::cout << "Clés de la MapP :" << std::endl;
120   for(itMP1=myMapP.begin();itMP1!=myMapP.end();++itMP1)
121     std::cout << (*itMP1).first  << " " ;
122   std::cout<< std::endl;
123  
124   //AdjacentPredicate<std::pair<int,double> > apMap(valueToFind);
125   //  std::pair<int,double> valuePToFind=std::make_pair<int,double>(valueToFind,myRand());
126   std::pair<int,double> valuePToFind=myMapP.begin()->first;
127   AdjacentPredicate< MapPIntDouble_Double::value_type  > apMapP(valuePToFind);
128   itMP1 = std::adjacent_find(myMapP.begin(),myMapP.end(),apMapP);
129   itMP2=itMP1;itMP2++;
130   if ( itMP1 == myMapP.end() )
131     std::cout << "MapP : Je n'est pas trouvé d'intervalle pour encadrer " << valuePToFind << std::endl;
132   else {
133     std::cout << "MapP : La valeur à trouver : " << valuePToFind <<" est entre (*itMP1).first :" << (*itMP1).first << " et valeur de *(itMP1+1).first :" << (*(itMP2)).first << std::endl;
134   }
135
136   std::cout << processTimeInterval<double>(valuePToFind,itMP1);
137   std::cout<< std::endl;
138
139   std::cout<< std::endl;
140
141 };
142