Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ListTuple.cxx

Go to the documentation of this file.
00001 
00012 #include "ListTuple.h"
00013 
00014 #include "axes/Range.h"
00015 #include "datasrcs/DataSourceException.h"
00016 
00017 using namespace boost::python;
00018 
00019 using std::string;
00020 using std::vector;
00021 
00022 namespace
00023 {
00024   unsigned int getSize ( const boost::python::list & sequence )
00025   {
00026     object obj = sequence.attr ( "__len__" ) ();
00027 
00028     return extract < unsigned int > ( obj );
00029   }
00030 }
00031 
00032 ListTuple::ListTuple ()
00033   : DataSource ()
00034 {
00035 }
00036 
00037 ListTuple::~ListTuple()
00038 {
00039 }
00040 
00041 void
00042 ListTuple::
00043 copy ( const DataSource & )
00044 {
00045   assert ( false );
00046 }
00047 
00048 void
00049 ListTuple::
00050 notifyObservers ( ) const
00051 {
00052   Observable::notifyObservers ();
00053 }
00054 
00055 unsigned int
00056 ListTuple::
00057 rows() const
00058 {
00059   unsigned int size = 0;
00060   if ( m_data.empty () == false ) {
00061     const boost::python::list & seq = m_data[0];
00062 
00063     size = getSize ( seq );
00064   }
00065 
00066   return size;
00067 }
00068 
00069 bool
00070 ListTuple::
00071 empty () const
00072 {
00073   return rows () == 0;
00074 }
00075 
00076 double
00077 ListTuple::
00078 valueAt( unsigned int row, unsigned int column ) const
00079 {
00080   assert ( column < m_data.size () );
00081 
00082   const boost::python::list & seq = m_data[column];
00083 
00084   unsigned int size = getSize ( seq );
00085 
00086   assert ( row < size );
00087 
00088   object result = seq[row];
00089   double value = extract < double > ( result );
00090 
00091   return value;
00092 }
00093 
00097 const std::vector < double > &
00098 ListTuple::
00099 getRow ( unsigned int row ) const
00100 {
00101   unsigned int size = m_data.size();
00102   m_row.resize ( size );
00103   for ( unsigned int column = 0; column < size; column++ ) {
00104     m_row [ column ] = valueAt ( row, column );
00105   }
00106 
00107   return m_row;
00108 }
00109 
00110 bool
00111 ListTuple::
00112 isAcceptable ( const  boost::python::list & array )
00113 {
00114   bool yes = true;
00115   unsigned int size = getSize ( array );
00116   for ( unsigned int i = 0; i < size; i++ ) {
00117     object obj = array[i];
00118     extract < double > check ( obj );
00119     if ( check.check() == false ) {
00120       yes = false;
00121       break;
00122     }
00123   }
00124   return yes;
00125 }
00126 
00129 int
00130 ListTuple::
00131 addColumn ( const std::string & label,
00132             boost::python::list array )
00133 {
00134   // Check if label already exists.
00135   int index = indexOf ( label );
00136   if ( index >= 0 ) {
00137     string what ( "ListTuple Attempt to add a column whose label"
00138                   " is same as other column." );
00139     throw DataSourceException ( what );
00140   }
00141 
00142   unsigned int new_size = getSize ( array );
00143   // Check if column has right size.
00144   if ( m_data.empty () == false ) {
00145     unsigned int old_size = rows ();
00146 
00147     if ( old_size != 0 && old_size != new_size ) {
00148       string what ( "ListTuple Attempt to add a column whose size"
00149                     " is not equal to other columns." );
00150       throw DataSourceException ( what );
00151     }
00152   }
00153   if ( isAcceptable ( array ) == false ) {
00154     string what ( "ListTuple: Attempt to add a column with one or more"
00155                   " elements not convertable to float" );
00156     throw DataSourceException ( what );
00157   }
00158 
00159   m_data.push_back ( array );
00160   addLabel ( label );
00161 
00162   return m_data.size() - 1;
00163 }
00164 
00165 void
00166 ListTuple::
00167 replaceColumn ( unsigned int col, 
00168                 boost::python::list array )
00169 {
00170   unsigned int size = columns ();
00171   if ( col >= size ) {
00172     const string what ( "NunArrayTuple: column doesn't exist" );
00173     throw DataSourceException ( what );
00174   }
00175 
00176   const boost::python::list & old_array = m_data[col];
00177   int old_size = getSize ( old_array );
00178   int new_size = getSize ( array );
00179 
00180   if ( old_size != 0 && old_size != new_size ) {
00181     const string what ( "ListTuple: Attempt to replace column with one "
00182                         "whose size is not equal to other columns." );
00183     throw DataSourceException ( what );
00184   }
00185   m_data[col] = array;
00186 
00187   notifyObservers ();
00188 }
00189 
00190 void
00191 ListTuple::
00192 replaceColumn ( const std::string & column,
00193                 boost::python::list array )
00194 {
00195   unsigned int index = indexOf ( column );
00196 
00197   replaceColumn ( index, array );
00198 }
00199 
00200 void
00201 ListTuple::
00202 setShape ( std::vector < unsigned int > & shape )
00203 {
00204   unsigned int size = 1;
00205   for ( unsigned int i = 0; i < shape.size(); i++ ) {
00206     size *= shape[i];
00207   }
00208 
00209   m_shape = shape;
00210 }
00211 
00212 const vector < unsigned int > &
00213 ListTuple::
00214 getShape () const
00215 {
00216   return m_shape;
00217 }
00218 
00219 unsigned int
00220 ListTuple::getRank () const
00221 {
00222   return m_shape.size();
00223 }
00224 
00225 void
00226 ListTuple::
00227 clear ()
00228 {
00229   assert ( false );
00230 }
00231 
00232 void
00233 ListTuple::
00234 reserve ( unsigned int count )
00235 {
00236   assert ( false );
00237 }
00238 
00239 void
00240 ListTuple::
00241 addRow ( const std::vector < double > & row )
00242 {
00243   assert ( false );
00244 }
00245 
00246 double 
00247 ListTuple::
00248 operator [] (  std::vector < unsigned int > & indices ) const
00249 {
00250   assert ( false );
00251   return 0.0;
00252 }

Generated for HippoDraw-1.14.8.5 by doxygen 1.4.3