DataSource.cxx
Go to the documentation of this file.
1 
12 // for distance defect
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #else
16 #ifdef _MSC_VER
17 #include "msdevstudio/MSconfig.h"
18 #define isnan _isnan
19 #endif
20 #endif
21 
22 //To have isnan.
23 #ifdef __APPLE__
24 #include <cstdlib>
25 #define _GLIBCPP_USE_C99 1
26 #endif
27 
28 #include "DataSource.h"
29 
30 #include "axes/Range.h"
31 #include "pattern/Observer.h"
32 #include "pattern/string_convert.h"
33 
34 #include <algorithm>
35 #include <functional>
36 #include <stdexcept>
37 
38 #include <cmath>
39 
40 #include <cassert>
41 
42 #ifdef __APPLE__
43 using std::isnan;
44 #endif
45 
46 #ifdef ITERATOR_MEMBER_DEFECT
47 using namespace std;
48 #else
49 using std::distance;
50 using std::runtime_error;
51 using std::string;
52 using std::vector;
53 #endif
54 
55 using namespace hippodraw;
56 
57 DataSource::
58 DataSource ( const std::string & name )
59  : m_ds_name ( name ),
60  m_title(),
61  m_is_null ( false )
62 {
63 }
64 
66 DataSource ( const char * name )
67  : m_ds_name ( name ),
68  m_title(),
69  m_is_null ( false )
70 {
71 }
72 
74 DataSource ( const DataSource & ds )
75  : Observable (),
76  m_ds_name ( ds.m_ds_name ),
77  m_title( ds.m_title ),
78  m_labels ( ds.m_labels ),
79  m_is_null ( false ),
80  m_shape ( ds.m_shape )
81 {
82 }
83 
85 DataSource ( const std::vector < std::string > & labels )
86  : m_labels ( labels ),
87  m_is_null ( false )
88 {
89 }
90 
93  : m_ds_name ( ),
94  m_title(),
95  m_is_null ( false )
96 {
97 }
98 
100 DataSource ( bool yes )
101  : m_ds_name ( "null" ),
102  m_title ( "Not valid DataSource" ),
103  m_is_null ( yes )
104 {
105 }
106 
108 {
110 }
111 
112 void
114 copyPrivate ( const DataSource & other )
115 {
116  m_ds_name = other.m_ds_name;
117  m_is_null = other.m_is_null;
118  m_labels = other.m_labels;
119  m_shape = other.m_shape;
120  m_title = other.m_title;
121 }
122 
123 bool
125 isNull () const
126 {
127  return m_is_null;
128 }
129 
130 void DataSource::setName ( const std::string & name )
131 {
132  m_ds_name = name;
133  notifyObservers ();
134 }
135 
136 const string & DataSource::getName () const
137 {
138  return m_ds_name;
139 }
140 
141 const string & DataSource::title () const
142 {
143  return m_title;
144 }
145 
146 void DataSource::setTitle ( const std::string & title )
147 {
148  m_title = title;
149  notifyObservers ();
150 }
151 
152 void
154 addLabel ( const std::string & label )
155 {
156  m_labels.push_back ( label );
157 }
158 
159 bool
161 setLabelAt( const std::string & s, unsigned int i )
162 {
163  if ( i >= m_labels.size() ) return false;
164  m_labels[i] = s;
165  notifyObservers ();
166 
167  return true;
168 }
169 
170 const vector <string> &
172 getLabels () const
173 {
174  return m_labels;
175 }
176 
177 const string &
179 getLabelAt ( unsigned int i ) const
180 {
181  if ( isNull() ) return title ();
182 
183  if ( m_labels.size () < i ) {
184  string what ( "DataSource: argument out of range" );
185  throw runtime_error ( what );
186  }
187 
188  return m_labels[i];
189 }
190 
191 int
193 indexOf ( const std::string & label ) const
194 {
195  int index = -1;
196  vector< string >::const_iterator first
197  = find ( m_labels.begin(), m_labels.end(), label );
198 
199  if ( first != m_labels.end() ) {
200 #ifdef DISTANCE_DEFECT
201  index = first - m_labels.begin();
202 #else
203  index = distance ( m_labels.begin(), first );
204 #endif
205  }
206 
207  return index;
208 }
209 
210 bool
212 isValidColumn ( unsigned int index ) const
213 {
214  unsigned int size = columns();
215  if ( index >= size ) {
216  string what ( "DataSource: Index `" );
217  what += String::convert ( index );
218  what += "' out of range 0 to ";
219  what += String::convert ( size - 1 );
220  throw runtime_error ( what );
221  }
222 
223  return true;
224 }
225 
226 const vector < double > &
228 getColumn ( unsigned int index ) const
229 {
230  isValidColumn ( index ); // will throw exception if bad
231  unsigned int size = rows ();
232 
233  m_array.resize ( size, 0.0 );
234  for ( unsigned int i = 0; i < size; i++ ) {
235  m_array [ i ] = valueAt ( i, index );
236  }
237 
238  return m_array;
239 }
240 
241 const vector < double > &
243 getColumn ( const std::string & label ) const
244 {
245  int index = indexOf ( label );
246  if ( index < 0 ) {
247  const string what ( "DataSource: Attempt to get column whose label"
248  " doesn't exist" );
249  throw runtime_error ( what );
250  }
251 
252  return getColumn ( index );
253 }
254 
255 bool
257 isValidLabel ( const std::string & label ) const
258 {
259  vector< string >::const_iterator first
260  = find ( m_labels.begin(), m_labels.end(), label );
261  bool yes = first != m_labels.end();
262 
263  return yes;
264 }
265 
266 void
268 throwIfInvalidLabel ( const std::string & label ) const
269 {
270  bool yes = isValidLabel ( label );
271  if ( yes == false ) {
272  string what ( "DataSource: `" );
273  what += label;
274  what += "' not found in this data source";
275  throw runtime_error ( what );
276  }
277 }
278 
279 void
281 throwIfInvalidRowSize ( const std:: vector < double > & row )
282 {
283  unsigned int size = columns ();
284  unsigned int cols = row.size (); // for Mac OS
285  if ( size != cols ) {
286  string what ( "DataSource: Attempt to add row of size `" );
287  what += String::convert ( cols );
288  what += "' to DataSource with `";
289  what += String::convert ( size );
290  what += "' columns";
291  throw runtime_error ( what );
292  }
293 }
294 
295 void
297 setLabels ( const std::vector < std::string > & v )
298 {
299  m_labels = v;
300 
301  notifyObservers ();
302 }
303 
304 void
306 setShape ( std::vector < unsigned int > & shape )
307 {
308  m_shape = shape;
309 }
310 
311 const vector < unsigned int > &
313 getShape () const
314 {
315  return m_shape;
316 }
317 
318 void
320 fillShape ( std::vector < int > & shape, unsigned int column ) const
321 {
322  shape.clear ();
323  shape.push_back ( rows () );
324 }
325 
326 void
328 fillShape ( std::vector < int > & v,
329  const std::string & label ) const
330 {
331  int index = indexOf ( label );
332  if ( index < 0 ) {
333  string what ( "DataSource: No column with label `" );
334  what += label;
335  what += "' exists";
336  throw runtime_error ( what );
337  }
338  fillShape ( v, index );
339 }
340 
341 
342 unsigned int
344 {
345  return m_shape.size();
346 }
347 
348 void
350 replaceColumn ( unsigned int,
351  const std::vector < double > & array )
352 {
353  string what ( "DataSource: The type of data source does not support "
354  "replacing a column." );
355  throw runtime_error ( what );
356 }
357 
358 void
360 replaceColumn ( const std::string & label,
361  const std::vector < double > & array )
362 {
363  int index = indexOf ( label );
364 
365  if ( index < 0 ) { // column doesn't exist
366  string what ( "DataSource: Attempt to replace column `" );
367  what += label;
368  what += "' which does not exist.";
369  throw runtime_error ( what );
370  }
371  else {
372  replaceColumn ( index, array );
373  }
374 }
375 
376 void
378 replaceColumn ( const std::string &,
379  const std::vector < double > &,
380  const std::vector < intptr_t > & )
381 {
382  string what ( "DataSource: This type of data source does not support"
383  " the notion of shape." );
384  throw runtime_error ( what );
385 }
386 
387 
388 int
390 addColumn ( const std::string &,
391  const std::vector < double > & )
392 {
393  string what ( "DataSource: This type of data source does not support "
394  "adding a column." );
395  throw runtime_error ( what );
396 }
397 
398 int
400 addColumn ( const std::string &,
401  const std::vector < double > &,
402  const std::vector < intptr_t > & )
403 {
404  string what ( "DataSource: This type of data source does not support"
405  " the notion of shape." );
406  throw runtime_error ( what );
407 }
408 
409 
413 bool
415 fillRange ( unsigned int column, Range & range ) const
416 {
417  assert ( column < columns () );
418  bool isValid = true;
419 
420  unsigned int size = rows ();
421  if ( size > 0 ) {
422  bool valid = false;
423  double min = DBL_MAX;
424  double max = DBL_MIN;
425  double pos = DBL_MAX;
426  for ( unsigned int i = 0; i < size; i++ ) {
427  double x = valueAt ( i, column );
428 
429  if ( x != HUGE_VAL &&
430  x != -HUGE_VAL &&
431  isnan ( x ) == false ) {
432  min = std::min ( min, x );
433  max = std::max ( max, x );
434  if ( x > 0.0 ) pos = std::min ( pos, x );
435  valid = true;
436  }
437  else {
438  isValid = false;
439  }
440  }
441  if ( valid == true ) {
442  range.setRange ( min, max, pos );
443  }
444  }
445 
446  return isValid;
447 }
448 
449 bool
451 isMultiDimensional( const std::string & ) const
452 {
453  return false;
454 }
455 
456 bool
458 isUseable( const std::string & ) const
459 {
460  return true;
461 }
462 
463 bool
465 setReleventIndex( const std::string &,
466  const std::vector< unsigned int > & )
467 {
468  // Do not call this fucntion unless implemented in base class
469  assert( 0 );
470 
471  return false;
472 }
473 
474 unsigned int
476 indexOfMaxElement ( unsigned int column ) const
477 {
478  assert ( column < columns () );
479 
480  unsigned int size = rows ();
481  unsigned int index = 0;
482  double m = valueAt ( 0, column );
483 
484  for ( unsigned int i = 1; i < size; i++ ) {
485  double v = valueAt ( i, column );
486  if ( v > m ) {
487  index = i;
488  m = v;
489  }
490  }
491 
492  return index;
493 }
494 
495 unsigned int
497 indexOfMinElement ( unsigned int column ) const
498 {
499  assert ( column < columns () );
500 
501  unsigned int size = rows ();
502  unsigned int index = 0;
503  double m = valueAt ( 0, column );
504 
505  for ( unsigned int i = 1; i < size; i++ ) {
506  double v = valueAt ( i, column );
507  if ( v < m ) {
508  index = i;
509  m = v;
510  }
511  }
512 
513  return index;
514 }
515 
516 double
518 sum ( unsigned int column ) const
519 {
520  assert ( column < columns () );
521 
522  unsigned int size = rows ();
523  double sum = 0.0;
524 
525  for ( unsigned int i = 0; i < size; i++ ) {
526  sum += valueAt ( i, column );
527  }
528 
529  return sum;
530 }
531 
532 const vector < string > &
535 {
536  return m_dups;
537 }
538 
539 double
541 valueAtNoCache ( unsigned int row, unsigned int column ) const
542 {
543  return valueAt ( row, column );
544 }
545 
546 void
548 addRow ( const std::vector < double > & row )
549 {
550  string what ( "This data source is not capable of adding rows" );
551  throw runtime_error ( what );
552 }
553 
554 void
556 eraseRow ( unsigned int index )
557 {
558  string what ("This data source is not capable of erasing rows" );
559  throw runtime_error ( what );
560 }
561 
562 
563 void
565 checkWidth ( const DataSource * source )
566 {
567  unsigned int ncolumns = source -> columns ();
568  if ( ncolumns != columns () ) {
569  string what ( "DataSource: Number of columns of source (" );
570  what += String::convert ( ncolumns );
571  what += ") not equal to current (";
572  what += String::convert ( columns() );
573  what += ").";
574  throw runtime_error ( what );
575  }
576 }
577 
578 void
580 append ( const DataSource * source )
581 {
582  checkWidth ( source ); // throws runtime_error
583 
584  unsigned int size = source -> rows ();
585  for ( unsigned int i = 0; i < size; i++ ) {
586  const vector < double > & src_row = source -> getRow ( i );
587  addRow ( src_row );
588  }
589 }
590 
591 double *
593 doubleArrayAt ( unsigned int row, unsigned int column ) const
594 {
595  string what ( "DataSource: This data source is not capable of containing\n"
596  "an array in a column." );
597  throw runtime_error ( what );
598 }
599 
600 void
602 expandIfNeeded ( const std::vector < std::string > & labels ) const
603 {
604 
605 }

Generated for HippoDraw Class Library by doxygen