Add copy constructor to std::map and make more efficient by passing keys and mapped type by reference instead of value
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@11234 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
7841adc675
commit
9361144964
1 changed files with 65 additions and 77 deletions
|
|
@ -34,8 +34,6 @@
|
||||||
* Warning: heavy macro usage in this file. Use swig -E to get a sane view on the real file contents!
|
* Warning: heavy macro usage in this file. Use swig -E to get a sane view on the real file contents!
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
//%include <std_common.i>
|
|
||||||
|
|
||||||
%{
|
%{
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
@ -45,39 +43,40 @@
|
||||||
// A minimal implementation to be used when no specialization exists.
|
// A minimal implementation to be used when no specialization exists.
|
||||||
%define SWIG_STD_MAP_MINIMAL_INTERNAL(K, T)
|
%define SWIG_STD_MAP_MINIMAL_INTERNAL(K, T)
|
||||||
public:
|
public:
|
||||||
|
|
||||||
map();
|
map();
|
||||||
map(const map<K, T > &);
|
map(const map<K, T > &other);
|
||||||
|
|
||||||
|
typedef K key_type;
|
||||||
|
typedef T mapped_type;
|
||||||
typedef size_t size_type;
|
typedef size_t size_type;
|
||||||
// size_type size() const;
|
size_type size() const;
|
||||||
bool empty() const;
|
bool empty() const;
|
||||||
%rename(Clear) clear;
|
%rename(Clear) clear;
|
||||||
void clear();
|
void clear();
|
||||||
%extend {
|
%extend {
|
||||||
T get(const K& key) throw (std::out_of_range) {
|
const mapped_type& get(const key_type& key) throw (std::out_of_range) {
|
||||||
std::map<K,T >::iterator i = $self->find(key);
|
std::map<K,T >::iterator iter = $self->find(key);
|
||||||
if (i != $self->end())
|
if (iter != $self->end())
|
||||||
return i->second;
|
return iter->second;
|
||||||
else
|
else
|
||||||
throw std::out_of_range("key not found");
|
throw std::out_of_range("key not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
void set(const K& key, const T& x) {
|
void set(const key_type& key, const mapped_type& x) {
|
||||||
(*$self)[key] = x;
|
(*$self)[key] = x;
|
||||||
}
|
}
|
||||||
|
|
||||||
void del(const K& key) throw (std::out_of_range) {
|
void del(const key_type& key) throw (std::out_of_range) {
|
||||||
std::map<K,T >::iterator i = $self->find(key);
|
std::map<K,T >::iterator iter = $self->find(key);
|
||||||
if (i != $self->end())
|
if (iter != $self->end())
|
||||||
$self->erase(i);
|
$self->erase(iter);
|
||||||
else
|
else
|
||||||
throw std::out_of_range("key not found");
|
throw std::out_of_range("key not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool has_key(const K& key) {
|
bool has_key(const key_type& key) {
|
||||||
std::map<K,T >::iterator i = $self->find(key);
|
std::map<K,T >::iterator iter = $self->find(key);
|
||||||
return i != $self->end();
|
return iter != $self->end();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -91,8 +90,8 @@
|
||||||
*/
|
*/
|
||||||
%define SWIG_STD_MAP_SPECIALIZED_INTERNAL(K, T, CSKEYTYPE, CSVALUETYPE)
|
%define SWIG_STD_MAP_SPECIALIZED_INTERNAL(K, T, CSKEYTYPE, CSVALUETYPE)
|
||||||
// add typemaps here
|
// add typemaps here
|
||||||
%typemap(csinterfaces) std::map<K, T > "IDisposable \n#if !SWIG_DOTNET_1\n , System.Collections.Generic.IDictionary<CSKEYTYPE, CSVALUETYPE >\n#endif\n";
|
%typemap(csinterfaces) std::map<K, T > "IDisposable \n#if !SWIG_DOTNET_1\n , System.Collections.Generic.IDictionary<CSKEYTYPE, CSVALUETYPE>\n#endif\n";
|
||||||
%typemap(cscode) std::map<K, T> %{
|
%typemap(cscode) std::map<K, T > %{
|
||||||
|
|
||||||
public CSVALUETYPE this[CSKEYTYPE key] {
|
public CSVALUETYPE this[CSKEYTYPE key] {
|
||||||
get {
|
get {
|
||||||
|
|
@ -277,28 +276,50 @@
|
||||||
%}
|
%}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
map();
|
map();
|
||||||
map(const map<K, T > &);
|
map(const map<K, T > &other);
|
||||||
|
|
||||||
|
typedef K key_type;
|
||||||
|
typedef T mapped_type;
|
||||||
typedef size_t size_type;
|
typedef size_t size_type;
|
||||||
size_type size() const;
|
size_type size() const;
|
||||||
bool empty() const;
|
bool empty() const;
|
||||||
%rename(Clear) clear;
|
%rename(Clear) clear;
|
||||||
void clear();
|
void clear();
|
||||||
%extend {
|
%extend {
|
||||||
T getitem(K key) throw (std::out_of_range) {
|
const mapped_type& getitem(const key_type& key) throw (std::out_of_range) {
|
||||||
std::map<K,T >::iterator i = $self->find(key);
|
std::map<K,T >::iterator iter = $self->find(key);
|
||||||
if (i != $self->end())
|
if (iter != $self->end())
|
||||||
return i->second;
|
return iter->second;
|
||||||
else
|
else
|
||||||
throw std::out_of_range("key not found");
|
throw std::out_of_range("key not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
void setitem(K key, T x) {
|
void setitem(const key_type& key, const mapped_type& x) {
|
||||||
(*$self)[key] = x;
|
(*$self)[key] = x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ContainsKey(const key_type& key) {
|
||||||
|
std::map<K, T >::iterator iter = $self->find(key);
|
||||||
|
return iter != $self->end();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Add(const key_type& key, const mapped_type& val) throw (std::out_of_range) {
|
||||||
|
std::map<K, T >::iterator iter = $self->find(key);
|
||||||
|
if (iter != $self->end())
|
||||||
|
throw std::out_of_range("key already exists");
|
||||||
|
$self->insert(std::pair<K, T >(key, val));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Remove(const key_type& key) {
|
||||||
|
std::map<K, T >::iterator iter = $self->find(key);
|
||||||
|
if (iter != $self->end()) {
|
||||||
|
$self->erase(iter);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// create_iterator_begin() and get_next_key() work together to provide a collection of keys to C#
|
// create_iterator_begin() and get_next_key() work together to provide a collection of keys to C#
|
||||||
%apply void *VOID_INT_PTR { std::map<K, T >::iterator *std::map<K, T >::create_iterator_begin }
|
%apply void *VOID_INT_PTR { std::map<K, T >::iterator *std::map<K, T >::create_iterator_begin }
|
||||||
%apply void *VOID_INT_PTR { std::map<K, T >::iterator *swigiterator }
|
%apply void *VOID_INT_PTR { std::map<K, T >::iterator *swigiterator }
|
||||||
|
|
@ -307,7 +328,7 @@
|
||||||
return new std::map<K, T >::iterator($self->begin());
|
return new std::map<K, T >::iterator($self->begin());
|
||||||
}
|
}
|
||||||
|
|
||||||
K get_next_key(std::map<K, T >::iterator *swigiterator) throw (std::out_of_range) {
|
const key_type& get_next_key(std::map<K, T >::iterator *swigiterator) throw (std::out_of_range) {
|
||||||
std::map<K, T >::iterator iter = *swigiterator;
|
std::map<K, T >::iterator iter = *swigiterator;
|
||||||
if (iter == $self->end()) {
|
if (iter == $self->end()) {
|
||||||
delete swigiterator;
|
delete swigiterator;
|
||||||
|
|
@ -316,43 +337,19 @@
|
||||||
(*swigiterator)++;
|
(*swigiterator)++;
|
||||||
return (*iter).first;
|
return (*iter).first;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ContainsKey(K key) {
|
|
||||||
std::map<K, T >::iterator iter = $self->find(key);
|
|
||||||
if (iter != $self->end()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Add(K key, T val) throw (std::out_of_range) {
|
|
||||||
std::map<K, T >::iterator iter = $self->find(key);
|
|
||||||
if (iter != $self->end()) {
|
|
||||||
throw std::out_of_range("key already exists");
|
|
||||||
}
|
|
||||||
$self->insert(std::pair<K, T >(key, val));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Remove(K key) {
|
|
||||||
std::map<K, T >::iterator iter = $self->find(key);
|
|
||||||
if (iter != $self->end()) {
|
|
||||||
$self->erase(iter);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
%csmethodmodifiers std::map<K, T >::size "private"
|
||||||
|
%csmethodmodifiers std::map<K, T >::getitem "private"
|
||||||
|
%csmethodmodifiers std::map<K, T >::setitem "private"
|
||||||
|
%csmethodmodifiers std::map<K, T >::create_iterator_begin "private"
|
||||||
|
%csmethodmodifiers std::map<K, T >::get_next_key "private"
|
||||||
|
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
|
|
||||||
%csmethodmodifiers std::map::size "private"
|
// Main specialization macros
|
||||||
%csmethodmodifiers std::map::getitem "private"
|
|
||||||
%csmethodmodifiers std::map::setitem "private"
|
|
||||||
%csmethodmodifiers std::map::create_iterator_begin "private"
|
|
||||||
%csmethodmodifiers std::map::get_next_key "private"
|
|
||||||
|
|
||||||
|
|
||||||
%define SWIG_STD_MAP_SPECIALIZED(K, T, CSKEY, CSVAL)
|
%define SWIG_STD_MAP_SPECIALIZED(K, T, CSKEY, CSVAL)
|
||||||
namespace std {
|
namespace std {
|
||||||
template<> class map<K, T > {
|
template<> class map<K, T > {
|
||||||
|
|
@ -361,36 +358,27 @@ namespace std {
|
||||||
}
|
}
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
|
|
||||||
%define SWIG_STD_MAP_SPECIALIZED_SIMPLE(K, T)
|
%define SWIG_STD_MAP_SPECIALIZED_SIMPLE(K, T)
|
||||||
SWIG_STD_MAP_SPECIALIZED(K, T, K, T)
|
SWIG_STD_MAP_SPECIALIZED(K, T, K, T)
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
// Backwards compatibility macros
|
// Old macros (deprecated)
|
||||||
%define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO)
|
%define specialize_std_map_on_key(K,CHECK,CONVERT_FROM,CONVERT_TO)
|
||||||
template<class T> class map<K,T> {
|
#warning specialize_std_map_on_key ignored - macro is deprecated, please use SWIG_STD_MAP_MINIMAL_INTERNAL in Lib/csharp/std_map.i
|
||||||
SWIG_STD_MAP_MINIMAL_INTERNAL(K, T)
|
|
||||||
};
|
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
%define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO)
|
%define specialize_std_map_on_value(T,CHECK,CONVERT_FROM,CONVERT_TO)
|
||||||
template<class K> class map<K,T> {
|
#warning specialize_std_map_on_value ignored - macro is deprecated, please use SWIG_STD_MAP_MINIMAL_INTERNAL in Lib/csharp/std_map.i
|
||||||
SWIG_STD_MAP_MINIMAL_INTERNAL(K, T)
|
|
||||||
};
|
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
%define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO,
|
%define specialize_std_map_on_both(K,CHECK_K,CONVERT_K_FROM,CONVERT_K_TO, T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO)
|
||||||
T,CHECK_T,CONVERT_T_FROM,CONVERT_T_TO)
|
#warning specialize_std_map_on_both ignored - macro is deprecated, please use SWIG_STD_MAP_MINIMAL_INTERNAL in Lib/csharp/std_map.i
|
||||||
template<> class map<K,T> {
|
|
||||||
SWIG_STD_MAP_MINIMAL_INTERNAL(K, T)
|
|
||||||
};
|
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
// exported class
|
// Default implementation
|
||||||
namespace std {
|
namespace std {
|
||||||
// Regular implementation
|
|
||||||
template<class K, class T> class map {
|
template<class K, class T> class map {
|
||||||
SWIG_STD_MAP_MINIMAL_INTERNAL(K, T)
|
SWIG_STD_MAP_MINIMAL_INTERNAL(K, T)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue