00001
00012 #ifdef HAVE_CONFIG_H
00013 #include "config.h"
00014 #else
00015 #ifdef _MSC_VER
00016 #include "msdevstudio/MSconfig.h"
00017 #endif
00018 #endif
00019
00020 #include "RTuple.h"
00021
00022 #include "DataSourceException.h"
00023
00024 #include "axes/Range.h"
00025 #include "pattern/Observer.h"
00026
00027 #include <algorithm>
00028 #include <functional>
00029 #include <numeric>
00030
00031 #include <cassert>
00032
00033 #ifdef ITERATOR_MEMBER_DEFECT
00034 using namespace std;
00035 #else
00036 using std::distance;
00037 using std::string;
00038 using std::vector;
00039 #endif
00040
00041 RTuple::RTuple ( const std::vector< std::string > & labels )
00042 : DataSource ( labels )
00043 {
00044 std::size_t size = labels.size ();
00045 for ( std::size_t i = 0; i < size; i++ ) {
00046 vector< double > * vp = new vector< double > ();
00047 m_data.push_back ( vp );
00048 }
00049 }
00050
00051 RTuple::RTuple ( const RTuple & nt )
00052 : DataSource ( nt )
00053 {
00054
00055 }
00056
00057 RTuple::RTuple ( unsigned int n )
00058 : DataSource ( )
00059 {
00060 vector < string > labels;
00061 for ( unsigned int i = 0; i < n; i++ ) {
00062 labels.push_back ( string ( "nil" ) );
00063 }
00064
00065 setLabels ( labels );
00066 }
00067
00068 RTuple::~RTuple()
00069 {
00070 Observable::notifyObservers ( &hippodraw::Observer::willDelete );
00071
00072 vector< vector<double> *>::iterator it = m_data.begin();
00073 for ( ; it != m_data.end(); ++it ) {
00074 delete *it;
00075 }
00076 }
00077 void
00078 RTuple::
00079 copy ( const DataSource & other )
00080 {
00081 DataSource::copyPrivate ( other );
00082 clear ();
00083
00084 try {
00085 const RTuple & ntuple = dynamic_cast < const RTuple & > ( other );
00086 vector < vector < double > * > ::const_iterator first
00087 = ntuple.m_data.begin ();
00088 while ( first != ntuple.m_data.end () ) {
00089 vector < double > * v = new vector < double > ( **first++ );
00090 m_data.push_back ( v );
00091 }
00092 }
00093 catch ( ... ) {
00094 unsigned int size = other.rows ();
00095 for ( unsigned int i = 0; i < size; i++ ) {
00096 const vector < double > & src = other.getRow ( i );
00097 vector < double > * dst = new vector < double > ( src );
00098 m_data.push_back ( dst );
00099 }
00100 }
00101 }
00102
00103 void RTuple::clear()
00104 {
00105 vector < vector < double > * >::iterator it = m_data.begin();
00106 while ( it != m_data.end() ) {
00107 delete *it++;
00108 }
00109
00110 m_data.clear ();
00111 }
00112
00113 bool
00114 RTuple::
00115 empty () const
00116 {
00117 return m_data.empty ();
00118 }
00119
00120 unsigned int
00121 RTuple::
00122 rows () const
00123 {
00124 return m_data.size ();
00125 }
00126
00127 void
00128 RTuple::
00129 addRow ( const std::vector < double > & v )
00130 {
00131 throwIfInvalidRowSize ( v );
00132
00133 vector < double > * row = new vector < double > ( v );
00134 m_data.push_back ( row );
00135
00136 notifyObservers ();
00137 }
00138
00139 const std::vector < double > & RTuple::getRow ( unsigned int row ) const
00140 {
00141 if ( row >= m_data.size() ) {
00142 string what ( "RTuple::getRow: argument out of range" );
00143 throw DataSourceException ( what );
00144 }
00145
00146 return *m_data[row];
00147 }
00148
00149 double
00150 RTuple::
00151 operator [] ( std::vector < unsigned int > & indices ) const
00152 {
00153 unsigned int rank = getRank ();
00154 assert ( indices.size() == rank );
00155
00156
00157 if ( rank == 1 ) {
00158 unsigned int size = columns ();
00159 unsigned int row = indices[0] / size;
00160 unsigned int col = indices[0] % size;
00161 const vector < double > & rowvec = *m_data[row];
00162 return rowvec[col];
00163 }
00164
00165 if ( rank == 2 ) {
00166 unsigned int col = indices[1];
00167 unsigned int row = indices[0];
00168 const vector < double > & rowvec = *m_data[row];
00169 return rowvec[col];
00170 }
00171
00172 if ( rank == 3 ) {
00173 unsigned int size = columns ();
00174 unsigned int col = indices[2];
00175 unsigned int j = indices[1];
00176 unsigned int i = indices[0];
00177
00178 assert ( col < size );
00179 assert ( j < m_shape[1] );
00180 assert ( i < m_shape[0] );
00181
00182 unsigned int row = j + i * m_shape[1];
00183 const vector < double > & rowvec = *m_data[row];
00184 return rowvec[col];
00185 }
00186 return 0.0;
00187 }
00188
00189 double
00190 RTuple::
00191 valueAt ( unsigned int row, unsigned int column ) const
00192 {
00193 return (*m_data[row])[column];
00194 }
00195
00196 void
00197 RTuple::
00198 reserve ( unsigned int count )
00199 {
00200 m_data.reserve ( count );
00201 }