IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit
PythonStdSetUtils.h
Go to the documentation of this file.
1 // PythonStdSetUtils.h:
3 // --------------------
4 //
14 
15 #ifndef __PYIPSDKBASE_PYTHONSTDSETUTILS_H__
16 #define __PYIPSDKBASE_PYTHONSTDSETUTILS_H__
17 
18 #include <set>
19 #include <boost/python/iterator.hpp>
20 #include <boost/python/self.hpp>
21 #include <boost/python/operators.hpp>
22 #include <boost/utility/enable_if.hpp>
23 #include <boost/type_traits/is_base_of.hpp>
24 
25 namespace ipsdk {
26 namespace python {
27 
30 
31 // std::set to py set wrapper
32 template<class KeyType>
33 class py_set : public std::set<KeyType>, public boost::python::wrapper<std::set<KeyType> >
34 {
35 public:
36  // some typedefs for convinience
37  typedef std::set<KeyType> source_type;
38  typedef py_set<KeyType> wrap_type;
39 
40  // constructors
41  py_set<KeyType>() {
42  }
43  py_set<KeyType>(const source_type& s) {
44  this->insert(s.begin(), s.end());
45  }
46 
47  // element access
48  bool contains(const KeyType key) {
49  return this->count(key)>0;
50  }
51 
52  // we must define add() for it gets explicit argument types
53  bool add(const KeyType key) {
54  return this->insert(key).second;
55  }
56 
57  bool remove(const KeyType key) {
58  if (!this->contains(key))
59  return false;
60  this->erase(key);
61  return true;
62  }
63 
64  // set operations
65  source_type set_union(wrap_type &other) {
66  source_type result;
67  std::set_union(this->begin(), this->end(), other.begin(), other.end(),
68  inserter(result, result.begin()));
69  return result;
70  }
71 
72  source_type set_intersection(wrap_type &other) {
73  source_type result;
74  std::set_intersection(this->begin(), this->end(), other.begin(), other.end(),
75  inserter(result, result.begin()));
76  return result;
77  }
78 
79  source_type set_difference(wrap_type &other) {
80  source_type result;
81  std::set_difference(this->begin(), this->end(), other.begin(), other.end(),
82  inserter(result, result.begin()));
83  return result;
84  }
85 
86  source_type set_symmetric_difference(wrap_type &other) {
87  source_type result;
88  std::set_symmetric_difference(this->begin(), this->end(), other.begin(), other.end(),
89  inserter(result, result.begin()));
90  return result;
91  }
92 
93  // create string associated to object
94  static std::string to_string(const std::set<KeyType>& cl)
95  {
96  typedef std::set<KeyType> Container;
97  std::stringstream ss;
98  ss << "[";
99  bool first = true;
100  for (typename Container::const_iterator
101  i = cl.begin(), e = cl.end(); i != e; ++i) {
102  if (first) {
103  first = false;
104  }
105  else {
106  ss << ", ";
107  }
108  const KeyType& curKey = *i;
109  ss << curKey;
110  }
111  ss << "]";
112  return ss.str();
113  }
114 };
115 
116 inline void block_hashing(boost::python::object) {
117  // do something more intelligent here
118  throw "objects of this type are unhashable";
119 }
120 
121 // export mutable set
122 template<class KeyType> void
123 export_set(const char* py_name) {
124 
125  typedef py_set<KeyType> set_T;
126 
127  boost::python::class_<set_T >(py_name, "mutable set")
128  .def("__len__", &set_T::size)
129  .def("__contains__", &set_T::contains)
130  .def("add", &set_T::add, "add element")
131  .def("__delitem__", &set_T::remove)
132  .def("remove", &set_T::remove, "remove element")
133  .def("__iter__", boost::python::iterator<set_T>())
134  .def("__hash__", &block_hashing)
135  .def("union", &set_T::set_union, "set union")
136  .def("__or__", &set_T::set_union, "set union")
137  .def("intersection", &set_T::set_intersection, "set intersection")
138  .def("__and__", &set_T::set_intersection, "set intersection")
139  .def("difference", &set_T::set_difference, "elements not in second set")
140  .def("__sub__", &set_T::set_difference, "set difference")
141  .def("symmetric_difference", &set_T::set_symmetric_difference, "elements unique to either set")
142  .def("__xor__", &set_T::set_symmetric_difference, "symmetric set difference")
143  .def("__str__", &set_T::to_string)
144  ;
145 
146  boost::python::implicitly_convertible<py_set<KeyType>, std::set<KeyType> >();
147  boost::python::implicitly_convertible<std::set<KeyType>, py_set<KeyType> >();
148 }
149 
152 
153 } // end of namespace python
154 } // end of namespace ipsdk
155 
156 #endif // __PYIPSDKBASE_PYTHONSTDSETUTILS_H__
IPSDK_FORCEINLINE void set(T *buffer, T value, ipUInt64 nbElts)
set function; assigns a given value to the &#39;nbElts&#39; elements of a buffer
Definition: set.h:34
Main namespace for IPSDK library.
Definition: AlgorithmFunctionEfficiency.h:22
Definition: PythonStdSetUtils.h:33