File: SetPy.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 (184 lines) | stat: -rw-r--r-- 5,468 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/////////////////////////////////////////////////////////////
//                                                         //
// 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, typename TC>
    SetPy<TE,TC>::SetPy() : Inherited()
    {
    }

    template <typename TE, typename TC>
    SetPy<TE,TC>::SetPy(const SetPy &set) : Inherited(set)
    {
    }

    template <typename TE, typename TC>
    SetPy<TE,TC>::SetPy(const Inherited &set) : Inherited(set)
    {
    }

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

    template <typename TE, typename TC>
    SetPy<TE,TC> SetPy<TE,TC>::getUnion(const SetPy &set) const
    {
      SetPy resultSet;
      std::set_union(
        this->begin(),
        this->end(),
        set.begin(),
        set.end(),
        InsertIterator(resultSet, resultSet.begin())
      );
      return resultSet;
    }

    template <typename TE, typename TC>
    SetPy<TE,TC> SetPy<TE,TC>::getDifference(const SetPy &set) const
    {
      SetPy resultSet;
      std::set_difference(
        this->begin(),
        this->end(),
        set.begin(),
        set.end(),
        InsertIterator(resultSet, resultSet.begin())
      );
      return resultSet;   
    }

    template <typename TE, typename TC>
    SetPy<TE,TC> SetPy<TE,TC>::getIntersection(const SetPy &set) const
    {
      SetPy resultSet;
      std::set_intersection(
        this->begin(),
        this->end(),
        set.begin(),
        set.end(),
        InsertIterator(resultSet, resultSet.begin())
      );
      return resultSet;
    }

    template <typename TE, typename TC>
    boost::python::tuple
    SetPy<TE,TC>::PickleSuite::getinitargs(SetPy const& s)
    {
      return boost::python::make_tuple(boost::python::list(s));
    }

    template <typename TE, typename TC>
    boost::python::class_<SetPy<TE,TC> >
    SetPy<TE,TC>::exportSet(
      const std::string &pyClassName,
      const std::string &pyClassDocString
    )
    {
      return
        boost::python::class_<SetPy<TE,TC> >(
          pyClassName.c_str(),
          pyClassDocString.c_str(),
          boost::python::init<>()
        )
        .def(
          boost::python::init<const SetPy<TE,TC> &>(
            (boost::python::arg("iterable"))
          )
        )
        .def(
          boost::python::init<boost::python::object &>(
            (boost::python::arg("iterable")),
            (
              std::string() + 
              "Constructs set of elements from specifed sequence.\n" +
              "@type iterable: iterable\n" +
              "@kwarg iterable: copy and insert elements from C{iterable}" +
              " into this set.\n"
            ).c_str()
          )
        )
        .def(
          "union",
          &SetPy::getUnion,
          (boost::python::arg("set")),
          (
            std::string() +
            "Returns new set which is the union of this set and C{set}.\n" +
            "@type set: " + pyClassName + "\n" +
            "@kwarg set: Set with which union is performed.\n" +
            "@rtype: " + pyClassName + "\n"
            "@return: new set which is the union of two sets."
          ).c_str()
        )
        .def(
          "difference",
          &SetPy::getDifference,
          (boost::python::arg("set")),
          (
            std::string() +
            "Returns new set which contains elements of this set not " + 
            " contained in the specified C{set}.\n" +
            "@type set: " + pyClassName + "\n" +
            "@kwarg set: Set with which difference is performed.\n" +
            "@rtype: " + pyClassName + "\n"
            "@return: new set which is the difference of two sets."
          ).c_str()
        )
        .def(
          "intersection",
          &SetPy::getIntersection,
          (boost::python::arg("set")),
          (
            std::string() +
            "Returns new set which contains elements common to this set " +
            " and the specified C{set}.\n" +
            "@type set: " + pyClassName + "\n" +
            "@kwarg set: Set with which intersection is performed.\n" +
            "@rtype: " + pyClassName + "\n"
            "@return: new set which is the intersection of two sets."
          ).c_str()
        )
        .def(
          "__iter__",
          boost::python::iterator<
            SetPy,
            boost::python::return_internal_reference<>
          >()
        )
        .def(
          "__len__",
          &SetPy::size
        )
        .def(
          boost::python::self == boost::python::self
        )
        .def_pickle(typename SetPy::PickleSuite())
        ;
    }
  }
}