ESyS-Particle  2.3.4
VectorPy.hpp
Go to the documentation of this file.
1 // //
3 // Copyright (c) 2003-2017 by The University of Queensland //
4 // Centre for Geoscience Computing //
5 // http://earth.uq.edu.au/centre-geoscience-computing //
6 // //
7 // Primary Business: Brisbane, Queensland, Australia //
8 // Licensed under the Open Software License version 3.0 //
9 // http://www.apache.org/licenses/LICENSE-2.0 //
10 // //
12 
14 #include <algorithm>
15 
16 namespace esys
17 {
18  namespace lsm
19  {
20  template <typename TE>
22  {
23  }
24 
25  template <typename TE>
27  {
28  }
29 
30  template <typename TE>
32  {
33  }
34 
35  template <typename TE>
36  VectorPy<TE>::VectorPy(boost::python::object &iterable)
37  : Inherited()
38  {
39  bpu::PythonIterIterator<TE> it(iterable);
40  while (it.hasNext())
41  {
42  this->push_back(it.next());
43  }
44  }
45 
46  template <typename TE>
48  {
49  this->push_back(e);
50  }
51 
52  template <typename TE>
53  size_t VectorPy<TE>::getIndex(int i) const
54  {
55  if (i < 0)
56  {
57  i += this->size();
58  }
59  if ((i >= static_cast<int>(this->size())) || (i < 0))
60  {
61  PyErr_SetString(PyExc_IndexError, "Index out of range");
62  boost::python::throw_error_already_set();
63  }
64  return static_cast<size_t>(i);
65  }
66 
67  template <typename TE>
69  {
70  return (*this)[getIndex(i)];
71  }
72 
73  template <typename TE>
75  {
76  (*this)[getIndex(i)] = e;
77  }
78 
79  template <typename TE>
80  boost::python::tuple
82  {
83  return boost::python::make_tuple(boost::python::list(v));
84  }
85 
86  template <typename TE>
87  boost::python::class_<VectorPy<TE> >
89  const std::string &pyClassName,
90  const std::string &pyClassDocString
91  )
92  {
93  return
94  boost::python::class_<VectorPy<TE> >(
95  pyClassName.c_str(),
96  pyClassDocString.c_str(),
97  boost::python::init<>()
98  )
99  .def(
100  boost::python::init<const VectorPy<TE> &>(
101  (boost::python::arg("iterable"))
102  )
103  )
104  .def(
105  boost::python::init<boost::python::object &>(
106  (boost::python::arg("iterable")),
107  (
108  std::string() +
109  "Constructs vec of elements from specifed sequence.\n" +
110  "@type iterable: iterable\n" +
111  "@kwarg iterable: copy and insert elements from C{iterable}" +
112  " into this vec.\n"
113  ).c_str()
114  )
115  )
116  .def(
117  "__iter__",
118  boost::python::iterator<
119  VectorPy,
120  boost::python::return_internal_reference<>
121  >()
122  )
123  .def(
124  "__len__",
126  )
127  .def(
128  "__getitem__",
130  boost::python::return_internal_reference<>()
131  )
132  .def(
133  "__setitem__",
135  )
136  .def(
137  "append",
139  (boost::python::arg("elem")),
140  "Appends an element to the end of this vector.\n"
141  "@type elem: object\n"
142  "@kwarg elem: Copy this element as the new last element."
143  )
144  .def(
145  "clear",
147  "Removes all elements from this vector."
148  )
149  .def(
150  boost::python::self == boost::python::self
151  )
152  .def_pickle(typename VectorPy::PickleSuite())
153  ;
154  }
155  }
156 }
esys::lsm::bpu::PythonIterIterator::hasNext
bool hasNext() const
Definition: PythonIterIterator.hpp:32
esys::lsm::VectorPy
Definition: VectorPy.h:26
esys::lsm::VectorPy::const_reference
Inherited::const_reference const_reference
Definition: VectorPy.h:29
esys::lsm::VectorPy::Inherited
std::vector< TmplElem > Inherited
Definition: VectorPy.h:28
esys::lsm::VectorPy::setItem
void setItem(int i, const_reference e)
Definition: VectorPy.hpp:74
PythonIterIterator.h
esys
Definition: CheckPointable.cpp:17
esys::lsm::VectorPy::reference
Inherited::reference reference
Definition: VectorPy.h:30
esys::lsm::VectorPy::exportVector
static boost::python::class_< VectorPy > exportVector(const std::string &pyClassName, const std::string &pyClassDocString)
Definition: VectorPy.hpp:88
esys::lsm::VectorPy::VectorPy
VectorPy()
Definition: VectorPy.hpp:21
esys::lsm::VectorPy::append
void append(const_reference e)
Definition: VectorPy.hpp:47
esys::lsm::VectorPy::PickleSuite
Definition: VectorPy.h:33
esys::lsm::bpu::PythonIterIterator
Definition: PythonIterIterator.h:28
esys::lsm::VectorPy::getItem
reference getItem(int i)
Definition: VectorPy.hpp:68
esys::lsm::bpu::PythonIterIterator::next
value_type next()
Definition: PythonIterIterator.hpp:38
esys::lsm::VectorPy::PickleSuite::getinitargs
static boost::python::tuple getinitargs(VectorPy const &v)
Definition: VectorPy.hpp:81
esys::lsm::VectorPy::getIndex
size_t getIndex(int i) const
Definition: VectorPy.hpp:53