ListTuple.cxx
Go to the documentation of this file.
1 
12 // include first to avoid _POSIX_C_SOURCE warning.
13 #include <boost/python.hpp>
14 
15 #ifdef HAVE_CONFIG_H
16 // for have smp and qt4
17 #include "config.h"
18 #endif
19 
20 #include "ListTuple.h"
21 
22 #include "axes/Range.h"
23 
24 #include <stdexcept>
25 
26 using namespace boost::python;
27 
28 using std::runtime_error;
29 using std::string;
30 using std::vector;
31 
32 // namespace
33 // {
34 // /** @note For multi-processor machines, need the to get the the
35 // Python global interpreter lock when compiled with Qt 4 on Mac OS
36 // X and maybe other systems. However, with Qt 3, obtaining the
37 // lock seems to lock up the system.
38 
39 // The implementation of this
40 // member function is taken from
41 // http://docs.python.org/api.thread.html
42 // */
43 // unsigned int getSize ( const boost::python::list & sequence )
44 // {
45 // #ifdef HAVE_QT4
46 // #ifdef HAVE_SMP
47 // PyGILState_STATE gstate;
48 // gstate = PyGILState_Ensure ();
49 // #endif
50 // #endif
51 
52 // object obj = sequence.attr ( "__len__" ) ();
53 // unsigned int size = extract < unsigned int > ( obj );
54 
55 // #ifdef HAVE_QT4
56 // #ifdef HAVE_SMP
57 // PyGILState_Release ( gstate );
58 // #endif
59 // #endif
60 
61 // return size;
62 // }
63 // }
64 
65 using namespace hippodraw;
66 
67 ListTuple::ListTuple ()
68  : DataSource ()
69 {
70 }
71 
73 {
74 }
75 
76 unsigned int
78 getSize ( const boost::python::list & sequence )
79 {
80 #ifdef HAVE_QT4
81 #ifdef HAVE_SMP
82  PyGILState_STATE gstate;
83  gstate = PyGILState_Ensure ();
84 #endif
85 #endif
86 
87  object obj = sequence.attr ( "__len__" ) ();
88  unsigned int size = extract < unsigned int > ( obj );
89 
90 #ifdef HAVE_QT4
91 #ifdef HAVE_SMP
92  PyGILState_Release ( gstate );
93 #endif
94 #endif
95 
96  return size;
97 }
98 
99 void
101 copy ( const DataSource & )
102 {
103  assert ( false );
104 }
105 
106 void
109 {
111 }
112 
113 unsigned int
115 rows() const
116 {
117  unsigned int size = 0;
118  if ( m_data.empty () == false ) {
119  const boost::python::list & seq = m_data[0];
120 
121  size = getSize ( seq );
122  }
123 
124  return size;
125 }
126 
127 bool
129 empty () const
130 {
131  return rows () == 0;
132 }
133 
134 double
136 valueAt( unsigned int row, unsigned int column ) const
137 {
138  assert ( column < m_data.size () );
139  double value = 0;
140 
141  const boost::python::list & seq = m_data[column];
142 
143  unsigned int size = getSize ( seq );
144 
145  assert ( row < size );
146 
147  object result = seq[row];
148  value = extract < double > ( result );
149 
150  return value;
151 }
152 
156 const std::vector < double > &
158 getRow ( unsigned int row ) const
159 {
160  unsigned int size = m_data.size();
161  m_row.resize ( size );
162  for ( unsigned int column = 0; column < size; column++ ) {
163  m_row [ column ] = valueAt ( row, column );
164  }
165 
166  return m_row;
167 }
168 
169 bool
172 {
173  bool yes = true;
174  unsigned int size = getSize ( array );
175  for ( unsigned int i = 0; i < size; i++ ) {
176  object obj = array[i];
177  extract < double > check ( obj );
178  if ( check.check() == false ) {
179  yes = false;
180  break;
181  }
182  }
183  return yes;
184 }
185 
188 int
190 addColumn ( const std::string & label,
191  boost::python::list array )
192 {
193  // Check if label already exists.
194  int index = indexOf ( label );
195  if ( index >= 0 ) {
196  string what ( "ListTuple Attempt to add a column whose label"
197  " is same as other column." );
198  throw runtime_error ( what );
199  }
200 
201  unsigned int new_size = getSize ( array );
202  // Check if column has right size.
203  if ( m_data.empty () == false ) {
204  unsigned int old_size = rows ();
205 
206  if ( old_size != 0 && old_size != new_size ) {
207  string what ( "ListTuple Attempt to add a column whose size"
208  " is not equal to other columns." );
209  throw runtime_error ( what );
210  }
211  }
212  if ( isAcceptable ( array ) == false ) {
213  string what ( "ListTuple: Attempt to add a column with one or more"
214  " elements not convertable to float" );
215  throw runtime_error ( what );
216  }
217 
218  m_data.push_back ( array );
219  addLabel ( label );
220 
221  return m_data.size() - 1;
222 }
223 
224 void
226 replaceColumn ( unsigned int col,
227  boost::python::list array )
228 {
229  unsigned int size = columns ();
230  if ( col >= size ) {
231  const string what ( "NunArrayTuple: column doesn't exist" );
232  throw runtime_error ( what );
233  }
234 
235  const boost::python::list & old_array = m_data[col];
236  int old_size = getSize ( old_array );
237  int new_size = getSize ( array );
238 
239  if ( old_size != 0 && old_size != new_size ) {
240  const string what ( "ListTuple: Attempt to replace column with one "
241  "whose size is not equal to other columns." );
242  throw runtime_error ( what );
243  }
244  m_data[col] = array;
245 
246  notifyObservers ();
247 }
248 
249 void
251 replaceColumn ( const std::string & column,
252  boost::python::list array )
253 {
254  unsigned int index = indexOf ( column );
255 
256  replaceColumn ( index, array );
257 }
258 
259 void
261 setShape ( std::vector < unsigned int > & shape )
262 {
263  m_shape = shape;
264 }
265 
266 const vector < unsigned int > &
268 getShape () const
269 {
270  return m_shape;
271 }
272 void
275 {
276  assert ( false );
277 }
278 
279 void
281 reserve ( unsigned int )
282 {
283  assert ( false );
284 }
285 
286 double
288 operator [] ( std::vector < unsigned int > & /* indices */ ) const
289 {
290  assert ( false );
291  return 0.0;
292 }

Generated for HippoDraw Class Library by doxygen