RTuple.cxx
Go to the documentation of this file.
1 
12 #ifdef _MSC_VER
13 #include "msdevstudio/MSconfig.h"
14 #endif
15 
16 #include "RTuple.h"
17 
18 #include "axes/Range.h"
19 #include "pattern/Observer.h"
20 
21 #include <algorithm>
22 #include <functional>
23 #include <numeric>
24 #include <stdexcept>
25 
26 #include <cassert>
27 
28 #ifdef ITERATOR_MEMBER_DEFECT
29 using namespace std;
30 #else
31 using std::distance;
32 using std::runtime_error;
33 using std::string;
34 using std::vector;
35 #endif
36 
37 using namespace hippodraw;
38 
39 RTuple::RTuple ( const std::vector< std::string > & labels )
40  : DataSource ( labels )
41 {
42  std::size_t size = labels.size ();
43  for ( std::size_t i = 0; i < size; i++ ) {
44  vector< double > * vp = new vector< double > ();
45  m_data.push_back ( vp );
46  }
47 }
48 
49 RTuple::RTuple ( const RTuple & nt )
50  : DataSource ( nt )
51 {
52  // do nothing, is private
53 }
54 
55 RTuple::RTuple ( unsigned int n )
56  : DataSource ( )
57 {
58  vector < string > labels;
59  for ( unsigned int i = 0; i < n; i++ ) {
60  labels.push_back ( string ( "nil" ) );
61  }
62 
63  setLabels ( labels );
64 }
65 
67 {
69 
70  vector< vector<double> *>::iterator it = m_data.begin();
71  for ( ; it != m_data.end(); ++it ) {
72  delete *it;
73  }
74 }
75 void
77 copy ( const DataSource & other )
78 {
79  DataSource::copyPrivate ( other );
80  clear ();
81 
82  try {
83  const RTuple & ntuple = dynamic_cast < const RTuple & > ( other );
84  vector < vector < double > * > ::const_iterator first
85  = ntuple.m_data.begin ();
86  while ( first != ntuple.m_data.end () ) {
87  vector < double > * v = new vector < double > ( **first++ );
88  m_data.push_back ( v );
89  }
90  }
91  catch ( ... ) {
92  unsigned int size = other.rows ();
93  for ( unsigned int i = 0; i < size; i++ ) {
94  const vector < double > & src = other.getRow ( i );
95  vector < double > * dst = new vector < double > ( src );
96  m_data.push_back ( dst );
97  }
98  }
99 }
100 
102 {
103  vector < vector < double > * >::iterator it = m_data.begin();
104  while ( it != m_data.end() ) {
105  delete *it++;
106  }
107 
108  m_data.clear ();
109 }
110 
111 bool
112 RTuple::
113 empty () const
114 {
115  return m_data.empty ();
116 }
117 
118 unsigned int
119 RTuple::
120 rows () const
121 {
122  return m_data.size ();
123 }
124 
125 void
126 RTuple::
127 addRow ( const std::vector < double > & v )
128 {
129  throwIfInvalidRowSize ( v );
130 
131  vector < double > * row = new vector < double > ( v );
132  m_data.push_back ( row );
133 
134 // notifyObservers ();
135 }
136 
137 const std::vector < double > & RTuple::getRow ( unsigned int row ) const
138 {
139  if ( row >= m_data.size() ) {
140  string what ( "RTuple::getRow: argument out of range" );
141  throw runtime_error ( what );
142  }
143 
144  return *m_data[row];
145 }
146 
147 double
148 RTuple::
149 operator [] ( std::vector < unsigned int > & indices ) const
150 {
151  unsigned int rank = getRank ();
152  assert ( indices.size() == rank );
153 
154 
155 if ( rank == 1 ) {
156  unsigned int size = columns ();
157  unsigned int row = indices[0] / size;
158  unsigned int col = indices[0] % size;
159  const vector < double > & rowvec = *m_data[row];
160  return rowvec[col];
161  }
162 
163  if ( rank == 2 ) {
164  unsigned int col = indices[1];
165  unsigned int row = indices[0];
166  const vector < double > & rowvec = *m_data[row];
167  return rowvec[col];
168  }
169 
170  if ( rank == 3 ) {
171  unsigned int size = columns ();
172  unsigned int col = indices[2];
173  unsigned int j = indices[1];
174  unsigned int i = indices[0];
175 
176  assert ( col < size );
177  assert ( j < m_shape[1] );
178  assert ( i < m_shape[0] );
179 
180  unsigned int row = j + i * m_shape[1];
181  const vector < double > & rowvec = *m_data[row];
182  return rowvec[col];
183  }
184  return 0.0;
185 }
186 
187 double
188 RTuple::
189 valueAt ( unsigned int row, unsigned int column ) const
190 {
191  return (*m_data[row])[column];
192 }
193 
194 void
195 RTuple::
196 reserve ( unsigned int count )
197 {
198  m_data.reserve ( count );
199 }

Generated for HippoDraw Class Library by doxygen