File: VectorPy.hpp

package info (click to toggle)
esys-particle 2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,284 kB
  • sloc: cpp: 77,304; python: 5,647; makefile: 1,176; sh: 10
file content (156 lines) | stat: -rw-r--r-- 4,243 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/////////////////////////////////////////////////////////////
//                                                         //
// Copyright (c) 2003-2011 by The University of Queensland //
// Earth Systems Science Computational Centre (ESSCC)      //
// http://www.uq.edu.au/esscc                              //
//                                                         //
// Primary Business: Brisbane, Queensland, Australia       //
// Licensed under the Open Software License version 3.0    //
// http://www.opensource.org/licenses/osl-3.0.php          //
//                                                         //
/////////////////////////////////////////////////////////////

#include "Python/BoostPythonUtil/PythonIterIterator.h"
#include <algorithm>

namespace esys
{
  namespace lsm
  {
    template <typename TE>
    VectorPy<TE>::VectorPy() : Inherited()
    {
    }

    template <typename TE>
    VectorPy<TE>::VectorPy(const VectorPy &vec) : Inherited(vec)
    {
    }

    template <typename TE>
    VectorPy<TE>::VectorPy(const Inherited &vec) : Inherited(vec)
    {
    }

    template <typename TE>
    VectorPy<TE>::VectorPy(boost::python::object &iterable)
      : Inherited()
    {
      bpu::PythonIterIterator<TE> it(iterable);
      while (it.hasNext())
      {
        this->push_back(it.next());
      }
    }

    template <typename TE>
    void VectorPy<TE>::append(const_reference e)
    {
      this->push_back(e);
    }

    template <typename TE>
    size_t VectorPy<TE>::getIndex(int i) const
    {
      if (i < 0)
      {
          i += this->size();
      }
      if ((i >= static_cast<int>(this->size())) || (i < 0))
      {
        PyErr_SetString(PyExc_IndexError, "Index out of range");
        boost::python::throw_error_already_set();
      }
      return static_cast<size_t>(i);
    }

    template <typename TE>
    typename VectorPy<TE>::reference VectorPy<TE>::getItem(int i)
    {
      return (*this)[getIndex(i)];
    }

    template <typename TE>
    void VectorPy<TE>::setItem(int i, const_reference e)
    {
      (*this)[getIndex(i)] = e;
    }

    template <typename TE>
    boost::python::tuple
    VectorPy<TE>::PickleSuite::getinitargs(VectorPy const& v)
    {
      return boost::python::make_tuple(boost::python::list(v));
    }

    template <typename TE>
    boost::python::class_<VectorPy<TE> >
    VectorPy<TE>::exportVector(
      const std::string &pyClassName,
      const std::string &pyClassDocString
    )
    {
      return
        boost::python::class_<VectorPy<TE> >(
          pyClassName.c_str(),
          pyClassDocString.c_str(),
          boost::python::init<>()
        )
        .def(
          boost::python::init<const VectorPy<TE> &>(
            (boost::python::arg("iterable"))
          )
        )
        .def(
          boost::python::init<boost::python::object &>(
            (boost::python::arg("iterable")),
            (
              std::string() + 
              "Constructs vec of elements from specifed sequence.\n" +
              "@type iterable: iterable\n" +
              "@kwarg iterable: copy and insert elements from C{iterable}" +
              " into this vec.\n"
            ).c_str()
          )
        )
        .def(
          "__iter__",
          boost::python::iterator<
            VectorPy,
            boost::python::return_internal_reference<>
          >()
        )
        .def(
          "__len__",
          &VectorPy<TE>::size
        )
        .def(
          "__getitem__",
          &VectorPy<TE>::getItem,
          boost::python::return_internal_reference<>()
        )
        .def(
          "__setitem__",
          &VectorPy<TE>::setItem
        )
        .def(
          "append",
          &VectorPy<TE>::append,
          (boost::python::arg("elem")),
          "Appends an element to the end of this vector.\n"
          "@type elem: object\n"
          "@kwarg elem: Copy this element as the new last element."
        )
        .def(
          "clear",
          &VectorPy<TE>::clear,
          "Removes all elements from this vector."
        )
        .def(
          boost::python::self == boost::python::self
        )
        .def_pickle(typename VectorPy::PickleSuite())
        ;
    }
  }
}