Home | History | Annotate | Download | only in python
      1 /*
      2   Sets
      3 */
      4 
      5 %fragment("StdSetTraits","header",fragment="StdSequenceTraits")
      6 %{
      7   namespace swig {
      8     template <class SwigPySeq, class T>
      9     inline void
     10     assign(const SwigPySeq& swigpyseq, std::set<T>* seq) {
     11       // seq->insert(swigpyseq.begin(), swigpyseq.end()); // not used as not always implemented
     12       typedef typename SwigPySeq::value_type value_type;
     13       typename SwigPySeq::const_iterator it = swigpyseq.begin();
     14       for (;it != swigpyseq.end(); ++it) {
     15 	seq->insert(seq->end(),(value_type)(*it));
     16       }
     17     }
     18 
     19     template <class T>
     20     struct traits_asptr<std::set<T> >  {
     21       static int asptr(PyObject *obj, std::set<T> **s) {
     22 	return traits_asptr_stdseq<std::set<T> >::asptr(obj, s);
     23       }
     24     };
     25 
     26     template <class T>
     27     struct traits_from<std::set<T> > {
     28       static PyObject *from(const std::set<T>& vec) {
     29 	return traits_from_stdseq<std::set<T> >::from(vec);
     30       }
     31     };
     32   }
     33 %}
     34 
     35 %define %swig_set_methods(set...)
     36   %swig_sequence_iterator(set);
     37   %swig_container_methods(set);
     38 
     39   %extend  {
     40      void append(value_type x) {
     41        self->insert(x);
     42      }
     43 
     44      bool __contains__(value_type x) {
     45        return self->find(x) != self->end();
     46      }
     47 
     48      value_type __getitem__(difference_type i) const throw (std::out_of_range) {
     49        return *(swig::cgetpos(self, i));
     50      }
     51 
     52      void add(value_type x) {
     53        self->insert(x);
     54      }
     55 
     56      void discard(value_type x) {
     57        self->erase(x);
     58      }
     59 
     60   };
     61 %enddef
     62 
     63 %include <std/std_set.i>
     64