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