util.cpp
Go to the documentation of this file.
1 //======================================================================
28 //======================================================================
29 
30 #include "sdhlibrary_settings.h"
31 
32 //----------------------------------------------------------------------
33 // System Includes - include with <>
34 //----------------------------------------------------------------------
35 
36 #define _USE_MATH_DEFINES
37 #include <math.h>
38 #include <time.h>
39 #include <assert.h>
40 
41 #include <iostream>
42 #if SDH_USE_VCC
43 # include <windows.h>
44 #endif
45 
46 //----------------------------------------------------------------------
47 // Project Includes - include with ""
48 //----------------------------------------------------------------------
49 
50 #include "util.h"
51 #include "sdhlibrary_settings.h"
52 
53 //----------------------------------------------------------------------
54 // Defines, enums, unions, structs,
55 //----------------------------------------------------------------------
56 
58 
59 
60 //----------------------------------------------------------------------
61 // Global variables
62 //----------------------------------------------------------------------
63 
64 
65 //----------------------------------------------------------------------
66 // Function declarations
67 //----------------------------------------------------------------------
68 
69 
70 //----------------------------------------------------------------------
71 // Class declarations
72 //----------------------------------------------------------------------
73 
74 bool InIndex( int v, int max )
75 {
76  return 0 <= v && v < max;
77 }
78 //-----------------------------------------------------------------
79 
80 bool InRange( double v, double min, double max )
81 {
82  return min <= v && v <= max;
83 }
84 //-----------------------------------------------------------------
85 
86 bool InRange( int n, double const* v, double const* min, double const* max )
87 {
88  for ( int i=0; i < n; i++ )
89  {
90  if (! InRange( v[i], min[i], max[i] ))
91  return false;
92  }
93  return true;
94 }
95 //-----------------------------------------------------------------
96 
97 double ToRange( double v, double min, double max )
98 {
99  if (v < min) return min;
100  if (v > max) return max;
101  return v;
102 }
103 //-----------------------------------------------------------------
104 
105 void ToRange( int n, double* v, double const* min, double const* max )
106 {
107  for ( int i=0; i < n; i++ )
108  {
109  v[i] = ToRange( v[i], min[i], max[i] );
110  }
111 }
112 //-----------------------------------------------------------------
113 
114 void ToRange( std::vector<double>& v, std::vector<double> const& min, std::vector<double> const& max )
115 {
116  ToRange( int(v.size()), &(v[0]), &(min[0]), &(max[0]) );
117 }
118 //-----------------------------------------------------------------
119 
120 void ToRange( cSimpleVector& v, std::vector<double> const& min, std::vector<double> const& max )
121 {
122  ToRange( cSimpleVector::eNUMBER_OF_ELEMENTS, &(v[0]), &(min[0]), &(max[0]) );
123 }
124 //-----------------------------------------------------------------
125 
126 double Approx( double a, double b, double eps )
127 {
128  return fabs( a - b ) < eps;
129 }
130 //-----------------------------------------------------------------
131 
132 bool Approx( int n, double* a, double* b, double* eps )
133 {
134  for ( int i=0; i < n; i++ )
135  {
136  if (! Approx( a[i], b[i], eps[i] ))
137  return false;
138  }
139  return true;
140 }
141 //-----------------------------------------------------------------
142 
143 double DegToRad( double d )
144 {
145  return d*M_PI/180.0;
146 }
147 //-----------------------------------------------------------------
148 
149 double RadToDeg( double r )
150 {
151  return r*180.0/M_PI;
152 }
153 //-----------------------------------------------------------------
154 
155 void SleepSec( double t )
156 {
157 #if SDH_USE_VCC
158  ::Sleep( static_cast<int>(1000.0*t) );
159 #else
160  timespec sleeptime;
161  sleeptime.tv_sec = (time_t) floor( t );
162  sleeptime.tv_nsec = (long) ((t - floor( t )) * 1E9);
163 
165 
166  nanosleep( &sleeptime, NULL );
167 #endif
168 }
169 //-----------------------------------------------------------------
170 
171 
177 std::vector<int> NumerifyRelease( char const* rev )
178 {
179  std::vector<int> result;
180  char const* rev_orig = rev;
181 
182  while ( rev != NULL && *rev != '\0' )
183  {
184  int r;
185  int chars_scanned = 0;
186  int rc;
187  rc = sscanf( rev, "%d%n", &r, &chars_scanned );
188 
189  //printf ( "Scanning rev='%s' r=%d chars_scanned=%d rc=%d\n", rev, r, chars_scanned, rc );
190 
191  if ( chars_scanned > 0 )
192  {
193  if ( rc==1 )
194  result.push_back(r);
195  rev += chars_scanned;
196  }
197  else if ( chars_scanned == 0 && 'a' <= *rev && *rev <= 'z' )
198  {
199  result.push_back( *rev - 'a' + 1 );
200  rev++;
201  }
202  else if ( chars_scanned == 0 && 'A' <= *rev && *rev <= 'Z' )
203  {
204  result.push_back( *rev - 'A' + 1 );
205  rev++;
206  }
207  else if ( chars_scanned == 0 && ( *rev == '.' || *rev == '-' ) )
208  {
209  rev++;
210  }
211  else
212  {
213  std::cerr << "NumerifyRelease( " << rev_orig << " ) Could not be handled!\n";
214  std::cerr.flush();
215  assert( "invalid rev string!" == NULL );
216  }
217  }
218  return result;
219 }
220 //-----------------------------------------------------------------
221 
222 
236 int CompareReleases( char const* rev1, char const* rev2 )
237 {
238  assert( rev1 != NULL );
239  assert( rev2 != NULL );
240 
241  std::vector<int> nums1 = NumerifyRelease( rev1);
242  std::vector<int> nums2 = NumerifyRelease( rev2 );
243 
244  std::vector<int>::const_iterator n1 = nums1.begin();
245  std::vector<int>::const_iterator n2 = nums2.begin();
246 
247  for ( ; n1 != nums1.end() && n2 != nums2.end(); n1++, n2++ )
248  {
249  if ( *n1 < *n2 )
250  return -1;
251  else if ( *n1 > *n2 )
252  return 1;
253  }
254  // elements existing in both lists are all the same
255 
256  if ( nums1.size() < nums2.size() )
257  return -1;
258  if ( nums1.size() > nums2.size() )
259  return 1;
260  return 0;
261 }
262 //-----------------------------------------------------------------
263 
264 
266 
267 
268 //======================================================================
269 /*
270  Here are some settings for the emacs/xemacs editor (and can be safely ignored):
271  (e.g. to explicitely set C++ mode for *.h header files)
272 
273  Local Variables:
274  mode:C++
275  mode:ELSE
276  End:
277 */
278 //======================================================================
NumerifyRelease
std::vector< int > NumerifyRelease(char const *rev)
Definition: util.cpp:177
NAMESPACE_SDH_END
#define NAMESPACE_SDH_END
Definition: sdhlibrary_settings.h:80
RadToDeg
double RadToDeg(double r)
Definition: util.cpp:149
InRange
bool InRange(double v, double min, double max)
Definition: util.cpp:80
NULL
#define NULL
Definition: getopt1.c:56
InIndex
NAMESPACE_SDH_START bool InIndex(int v, int max)
Definition: util.cpp:74
CompareReleases
int CompareReleases(char const *rev1, char const *rev2)
compare release strings
Definition: util.cpp:236
cSimpleVector
A simple vector implementation.
Definition: simplevector.h:91
SleepSec
void SleepSec(double t)
Definition: util.cpp:155
Approx
double Approx(double a, double b, double eps)
Definition: util.cpp:126
DegToRad
double DegToRad(double d)
Definition: util.cpp:143
cSimpleVector::eNUMBER_OF_ELEMENTS
@ eNUMBER_OF_ELEMENTS
number of elements in vector
Definition: simplevector.h:97
NAMESPACE_SDH_START
#define NAMESPACE_SDH_START
Definition: sdhlibrary_settings.h:79
ToRange
double ToRange(double v, double min, double max)
Definition: util.cpp:97
sdhlibrary_settings.h
This file contains settings to make the SDHLibrary compile on differen systems:
util.h
Interface of auxilliary utility functions for SDHLibrary-CPP.


sdhlibrary_cpp
Author(s): Dirk Osswald
autogenerated on Wed Mar 2 2022 01:00:58