15 #ifndef __PYIPSDKBASE_PYTHONSTDSETUTILS_H__ 16 #define __PYIPSDKBASE_PYTHONSTDSETUTILS_H__ 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> 32 template<
class KeyType>
33 class py_set :
public std::set<KeyType>,
public boost::python::wrapper<std::set<KeyType> >
37 typedef std::set<KeyType> source_type;
44 this->insert(s.begin(), s.end());
48 bool contains(
const KeyType key) {
49 return this->count(key)>0;
53 bool add(
const KeyType key) {
54 return this->insert(key).second;
57 bool remove(
const KeyType key) {
58 if (!this->contains(key))
67 std::set_union(this->begin(), this->end(), other.begin(), other.end(),
68 inserter(result, result.begin()));
72 source_type set_intersection(
wrap_type &other) {
74 std::set_intersection(this->begin(), this->end(), other.begin(), other.end(),
75 inserter(result, result.begin()));
79 source_type set_difference(
wrap_type &other) {
81 std::set_difference(this->begin(), this->end(), other.begin(), other.end(),
82 inserter(result, result.begin()));
86 source_type set_symmetric_difference(
wrap_type &other) {
88 std::set_symmetric_difference(this->begin(), this->end(), other.begin(), other.end(),
89 inserter(result, result.begin()));
94 static std::string to_string(
const std::set<KeyType>& cl)
96 typedef std::set<KeyType> Container;
100 for (
typename Container::const_iterator
101 i = cl.begin(), e = cl.end(); i != e; ++i) {
108 const KeyType& curKey = *i;
116 inline void block_hashing(boost::python::object) {
118 throw "objects of this type are unhashable";
122 template<
class KeyType>
void 123 export_set(
const char* py_name) {
125 typedef py_set<KeyType> set_T;
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)
146 boost::python::implicitly_convertible<py_set<KeyType>, std::set<KeyType> >();
147 boost::python::implicitly_convertible<std::set<KeyType>, py_set<KeyType> >();
156 #endif // __PYIPSDKBASE_PYTHONSTDSETUTILS_H__ IPSDK_FORCEINLINE void set(T *buffer, T value, ipUInt64 nbElts)
set function; assigns a given value to the 'nbElts' elements of a buffer
Definition: set.h:34
Main namespace for IPSDK library.
Definition: AlgorithmFunctionEfficiency.h:22
Definition: PythonStdSetUtils.h:33