Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
RangeList.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "RangeList.h"
14 
15 #include <float.h>
16 #include <limits.h>
17 #include <boost/bind.hpp>
18 
19 
20 using namespace kome::core;
21 
22 
23 #include <crtdbg.h>
24 #ifdef _DEBUG
25  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
26  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
27 #endif // _DEBUG
28 
29 
30 
31 // constructor
32 RangeList::RangeList( const DefaultType type ) : m_type( type ) {
33 }
34 
35 // destructor
37 }
38 
39 // get default type
41  return m_type;
42 }
43 
44 // add int range
45 void RangeList::addIntRange( const int start, const int end ) {
46  m_ranges.resize( m_ranges.size() + 1 );
47  m_ranges.back().first.intVal = start;
48  m_ranges.back().second.intVal = end;
49 }
50 
51 // add double range
52 void RangeList::addDoubleRange( const double start, const double end ) {
53  m_ranges.resize( m_ranges.size() + 1 );
54  m_ranges.back().first.dblVal = start;
55  m_ranges.back().second.dblVal = end;
56 }
57 
58 // clear ranges
60  m_ranges.clear();
61 }
62 
63 // get the number of ranges
65  return m_ranges.size();
66 }
67 
68 // get int range start
69 int RangeList::getStartInt( const unsigned int idx ) {
70  if( idx >= m_ranges.size() ) {
71  return INT_MAX;
72  }
73  return m_ranges[ idx ].first.intVal;
74 }
75 
76 // get int range end
77 int RangeList::getEndInt( const unsigned int idx ) {
78  if( idx >= m_ranges.size() ) {
79  return INT_MIN;
80  }
81  return m_ranges[ idx ].second.intVal;
82 }
83 // get double range start
84 double RangeList::getStartDouble( const unsigned int idx ) {
85  if( idx >= m_ranges.size() ) {
86  return FLT_MAX;
87  }
88  return m_ranges[ idx ].first.dblVal;
89 }
90 
91 // get double range end
92 double RangeList::getEndDouble( const unsigned int idx ) {
93  if( idx >= m_ranges.size() ) {
94  return - FLT_MAX;
95  }
96  return m_ranges[ idx ].second.dblVal;
97 }
98 
99 // check int vlaue
100 bool RangeList::checkIntValue( const int val ) {
101  // empty
102  if( m_ranges.size() == 0 ) {
103  return ( m_type == DEFAULT_ALL );
104  }
105 
106  // check
107  for( unsigned int i = 0; i < m_ranges.size(); i++ ) {
108  if( val >= m_ranges[ i ].first.intVal
109  && val <= m_ranges[ i ].second.intVal ) { // included
110  return true;
111  }
112  }
113  return false;
114 }
115 
116 // check double value
117 bool RangeList::checkDoubleValue( const double val ) {
118  // empty
119  if( m_ranges.size() == 0 ) {
120  return ( m_type == DEFAULT_ALL );
121  }
122 
123  // check
124  for( unsigned int i = 0; i < m_ranges.size(); i++ ) {
125  if( val >= m_ranges[ i ].first.dblVal
126  && val <= m_ranges[ i ].second.dblVal ) { // included
127  return true;
128  }
129  }
130  return false;
131 }
132 
133 // import integer ranges
134 void RangeList::importIntRanges( const char* str ) {
135  // clear
136  clearRanges();
137 
138  // token by ","
139  std::vector< std::string > tokens;
140 
141  stringseparate(str,",",tokens);
142 
143  for( unsigned int i = 0; i < tokens.size(); i++ ) {
144  // token by ":"
145  std::vector< std::string > range;
146 
147  stringseparate(tokens[ i ].c_str(),":",range);
148 
149  if( range.size() == 1 ) { // single value
150  std::string val = trimstring( range[ 0 ].c_str() );
151  int v = int();
152  if( isint( val.c_str(), 10, &v ) ) {
153  m_ranges.resize( m_ranges.size() + 1 );
154  m_ranges.back().first.intVal = v;
155  m_ranges.back().second.intVal = v;
156  }
157  }
158  else if( range.size() > 1 ) { // range value
159  std::string val0 = trimstring( range[ 0 ].c_str() );
160  std::string val1 = trimstring( range[ 1 ].c_str() );
161 
162  int v0 = int();
163  int v1 = int();
164 
165  bool ret0 = isint( val0.c_str(), 10, &v0 );
166  bool ret1 = isint( val1.c_str(), 10, &v1 );
167 
168  if( ret0 || ret1 ) {
169  m_ranges.resize( m_ranges.size() + 1 );
170 
171  m_ranges.back().first.intVal = ( ret0 ? v0 : INT_MIN );
172  m_ranges.back().second.intVal = ( ret1 ? v1 : INT_MAX );
173  }
174  }
175  }
176 }
177 
178 // import double ranges
179 void RangeList::importDoubleRanges( const char* str ) {
180  // clear
181  clearRanges();
182 
183  // token by ","
184  std::vector< std::string > tokens;
185 
186  stringseparate(str, ",", tokens);
187 
188  for( unsigned int i = 0; i < tokens.size(); i++ ) {
189  // token by ":"
190  std::vector< std::string > range;
191 
192  stringseparate(tokens[ i ].c_str(),":",range);
193 
194  if( range.size() == 1 ) { // single value
195  std::string val = trimstring( range[ 0 ].c_str() );
196  double v = double();
197  if( isdouble( val.c_str(), &v ) ) {
198  m_ranges.resize( m_ranges.size() + 1 );
199  m_ranges.back().first.dblVal = v;
200  m_ranges.back().second.dblVal = v;
201  }
202  }
203  else if( range.size() > 1 ) { // range value
204  std::string val0 = trimstring( range[ 0 ].c_str() );
205  std::string val1 = trimstring( range[ 1 ].c_str() );
206 
207  double v0 = double();
208  double v1 = double();
209 
210  bool ret0 = isdouble( val0.c_str(), &v0 );
211  bool ret1 = isdouble( val1.c_str(), &v1 );
212 
213  if( ret0 || ret1 ) {
214  m_ranges.resize( m_ranges.size() + 1 );
215 
216  m_ranges.back().first.dblVal = ( ret0 ? v0 : - FLT_MAX );
217  m_ranges.back().second.dblVal = ( ret1 ? v1 : FLT_MAX );
218  }
219  }
220  }
221 }
222 
223 // export integer ranges
225  // string
226  std::string str;
227 
228  // create character string
229  for( unsigned int i = 0; i < m_ranges.size(); i++ ) {
230  // create range string
231  std::string range;
232 
233  int v0 = m_ranges[ i ].first.intVal;
234  int v1 = m_ranges[ i ].second.intVal;
235 
236  if( v0 == v1 ) {
237  range = FMT( "%d", v0 );
238  }
239  else {
240  if( v0 != INT_MIN ) {
241  range = FMT( "%d", v0 );
242  }
243 
244  range.append( ":" );
245 
246  if( v1 != INT_MAX ) {
247  range = FMT( "%d", v1 );
248  }
249  }
250 
251  // append
252  if( !str.empty() ) {
253  str.append( "," );
254  }
255  str.append( range );
256  }
257 
258  return str;
259 }
260 
261 // export double ranges
263  // string
264  std::string str;
265 
266  // create character string
267  for( unsigned int i = 0; i < m_ranges.size(); i++ ) {
268  // create range string
269  std::string range;
270 
271  double v0 = m_ranges[ i ].first.dblVal;
272  double v1 = m_ranges[ i ].second.dblVal;
273 
274  if( v0 == v1 ) {
275  range = FMT( "%f", v0 );
276  }
277  else {
278  if( v0 != - FLT_MAX ) {
279  range = FMT( "%f", v0 );
280  }
281 
282  range.append( ":" );
283 
284  if( v1 != FLT_MAX ) {
285  range = FMT( "%f", v1 );
286  }
287  }
288 
289  // append
290  if( !str.empty() ) {
291  str.append( "," );
292  }
293  str.append( range );
294  }
295 
296  return str;
297 }
double getEndDouble(const unsigned int idx)
gets range end value
Definition: RangeList.cpp:92
DefaultType m_type
Definition: RangeList.h:53
DefaultType getType()
gets default type
Definition: RangeList.cpp:40
RangeList(const DefaultType type=DEFAULT_NONE)
constructor
Definition: RangeList.cpp:32
void addIntRange(const int start, const int end)
adds integer range
Definition: RangeList.cpp:45
void clearRanges()
clears range
Definition: RangeList.cpp:59
DefaultType
default type
Definition: RangeList.h:33
std::string exportIntRanges()
exports integer ranges to character string
Definition: RangeList.cpp:224
bool isdouble(const char *s, double *val)
judge whether sutisfied character string shows real number.
unsigned int stringseparate(const char *s, const char *separator, std::vector< std::string > &tokens)
separates string
void importIntRanges(const char *str)
import integer ranges from character string
Definition: RangeList.cpp:134
std::string trimstring(const char *s)
remove white spaces from both ends of specified string
bool checkIntValue(const int val)
checks integer vlaue
Definition: RangeList.cpp:100
bool isint(const char *s, const int radix, int *val)
judge whether sutisfied character string shows integer number.
interfarces of RangeList class
int getEndInt(const unsigned int idx)
gets range end value
Definition: RangeList.cpp:77
int getStartInt(const unsigned int idx)
gets range start value
Definition: RangeList.cpp:69
void addDoubleRange(const double start, const double end)
adds double range
Definition: RangeList.cpp:52
unsigned int getNumberOfRanges()
gets the number of ranges
Definition: RangeList.cpp:64
void importDoubleRanges(const char *str)
import double ranges from character string
Definition: RangeList.cpp:179
std::string exportDoubleRanges()
exports double ranges to character string
Definition: RangeList.cpp:262
virtual ~RangeList()
destructor
Definition: RangeList.cpp:36
std::vector< std::pair< NumberValue, NumberValue > > m_ranges
Definition: RangeList.h:66
double getStartDouble(const unsigned int idx)
gets range start value
Definition: RangeList.cpp:84
bool checkDoubleValue(const double val)
checks double value
Definition: RangeList.cpp:117