diff options
author | Luigi Ballabio <luigi.ballabio@fastwebnet.it> | 2003-02-27 10:05:55 +0000 |
---|---|---|
committer | Luigi Ballabio <luigi.ballabio@fastwebnet.it> | 2003-02-27 10:05:55 +0000 |
commit | 9d50216d9cc57654a484574f72236c2e3a791dad (patch) | |
tree | 73cc0d846d771eaa54f3e81c22e9f380c5a4e695 /Lib | |
parent | a1fa5ec2c17792b3105aa05f1967359bd3116a6d (diff) | |
download | swig-9d50216d9cc57654a484574f72236c2e3a791dad.tar.gz |
Support for std::map (thanks, Dave)
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4414 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/guile/std_map.i | 1386 | ||||
-rw-r--r-- | Lib/java/std_map.i | 190 | ||||
-rw-r--r-- | Lib/mzscheme/std_map.i | 1386 | ||||
-rw-r--r-- | Lib/ocaml/std_map.i | 190 | ||||
-rw-r--r-- | Lib/perl5/std_map.i | 190 | ||||
-rw-r--r-- | Lib/php4/std_map.i | 190 | ||||
-rw-r--r-- | Lib/python/std_map.i | 1551 | ||||
-rw-r--r-- | Lib/ruby/std_map.i | 1281 | ||||
-rw-r--r-- | Lib/stl.i | 1 | ||||
-rw-r--r-- | Lib/swig.swg | 1 | ||||
-rw-r--r-- | Lib/tcl/std_map.i | 190 |
11 files changed, 6556 insertions, 0 deletions
diff --git a/Lib/guile/std_map.i b/Lib/guile/std_map.i new file mode 100644 index 000000000..86b99ee7d --- /dev/null +++ b/Lib/guile/std_map.i @@ -0,0 +1,1386 @@ +// +// SWIG typemaps for std::map +// Luigi Ballabio +// Jan. 2003 +// +// Guile implementation + +%include std_common.i +%include exception.i + +%exception std::map::__getitem__ { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +%exception std::map::__delitem__ { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +// ------------------------------------------------------------------------ +// std::map +// +// The aim of all that follows would be to integrate std::map with +// Guile as much as possible, namely, to allow the user to pass and +// be returned Scheme association lists. +// const declarations are used to guess the intent of the function being +// exported; therefore, the following rationale is applied: +// +// -- f(std::map<T>), f(const std::map<T>&), f(const std::map<T>*): +// the parameter being read-only, either a Scheme alist or a +// previously wrapped std::map<T> can be passed. +// -- f(std::map<T>&), f(std::map<T>*): +// the parameter must be modified; therefore, only a wrapped std::map +// can be passed. +// -- std::map<T> f(): +// the map is returned by copy; therefore, a Scheme alist +// is returned which is most easily used in other Scheme functions +// -- std::map<T>& f(), std::map<T>* f(), const std::map<T>& f(), +// const std::map<T>* f(): +// the map is returned by reference; therefore, a wrapped std::map +// is returned +// ------------------------------------------------------------------------ + +%{ +#include <map> +#include <algorithm> +#include <stdexcept> +%} + +// exported class + +namespace std { + + template<class K, class T> class map { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (gh_null_p($input)) { + $1 = std::map<K,T >(); + } else if (gh_pair_p($input)) { + $1 = std::map<K,T >(); + SCM alist = $input; + while (!gh_null_p(alist)) { + K* k; + T* x; + SCM entry, key, val; + entry = gh_car(alist); + if (!gh_pair_p(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = gh_car(entry); + val = gh_cdr(entry); + k = (K*) SWIG_MustGetPtr(key,$descriptor(K *),$argnum); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) != 0) { + if (!gh_pair_p(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = gh_car(val); + x = (T*) SWIG_MustGetPtr(val,$descriptor(T *),$argnum); + } + (($1_type &)$1)[*k] = *x; + alist = gh_cdr(alist); + } + } else { + $1 = *(($&1_type) + SWIG_MustGetPtr($input,$&1_descriptor,$argnum)); + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (gh_null_p($input)) { + temp = std::map<K,T >(); + $1 = &temp; + } else if (gh_pair_p($input)) { + temp = std::map<K,T >(); + $1 = &temp; + SCM alist = $input; + while (!gh_null_p(alist)) { + K* k; + T* x; + SCM entry, key, val; + entry = gh_car(alist); + if (!gh_pair_p(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = gh_car(entry); + val = gh_cdr(entry); + k = (K*) SWIG_MustGetPtr(key,$descriptor(K *),$argnum); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) != 0) { + if (!gh_pair_p(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = gh_car(val); + x = (T*) SWIG_MustGetPtr(val,$descriptor(T *),$argnum); + } + temp[*k] = *x; + alist = gh_cdr(alist); + } + } else { + $1 = ($1_ltype) SWIG_MustGetPtr($input,$1_descriptor,$argnum); + } + } + %typemap(out) map<K,T> { + SCM alist = SCM_EOL; + for (std::map<K,T >::reverse_iterator i=$1.rbegin(); + i!=$1.rend(); ++i) { + K* key = new K(i->first); + T* val = new T(i->second); + SCM k = SWIG_MakePtr(key,$descriptor(K *)); + SCM x = SWIG_MakePtr(val,$descriptor(T *)); + SCM entry = gh_cons(k,x); + alist = gh_cons(entry,alist); + } + $result = alist; + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + /* native sequence? */ + if (gh_null_p($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (gh_pair_p($input)) { + /* check the first element only */ + K* k; + T* x; + SCM head = gh_car($input); + if (gh_pair_p(head)) { + SCM key = gh_car(head); + SCM val = gh_cdr(head); + if (SWIG_GetPtr(key,(void**) &k, + $descriptor(K *)) != 0) { + $1 = 0; + } else { + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) == 0) { + $1 = 1; + } else if (gh_pair_p(val)) { + val = gh_car(val); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) == 0) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + /* wrapped map? */ + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $&1_descriptor) == 0) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + /* native sequence? */ + if (gh_null_p($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (gh_pair_p($input)) { + /* check the first element only */ + K* k; + T* x; + SCM head = gh_car($input); + if (gh_pair_p(head)) { + SCM key = gh_car(head); + SCM val = gh_cdr(head); + if (SWIG_GetPtr(key,(void**) &k, + $descriptor(K *)) != 0) { + $1 = 0; + } else { + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) == 0) { + $1 = 1; + } else if (gh_pair_p(val)) { + val = gh_car(val); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) == 0) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + /* wrapped map? */ + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $1_descriptor) == 0) + $1 = 1; + else + $1 = 0; + } + } + %rename("length") size; + %rename("null?") empty; + %rename("clear!") clear; + %rename("ref") __getitem__; + %rename("set!") __setitem__; + %rename("delete!") __delitem__; + %rename("has-key?") has_key; + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& __getitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(const K& key, const T& x) { + (*self)[key] = x; + } + void __delitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + /* + void each() { + swig_type_info* k_type = SWIG_TypeQuery(#K " *"); + swig_type_info* t_type = SWIG_TypeQuery(#T " *"); + std::map<K,T >::iterator i; + for (i=self->begin(); i!=self->end(); ++i) { + K* key = new K(i->first); + T* val = &(i->second); + VALUE entry = rb_ary_new2(2); + VALUE k = SWIG_NewPointerObj((void *) key,k_type,1); + VALUE x = SWIG_NewPointerObj((void *) val,t_type,0); + rb_ary_store(entry,0,k); + rb_ary_store(entry,1,x); + rb_yield(entry); + } + } + */ + } + }; + + + // specializations for built-ins + + %define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO) + + template<class T> class map<K,T> { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (gh_null_p($input)) { + $1 = std::map<K,T >(); + } else if (gh_pair_p($input)) { + $1 = std::map<K,T >(); + SCM alist = $input; + while (!gh_null_p(alist)) { + T* x; + SCM entry, key, val; + entry = gh_car(alist); + if (!gh_pair_p(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = gh_car(entry); + val = gh_cdr(entry); + if (!CHECK(key)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) != 0) { + if (!gh_pair_p(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = gh_car(val); + x = (T*) SWIG_MustGetPtr(val,$descriptor(T *),$argnum); + } + (($1_type &)$1)[CONVERT_FROM(key)] = *x; + alist = gh_cdr(alist); + } + } else { + $1 = *(($&1_type) + SWIG_MustGetPtr($input,$&1_descriptor,$argnum)); + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (gh_null_p($input)) { + temp = std::map<K,T >(); + $1 = &temp; + } else if (gh_pair_p($input)) { + temp = std::map<K,T >(); + $1 = &temp; + SCM alist = $input; + while (!gh_null_p(alist)) { + T* x; + SCM entry, key, val; + entry = gh_car(alist); + if (!gh_pair_p(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = gh_car(entry); + val = gh_cdr(entry); + if (!CHECK(key)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) != 0) { + if (!gh_pair_p(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = gh_car(val); + x = (T*) SWIG_MustGetPtr(val,$descriptor(T *),$argnum); + } + temp[CONVERT_FROM(key)] = *x; + alist = gh_cdr(alist); + } + } else { + $1 = ($1_ltype) SWIG_MustGetPtr($input,$1_descriptor,$argnum); + } + } + %typemap(out) map<K,T> { + SCM alist = SCM_EOL; + for (std::map<K,T >::reverse_iterator i=$1.rbegin(); + i!=$1.rend(); ++i) { + T* val = new T(i->second); + SCM k = CONVERT_TO(i->first); + SCM x = SWIG_MakePtr(val,$descriptor(T *)); + SCM entry = gh_cons(k,x); + alist = gh_cons(entry,alist); + } + $result = alist; + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + // native sequence? + if (gh_null_p($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (gh_pair_p($input)) { + // check the first element only + T* x; + SCM head = gh_car($input); + if (gh_pair_p(head)) { + SCM key = gh_car(head); + SCM val = gh_cdr(head); + if (!CHECK(key)) { + $1 = 0; + } else { + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) == 0) { + $1 = 1; + } else if (gh_pair_p(val)) { + val = gh_car(val); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) == 0) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $&1_descriptor) == 0) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + // native sequence? + if (gh_null_p($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (gh_pair_p($input)) { + // check the first element only + T* x; + SCM head = gh_car($input); + if (gh_pair_p(head)) { + SCM key = gh_car(head); + SCM val = gh_cdr(head); + if (!CHECK(key)) { + $1 = 0; + } else { + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) == 0) { + $1 = 1; + } else if (gh_pair_p(val)) { + val = gh_car(val); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) == 0) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $1_descriptor) == 0) + $1 = 1; + else + $1 = 0; + } + } + %rename("length") size; + %rename("null?") empty; + %rename("clear!") clear; + %rename("ref") __getitem__; + %rename("set!") __setitem__; + %rename("delete!") __delitem__; + %rename("has-key?") has_key; + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& __getitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(K key, const T& x) { + (*self)[key] = x; + } + void __delitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + /* + void each() { + swig_type_info* t_type = SWIG_TypeQuery(#T " *"); + std::map<K,T >::iterator i; + for (i=self->begin(); i!=self->end(); ++i) { + T* val = &(i->second); + VALUE entry = rb_ary_new2(2); + VALUE k = CONVERT_TO(i->first); + VALUE x = SWIG_NewPointerObj((void *) val,t_type,0); + rb_ary_store(entry,0,k); + rb_ary_store(entry,1,x); + rb_yield(entry); + } + } + */ + } + }; + %enddef + + %define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO) + template<class K> class map<K,T> { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (gh_null_p($input)) { + $1 = std::map<K,T >(); + } else if (gh_pair_p($input)) { + $1 = std::map<K,T >(); + SCM alist = $input; + while (!gh_null_p(alist)) { + K* k; + SCM entry, key, val; + entry = gh_car(alist); + if (!gh_pair_p(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = gh_car(entry); + val = gh_cdr(entry); + k = (K*) SWIG_MustGetPtr(key,$descriptor(K *),$argnum); + if (!CHECK(val)) { + if (!gh_pair_p(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = gh_car(val); + if (!CHECK(val)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + } + (($1_type &)$1)[*k] = CONVERT_FROM(val); + alist = gh_cdr(alist); + } + } else { + $1 = *(($&1_type) + SWIG_MustGetPtr($input,$&1_descriptor,$argnum)); + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (gh_null_p($input)) { + temp = std::map<K,T >(); + $1 = &temp; + } else if (gh_pair_p($input)) { + temp = std::map<K,T >(); + $1 = &temp; + SCM alist = $input; + while (!gh_null_p(alist)) { + K* k; + SCM entry, key, val; + entry = gh_car(alist); + if (!gh_pair_p(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = gh_car(entry); + val = gh_cdr(entry); + k = (K*) SWIG_MustGetPtr(key,$descriptor(K *),$argnum); + if (!CHECK(val)) { + if (!gh_pair_p(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = gh_car(val); + if (!CHECK(val)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + } + temp[*k] = CONVERT_FROM(val); + alist = gh_cdr(alist); + } + } else { + $1 = ($1_ltype) SWIG_MustGetPtr($input,$1_descriptor,$argnum); + } + } + %typemap(out) map<K,T> { + SCM alist = SCM_EOL; + for (std::map<K,T >::reverse_iterator i=$1.rbegin(); + i!=$1.rend(); ++i) { + K* key = new K(i->first); + SCM k = SWIG_MakePtr(key,$descriptor(K *)); + SCM x = CONVERT_TO(i->second); + SCM entry = gh_cons(k,x); + alist = gh_cons(entry,alist); + } + $result = alist; + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + // native sequence? + if (gh_null_p($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (gh_pair_p($input)) { + // check the first element only + K* k; + SCM head = gh_car($input); + if (gh_pair_p(head)) { + SCM key = gh_car(head); + SCM val = gh_cdr(head); + if (SWIG_GetPtr(val,(void **) &k, + $descriptor(K *)) != 0) { + $1 = 0; + } else { + if (CHECK(val)) { + $1 = 1; + } else if (gh_pair_p(val)) { + val = gh_car(val); + if (CHECK(val)) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $&1_descriptor) == 0) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + // native sequence? + if (gh_null_p($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (gh_pair_p($input)) { + // check the first element only + K* k; + SCM head = gh_car($input); + if (gh_pair_p(head)) { + SCM key = gh_car(head); + SCM val = gh_cdr(head); + if (SWIG_GetPtr(val,(void **) &k, + $descriptor(K *)) != 0) { + $1 = 0; + } else { + if (CHECK(val)) { + $1 = 1; + } else if (gh_pair_p(val)) { + val = gh_car(val); + if (CHECK(val)) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $1_descriptor) == 0) + $1 = 1; + else + $1 = 0; + } + } + %rename("length") size; + %rename("null?") empty; + %rename("clear!") clear; + %rename("ref") __getitem__; + %rename("set!") __setitem__; + %rename("delete!") __delitem__; + %rename("has-key?") has_key; + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T __getitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(const K& key, T x) { + (*self)[key] = x; + } + void __delitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + /* + void each() { + swig_type_info* k_type = SWIG_TypeQuery(#K " *"); + std::map<K,T >::iterator i; + for (i=self->begin(); i!=self->end(); ++i) { + K* key = new K(i->first); + VALUE entry = rb_ary_new2(2); + VALUE k = SWIG_NewPointerObj((void *) key,k_type,1); + VALUE x = CONVERT_TO(i->second); + rb_ary_store(entry,0,k); + rb_ary_store(entry,1,x); + rb_yield(entry); + } + } + */ + } + }; + %enddef + + %define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO, + T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO) + template<> class map<K,T> { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (gh_null_p($input)) { + $1 = std::map<K,T >(); + } else if (gh_pair_p($input)) { + $1 = std::map<K,T >(); + SCM alist = $input; + while (!gh_null_p(alist)) { + SCM entry, key, val; + entry = gh_car(alist); + if (!gh_pair_p(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = gh_car(entry); + val = gh_cdr(entry); + if (!CHECK_K(key)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + if (!CHECK_T(val)) { + if (!gh_pair_p(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = gh_car(val); + if (!CHECK_T(val)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + } + (($1_type &)$1)[CONVERT_K_FROM(key)] = + CONVERT_T_FROM(val); + alist = gh_cdr(alist); + } + } else { + $1 = *(($&1_type) + SWIG_MustGetPtr($input,$&1_descriptor,$argnum)); + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (gh_null_p($input)) { + temp = std::map<K,T >(); + $1 = &temp; + } else if (gh_pair_p($input)) { + temp = std::map<K,T >(); + $1 = &temp; + SCM alist = $input; + while (!gh_null_p(alist)) { + SCM entry, key, val; + entry = gh_car(alist); + if (!gh_pair_p(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = gh_car(entry); + val = gh_cdr(entry); + if (!CHECK_K(key)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + if (!CHECK_T(val)) { + if (!gh_pair_p(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = gh_car(val); + if (!CHECK_T(val)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + } + temp[CONVERT_K_FROM(key)] = CONVERT_T_FROM(val); + alist = gh_cdr(alist); + } + } else { + $1 = ($1_ltype) SWIG_MustGetPtr($input,$1_descriptor,$argnum); + } + } + %typemap(out) map<K,T> { + SCM alist = SCM_EOL; + for (std::map<K,T >::reverse_iterator i=$1.rbegin(); + i!=$1.rend(); ++i) { + SCM k = CONVERT_K_TO(i->first); + SCM x = CONVERT_T_TO(i->second); + SCM entry = gh_cons(k,x); + alist = gh_cons(entry,alist); + } + $result = alist; + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + // native sequence? + if (gh_null_p($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (gh_pair_p($input)) { + // check the first element only + SCM head = gh_car($input); + if (gh_pair_p(head)) { + SCM key = gh_car(head); + SCM val = gh_cdr(head); + if (!CHECK_K(key)) { + $1 = 0; + } else { + if (CHECK_T(val)) { + $1 = 1; + } else if (gh_pair_p(val)) { + val = gh_car(val); + if (CHECK_T(val)) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $&1_descriptor) == 0) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + // native sequence? + if (gh_null_p($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (gh_pair_p($input)) { + // check the first element only + SCM head = gh_car($input); + if (gh_pair_p(head)) { + SCM key = gh_car(head); + SCM val = gh_cdr(head); + if (!CHECK_K(key)) { + $1 = 0; + } else { + if (CHECK_T(val)) { + $1 = 1; + } else if (gh_pair_p(val)) { + val = gh_car(val); + if (CHECK_T(val)) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $1_descriptor) == 0) + $1 = 1; + else + $1 = 0; + } + } + %rename("length") size; + %rename("null?") empty; + %rename("clear!") clear; + %rename("ref") __getitem__; + %rename("set!") __setitem__; + %rename("delete!") __delitem__; + %rename("has-key?") has_key; + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T __getitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(K key, T x) { + (*self)[key] = x; + } + void __delitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + /* + void each() { + std::map<K,T >::iterator i; + for (i=self->begin(); i!=self->end(); ++i) { + VALUE entry = rb_ary_new2(2); + rb_ary_store(entry,0,CONVERT_K_TO(i->first)); + rb_ary_store(entry,1,CONVERT_T_TO(i->second)); + rb_yield(entry); + } + } + */ + } + }; + %enddef + + + specialize_std_map_on_key(bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm); + specialize_std_map_on_key(int,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_key(short,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_key(long,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_key(unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_key(unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_key(unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_key(double,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_key(float,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_key(std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm); + + specialize_std_map_on_value(bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm); + specialize_std_map_on_value(int,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_value(short,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_value(long,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_value(unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_value(unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_value(unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_value(double,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_value(float,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_value(std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm); + + specialize_std_map_on_both(bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm, + bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm); + specialize_std_map_on_both(bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm, + int,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm, + short,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm, + long,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm, + unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm, + unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm, + unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm, + double,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm, + float,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm, + std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm); + specialize_std_map_on_both(int,gh_number_p, + gh_scm2long,gh_long2scm, + bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm); + specialize_std_map_on_both(int,gh_number_p, + gh_scm2long,gh_long2scm, + int,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(int,gh_number_p, + gh_scm2long,gh_long2scm, + short,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(int,gh_number_p, + gh_scm2long,gh_long2scm, + long,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(int,gh_number_p, + gh_scm2long,gh_long2scm, + unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(int,gh_number_p, + gh_scm2long,gh_long2scm, + unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(int,gh_number_p, + gh_scm2long,gh_long2scm, + unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(int,gh_number_p, + gh_scm2long,gh_long2scm, + double,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(int,gh_number_p, + gh_scm2long,gh_long2scm, + float,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(int,gh_number_p, + gh_scm2long,gh_long2scm, + std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm); + specialize_std_map_on_both(short,gh_number_p, + gh_scm2long,gh_long2scm, + bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm); + specialize_std_map_on_both(short,gh_number_p, + gh_scm2long,gh_long2scm, + int,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(short,gh_number_p, + gh_scm2long,gh_long2scm, + short,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(short,gh_number_p, + gh_scm2long,gh_long2scm, + long,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(short,gh_number_p, + gh_scm2long,gh_long2scm, + unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(short,gh_number_p, + gh_scm2long,gh_long2scm, + unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(short,gh_number_p, + gh_scm2long,gh_long2scm, + unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(short,gh_number_p, + gh_scm2long,gh_long2scm, + double,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(short,gh_number_p, + gh_scm2long,gh_long2scm, + float,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(short,gh_number_p, + gh_scm2long,gh_long2scm, + std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm); + specialize_std_map_on_both(long,gh_number_p, + gh_scm2long,gh_long2scm, + bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm); + specialize_std_map_on_both(long,gh_number_p, + gh_scm2long,gh_long2scm, + int,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(long,gh_number_p, + gh_scm2long,gh_long2scm, + short,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(long,gh_number_p, + gh_scm2long,gh_long2scm, + long,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(long,gh_number_p, + gh_scm2long,gh_long2scm, + unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(long,gh_number_p, + gh_scm2long,gh_long2scm, + unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(long,gh_number_p, + gh_scm2long,gh_long2scm, + unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(long,gh_number_p, + gh_scm2long,gh_long2scm, + double,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(long,gh_number_p, + gh_scm2long,gh_long2scm, + float,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(long,gh_number_p, + gh_scm2long,gh_long2scm, + std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm); + specialize_std_map_on_both(unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm); + specialize_std_map_on_both(unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + int,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + short,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + long,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + double,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + float,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm); + specialize_std_map_on_both(unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm); + specialize_std_map_on_both(unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + int,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + short,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + long,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + double,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + float,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm); + specialize_std_map_on_both(unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm); + specialize_std_map_on_both(unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + int,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + short,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + long,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + double,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + float,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm, + std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm); + specialize_std_map_on_both(double,gh_number_p, + gh_scm2double,gh_double2scm, + bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm); + specialize_std_map_on_both(double,gh_number_p, + gh_scm2double,gh_double2scm, + int,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(double,gh_number_p, + gh_scm2double,gh_double2scm, + short,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(double,gh_number_p, + gh_scm2double,gh_double2scm, + long,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(double,gh_number_p, + gh_scm2double,gh_double2scm, + unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(double,gh_number_p, + gh_scm2double,gh_double2scm, + unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(double,gh_number_p, + gh_scm2double,gh_double2scm, + unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(double,gh_number_p, + gh_scm2double,gh_double2scm, + double,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(double,gh_number_p, + gh_scm2double,gh_double2scm, + float,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(double,gh_number_p, + gh_scm2double,gh_double2scm, + std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm); + specialize_std_map_on_both(float,gh_number_p, + gh_scm2double,gh_double2scm, + bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm); + specialize_std_map_on_both(float,gh_number_p, + gh_scm2double,gh_double2scm, + int,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(float,gh_number_p, + gh_scm2double,gh_double2scm, + short,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(float,gh_number_p, + gh_scm2double,gh_double2scm, + long,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(float,gh_number_p, + gh_scm2double,gh_double2scm, + unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(float,gh_number_p, + gh_scm2double,gh_double2scm, + unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(float,gh_number_p, + gh_scm2double,gh_double2scm, + unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(float,gh_number_p, + gh_scm2double,gh_double2scm, + double,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(float,gh_number_p, + gh_scm2double,gh_double2scm, + float,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(float,gh_number_p, + gh_scm2double,gh_double2scm, + std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm); + specialize_std_map_on_both(std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm, + bool,gh_boolean_p, + gh_scm2bool,SWIG_bool2scm); + specialize_std_map_on_both(std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm, + int,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm, + short,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm, + long,gh_number_p, + gh_scm2long,gh_long2scm); + specialize_std_map_on_both(std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm, + unsigned int,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm, + unsigned short,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm, + unsigned long,gh_number_p, + gh_scm2ulong,gh_ulong2scm); + specialize_std_map_on_both(std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm, + double,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm, + float,gh_number_p, + gh_scm2double,gh_double2scm); + specialize_std_map_on_both(std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm, + std::string,gh_string_p, + SWIG_scm2string,SWIG_string2scm); +} diff --git a/Lib/java/std_map.i b/Lib/java/std_map.i new file mode 100644 index 000000000..38a063e95 --- /dev/null +++ b/Lib/java/std_map.i @@ -0,0 +1,190 @@ +// +// SWIG typemaps for std::map +// Luigi Ballabio +// Jan. 2003 +// +// Common implementation + +%include std_common.i +%include exception.i + +%exception std::map::get { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +%exception std::map::del { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +// ------------------------------------------------------------------------ +// std::map +// ------------------------------------------------------------------------ + +%{ +#include <map> +#include <algorithm> +#include <stdexcept> +%} + +// exported class + +namespace std { + + template<class K, class T> class map { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& get(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(const K& key, const T& x) { + (*self)[key] = x; + } + void del(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + + + // specializations for built-ins + + %define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO) + + template<class T> class map<K,T> { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& get(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(K key, const T& x) { + (*self)[key] = x; + } + void del(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + %enddef + + %define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO) + template<class K> class map<K,T> { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T get(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(const K& key, T x) { + (*self)[key] = x; + } + void del(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + %enddef + + %define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO, + T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO) + template<> class map<K,T> { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T get(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(K key, T x) { + (*self)[key] = x; + } + void del(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + %enddef + + // add specializations here + +} diff --git a/Lib/mzscheme/std_map.i b/Lib/mzscheme/std_map.i new file mode 100644 index 000000000..dd56444ea --- /dev/null +++ b/Lib/mzscheme/std_map.i @@ -0,0 +1,1386 @@ +// +// SWIG typemaps for std::map +// Luigi Ballabio +// Jan. 2003 +// +// MzScheme implementation + +%include std_common.i +%include exception.i + +%exception std::map::__getitem__ { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +%exception std::map::__delitem__ { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +// ------------------------------------------------------------------------ +// std::map +// +// The aim of all that follows would be to integrate std::map with +// MzScheme as much as possible, namely, to allow the user to pass and +// be returned Scheme association lists. +// const declarations are used to guess the intent of the function being +// exported; therefore, the following rationale is applied: +// +// -- f(std::map<T>), f(const std::map<T>&), f(const std::map<T>*): +// the parameter being read-only, either a Scheme alist or a +// previously wrapped std::map<T> can be passed. +// -- f(std::map<T>&), f(std::map<T>*): +// the parameter must be modified; therefore, only a wrapped std::map +// can be passed. +// -- std::map<T> f(): +// the map is returned by copy; therefore, a Scheme alist +// is returned which is most easily used in other Scheme functions +// -- std::map<T>& f(), std::map<T>* f(), const std::map<T>& f(), +// const std::map<T>* f(): +// the map is returned by reference; therefore, a wrapped std::map +// is returned +// ------------------------------------------------------------------------ + +%{ +#include <map> +#include <algorithm> +#include <stdexcept> +%} + +// exported class + +namespace std { + + template<class K, class T> class map { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (SCHEME_NULLP($input)) { + $1 = std::map<K,T >(); + } else if (SCHEME_PAIRP($input)) { + $1 = std::map<K,T >(); + Scheme_Object* alist = $input; + while (!SCHEME_NULLP(alist)) { + K* k; + T* x; + Scheme_Object *entry, *key, *val; + entry = scheme_car(alist); + if (!SCHEME_PAIRP(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = scheme_car(entry); + val = scheme_cdr(entry); + k = (K*) SWIG_MustGetPtr(key,$descriptor(K *),$argnum); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) == -1) { + if (!SCHEME_PAIRP(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = scheme_car(val); + x = (T*) SWIG_MustGetPtr(val,$descriptor(T *),$argnum); + } + (($1_type &)$1)[*k] = *x; + alist = scheme_cdr(alist); + } + } else { + $1 = *(($&1_type) + SWIG_MustGetPtr($input,$&1_descriptor,$argnum)); + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (SCHEME_NULLP($input)) { + temp = std::map<K,T >(); + $1 = &temp; + } else if (SCHEME_PAIRP($input)) { + temp = std::map<K,T >(); + $1 = &temp; + Scheme_Object* alist = $input; + while (!SCHEME_NULLP(alist)) { + K* k; + T* x; + Scheme_Object *entry, *key, *val; + entry = scheme_car(alist); + if (!SCHEME_PAIRP(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = scheme_car(entry); + val = scheme_cdr(entry); + k = (K*) SWIG_MustGetPtr(key,$descriptor(K *),$argnum); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) == -1) { + if (!SCHEME_PAIRP(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = scheme_car(val); + x = (T*) SWIG_MustGetPtr(val,$descriptor(T *),$argnum); + } + temp[*k] = *x; + alist = scheme_cdr(alist); + } + } else { + $1 = ($1_ltype) SWIG_MustGetPtr($input,$1_descriptor,$argnum); + } + } + %typemap(out) map<K,T> { + Scheme_Object* alist = scheme_null; + for (std::map<K,T >::reverse_iterator i=$1.rbegin(); + i!=$1.rend(); ++i) { + K* key = new K(i->first); + T* val = new T(i->second); + Scheme_Object* k = SWIG_MakePtr(key,$descriptor(K *)); + Scheme_Object* x = SWIG_MakePtr(val,$descriptor(T *)); + Scheme_Object* entry = scheme_make_pair(k,x); + alist = scheme_make_pair(entry,alist); + } + $result = alist; + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + /* native sequence? */ + if (SCHEME_NULLP($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (SCHEME_PAIRP($input)) { + /* check the first element only */ + K* k; + T* x; + Scheme_Object* head = scheme_car($input); + if (SCHEME_PAIRP(head)) { + Scheme_Object* key = scheme_car(head); + Scheme_Object* val = scheme_cdr(head); + if (SWIG_GetPtr(key,(void**) &k, + $descriptor(K *)) == -1) { + $1 = 0; + } else { + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) != -1) { + $1 = 1; + } else if (SCHEME_PAIRP(val)) { + val = scheme_car(val); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) != -1) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + /* wrapped map? */ + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $&1_descriptor) != -1) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + /* native sequence? */ + if (SCHEME_NULLP($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (SCHEME_PAIRP($input)) { + /* check the first element only */ + K* k; + T* x; + Scheme_Object* head = scheme_car($input); + if (SCHEME_PAIRP(head)) { + Scheme_Object* key = scheme_car(head); + Scheme_Object* val = scheme_cdr(head); + if (SWIG_GetPtr(key,(void**) &k, + $descriptor(K *)) == -1) { + $1 = 0; + } else { + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) != -1) { + $1 = 1; + } else if (SCHEME_PAIRP(val)) { + val = scheme_car(val); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) != -1) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + /* wrapped map? */ + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $1_descriptor) != -1) + $1 = 1; + else + $1 = 0; + } + } + %rename("length") size; + %rename("null?") empty; + %rename("clear!") clear; + %rename("ref") __getitem__; + %rename("set!") __setitem__; + %rename("delete!") __delitem__; + %rename("has-key?") has_key; + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& __getitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(const K& key, const T& x) { + (*self)[key] = x; + } + void __delitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + /* + void each() { + swig_type_info* k_type = SWIG_TypeQuery(#K " *"); + swig_type_info* t_type = SWIG_TypeQuery(#T " *"); + std::map<K,T >::iterator i; + for (i=self->begin(); i!=self->end(); ++i) { + K* key = new K(i->first); + T* val = &(i->second); + VALUE entry = rb_ary_new2(2); + VALUE k = SWIG_NewPointerObj((void *) key,k_type,1); + VALUE x = SWIG_NewPointerObj((void *) val,t_type,0); + rb_ary_store(entry,0,k); + rb_ary_store(entry,1,x); + rb_yield(entry); + } + } + */ + } + }; + + + // specializations for built-ins + + %define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO) + + template<class T> class map<K,T> { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (SCHEME_NULLP($input)) { + $1 = std::map<K,T >(); + } else if (SCHEME_PAIRP($input)) { + $1 = std::map<K,T >(); + Scheme_Object* alist = $input; + while (!SCHEME_NULLP(alist)) { + T* x; + Scheme_Object *entry, *key, *val; + entry = scheme_car(alist); + if (!SCHEME_PAIRP(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = scheme_car(entry); + val = scheme_cdr(entry); + if (!CHECK(key)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) == -1) { + if (!SCHEME_PAIRP(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = scheme_car(val); + x = (T*) SWIG_MustGetPtr(val,$descriptor(T *),$argnum); + } + (($1_type &)$1)[CONVERT_FROM(key)] = *x; + alist = scheme_cdr(alist); + } + } else { + $1 = *(($&1_type) + SWIG_MustGetPtr($input,$&1_descriptor,$argnum)); + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (SCHEME_NULLP($input)) { + temp = std::map<K,T >(); + $1 = &temp; + } else if (SCHEME_PAIRP($input)) { + temp = std::map<K,T >(); + $1 = &temp; + Scheme_Object* alist = $input; + while (!SCHEME_NULLP(alist)) { + T* x; + Scheme_Object *entry, *key, *val; + entry = scheme_car(alist); + if (!SCHEME_PAIRP(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = scheme_car(entry); + val = scheme_cdr(entry); + if (!CHECK(key)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) == -1) { + if (!SCHEME_PAIRP(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = scheme_car(val); + x = (T*) SWIG_MustGetPtr(val,$descriptor(T *),$argnum); + } + temp[CONVERT_FROM(key)] = *x; + alist = scheme_cdr(alist); + } + } else { + $1 = ($1_ltype) SWIG_MustGetPtr($input,$1_descriptor,$argnum); + } + } + %typemap(out) map<K,T> { + Scheme_Object* alist = scheme_null; + for (std::map<K,T >::reverse_iterator i=$1.rbegin(); + i!=$1.rend(); ++i) { + T* val = new T(i->second); + Scheme_Object* k = CONVERT_TO(i->first); + Scheme_Object* x = SWIG_MakePtr(val,$descriptor(T *)); + Scheme_Object* entry = scheme_make_pair(k,x); + alist = scheme_make_pair(entry,alist); + } + $result = alist; + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + // native sequence? + if (SCHEME_NULLP($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (SCHEME_PAIRP($input)) { + // check the first element only + T* x; + Scheme_Object* head = scheme_car($input); + if (SCHEME_PAIRP(head)) { + Scheme_Object* key = scheme_car(head); + Scheme_Object* val = scheme_cdr(head); + if (!CHECK(key)) { + $1 = 0; + } else { + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) != -1) { + $1 = 1; + } else if (SCHEME_PAIRP(val)) { + val = scheme_car(val); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) != -1) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $&1_descriptor) != -1) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + // native sequence? + if (SCHEME_NULLP($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (SCHEME_PAIRP($input)) { + // check the first element only + T* x; + Scheme_Object* head = scheme_car($input); + if (SCHEME_PAIRP(head)) { + Scheme_Object* key = scheme_car(head); + Scheme_Object* val = scheme_cdr(head); + if (!CHECK(key)) { + $1 = 0; + } else { + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) != -1) { + $1 = 1; + } else if (SCHEME_PAIRP(val)) { + val = scheme_car(val); + if (SWIG_GetPtr(val,(void**) &x, + $descriptor(T *)) != -1) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $1_descriptor) != -1) + $1 = 1; + else + $1 = 0; + } + } + %rename("length") size; + %rename("null?") empty; + %rename("clear!") clear; + %rename("ref") __getitem__; + %rename("set!") __setitem__; + %rename("delete!") __delitem__; + %rename("has-key?") has_key; + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& __getitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(K key, const T& x) { + (*self)[key] = x; + } + void __delitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + /* + void each() { + swig_type_info* t_type = SWIG_TypeQuery(#T " *"); + std::map<K,T >::iterator i; + for (i=self->begin(); i!=self->end(); ++i) { + T* val = &(i->second); + VALUE entry = rb_ary_new2(2); + VALUE k = CONVERT_TO(i->first); + VALUE x = SWIG_NewPointerObj((void *) val,t_type,0); + rb_ary_store(entry,0,k); + rb_ary_store(entry,1,x); + rb_yield(entry); + } + } + */ + } + }; + %enddef + + %define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO) + template<class K> class map<K,T> { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (SCHEME_NULLP($input)) { + $1 = std::map<K,T >(); + } else if (SCHEME_PAIRP($input)) { + $1 = std::map<K,T >(); + Scheme_Object* alist = $input; + while (!SCHEME_NULLP(alist)) { + K* k; + Scheme_Object *entry, *key, *val; + entry = scheme_car(alist); + if (!SCHEME_PAIRP(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = scheme_car(entry); + val = scheme_cdr(entry); + k = (K*) SWIG_MustGetPtr(key,$descriptor(K *),$argnum); + if (!CHECK(val)) { + if (!SCHEME_PAIRP(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = scheme_car(val); + if (!CHECK(val)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + } + (($1_type &)$1)[*k] = CONVERT_FROM(val); + alist = scheme_cdr(alist); + } + } else { + $1 = *(($&1_type) + SWIG_MustGetPtr($input,$&1_descriptor,$argnum)); + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (SCHEME_NULLP($input)) { + temp = std::map<K,T >(); + $1 = &temp; + } else if (SCHEME_PAIRP($input)) { + temp = std::map<K,T >(); + $1 = &temp; + Scheme_Object* alist = $input; + while (!SCHEME_NULLP(alist)) { + K* k; + Scheme_Object *entry, *key, *val; + entry = scheme_car(alist); + if (!SCHEME_PAIRP(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = scheme_car(entry); + val = scheme_cdr(entry); + k = (K*) SWIG_MustGetPtr(key,$descriptor(K *),$argnum); + if (!CHECK(val)) { + if (!SCHEME_PAIRP(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = scheme_car(val); + if (!CHECK(val)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + } + temp[*k] = CONVERT_FROM(val); + alist = scheme_cdr(alist); + } + } else { + $1 = ($1_ltype) SWIG_MustGetPtr($input,$1_descriptor,$argnum); + } + } + %typemap(out) map<K,T> { + Scheme_Object* alist = scheme_null; + for (std::map<K,T >::reverse_iterator i=$1.rbegin(); + i!=$1.rend(); ++i) { + K* key = new K(i->first); + Scheme_Object* k = SWIG_MakePtr(key,$descriptor(K *)); + Scheme_Object* x = CONVERT_TO(i->second); + Scheme_Object* entry = scheme_make_pair(k,x); + alist = scheme_make_pair(entry,alist); + } + $result = alist; + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + // native sequence? + if (SCHEME_NULLP($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (SCHEME_PAIRP($input)) { + // check the first element only + K* k; + Scheme_Object* head = scheme_car($input); + if (SCHEME_PAIRP(head)) { + Scheme_Object* key = scheme_car(head); + Scheme_Object* val = scheme_cdr(head); + if (SWIG_GetPtr(val,(void **) &k, + $descriptor(K *)) == -1) { + $1 = 0; + } else { + if (CHECK(val)) { + $1 = 1; + } else if (SCHEME_PAIRP(val)) { + val = scheme_car(val); + if (CHECK(val)) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $&1_descriptor) != -1) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + // native sequence? + if (SCHEME_NULLP($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (SCHEME_PAIRP($input)) { + // check the first element only + K* k; + Scheme_Object* head = scheme_car($input); + if (SCHEME_PAIRP(head)) { + Scheme_Object* key = scheme_car(head); + Scheme_Object* val = scheme_cdr(head); + if (SWIG_GetPtr(val,(void **) &k, + $descriptor(K *)) == -1) { + $1 = 0; + } else { + if (CHECK(val)) { + $1 = 1; + } else if (SCHEME_PAIRP(val)) { + val = scheme_car(val); + if (CHECK(val)) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $1_descriptor) != -1) + $1 = 1; + else + $1 = 0; + } + } + %rename("length") size; + %rename("null?") empty; + %rename("clear!") clear; + %rename("ref") __getitem__; + %rename("set!") __setitem__; + %rename("delete!") __delitem__; + %rename("has-key?") has_key; + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T __getitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(const K& key, T x) { + (*self)[key] = x; + } + void __delitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + /* + void each() { + swig_type_info* k_type = SWIG_TypeQuery(#K " *"); + std::map<K,T >::iterator i; + for (i=self->begin(); i!=self->end(); ++i) { + K* key = new K(i->first); + VALUE entry = rb_ary_new2(2); + VALUE k = SWIG_NewPointerObj((void *) key,k_type,1); + VALUE x = CONVERT_TO(i->second); + rb_ary_store(entry,0,k); + rb_ary_store(entry,1,x); + rb_yield(entry); + } + } + */ + } + }; + %enddef + + %define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO, + T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO) + template<> class map<K,T> { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (SCHEME_NULLP($input)) { + $1 = std::map<K,T >(); + } else if (SCHEME_PAIRP($input)) { + $1 = std::map<K,T >(); + Scheme_Object* alist = $input; + while (!SCHEME_NULLP(alist)) { + Scheme_Object *entry, *key, *val; + entry = scheme_car(alist); + if (!SCHEME_PAIRP(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = scheme_car(entry); + val = scheme_cdr(entry); + if (!CHECK_K(key)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + if (!CHECK_T(val)) { + if (!SCHEME_PAIRP(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = scheme_car(val); + if (!CHECK_T(val)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + } + (($1_type &)$1)[CONVERT_K_FROM(key)] = + CONVERT_T_FROM(val); + alist = scheme_cdr(alist); + } + } else { + $1 = *(($&1_type) + SWIG_MustGetPtr($input,$&1_descriptor,$argnum)); + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (SCHEME_NULLP($input)) { + temp = std::map<K,T >(); + $1 = &temp; + } else if (SCHEME_PAIRP($input)) { + temp = std::map<K,T >(); + $1 = &temp; + Scheme_Object* alist = $input; + while (!SCHEME_NULLP(alist)) { + Scheme_Object *entry, *key, *val; + entry = scheme_car(alist); + if (!SCHEME_PAIRP(entry)) + SWIG_exception(SWIG_TypeError,"alist expected"); + key = scheme_car(entry); + val = scheme_cdr(entry); + if (!CHECK_K(key)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + if (!CHECK_T(val)) { + if (!SCHEME_PAIRP(val)) + SWIG_exception(SWIG_TypeError,"alist expected"); + val = scheme_car(val); + if (!CHECK_T(val)) + SWIG_exception(SWIG_TypeError, + "map<" #K "," #T "> expected"); + } + temp[CONVERT_K_FROM(key)] = CONVERT_T_FROM(val); + alist = scheme_cdr(alist); + } + } else { + $1 = ($1_ltype) SWIG_MustGetPtr($input,$1_descriptor,$argnum); + } + } + %typemap(out) map<K,T> { + Scheme_Object* alist = scheme_null; + for (std::map<K,T >::reverse_iterator i=$1.rbegin(); + i!=$1.rend(); ++i) { + Scheme_Object* k = CONVERT_K_TO(i->first); + Scheme_Object* x = CONVERT_T_TO(i->second); + Scheme_Object* entry = scheme_make_pair(k,x); + alist = scheme_make_pair(entry,alist); + } + $result = alist; + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + // native sequence? + if (SCHEME_NULLP($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (SCHEME_PAIRP($input)) { + // check the first element only + Scheme_Object* head = scheme_car($input); + if (SCHEME_PAIRP(head)) { + Scheme_Object* key = scheme_car(head); + Scheme_Object* val = scheme_cdr(head); + if (!CHECK_K(key)) { + $1 = 0; + } else { + if (CHECK_T(val)) { + $1 = 1; + } else if (SCHEME_PAIRP(val)) { + val = scheme_car(val); + if (CHECK_T(val)) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $&1_descriptor) != -1) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + // native sequence? + if (SCHEME_NULLP($input)) { + /* an empty sequence can be of any type */ + $1 = 1; + } else if (SCHEME_PAIRP($input)) { + // check the first element only + Scheme_Object* head = scheme_car($input); + if (SCHEME_PAIRP(head)) { + Scheme_Object* key = scheme_car(head); + Scheme_Object* val = scheme_cdr(head); + if (!CHECK_K(key)) { + $1 = 0; + } else { + if (CHECK_T(val)) { + $1 = 1; + } else if (SCHEME_PAIRP(val)) { + val = scheme_car(val); + if (CHECK_T(val)) + $1 = 1; + else + $1 = 0; + } else { + $1 = 0; + } + } + } else { + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_GetPtr($input,(void **) &m, + $1_descriptor) != -1) + $1 = 1; + else + $1 = 0; + } + } + %rename("length") size; + %rename("null?") empty; + %rename("clear!") clear; + %rename("ref") __getitem__; + %rename("set!") __setitem__; + %rename("delete!") __delitem__; + %rename("has-key?") has_key; + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T __getitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(K key, T x) { + (*self)[key] = x; + } + void __delitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + /* + void each() { + std::map<K,T >::iterator i; + for (i=self->begin(); i!=self->end(); ++i) { + VALUE entry = rb_ary_new2(2); + rb_ary_store(entry,0,CONVERT_K_TO(i->first)); + rb_ary_store(entry,1,CONVERT_T_TO(i->second)); + rb_yield(entry); + } + } + */ + } + }; + %enddef + + + specialize_std_map_on_key(bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean); + specialize_std_map_on_key(int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_key(short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_key(long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_key(unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_key(unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_key(unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_key(double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_key(float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_key(std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string); + + specialize_std_map_on_value(bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean); + specialize_std_map_on_value(int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_value(short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_value(long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_value(unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_value(unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_value(unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_value(double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_value(float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_value(std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string); + + specialize_std_map_on_both(bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean, + bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean); + specialize_std_map_on_both(bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean, + int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean, + short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean, + long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean, + unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean, + unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean, + unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean, + double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean, + float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean, + std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string); + specialize_std_map_on_both(int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean); + specialize_std_map_on_both(int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string); + specialize_std_map_on_both(short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean); + specialize_std_map_on_both(short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string); + specialize_std_map_on_both(long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean); + specialize_std_map_on_both(long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string); + specialize_std_map_on_both(unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean); + specialize_std_map_on_both(unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string); + specialize_std_map_on_both(unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean); + specialize_std_map_on_both(unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string); + specialize_std_map_on_both(unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean); + specialize_std_map_on_both(unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value, + std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string); + specialize_std_map_on_both(double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean); + specialize_std_map_on_both(double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string); + specialize_std_map_on_both(float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean); + specialize_std_map_on_both(float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double, + std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string); + specialize_std_map_on_both(std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string, + bool,SCHEME_BOOLP, + SCHEME_TRUEP,swig_make_boolean); + specialize_std_map_on_both(std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string, + int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string, + short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string, + long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string, + unsigned int,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string, + unsigned short,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string, + unsigned long,SCHEME_INTP, + SCHEME_INT_VAL,scheme_make_integer_value); + specialize_std_map_on_both(std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string, + double,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string, + float,SCHEME_REALP, + scheme_real_to_double,scheme_make_double); + specialize_std_map_on_both(std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string, + std::string,SCHEME_STRINGP, + swig_scm_to_string,swig_make_string); +} diff --git a/Lib/ocaml/std_map.i b/Lib/ocaml/std_map.i new file mode 100644 index 000000000..38a063e95 --- /dev/null +++ b/Lib/ocaml/std_map.i @@ -0,0 +1,190 @@ +// +// SWIG typemaps for std::map +// Luigi Ballabio +// Jan. 2003 +// +// Common implementation + +%include std_common.i +%include exception.i + +%exception std::map::get { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +%exception std::map::del { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +// ------------------------------------------------------------------------ +// std::map +// ------------------------------------------------------------------------ + +%{ +#include <map> +#include <algorithm> +#include <stdexcept> +%} + +// exported class + +namespace std { + + template<class K, class T> class map { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& get(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(const K& key, const T& x) { + (*self)[key] = x; + } + void del(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + + + // specializations for built-ins + + %define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO) + + template<class T> class map<K,T> { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& get(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(K key, const T& x) { + (*self)[key] = x; + } + void del(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + %enddef + + %define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO) + template<class K> class map<K,T> { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T get(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(const K& key, T x) { + (*self)[key] = x; + } + void del(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + %enddef + + %define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO, + T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO) + template<> class map<K,T> { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T get(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(K key, T x) { + (*self)[key] = x; + } + void del(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + %enddef + + // add specializations here + +} diff --git a/Lib/perl5/std_map.i b/Lib/perl5/std_map.i new file mode 100644 index 000000000..38a063e95 --- /dev/null +++ b/Lib/perl5/std_map.i @@ -0,0 +1,190 @@ +// +// SWIG typemaps for std::map +// Luigi Ballabio +// Jan. 2003 +// +// Common implementation + +%include std_common.i +%include exception.i + +%exception std::map::get { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +%exception std::map::del { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +// ------------------------------------------------------------------------ +// std::map +// ------------------------------------------------------------------------ + +%{ +#include <map> +#include <algorithm> +#include <stdexcept> +%} + +// exported class + +namespace std { + + template<class K, class T> class map { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& get(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(const K& key, const T& x) { + (*self)[key] = x; + } + void del(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + + + // specializations for built-ins + + %define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO) + + template<class T> class map<K,T> { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& get(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(K key, const T& x) { + (*self)[key] = x; + } + void del(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + %enddef + + %define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO) + template<class K> class map<K,T> { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T get(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(const K& key, T x) { + (*self)[key] = x; + } + void del(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + %enddef + + %define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO, + T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO) + template<> class map<K,T> { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T get(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(K key, T x) { + (*self)[key] = x; + } + void del(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + %enddef + + // add specializations here + +} diff --git a/Lib/php4/std_map.i b/Lib/php4/std_map.i new file mode 100644 index 000000000..38a063e95 --- /dev/null +++ b/Lib/php4/std_map.i @@ -0,0 +1,190 @@ +// +// SWIG typemaps for std::map +// Luigi Ballabio +// Jan. 2003 +// +// Common implementation + +%include std_common.i +%include exception.i + +%exception std::map::get { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +%exception std::map::del { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +// ------------------------------------------------------------------------ +// std::map +// ------------------------------------------------------------------------ + +%{ +#include <map> +#include <algorithm> +#include <stdexcept> +%} + +// exported class + +namespace std { + + template<class K, class T> class map { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& get(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(const K& key, const T& x) { + (*self)[key] = x; + } + void del(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + + + // specializations for built-ins + + %define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO) + + template<class T> class map<K,T> { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& get(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(K key, const T& x) { + (*self)[key] = x; + } + void del(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + %enddef + + %define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO) + template<class K> class map<K,T> { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T get(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(const K& key, T x) { + (*self)[key] = x; + } + void del(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + %enddef + + %define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO, + T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO) + template<> class map<K,T> { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T get(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(K key, T x) { + (*self)[key] = x; + } + void del(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + %enddef + + // add specializations here + +} diff --git a/Lib/python/std_map.i b/Lib/python/std_map.i new file mode 100644 index 000000000..ab7b16ddc --- /dev/null +++ b/Lib/python/std_map.i @@ -0,0 +1,1551 @@ +// +// SWIG typemaps for std::map +// Luigi Ballabio +// Jan. 2003 +// +// Python implementation + +%include std_common.i +%include exception.i + +%exception std::map::__getitem__ { + try { + $action + } catch (std::out_of_range& e) { + PyErr_SetString(PyExc_KeyError,const_cast<char*>(e.what())); + SWIG_fail; + } +} + +%exception std::map::__delitem__ { + try { + $action + } catch (std::out_of_range& e) { + PyErr_SetString(PyExc_KeyError,const_cast<char*>(e.what())); + SWIG_fail; + } +} + +%exception std::map::__iter__ { + try { + $action + } catch (std::runtime_error& e) { + PyErr_SetString(PyExc_RuntimeError,const_cast<char*>(e.what())); + SWIG_fail; + } +} + + +// ------------------------------------------------------------------------ +// std::map +// +// The aim of all that follows would be to integrate std::map with +// Python as much as possible, namely, to allow the user to pass and +// be returned Python dictionaries. +// const declarations are used to guess the intent of the function being +// exported; therefore, the following rationale is applied: +// +// -- f(std::map<T>), f(const std::map<T>&), f(const std::map<T>*): +// the parameter being read-only, either a Python dictionary or a +// previously wrapped std::map<T> can be passed. +// -- f(std::map<T>&), f(std::map<T>*): +// the parameter must be modified; therefore, only a wrapped std::map +// can be passed. +// -- std::map<T> f(): +// the map is returned by copy; therefore, a Python dictionary +// is returned which is most easily used in other Python functions +// -- std::map<T>& f(), std::map<T>* f(), const std::map<T>& f(), +// const std::map<T>* f(): +// the map is returned by reference; therefore, a wrapped std::map +// is returned +// ------------------------------------------------------------------------ + +%{ +#include <map> +#include <algorithm> +#include <stdexcept> +%} + +// exported class + +namespace std { + + template<class K, class T> class map { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (PyDict_Check($input)) { + $1 = std::map<K,T >(); + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + for (unsigned int i=0; i<size; i++) { + K* k; + T* x; + PyObject* pair = PySequence_GetItem(items,i); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (SWIG_ConvertPtr(key,(void **) &k, + $descriptor(K *),0) != -1 && + SWIG_ConvertPtr(o,(void **) &x, + $descriptor(T *),0) != -1) { + (($1_type &)$1)[*k] = *x; + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } else { + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + Py_DECREF(items); + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + Py_DECREF(items); + } else if (SWIG_ConvertPtr($input,(void **) &m, + $&1_descriptor,0) != -1) { + $1 = *m; + } else { + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (PyDict_Check($input)) { + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + temp = std::map<K,T >(); + $1 = &temp; + for (unsigned int i=0; i<size; i++) { + K* k; + T* x; + PyObject* pair = PySequence_GetItem(items,i); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (SWIG_ConvertPtr(key,(void **) &k, + $descriptor(K *),0) != -1 && + SWIG_ConvertPtr(o,(void **) &x, + $descriptor(T *),0) != -1) { + temp[*k] = *x; + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } else { + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + Py_DECREF(items); + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + Py_DECREF(items); + } else if (SWIG_ConvertPtr($input,(void **) &m, + $1_descriptor,0) != -1) { + $1 = m; + } else { + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + %typemap(out) map<K,T> { + $result = PyDict_New(); + for (std::map<K,T >::iterator i=$1.begin(); i!=$1.end(); ++i) { + K* key = new K(i->first); + T* obj = new T(i->second); + PyDict_SetItem($result, + SWIG_NewPointerObj((void *) key, + $descriptor(K *), 1), + SWIG_NewPointerObj((void *) obj, + $descriptor(T *), 1)); + } + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + /* native sequence? */ + if (PyDict_Check($input)) { + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + if (size == 0) { + /* an empty dictionary can be of any type */ + $1 = 1; + } else { + /* check the first element only */ + K* k; + T* x; + PyObject* pair = PySequence_GetItem(items,0); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (SWIG_ConvertPtr(key,(void **) &k, + $descriptor(K *),0) != -1 && + SWIG_ConvertPtr(o,(void **) &x, + $descriptor(T *),0) != -1) + $1 = 1; + else + $1 = 0; + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } + Py_DECREF(items); + } else { + /* wrapped map? */ + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $&1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + /* native sequence? */ + if (PyDict_Check($input)) { + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + if (size == 0) { + /* an empty dictionary can be of any type */ + $1 = 1; + } else { + /* check the first element only */ + K* k; + T* x; + PyObject* pair = PySequence_GetItem(items,0); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (SWIG_ConvertPtr(key,(void **) &k, + $descriptor(K *),0) != -1 && + SWIG_ConvertPtr(o,(void **) &x, + $descriptor(T *),0) != -1) + $1 = 1; + else + $1 = 0; + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } + Py_DECREF(items); + } else { + /* wrapped map? */ + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + public: + map(); + map(const map<K,T> &); + + %rename(__len__) size; + unsigned int size() const; + void clear(); + %extend { + bool __nonzero__() { + return !(self->empty()); + } + T& __getitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(const K& key, const T& x) { + (*self)[key] = x; + } + void __delitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + PyObject* keys() { + PyObject* keyList = PyList_New(self->size()); + swig_type_info* type = SWIG_TypeQuery(#K " *"); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + K* ptr = new K(i->first); + PyList_SetItem(keyList,j, + SWIG_NewPointerObj((void *) ptr,type,1)); + } + return keyList; + } + PyObject* values() { + PyObject* valueList = PyList_New(self->size()); + swig_type_info* type = SWIG_TypeQuery(#T " *"); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + T* ptr = new T(i->second); + PyList_SetItem(valueList,j, + SWIG_NewPointerObj((void *) ptr,type,1)); + } + return valueList; + } + PyObject* items() { + PyObject* itemList = PyList_New(self->size()); + swig_type_info* k_type = SWIG_TypeQuery(#K " *"); + swig_type_info* t_type = SWIG_TypeQuery(#T " *"); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + K* k_ptr = new K(i->first); + T* t_ptr = new T(i->second); + PyObject* item = PyTuple_New(2); + PyTuple_SetItem(item,0, + SWIG_NewPointerObj((void *) k_ptr, + k_type,1)); + PyTuple_SetItem(item,1, + SWIG_NewPointerObj((void *) t_ptr, + t_type,1)); + PyList_SetItem(itemList,j,item); + } + return itemList; + } + // Python 2.2 methods + bool __contains__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + PyObject* __iter__() { + %#if PY_VERSION_HEX >= 0x02020000 + PyObject* keyList = PyList_New(self->size()); + swig_type_info* type = SWIG_TypeQuery(#K " *"); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + K* ptr = new K(i->first); + PyList_SetItem(keyList,j, + SWIG_NewPointerObj((void *) ptr,type,1)); + } + PyObject* iter = PyObject_GetIter(keyList); + Py_DECREF(keyList); + return iter; + %#else + throw std::runtime_error("Python 2.2 or later is needed" + " for iterator support"); + %#endif + } + } + }; + + + // specializations for built-ins + + %define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO) + template<class T> class map<K,T> { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (PyDict_Check($input)) { + $1 = std::map<K,T >(); + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + for (unsigned int i=0; i<size; i++) { + T* x; + PyObject* pair = PySequence_GetItem(items,i); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (CHECK(key) && + SWIG_ConvertPtr(o,(void **) &x, + $descriptor(T *),0) != -1) { + (($1_type &)$1)[CONVERT_FROM(key)] = *x; + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } else { + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + Py_DECREF(items); + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + Py_DECREF(items); + } else if (SWIG_ConvertPtr($input,(void **) &m, + $&1_descriptor,0) != -1) { + $1 = *m; + } else { + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (PyDict_Check($input)) { + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + temp = std::map<K,T >(); + $1 = &temp; + for (unsigned int i=0; i<size; i++) { + T* x; + PyObject* pair = PySequence_GetItem(items,i); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (CHECK(key) && + SWIG_ConvertPtr(o,(void **) &x, + $descriptor(T *),0) != -1) { + temp[CONVERT_FROM(key)] = *x; + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } else { + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + Py_DECREF(items); + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + Py_DECREF(items); + } else if (SWIG_ConvertPtr($input,(void **) &m, + $1_descriptor,0) != -1) { + $1 = m; + } else { + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + %typemap(out) map<K,T> { + $result = PyDict_New(); + for (std::map<K,T >::iterator i=$1.begin(); i!=$1.end(); ++i) { + T* obj = new T(i->second); + PyDict_SetItem($result, + CONVERT_TO(i->first), + SWIG_NewPointerObj((void *) obj, + $descriptor(T *), 1)); + } + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + /* native sequence? */ + if (PyDict_Check($input)) { + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + if (size == 0) { + /* an empty dictionary can be of any type */ + $1 = 1; + } else { + /* check the first element only */ + T* x; + PyObject* pair = PySequence_GetItem(items,0); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (CHECK(key) && + SWIG_ConvertPtr(o,(void **) &x, + $descriptor(T *),0) != -1) + $1 = 1; + else + $1 = 0; + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } + Py_DECREF(items); + } else { + /* wrapped map? */ + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $&1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + /* native sequence? */ + if (PyDict_Check($input)) { + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + if (size == 0) { + /* an empty dictionary can be of any type */ + $1 = 1; + } else { + /* check the first element only */ + T* x; + PyObject* pair = PySequence_GetItem(items,0); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (CHECK(key) && + SWIG_ConvertPtr(o,(void **) &x, + $descriptor(T *),0) != -1) + $1 = 1; + else + $1 = 0; + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } + Py_DECREF(items); + } else { + /* wrapped map? */ + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + public: + map(); + map(const map<K,T> &); + + %rename(__len__) size; + unsigned int size() const; + void clear(); + %extend { + bool __nonzero__() { + return !(self->empty()); + } + T& __getitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(K key, const T& x) { + (*self)[key] = x; + } + void __delitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + PyObject* keys() { + PyObject* keyList = PyList_New(self->size()); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + PyList_SetItem(keyList,j, + CONVERT_TO(i->first)); + } + return keyList; + } + PyObject* values() { + PyObject* valueList = PyList_New(self->size()); + swig_type_info* type = SWIG_TypeQuery(#T " *"); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + T* ptr = new T(i->second); + PyList_SetItem(valueList,j, + SWIG_NewPointerObj((void *) ptr,type,1)); + } + return valueList; + } + PyObject* items() { + PyObject* itemList = PyList_New(self->size()); + swig_type_info* t_type = SWIG_TypeQuery(#T " *"); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + T* t_ptr = new T(i->second); + PyObject* item = PyTuple_New(2); + PyTuple_SetItem(item,0, + CONVERT_TO(i->first)); + PyTuple_SetItem(item,1, + SWIG_NewPointerObj((void *) t_ptr, + t_type,1)); + PyList_SetItem(itemList,j,item); + } + return itemList; + } + // Python 2.2 methods + bool __contains__(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + PyObject* __iter__() { + %#if PY_VERSION_HEX >= 0x02020000 + PyObject* keyList = PyList_New(self->size()); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + PyList_SetItem(keyList,j, + CONVERT_TO(i->first)); + } + PyObject* iter = PyObject_GetIter(keyList); + Py_DECREF(keyList); + return iter; + %#else + throw std::runtime_error("Python 2.2 or later is needed" + " for iterator support"); + %#endif + } + } + }; + %enddef + + %define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO) + template<class K> class map<K,T> { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (PyDict_Check($input)) { + $1 = std::map<K,T >(); + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + for (unsigned int i=0; i<size; i++) { + K* k; + PyObject* pair = PySequence_GetItem(items,i); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (SWIG_ConvertPtr(key,(void **) &k, + $descriptor(K *),0) != -1 && + CHECK(o)) { + (($1_type &)$1)[*k] = CONVERT_FROM(o); + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } else { + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + Py_DECREF(items); + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + Py_DECREF(items); + } else if (SWIG_ConvertPtr($input,(void **) &m, + $&1_descriptor,0) != -1) { + $1 = *m; + } else { + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (PyDict_Check($input)) { + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + temp = std::map<K,T >(); + $1 = &temp; + for (unsigned int i=0; i<size; i++) { + K* k; + PyObject* pair = PySequence_GetItem(items,i); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (SWIG_ConvertPtr(key,(void **) &k, + $descriptor(K *),0) != -1 && + CHECK(o)) { + temp[*k] = CONVERT_FROM(o); + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } else { + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + Py_DECREF(items); + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + Py_DECREF(items); + } else if (SWIG_ConvertPtr($input,(void **) &m, + $1_descriptor,0) != -1) { + $1 = m; + } else { + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + %typemap(out) map<K,T> { + $result = PyDict_New(); + for (std::map<K,T >::iterator i=$1.begin(); i!=$1.end(); ++i) { + K* key = new K(i->first); + PyDict_SetItem($result, + SWIG_NewPointerObj((void *) key, + $descriptor(K *), 1), + CONVERT_TO(i->second)); + } + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + /* native sequence? */ + if (PyDict_Check($input)) { + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + if (size == 0) { + /* an empty dictionary can be of any type */ + $1 = 1; + } else { + /* check the first element only */ + K* k; + PyObject* pair = PySequence_GetItem(items,0); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (SWIG_ConvertPtr(key,(void **) &k, + $descriptor(K *),0) != -1 && + CHECK(o)) + $1 = 1; + else + $1 = 0; + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } + Py_DECREF(items); + } else { + /* wrapped map? */ + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $&1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + /* native sequence? */ + if (PyDict_Check($input)) { + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + if (size == 0) { + /* an empty dictionary can be of any type */ + $1 = 1; + } else { + /* check the first element only */ + K* k; + PyObject* pair = PySequence_GetItem(items,0); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (SWIG_ConvertPtr(key,(void **) &k, + $descriptor(K *),0) != -1 && + CHECK(o)) + $1 = 1; + else + $1 = 0; + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } + Py_DECREF(items); + } else { + /* wrapped map? */ + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + public: + map(); + map(const map<K,T> &); + + %rename(__len__) size; + unsigned int size() const; + void clear(); + %extend { + bool __nonzero__() { + return !(self->empty()); + } + T __getitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(const K& key, T x) { + (*self)[key] = x; + } + void __delitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + PyObject* keys() { + PyObject* keyList = PyList_New(self->size()); + swig_type_info* type = SWIG_TypeQuery(#K " *"); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + K* ptr = new K(i->first); + PyList_SetItem(keyList,j, + SWIG_NewPointerObj((void *) ptr,type,1)); + } + return keyList; + } + PyObject* values() { + PyObject* valueList = PyList_New(self->size()); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + PyList_SetItem(valueList,j, + CONVERT_TO(i->second)); + } + return valueList; + } + PyObject* items() { + PyObject* itemList = PyList_New(self->size()); + swig_type_info* k_type = SWIG_TypeQuery(#K " *"); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + K* k_ptr = new K(i->first); + PyObject* item = PyTuple_New(2); + PyTuple_SetItem(item,0, + SWIG_NewPointerObj((void *) k_ptr, + k_type,1)); + PyTuple_SetItem(item,1, + CONVERT_TO(i->second)); + PyList_SetItem(itemList,j,item); + } + return itemList; + } + // Python 2.2 methods + bool __contains__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + PyObject* __iter__() { + %#if PY_VERSION_HEX >= 0x02020000 + PyObject* keyList = PyList_New(self->size()); + swig_type_info* type = SWIG_TypeQuery(#K " *"); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + K* ptr = new K(i->first); + PyList_SetItem(keyList,j, + SWIG_NewPointerObj((void *) ptr,type,1)); + } + PyObject* iter = PyObject_GetIter(keyList); + Py_DECREF(keyList); + return iter; + %#else + throw std::runtime_error("Python 2.2 or later is needed" + " for iterator support"); + %#endif + } + } + }; + %enddef + + %define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO, + T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO) + template<> class map<K,T> { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (PyDict_Check($input)) { + $1 = std::map<K,T >(); + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + for (unsigned int i=0; i<size; i++) { + PyObject* pair = PySequence_GetItem(items,i); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (CHECK_K(key) && CHECK_T(o)) { + (($1_type &)$1)[CONVERT_K_FROM(key)] = + CONVERT_T_FROM(o); + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } else { + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + Py_DECREF(items); + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + Py_DECREF(items); + } else if (SWIG_ConvertPtr($input,(void **) &m, + $&1_descriptor,0) != -1) { + $1 = *m; + } else { + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (PyDict_Check($input)) { + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + temp = std::map<K,T >(); + $1 = &temp; + for (unsigned int i=0; i<size; i++) { + PyObject* pair = PySequence_GetItem(items,i); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (CHECK_K(key) && CHECK_T(o)) { + temp[CONVERT_K_FROM(key)] = CONVERT_T_FROM(o); + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } else { + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + Py_DECREF(items); + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + Py_DECREF(items); + } else if (SWIG_ConvertPtr($input,(void **) &m, + $1_descriptor,0) != -1) { + $1 = m; + } else { + PyErr_SetString(PyExc_TypeError, + "map<" #K "," #T "> expected"); + SWIG_fail; + } + } + %typemap(out) map<K,T> { + $result = PyDict_New(); + for (std::map<K,T >::iterator i=$1.begin(); i!=$1.end(); ++i) { + PyDict_SetItem($result, + CONVERT_K_TO(i->first), + CONVERT_T_TO(i->second)); + } + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + /* native sequence? */ + if (PyDict_Check($input)) { + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + if (size == 0) { + /* an empty dictionary can be of any type */ + $1 = 1; + } else { + /* check the first element only */ + PyObject* pair = PySequence_GetItem(items,0); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (CHECK_K(key) && CHECK_T(o)) + $1 = 1; + else + $1 = 0; + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } + Py_DECREF(items); + } else { + /* wrapped map? */ + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $&1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + /* native sequence? */ + if (PyDict_Check($input)) { + PyObject* items = PyMapping_Items($input); + unsigned int size = PyList_Size(items); + if (size == 0) { + /* an empty dictionary can be of any type */ + $1 = 1; + } else { + /* check the first element only */ + PyObject* pair = PySequence_GetItem(items,0); + PyObject* key = PySequence_GetItem(pair,0); + PyObject* o = PySequence_GetItem(pair,1); + if (CHECK_K(key) && CHECK_T(o)) + $1 = 1; + else + $1 = 0; + Py_DECREF(key); + Py_DECREF(o); + Py_DECREF(pair); + } + Py_DECREF(items); + } else { + /* wrapped map? */ + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + public: + map(); + map(const map<K,T> &); + + %rename(__len__) size; + unsigned int size() const; + void clear(); + %extend { + bool __nonzero__() { + return !(self->empty()); + } + T __getitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(K key, T x) { + (*self)[key] = x; + } + void __delitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + PyObject* keys() { + PyObject* keyList = PyList_New(self->size()); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + PyList_SetItem(keyList,j, + CONVERT_K_TO(i->first)); + } + return keyList; + } + PyObject* values() { + PyObject* valueList = PyList_New(self->size()); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + PyList_SetItem(valueList,j, + CONVERT_T_TO(i->second)); + } + return valueList; + } + PyObject* items() { + PyObject* itemList = PyList_New(self->size()); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + PyObject* item = PyTuple_New(2); + PyTuple_SetItem(item,0, + CONVERT_K_TO(i->first)); + PyTuple_SetItem(item,1, + CONVERT_T_TO(i->second)); + PyList_SetItem(itemList,j,item); + } + return itemList; + } + // Python 2.2 methods + bool __contains__(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + PyObject* __iter__() { + %#if PY_VERSION_HEX >= 0x02020000 + PyObject* keyList = PyList_New(self->size()); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + PyList_SetItem(keyList,j, + CONVERT_K_TO(i->first)); + } + PyObject* iter = PyObject_GetIter(keyList); + Py_DECREF(keyList); + return iter; + %#else + throw std::runtime_error("Python 2.2 or later is needed" + " for iterator support"); + %#endif + } + } + }; + %enddef + + specialize_std_map_on_key(bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool); + specialize_std_map_on_key(int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_key(short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_key(long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong); + specialize_std_map_on_key(unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_key(unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_key(unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong); + specialize_std_map_on_key(double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_key(float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_key(std::string,PyString_Check, + SwigString_AsString,SwigString_FromString); + + specialize_std_map_on_value(bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool); + specialize_std_map_on_value(int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_value(short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_value(long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong); + specialize_std_map_on_value(unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_value(unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_value(unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong); + specialize_std_map_on_value(double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_value(float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_value(std::string,PyString_Check, + SwigString_AsString,SwigString_FromString); + + specialize_std_map_on_both(bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool, + bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool); + specialize_std_map_on_both(bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool, + int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool, + short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool, + long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong); + specialize_std_map_on_both(bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool, + unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool, + unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool, + unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong); + specialize_std_map_on_both(bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool, + double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool, + float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool, + std::string,PyString_Check, + SwigString_AsString,SwigString_FromString); + specialize_std_map_on_both(int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool); + specialize_std_map_on_both(int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong); + specialize_std_map_on_both(int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong); + specialize_std_map_on_both(int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + std::string,PyString_Check, + SwigString_AsString,SwigString_FromString); + specialize_std_map_on_both(short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool); + specialize_std_map_on_both(short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong); + specialize_std_map_on_both(short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong); + specialize_std_map_on_both(short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + std::string,PyString_Check, + SwigString_AsString,SwigString_FromString); + specialize_std_map_on_both(long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong, + bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool); + specialize_std_map_on_both(long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong, + int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong, + short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong, + long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong); + specialize_std_map_on_both(long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong, + unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong, + unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong, + unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong); + specialize_std_map_on_both(long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong, + double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong, + float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong, + std::string,PyString_Check, + SwigString_AsString,SwigString_FromString); + specialize_std_map_on_both(unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool); + specialize_std_map_on_both(unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong); + specialize_std_map_on_both(unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong); + specialize_std_map_on_both(unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + std::string,PyString_Check, + SwigString_AsString,SwigString_FromString); + specialize_std_map_on_both(unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool); + specialize_std_map_on_both(unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong); + specialize_std_map_on_both(unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong); + specialize_std_map_on_both(unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong, + std::string,PyString_Check, + SwigString_AsString,SwigString_FromString); + specialize_std_map_on_both(unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong, + bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool); + specialize_std_map_on_both(unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong, + int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong, + short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong, + long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong); + specialize_std_map_on_both(unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong, + unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong, + unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong, + unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong); + specialize_std_map_on_both(unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong, + double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong, + float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong, + std::string,PyString_Check, + SwigString_AsString,SwigString_FromString); + specialize_std_map_on_both(double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool); + specialize_std_map_on_both(double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong); + specialize_std_map_on_both(double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong); + specialize_std_map_on_both(double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + std::string,PyString_Check, + SwigString_AsString,SwigString_FromString); + specialize_std_map_on_both(float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool); + specialize_std_map_on_both(float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong); + specialize_std_map_on_both(float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong); + specialize_std_map_on_both(float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble, + std::string,PyString_Check, + SwigString_AsString,SwigString_FromString); + specialize_std_map_on_both(std::string,PyString_Check, + SwigString_AsString,SwigString_FromString, + bool,PyInt_Check, + PyInt_AsLong,SwigInt_FromBool); + specialize_std_map_on_both(std::string,PyString_Check, + SwigString_AsString,SwigString_FromString, + int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(std::string,PyString_Check, + SwigString_AsString,SwigString_FromString, + short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(std::string,PyString_Check, + SwigString_AsString,SwigString_FromString, + long,PyLong_Check, + PyLong_AsLong,PyLong_FromLong); + specialize_std_map_on_both(std::string,PyString_Check, + SwigString_AsString,SwigString_FromString, + unsigned int,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(std::string,PyString_Check, + SwigString_AsString,SwigString_FromString, + unsigned short,PyInt_Check, + PyInt_AsLong,PyInt_FromLong); + specialize_std_map_on_both(std::string,PyString_Check, + SwigString_AsString,SwigString_FromString, + unsigned long,PyLong_Check, + PyLong_AsUnsignedLong,PyLong_FromUnsignedLong); + specialize_std_map_on_both(std::string,PyString_Check, + SwigString_AsString,SwigString_FromString, + double,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(std::string,PyString_Check, + SwigString_AsString,SwigString_FromString, + float,SwigNumber_Check, + SwigNumber_AsDouble,PyFloat_FromDouble); + specialize_std_map_on_both(std::string,PyString_Check, + SwigString_AsString,SwigString_FromString, + std::string,PyString_Check, + SwigString_AsString,SwigString_FromString); + +} diff --git a/Lib/ruby/std_map.i b/Lib/ruby/std_map.i new file mode 100644 index 000000000..1e11280f1 --- /dev/null +++ b/Lib/ruby/std_map.i @@ -0,0 +1,1281 @@ +// +// SWIG typemaps for std::map +// Luigi Ballabio +// Jan. 2003 +// +// Ruby implementation + +%include std_common.i +%include exception.i + +%exception std::map::__getitem__ { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +%exception std::map::__delitem__ { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +// ------------------------------------------------------------------------ +// std::map +// +// The aim of all that follows would be to integrate std::map with +// Ruby as much as possible, namely, to allow the user to pass and +// be returned Ruby hash maps. +// const declarations are used to guess the intent of the function being +// exported; therefore, the following rationale is applied: +// +// -- f(std::map<T>), f(const std::map<T>&), f(const std::map<T>*): +// the parameter being read-only, either a Ruby hash or a +// previously wrapped std::map<T> can be passed. +// -- f(std::map<T>&), f(std::map<T>*): +// the parameter must be modified; therefore, only a wrapped std::map +// can be passed. +// -- std::map<T> f(): +// the map is returned by copy; therefore, a Ruby hash +// is returned which is most easily used in other Ruby functions +// -- std::map<T>& f(), std::map<T>* f(), const std::map<T>& f(), +// const std::map<T>* f(): +// the map is returned by reference; therefore, a wrapped std::map +// is returned +// ------------------------------------------------------------------------ + +%{ +#include <map> +#include <algorithm> +#include <stdexcept> +%} + +// exported class + +namespace std { + + %mixin map "Enumerable"; + + template<class K, class T> class map { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (rb_obj_is_kind_of($input,rb_cHash)) { + $1 = std::map<K,T >(); + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + for (unsigned int i=0; i<size; i++) { + K* k; + T* x; + VALUE key = RARRAY(keys)->ptr[i]; + VALUE val = rb_hash_aref($input,key); + SWIG_ConvertPtr(key, (void **) &k, $descriptor(K *), 1); + SWIG_ConvertPtr(val, (void **) &x, $descriptor(T *), 1); + (($1_type &)$1)[*k] = *x; + } + } else { + SWIG_ConvertPtr($input, (void **) &m, $&1_descriptor, 1); + $1 = *m; + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (rb_obj_is_kind_of($input,rb_cHash)) { + temp = std::map<K,T >(); + $1 = &temp; + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + for (unsigned int i=0; i<size; i++) { + K* k; + T* x; + VALUE key = RARRAY(keys)->ptr[i]; + VALUE val = rb_hash_aref($input,key); + SWIG_ConvertPtr(key, (void **) &k, $descriptor(K *), 1); + SWIG_ConvertPtr(val, (void **) &x, $descriptor(T *), 1); + temp[*k] = *x; + } + } else { + SWIG_ConvertPtr($input, (void **) &m, $1_descriptor, 1); + $1 = m; + } + } + %typemap(out) map<K,T> { + $result = rb_hash_new(); + for (std::map<K,T >::iterator i=$1.begin(); i!=$1.end(); ++i) { + K* key = new K(i->first); + T* val = new T(i->second); + rb_hash_aset($result, + SWIG_NewPointerObj((void *) key, + $descriptor(K *), 1), + SWIG_NewPointerObj((void *) val, + $descriptor(T *), 1)); + } + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + /* native sequence? */ + if (rb_obj_is_kind_of($input,rb_cHash)) { + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + if (size == 0) { + /* an empty dictionary can be of any type */ + $1 = 1; + } else { + /* check the first element only */ + K* k; + T* x; + VALUE key = RARRAY(keys)->ptr[0]; + VALUE val = rb_hash_aref($input,key); + if (SWIG_ConvertPtr(key,(void **) &k, + $descriptor(K *),0) != -1 && + SWIG_ConvertPtr(val,(void **) &x, + $descriptor(T *),0) != -1) + $1 = 1; + else + $1 = 0; + } + } else { + /* wrapped map? */ + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $&1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + /* native sequence? */ + if (rb_obj_is_kind_of($input,rb_cHash)) { + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + if (size == 0) { + /* an empty dictionary can be of any type */ + $1 = 1; + } else { + /* check the first element only */ + K* k; + T* x; + VALUE key = RARRAY(keys)->ptr[0]; + VALUE val = rb_hash_aref($input,key); + if (SWIG_ConvertPtr(key,(void **) &k, + $descriptor(K *),0) != -1 && + SWIG_ConvertPtr(val,(void **) &x, + $descriptor(T *),0) != -1) + $1 = 1; + else + $1 = 0; + } + } else { + /* wrapped map? */ + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + %rename(__len__) size; + %rename("empty?") empty; + %rename("delete") __delitem__; + %rename("has_key?") has_key; + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& __getitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(const K& key, const T& x) { + (*self)[key] = x; + } + T __delitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) { + T x = i->second; + self->erase(i); + return x; + } else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + VALUE keys() { + VALUE keyList = rb_ary_new2(self->size()); + swig_type_info* type = SWIG_TypeQuery(#K " *"); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + K* ptr = new K(i->first); + rb_ary_store(keyList,j, + SWIG_NewPointerObj((void *) ptr,type,1)); + } + return keyList; + } + VALUE values() { + VALUE valueList = rb_ary_new2(self->size()); + swig_type_info* type = SWIG_TypeQuery(#T " *"); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + T* ptr = new T(i->second); + rb_ary_store(valueList,j, + SWIG_NewPointerObj((void *) ptr,type,1)); + } + return valueList; + } + void each() { + swig_type_info* k_type = SWIG_TypeQuery(#K " *"); + swig_type_info* t_type = SWIG_TypeQuery(#T " *"); + std::map<K,T >::iterator i; + for (i=self->begin(); i!=self->end(); ++i) { + K* key = new K(i->first); + T* val = &(i->second); + VALUE entry = rb_ary_new2(2); + VALUE k = SWIG_NewPointerObj((void *) key,k_type,1); + VALUE x = SWIG_NewPointerObj((void *) val,t_type,0); + rb_ary_store(entry,0,k); + rb_ary_store(entry,1,x); + rb_yield(entry); + } + } + } + }; + + + // specializations for built-ins + + %define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO) + + %mixin map<K,T> "Enumerable"; + + template<class T> class map<K,T> { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (rb_obj_is_kind_of($input,rb_cHash)) { + $1 = std::map<K,T >(); + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + for (unsigned int i=0; i<size; i++) { + T* x; + VALUE key = RARRAY(keys)->ptr[i]; + VALUE val = rb_hash_aref($input,key); + if (!CHECK(key)) + rb_raise(rb_eTypeError, + "wrong argument type" + " (expected map<" #K "," #T ">)"); + SWIG_ConvertPtr(val,(void **) &x, + $descriptor(T *),1); + (($1_type &)$1)[CONVERT_FROM(key)] = *x; + } + } else { + SWIG_ConvertPtr($input,(void **) &m, $&1_descriptor,1); + $1 = *m; + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (rb_obj_is_kind_of($input,rb_cHash)) { + temp = std::map<K,T >(); + $1 = &temp; + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + for (unsigned int i=0; i<size; i++) { + T* x; + VALUE key = RARRAY(keys)->ptr[i]; + VALUE val = rb_hash_aref($input,key); + if (!CHECK(key)) + rb_raise(rb_eTypeError, + "wrong argument type" + " (expected map<" #K "," #T ">)"); + SWIG_ConvertPtr(val,(void **) &x, + $descriptor(T *),1); + temp[CONVERT_FROM(key)] = *x; + } + } else { + SWIG_ConvertPtr($input,(void **) &m,$1_descriptor,1); + $1 = m; + } + } + %typemap(out) map<K,T> { + $result = rb_hash_new(); + for (std::map<K,T >::iterator i=$1.begin(); i!=$1.end(); ++i) { + T* obj = new T(i->second); + rb_hash_aset($result, + CONVERT_TO(i->first), + SWIG_NewPointerObj((void *) obj, + $descriptor(T *), 1)); + } + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + // native sequence? + if (rb_obj_is_kind_of($input,rb_cHash)) { + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + if (size == 0) { + // an empty dictionary can be of any type + $1 = 1; + } else { + // check the first element only + T* x; + VALUE key = RARRAY(keys)->ptr[0]; + VALUE val = rb_hash_aref($input,key); + if (CHECK(key) && + SWIG_ConvertPtr(val,(void **) &x, + $descriptor(T *),0) != -1) + $1 = 1; + else + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $&1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + // native sequence? + if (rb_obj_is_kind_of($input,rb_cHash)) { + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + if (size == 0) { + // an empty dictionary can be of any type + $1 = 1; + } else { + // check the first element only + T* x; + VALUE key = RARRAY(keys)->ptr[0]; + VALUE val = rb_hash_aref($input,key); + if (CHECK(key) && + SWIG_ConvertPtr(val,(void **) &x, + $descriptor(T *),0) != -1) + $1 = 1; + else + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + %rename(__len__) size; + %rename("empty?") empty; + %rename("delete") __delitem__; + %rename("has_key?") has_key; + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& __getitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(K key, const T& x) { + (*self)[key] = x; + } + T __delitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) { + T x = i->second; + self->erase(i); + return x; + } else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + VALUE keys() { + VALUE keyList = rb_ary_new2(self->size()); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + rb_ary_store(keyList,j, + CONVERT_TO(i->first)); + } + return keyList; + } + VALUE values() { + VALUE valueList = rb_ary_new2(self->size()); + swig_type_info* type = SWIG_TypeQuery(#T " *"); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + T* ptr = new T(i->second); + rb_ary_store(valueList,j, + SWIG_NewPointerObj((void *) ptr,type,1)); + } + return valueList; + } + void each() { + swig_type_info* t_type = SWIG_TypeQuery(#T " *"); + std::map<K,T >::iterator i; + for (i=self->begin(); i!=self->end(); ++i) { + T* val = &(i->second); + VALUE entry = rb_ary_new2(2); + VALUE k = CONVERT_TO(i->first); + VALUE x = SWIG_NewPointerObj((void *) val,t_type,0); + rb_ary_store(entry,0,k); + rb_ary_store(entry,1,x); + rb_yield(entry); + } + } + } + }; + %enddef + + %define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO) + + %mixin map<K,T> "Enumerable"; + + template<class K> class map<K,T> { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (rb_obj_is_kind_of($input,rb_cHash)) { + $1 = std::map<K,T >(); + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + for (unsigned int i=0; i<size; i++) { + K* k; + VALUE key = RARRAY(keys)->ptr[i]; + VALUE val = rb_hash_aref($input,key); + if (!CHECK(val)) + rb_raise(rb_eTypeError, + "wrong argument type" + " (expected map<" #K "," #T ">)"); + SWIG_ConvertPtr(key,(void **) &k, + $descriptor(K *),1); + (($1_type &)$1)[*k] = CONVERT_FROM(val); + } + } else { + SWIG_ConvertPtr($input,(void **) &m, $&1_descriptor,1); + $1 = *m; + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (rb_obj_is_kind_of($input,rb_cHash)) { + temp = std::map<K,T >(); + $1 = &temp; + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + for (unsigned int i=0; i<size; i++) { + K* k; + VALUE key = RARRAY(keys)->ptr[i]; + VALUE val = rb_hash_aref($input,key); + if (!CHECK(val)) + rb_raise(rb_eTypeError, + "wrong argument type" + " (expected map<" #K "," #T ">)"); + SWIG_ConvertPtr(key,(void **) &k, + $descriptor(K *),1); + temp[*k] = CONVERT_FROM(val); + } + } else { + SWIG_ConvertPtr($input,(void **) &m, $1_descriptor,1); + $1 = m; + } + } + %typemap(out) map<K,T> { + $result = rb_hash_new(); + for (std::map<K,T >::iterator i=$1.begin(); i!=$1.end(); ++i) { + K* key = new K(i->first); + rb_hash_aset($result, + SWIG_NewPointerObj((void *) key, + $descriptor(K *), 1), + CONVERT_TO(i->second)); + } + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + // native sequence? + if (rb_obj_is_kind_of($input,rb_cHash)) { + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + if (size == 0) { + // an empty dictionary can be of any type + $1 = 1; + } else { + // check the first element only + K* k; + VALUE key = RARRAY(keys)->ptr[0]; + VALUE val = rb_hash_aref($input,key); + if (SWIG_ConvertPtr(val,(void **) &k, + $descriptor(K *),0) != -1 && + CHECK(val)) + $1 = 1; + else + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $&1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + // native sequence? + if (rb_obj_is_kind_of($input,rb_cHash)) { + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + if (size == 0) { + // an empty dictionary can be of any type + $1 = 1; + } else { + // check the first element only + K* k; + VALUE key = RARRAY(keys)->ptr[0]; + VALUE val = rb_hash_aref($input,key); + if (SWIG_ConvertPtr(val,(void **) &k, + $descriptor(K *),0) != -1 && + CHECK(val)) + $1 = 1; + else + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + %rename(__len__) size; + %rename("empty?") empty; + %rename("delete") __delitem__; + %rename("has_key?") has_key; + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T __getitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(const K& key, T x) { + (*self)[key] = x; + } + T __delitem__(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) { + T x = i->second; + self->erase(i); + return x; + } else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + VALUE keys() { + VALUE keyList = rb_ary_new2(self->size()); + swig_type_info* type = SWIG_TypeQuery(#K " *"); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + K* ptr = new K(i->first); + rb_ary_store(keyList,j, + SWIG_NewPointerObj((void *) ptr,type,1)); + } + return keyList; + } + VALUE values() { + VALUE valueList = rb_ary_new2(self->size()); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + rb_ary_store(valueList,j, + CONVERT_TO(i->second)); + } + return valueList; + } + void each() { + swig_type_info* k_type = SWIG_TypeQuery(#K " *"); + std::map<K,T >::iterator i; + for (i=self->begin(); i!=self->end(); ++i) { + K* key = new K(i->first); + VALUE entry = rb_ary_new2(2); + VALUE k = SWIG_NewPointerObj((void *) key,k_type,1); + VALUE x = CONVERT_TO(i->second); + rb_ary_store(entry,0,k); + rb_ary_store(entry,1,x); + rb_yield(entry); + } + } + } + }; + %enddef + + %define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO, + T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO) + %mixin map<K,T> "Enumerable"; + + template<> class map<K,T> { + %typemap(in) map<K,T> (std::map<K,T>* m) { + if (rb_obj_is_kind_of($input,rb_cHash)) { + $1 = std::map<K,T >(); + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + for (unsigned int i=0; i<size; i++) { + VALUE key = RARRAY(keys)->ptr[i]; + VALUE val = rb_hash_aref($input,key); + if (!(CHECK_K(key) && CHECK_T(val))) + rb_raise(rb_eTypeError, + "wrong argument type" + " (expected map<" #K "," #T ">)"); + (($1_type &)$1)[CONVERT_K_FROM(key)] = + CONVERT_T_FROM(val); + } + } else { + SWIG_ConvertPtr($input,(void **) &m, $&1_descriptor,1); + $1 = *m; + } + } + %typemap(in) const map<K,T>& (std::map<K,T> temp, + std::map<K,T>* m), + const map<K,T>* (std::map<K,T> temp, + std::map<K,T>* m) { + if (rb_obj_is_kind_of($input,rb_cHash)) { + temp = std::map<K,T >(); + $1 = &temp; + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + for (unsigned int i=0; i<size; i++) { + VALUE key = RARRAY(keys)->ptr[i]; + VALUE val = rb_hash_aref($input,key); + if (!(CHECK_K(key) && CHECK_T(val))) + rb_raise(rb_eTypeError, + "wrong argument type" + " (expected map<" #K "," #T ">)"); + temp[CONVERT_K_FROM(key)] = CONVERT_T_FROM(val); + } + } else { + SWIG_ConvertPtr($input,(void **) &m, $1_descriptor,1); + $1 = m; + } + } + %typemap(out) map<K,T> { + $result = rb_hash_new(); + for (std::map<K,T >::iterator i=$1.begin(); i!=$1.end(); ++i) { + rb_hash_aset($result, + CONVERT_K_TO(i->first), + CONVERT_T_TO(i->second)); + } + } + %typecheck(SWIG_TYPECHECK_MAP) map<K,T> { + // native sequence? + if (rb_obj_is_kind_of($input,rb_cHash)) { + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + if (size == 0) { + // an empty dictionary can be of any type + $1 = 1; + } else { + // check the first element only + VALUE key = RARRAY(keys)->ptr[0]; + VALUE val = rb_hash_aref($input,key); + if (CHECK_K(key) && CHECK_T(val)) + $1 = 1; + else + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $&1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + %typecheck(SWIG_TYPECHECK_MAP) const map<K,T>&, + const map<K,T>* { + // native sequence? + if (rb_obj_is_kind_of($input,rb_cHash)) { + VALUE keys = rb_funcall($input,rb_intern("keys"),0); + unsigned int size = RARRAY(keys)->len; + if (size == 0) { + // an empty dictionary can be of any type + $1 = 1; + } else { + // check the first element only + VALUE key = RARRAY(keys)->ptr[0]; + VALUE val = rb_hash_aref($input,key); + if (CHECK_K(key) && CHECK_T(val)) + $1 = 1; + else + $1 = 0; + } + } else { + // wrapped map? + std::map<K,T >* m; + if (SWIG_ConvertPtr($input,(void **) &m, + $1_descriptor,0) != -1) + $1 = 1; + else + $1 = 0; + } + } + %rename(__len__) size; + %rename("empty?") empty; + %rename("delete") __delitem__; + %rename("has_key?") has_key; + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T __getitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void __setitem__(K key, T x) { + (*self)[key] = x; + } + T __delitem__(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) { + T x = i->second; + self->erase(i); + return x; + } else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + VALUE keys() { + VALUE keyList = rb_ary_new2(self->size()); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + rb_ary_store(keyList,j, + CONVERT_K_TO(i->first)); + } + return keyList; + } + VALUE values() { + VALUE valueList = rb_ary_new2(self->size()); + std::map<K,T >::iterator i; + unsigned int j; + for (i=self->begin(), j=0; i!=self->end(); ++i, ++j) { + rb_ary_store(valueList,j, + CONVERT_T_TO(i->second)); + } + return valueList; + } + void each() { + std::map<K,T >::iterator i; + for (i=self->begin(); i!=self->end(); ++i) { + VALUE entry = rb_ary_new2(2); + rb_ary_store(entry,0,CONVERT_K_TO(i->first)); + rb_ary_store(entry,1,CONVERT_T_TO(i->second)); + rb_yield(entry); + } + } + } + }; + %enddef + + + specialize_std_map_on_key(bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB); + specialize_std_map_on_key(int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_key(short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_key(long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_key(unsigned int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_key(unsigned short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_key(unsigned long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_key(double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_key(float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_key(std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB); + + specialize_std_map_on_value(bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB); + specialize_std_map_on_value(int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_value(short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_value(long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_value(unsigned int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_value(unsigned short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_value(unsigned long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_value(double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_value(float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_value(std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB); + + specialize_std_map_on_both(bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB, + bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB); + specialize_std_map_on_both(bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB, + int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB, + short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB, + long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB, + unsigned int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB, + unsigned short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB, + unsigned long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB, + double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB, + float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB, + std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB); + specialize_std_map_on_both(int,FIXNUM_P, + FIX2INT,INT2NUM, + bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB); + specialize_std_map_on_both(int,FIXNUM_P, + FIX2INT,INT2NUM, + int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(int,FIXNUM_P, + FIX2INT,INT2NUM, + short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(int,FIXNUM_P, + FIX2INT,INT2NUM, + long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(int,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(int,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(int,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(int,FIXNUM_P, + FIX2INT,INT2NUM, + double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(int,FIXNUM_P, + FIX2INT,INT2NUM, + float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(int,FIXNUM_P, + FIX2INT,INT2NUM, + std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB); + specialize_std_map_on_both(short,FIXNUM_P, + FIX2INT,INT2NUM, + bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB); + specialize_std_map_on_both(short,FIXNUM_P, + FIX2INT,INT2NUM, + int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(short,FIXNUM_P, + FIX2INT,INT2NUM, + short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(short,FIXNUM_P, + FIX2INT,INT2NUM, + long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(short,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(short,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(short,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(short,FIXNUM_P, + FIX2INT,INT2NUM, + double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(short,FIXNUM_P, + FIX2INT,INT2NUM, + float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(short,FIXNUM_P, + FIX2INT,INT2NUM, + std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB); + specialize_std_map_on_both(long,FIXNUM_P, + FIX2INT,INT2NUM, + bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB); + specialize_std_map_on_both(long,FIXNUM_P, + FIX2INT,INT2NUM, + int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(long,FIXNUM_P, + FIX2INT,INT2NUM, + short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(long,FIXNUM_P, + FIX2INT,INT2NUM, + long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(long,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(long,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(long,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(long,FIXNUM_P, + FIX2INT,INT2NUM, + double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(long,FIXNUM_P, + FIX2INT,INT2NUM, + float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(long,FIXNUM_P, + FIX2INT,INT2NUM, + std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB); + specialize_std_map_on_both(unsigned int,FIXNUM_P, + FIX2INT,INT2NUM, + bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB); + specialize_std_map_on_both(unsigned int,FIXNUM_P, + FIX2INT,INT2NUM, + int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned int,FIXNUM_P, + FIX2INT,INT2NUM, + short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned int,FIXNUM_P, + FIX2INT,INT2NUM, + long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned int,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned int,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned int,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned int,FIXNUM_P, + FIX2INT,INT2NUM, + double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(unsigned int,FIXNUM_P, + FIX2INT,INT2NUM, + float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(unsigned int,FIXNUM_P, + FIX2INT,INT2NUM, + std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB); + specialize_std_map_on_both(unsigned short,FIXNUM_P, + FIX2INT,INT2NUM, + bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB); + specialize_std_map_on_both(unsigned short,FIXNUM_P, + FIX2INT,INT2NUM, + int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned short,FIXNUM_P, + FIX2INT,INT2NUM, + short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned short,FIXNUM_P, + FIX2INT,INT2NUM, + long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned short,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned short,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned short,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned short,FIXNUM_P, + FIX2INT,INT2NUM, + double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(unsigned short,FIXNUM_P, + FIX2INT,INT2NUM, + float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(unsigned short,FIXNUM_P, + FIX2INT,INT2NUM, + std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB); + specialize_std_map_on_both(unsigned long,FIXNUM_P, + FIX2INT,INT2NUM, + bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB); + specialize_std_map_on_both(unsigned long,FIXNUM_P, + FIX2INT,INT2NUM, + int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned long,FIXNUM_P, + FIX2INT,INT2NUM, + short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned long,FIXNUM_P, + FIX2INT,INT2NUM, + long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned long,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned long,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned long,FIXNUM_P, + FIX2INT,INT2NUM, + unsigned long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(unsigned long,FIXNUM_P, + FIX2INT,INT2NUM, + double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(unsigned long,FIXNUM_P, + FIX2INT,INT2NUM, + float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(unsigned long,FIXNUM_P, + FIX2INT,INT2NUM, + std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB); + specialize_std_map_on_both(double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB); + specialize_std_map_on_both(double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + unsigned int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + unsigned short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + unsigned long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB); + specialize_std_map_on_both(float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB); + specialize_std_map_on_both(float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + unsigned int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + unsigned short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + unsigned long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new, + std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB); + specialize_std_map_on_both(std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB, + bool,SWIG_BOOL_P, + SWIG_RB2BOOL,SWIG_BOOL2RB); + specialize_std_map_on_both(std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB, + int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB, + short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB, + long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB, + unsigned int,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB, + unsigned short,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB, + unsigned long,FIXNUM_P, + FIX2INT,INT2NUM); + specialize_std_map_on_both(std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB, + double,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB, + float,SWIG_FLOAT_P, + SWIG_NUM2DBL,rb_float_new); + specialize_std_map_on_both(std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB, + std::string,SWIG_STRING_P, + SWIG_RB2STR,SWIG_STR2RB); +} @@ -8,4 +8,5 @@ %include std_string.i %include std_vector.i +%include std_map.i diff --git a/Lib/swig.swg b/Lib/swig.swg index 1acc2a394..044929d32 100644 --- a/Lib/swig.swg +++ b/Lib/swig.swg @@ -235,6 +235,7 @@ namespace std { %define SWIG_TYPECHECK_CHAR 130 %enddef %define SWIG_TYPECHECK_STRING 140 %enddef %define SWIG_TYPECHECK_VECTOR 150 %enddef +%define SWIG_TYPECHECK_MAP 160 %enddef %define SWIG_TYPECHECK_BOOL_ARRAY 1015 %enddef %define SWIG_TYPECHECK_INT8_ARRAY 1025 %enddef diff --git a/Lib/tcl/std_map.i b/Lib/tcl/std_map.i new file mode 100644 index 000000000..38a063e95 --- /dev/null +++ b/Lib/tcl/std_map.i @@ -0,0 +1,190 @@ +// +// SWIG typemaps for std::map +// Luigi Ballabio +// Jan. 2003 +// +// Common implementation + +%include std_common.i +%include exception.i + +%exception std::map::get { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +%exception std::map::del { + try { + $action + } catch (std::out_of_range& e) { + SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); + } +} + +// ------------------------------------------------------------------------ +// std::map +// ------------------------------------------------------------------------ + +%{ +#include <map> +#include <algorithm> +#include <stdexcept> +%} + +// exported class + +namespace std { + + template<class K, class T> class map { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& get(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(const K& key, const T& x) { + (*self)[key] = x; + } + void del(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + + + // specializations for built-ins + + %define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO) + + template<class T> class map<K,T> { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T& get(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(K key, const T& x) { + (*self)[key] = x; + } + void del(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + %enddef + + %define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO) + template<class K> class map<K,T> { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T get(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(const K& key, T x) { + (*self)[key] = x; + } + void del(const K& key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(const K& key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + %enddef + + %define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO, + T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO) + template<> class map<K,T> { + // add typemaps here + public: + map(); + map(const map<K,T> &); + + unsigned int size() const; + bool empty() const; + void clear(); + %extend { + T get(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + return i->second; + else + throw std::out_of_range("key not found"); + } + void set(K key, T x) { + (*self)[key] = x; + } + void del(K key) { + std::map<K,T >::iterator i = self->find(key); + if (i != self->end()) + self->erase(i); + else + throw std::out_of_range("key not found"); + } + bool has_key(K key) { + std::map<K,T >::iterator i = self->find(key); + return i != self->end(); + } + } + }; + %enddef + + // add specializations here + +} |