PageRenderTime 31ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/trunk/Lib/python/std_map.i

#
Swig | 305 lines | 261 code | 40 blank | 4 comment | 0 complexity | 9ee527b76249b51c08942cacfc73f341 MD5 | raw file
  1/*
  2  Maps
  3*/
  4
  5%fragment("StdMapTraits","header",fragment="StdSequenceTraits")
  6{
  7  namespace swig {
  8    template <class SwigPySeq, class K, class T, class Compare, class Alloc >
  9    inline void
 10    assign(const SwigPySeq& swigpyseq, std::map<K,T,Compare,Alloc > *map) {
 11      typedef typename std::map<K,T,Compare,Alloc >::value_type value_type;
 12      typename SwigPySeq::const_iterator it = swigpyseq.begin();
 13      for (;it != swigpyseq.end(); ++it) {
 14	map->insert(value_type(it->first, it->second));
 15      }
 16    }
 17
 18    template <class K, class T, class Compare, class Alloc>
 19    struct traits_asptr<std::map<K,T,Compare,Alloc > >  {
 20      typedef std::map<K,T,Compare,Alloc > map_type;
 21      static int asptr(PyObject *obj, map_type **val) {
 22	int res = SWIG_ERROR;
 23	SWIG_PYTHON_THREAD_BEGIN_BLOCK;
 24	if (PyDict_Check(obj)) {
 25	  SwigVar_PyObject items = PyObject_CallMethod(obj,(char *)"items",NULL);
 26%#if PY_VERSION_HEX >= 0x03000000
 27          /* In Python 3.x the ".items()" method returns a dict_items object */
 28          items = PySequence_Fast(items, ".items() didn't return a sequence!");
 29%#endif
 30	  res = traits_asptr_stdseq<map_type, std::pair<K, T> >::asptr(items, val);
 31	} else {
 32	  map_type *p;
 33	  res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info<map_type>(),0);
 34	  if (SWIG_IsOK(res) && val)  *val = p;
 35	}
 36	SWIG_PYTHON_THREAD_END_BLOCK;
 37	return res;
 38      }      
 39    };
 40      
 41    template <class K, class T, class Compare, class Alloc >
 42    struct traits_from<std::map<K,T,Compare,Alloc > >  {
 43      typedef std::map<K,T,Compare,Alloc > map_type;
 44      typedef typename map_type::const_iterator const_iterator;
 45      typedef typename map_type::size_type size_type;
 46
 47      static PyObject *asdict(const map_type& map) {
 48	SWIG_PYTHON_THREAD_BEGIN_BLOCK;
 49	size_type size = map.size();
 50	int pysize = (size <= (size_type) INT_MAX) ? (int) size : -1;
 51	if (pysize < 0) {
 52	  PyErr_SetString(PyExc_OverflowError,
 53			  "map size not valid in python");
 54	  SWIG_PYTHON_THREAD_END_BLOCK;
 55	  return NULL;
 56	}
 57	PyObject *obj = PyDict_New();
 58	for (const_iterator i= map.begin(); i!= map.end(); ++i) {
 59	  swig::SwigVar_PyObject key = swig::from(i->first);
 60	  swig::SwigVar_PyObject val = swig::from(i->second);
 61	  PyDict_SetItem(obj, key, val);
 62	}
 63	SWIG_PYTHON_THREAD_END_BLOCK;
 64	return obj;
 65      }
 66                
 67      static PyObject *from(const map_type& map) {
 68	swig_type_info *desc = swig::type_info<map_type>();
 69	if (desc && desc->clientdata) {
 70	  return SWIG_InternalNewPointerObj(new map_type(map), desc, SWIG_POINTER_OWN);
 71	} else {
 72	  return asdict(map);
 73	}
 74      }
 75    };
 76
 77    template <class ValueType>
 78    struct from_key_oper 
 79    {
 80      typedef const ValueType& argument_type;
 81      typedef  PyObject *result_type;
 82      result_type operator()(argument_type v) const
 83      {
 84	return swig::from(v.first);
 85      }
 86    };
 87
 88    template <class ValueType>
 89    struct from_value_oper 
 90    {
 91      typedef const ValueType& argument_type;
 92      typedef  PyObject *result_type;
 93      result_type operator()(argument_type v) const
 94      {
 95	return swig::from(v.second);
 96      }
 97    };
 98
 99    template<class OutIterator, class FromOper, class ValueType = typename OutIterator::value_type>
100    struct SwigPyMapIterator_T : SwigPyIteratorClosed_T<OutIterator, ValueType, FromOper>
101    {
102      SwigPyMapIterator_T(OutIterator curr, OutIterator first, OutIterator last, PyObject *seq)
103	: SwigPyIteratorClosed_T<OutIterator,ValueType,FromOper>(curr, first, last, seq)
104      {
105      }
106    };
107
108
109    template<class OutIterator,
110	     class FromOper = from_key_oper<typename OutIterator::value_type> >
111    struct SwigPyMapKeyIterator_T : SwigPyMapIterator_T<OutIterator, FromOper>
112    {
113      SwigPyMapKeyIterator_T(OutIterator curr, OutIterator first, OutIterator last, PyObject *seq)
114	: SwigPyMapIterator_T<OutIterator, FromOper>(curr, first, last, seq)
115      {
116      }
117    };
118
119    template<typename OutIter>
120    inline SwigPyIterator*
121    make_output_key_iterator(const OutIter& current, const OutIter& begin, const OutIter& end, PyObject *seq = 0)
122    {
123      return new SwigPyMapKeyIterator_T<OutIter>(current, begin, end, seq);
124    }
125
126    template<class OutIterator,
127	     class FromOper = from_value_oper<typename OutIterator::value_type> >
128    struct SwigPyMapValueITerator_T : SwigPyMapIterator_T<OutIterator, FromOper>
129    {
130      SwigPyMapValueITerator_T(OutIterator curr, OutIterator first, OutIterator last, PyObject *seq)
131	: SwigPyMapIterator_T<OutIterator, FromOper>(curr, first, last, seq)
132      {
133      }
134    };
135    
136
137    template<typename OutIter>
138    inline SwigPyIterator*
139    make_output_value_iterator(const OutIter& current, const OutIter& begin, const OutIter& end, PyObject *seq = 0)
140    {
141      return new SwigPyMapValueITerator_T<OutIter>(current, begin, end, seq);
142    }
143
144  }
145}
146
147%define %swig_map_common(Map...)
148  %swig_sequence_iterator(Map);
149  %swig_container_methods(Map)
150
151#if defined(SWIGPYTHON_BUILTIN)
152  %feature("python:slot", "mp_length", functype="lenfunc") __len__;
153  %feature("python:slot", "mp_subscript", functype="binaryfunc") __getitem__;
154  %feature("python:slot", "tp_iter", functype="getiterfunc") key_iterator;
155
156  %extend {
157    %newobject iterkeys(PyObject **PYTHON_SELF);
158    swig::SwigPyIterator* iterkeys(PyObject **PYTHON_SELF) {
159      return swig::make_output_key_iterator(self->begin(), self->begin(), self->end(), *PYTHON_SELF);
160    }
161      
162    %newobject itervalues(PyObject **PYTHON_SELF);
163    swig::SwigPyIterator* itervalues(PyObject **PYTHON_SELF) {
164      return swig::make_output_value_iterator(self->begin(), self->begin(), self->end(), *PYTHON_SELF);
165    }
166
167    %newobject iteritems(PyObject **PYTHON_SELF);
168    swig::SwigPyIterator* iteritems(PyObject **PYTHON_SELF) {
169      return swig::make_output_iterator(self->begin(), self->begin(), self->end(), *PYTHON_SELF);
170    }
171  }
172
173#else
174  %extend {
175    %pythoncode {def __iter__(self): return self.key_iterator()}    
176    %pythoncode {def iterkeys(self): return self.key_iterator()}
177    %pythoncode {def itervalues(self): return self.value_iterator()}
178    %pythoncode {def iteritems(self): return self.iterator()}
179  }
180#endif
181
182  %extend {
183    mapped_type const & __getitem__(const key_type& key) throw (std::out_of_range) {
184      Map::const_iterator i = self->find(key);
185      if (i != self->end())
186	return i->second;
187      else
188	throw std::out_of_range("key not found");
189    }
190
191    void __delitem__(const key_type& key) throw (std::out_of_range) {
192      Map::iterator i = self->find(key);
193      if (i != self->end())
194	self->erase(i);
195      else
196	throw std::out_of_range("key not found");
197    }
198    
199    bool has_key(const key_type& key) const {
200      Map::const_iterator i = self->find(key);
201      return i != self->end();
202    }
203    
204    PyObject* keys() {
205      Map::size_type size = self->size();
206      int pysize = (size <= (Map::size_type) INT_MAX) ? (int) size : -1;
207      SWIG_PYTHON_THREAD_BEGIN_BLOCK;
208      if (pysize < 0) {
209	PyErr_SetString(PyExc_OverflowError,
210			"map size not valid in python");
211	SWIG_PYTHON_THREAD_END_BLOCK;
212	return NULL;
213      }
214      PyObject* keyList = PyList_New(pysize);
215      Map::const_iterator i = self->begin();
216      for (int j = 0; j < pysize; ++i, ++j) {
217	PyList_SET_ITEM(keyList, j, swig::from(i->first));
218      }
219      SWIG_PYTHON_THREAD_END_BLOCK;
220      return keyList;
221    }
222    
223    PyObject* values() {
224      Map::size_type size = self->size();
225      int pysize = (size <= (Map::size_type) INT_MAX) ? (int) size : -1;
226      SWIG_PYTHON_THREAD_BEGIN_BLOCK;
227      if (pysize < 0) {
228	PyErr_SetString(PyExc_OverflowError,
229			"map size not valid in python");
230	SWIG_PYTHON_THREAD_END_BLOCK;
231	return NULL;
232      }
233      PyObject* valList = PyList_New(pysize);
234      Map::const_iterator i = self->begin();
235      for (int j = 0; j < pysize; ++i, ++j) {
236	PyList_SET_ITEM(valList, j, swig::from(i->second));
237      }
238      SWIG_PYTHON_THREAD_END_BLOCK;
239      return valList;
240    }
241    
242    PyObject* items() {
243      Map::size_type size = self->size();
244      int pysize = (size <= (Map::size_type) INT_MAX) ? (int) size : -1;
245      SWIG_PYTHON_THREAD_BEGIN_BLOCK;
246      if (pysize < 0) {
247	PyErr_SetString(PyExc_OverflowError,
248			"map size not valid in python");
249	SWIG_PYTHON_THREAD_END_BLOCK;
250	return NULL;
251      }    
252      PyObject* itemList = PyList_New(pysize);
253      Map::const_iterator i = self->begin();
254      for (int j = 0; j < pysize; ++i, ++j) {
255	PyList_SET_ITEM(itemList, j, swig::from(*i));
256      }
257      SWIG_PYTHON_THREAD_END_BLOCK;
258      return itemList;
259    }
260    
261    // Python 2.2 methods
262    bool __contains__(const key_type& key) {
263      return self->find(key) != self->end();
264    }
265
266    %newobject key_iterator(PyObject **PYTHON_SELF);
267    swig::SwigPyIterator* key_iterator(PyObject **PYTHON_SELF) {
268      return swig::make_output_key_iterator(self->begin(), self->begin(), self->end(), *PYTHON_SELF);
269    }
270
271    %newobject value_iterator(PyObject **PYTHON_SELF);
272    swig::SwigPyIterator* value_iterator(PyObject **PYTHON_SELF) {
273      return swig::make_output_value_iterator(self->begin(), self->begin(), self->end(), *PYTHON_SELF);
274    }
275  }
276
277%enddef
278
279%define %swig_map_methods(Map...)
280  %swig_map_common(Map)
281
282#if defined(SWIGPYTHON_BUILTIN)
283  %feature("python:slot", "mp_ass_subscript", functype="objobjargproc") __setitem__;
284#endif
285
286  %extend {
287    // This will be called through the mp_ass_subscript slot to delete an entry.
288    void __setitem__(const key_type& key) {
289      self->erase(key);
290    }
291
292    void __setitem__(const key_type& key, const mapped_type& x) throw (std::out_of_range) {
293      (*self)[key] = x;
294    }
295
296    PyObject* asdict() {
297      return swig::traits_from< Map >::asdict(*self);
298    }
299  }
300
301
302%enddef
303
304
305%include <std/std_map.i>