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 }
virtual const std::vector< double > & getRow(unsigned int index) const
Returns a vector of data elements in one row.
Definition: RTuple.cxx:137
unsigned int i
virtual void clear()
Clears the RTuple.
Definition: RTuple.cxx:101
virtual void reserve(unsigned int count)
For each column, reserves enough space for the data source to grow to count rows. ...
Definition: RTuple.cxx:196
virtual void notifyObservers() const
Notifies Observer objects of a change.
Definition: Observable.cxx:93
virtual double valueAt(unsigned int row, unsigned int column) const
Returns the value in the table in position given by the row and column indexes.
Definition: RTuple.cxx:189
RTuple(const RTuple &nt)
The copy constructor creates an ntuple that is a copy of an existing one.
Definition: RTuple.cxx:49
void throwIfInvalidRowSize(const std::vector< double > &row)
Throws a DataSourceException if the size of the row is not equal to the number of columns...
Definition: DataSource.cxx:281
virtual ~RTuple()
The destructor.
Definition: RTuple.cxx:66
column
The column indices for 2 dimension data point tuple.
virtual void willDelete(const Observable *)
Notifies this Observer object that one of its Observable objects is about to be deleted.
Definition: Observer.cxx:25
std::vector< unsigned int > m_shape
The shape of the data.
Definition: DataSource.h:96
RTuple class interface.
unsigned int getRank() const
Returns the rank of the data source.
Definition: DataSource.cxx:343
virtual unsigned int rows() const
Returns the number of rows.
Definition: RTuple.cxx:120
virtual void copyPrivate(const DataSource &other)
Copies the contents of the other DataSource.
Definition: DataSource.cxx:114
virtual void addRow(const std::vector< double > &v)
Adds a row to the end of the ntuple.
Definition: RTuple.cxx:127
A DataSource class implemented with std::vector&lt;double&gt; to store the row data.
Definition: RTuple.h:30
ViewBase * v
Definition: PlotTable.cxx:104
intp size(numeric::array arr)
Definition: num_util.cpp:296
int rank(numeric::array arr)
Definition: num_util.cpp:271
virtual void copy(const DataSource &other)
Definition: RTuple.cxx:77
std::vector< std::vector< double > * > m_data
The data rows of the ntuple table.
Definition: RTuple.h:39
virtual unsigned int rows() const =0
Returns the number of rows.
hippodraw::Range class interface
virtual double operator[](std::vector< unsigned int > &indices) const
Definition: RTuple.cxx:149
unsigned int columns() const
Returns the number of columns or data arrays available from this DataSource.
Definition: DataSource.h:458
virtual void setLabels(const std::vector< std::string > &v)
Assigns the label to each column from the vector of strings @ v.
Definition: DataSource.cxx:297
virtual const std::vector< double > & getRow(unsigned int) const =0
Returns a const reference to slice along the axis known as a row.
list< QAction * >::iterator it
virtual bool empty() const
Returns true, if RTuple is empty, i.e.
Definition: RTuple.cxx:113
Observer class interface.
Base class for DataSource.
Definition: DataSource.h:55

Generated for HippoDraw Class Library by doxygen