Added proper each(), printing and other methods to map
and multimap. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9733 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
aa3c28e230
commit
c0a7c7e081
5 changed files with 104 additions and 50 deletions
|
|
@ -23,6 +23,10 @@ begin
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
swig_assert_each_line(<<'EOF', binding)
|
||||||
|
iv.respond_to?(:each) == true
|
||||||
|
iv.respond_to?(:each_with_index) == true
|
||||||
|
EOF
|
||||||
|
|
||||||
iv.each_with_index { |e,i|
|
iv.each_with_index { |e,i|
|
||||||
swig_assert("#{e} == 0", binding, "for iv[#{i}] == 0")
|
swig_assert("#{e} == 0", binding, "for iv[#{i}] == 0")
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,7 @@ def swig_assert( expr, scope = nil, msg = nil )
|
||||||
raise
|
raise
|
||||||
end
|
end
|
||||||
|
|
||||||
raise SwigRubyError.new("FAILED: #{expr.to_s} - #{e}") unless ok
|
raise SwigRubyError.new("FAILED: #{expr.to_s} - #{msg}") unless ok
|
||||||
|
|
||||||
if $VERBOSE
|
if $VERBOSE
|
||||||
$stdout.puts "\tPASSED #{expr} #{msg}"
|
$stdout.puts "\tPASSED #{expr} #{msg}"
|
||||||
|
|
|
||||||
|
|
@ -656,7 +656,23 @@ namespace swig
|
||||||
swig::yield< Sequence::value_type >() );
|
swig::yield< Sequence::value_type >() );
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Sequence* each()
|
||||||
|
{
|
||||||
|
if ( !rb_block_given_p() )
|
||||||
|
rb_raise( rb_eArgError, "no block given");
|
||||||
|
|
||||||
|
VALUE r;
|
||||||
|
Sequence::iterator i = self->begin();
|
||||||
|
Sequence::iterator e = self->end();
|
||||||
|
for ( ; i != e; ++i )
|
||||||
|
{
|
||||||
|
r = swig::from<Sequence::value_type>(*i);
|
||||||
|
rb_yield(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
%rename("reject!") reject_bang;
|
%rename("reject!") reject_bang;
|
||||||
%alias reject_bang "delete_if";
|
%alias reject_bang "delete_if";
|
||||||
|
|
@ -692,41 +708,6 @@ namespace swig
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
Sequence* each_with_index()
|
|
||||||
{
|
|
||||||
if ( !rb_block_given_p() )
|
|
||||||
rb_raise( rb_eArgError, "no block given");
|
|
||||||
|
|
||||||
VALUE r, idx;
|
|
||||||
Sequence::iterator i = self->begin();
|
|
||||||
Sequence::iterator e = self->end();
|
|
||||||
unsigned c = 0;
|
|
||||||
for ( ; i != e; ++i, ++c )
|
|
||||||
{
|
|
||||||
r = swig::from<Sequence::value_type>(*i);
|
|
||||||
idx = UINT2NUM( c );
|
|
||||||
rb_yield_values(2, r, idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
|
|
||||||
Sequence* each()
|
|
||||||
{
|
|
||||||
if ( !rb_block_given_p() )
|
|
||||||
rb_raise( rb_eArgError, "no block given");
|
|
||||||
|
|
||||||
VALUE r;
|
|
||||||
Sequence::iterator i = self->begin();
|
|
||||||
Sequence::iterator e = self->end();
|
|
||||||
for ( ; i != e; ++i )
|
|
||||||
{
|
|
||||||
r = swig::from<Sequence::value_type>(*i);
|
|
||||||
rb_yield(r);
|
|
||||||
}
|
|
||||||
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -132,18 +132,12 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
%define %swig_map_common(Map...)
|
%define %swig_map_common(Map...)
|
||||||
// %swig_sequence_methods_common(Map);
|
%swig_container_methods(%arg(Map));
|
||||||
// %swig_sequence_iterator(Map);
|
// %swig_sequence_iterator(%arg(Map));
|
||||||
|
|
||||||
%extend {
|
%extend {
|
||||||
VALUE __getitem__(const key_type& key) const {
|
|
||||||
Map::const_iterator i = self->find(key);
|
|
||||||
if ( i != self->end() )
|
|
||||||
return swig::from( i->second );
|
|
||||||
else
|
|
||||||
return Qnil;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
%rename("delete") __delitem__;
|
||||||
VALUE __delitem__(const key_type& key) {
|
VALUE __delitem__(const key_type& key) {
|
||||||
Map::iterator i = self->find(key);
|
Map::iterator i = self->find(key);
|
||||||
if (i != self->end()) {
|
if (i != self->end()) {
|
||||||
|
|
@ -179,6 +173,27 @@
|
||||||
return ary;
|
return ary;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Map* each()
|
||||||
|
{
|
||||||
|
if ( !rb_block_given_p() )
|
||||||
|
rb_raise( rb_eArgError, "no block given");
|
||||||
|
|
||||||
|
VALUE k, v;
|
||||||
|
Map::iterator i = self->begin();
|
||||||
|
Map::iterator e = self->end();
|
||||||
|
for ( ; i != e; ++i )
|
||||||
|
{
|
||||||
|
const Map::key_type& key = i->first;
|
||||||
|
const Map::mapped_type& val = i->second;
|
||||||
|
|
||||||
|
k = swig::from<Map::key_type>(key);
|
||||||
|
v = swig::from<Map::mapped_type>(val);
|
||||||
|
rb_yield_values(2, k, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
Map* each_key()
|
Map* each_key()
|
||||||
{
|
{
|
||||||
if ( !rb_block_given_p() )
|
if ( !rb_block_given_p() )
|
||||||
|
|
@ -271,6 +286,13 @@
|
||||||
%define %swig_map_methods(Map...)
|
%define %swig_map_methods(Map...)
|
||||||
%swig_map_common(Map)
|
%swig_map_common(Map)
|
||||||
%extend {
|
%extend {
|
||||||
|
VALUE __getitem__(const key_type& key) const {
|
||||||
|
Map::const_iterator i = self->find(key);
|
||||||
|
if ( i != self->end() )
|
||||||
|
return swig::from<Map::mapped_type>( i->second );
|
||||||
|
else
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
|
|
||||||
void __setitem__(const key_type& key, const mapped_type& x) throw (std::out_of_range) {
|
void __setitem__(const key_type& key, const mapped_type& x) throw (std::out_of_range) {
|
||||||
(*self)[key] = x;
|
(*self)[key] = x;
|
||||||
|
|
@ -287,9 +309,11 @@
|
||||||
for ( ; i != e; ++i, comma = true )
|
for ( ; i != e; ++i, comma = true )
|
||||||
{
|
{
|
||||||
if (comma) str = rb_str_cat2( str, "," );
|
if (comma) str = rb_str_cat2( str, "," );
|
||||||
// @todo: improve -- this should just be swig::from(*i)
|
tmp = swig::from< Map::key_type >( i->first );
|
||||||
tmp = swig::from< std::pair<Map::key_type,
|
tmp = rb_obj_as_string( tmp );
|
||||||
Map::mapped_type> >( *i );
|
str = rb_str_buf_append( str, tmp );
|
||||||
|
str = rb_str_cat2( str, "=>" );
|
||||||
|
tmp = swig::from< Map::mapped_type >( i->second );
|
||||||
tmp = rb_obj_as_string( tmp );
|
tmp = rb_obj_as_string( tmp );
|
||||||
str = rb_str_buf_append( str, tmp );
|
str = rb_str_buf_append( str, tmp );
|
||||||
}
|
}
|
||||||
|
|
@ -334,4 +358,16 @@
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(SWIG_RUBY_AUTORENAME)
|
||||||
|
|
||||||
|
%mixin std::map "Enumerable";
|
||||||
|
%rename("empty?") std::map::empty;
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
%mixin std::map "Enumerable";
|
||||||
|
%rename("empty?") std::map::empty;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
%include <std/std_map.i>
|
%include <std/std_map.i>
|
||||||
|
|
|
||||||
|
|
@ -86,9 +86,28 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
%define %swig_multimap_methods(MultiMap...)
|
%define %swig_multimap_methods(MultiMap...)
|
||||||
%swig_map_common(MultiMap);
|
%swig_container_methods(%arg(MultiMap));
|
||||||
|
%swig_map_common(%arg(MultiMap));
|
||||||
|
|
||||||
%extend {
|
%extend {
|
||||||
|
VALUE __getitem__(const key_type& key) const {
|
||||||
|
MultiMap::const_iterator i = self->find(key);
|
||||||
|
if ( i != self->end() )
|
||||||
|
{
|
||||||
|
MultiMap::const_iterator e = $self->upper_bound(key);
|
||||||
|
VALUE ary = rb_ary_new();
|
||||||
|
for ( ; i != e; ++i )
|
||||||
|
{
|
||||||
|
rb_ary_push( ary, swig::from<MultiMap::mapped_type>( i->second ) );
|
||||||
|
}
|
||||||
|
if ( RARRAY_LEN(ary) == 1 )
|
||||||
|
return RARRAY_PTR(ary)[0];
|
||||||
|
return ary;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
|
|
||||||
void __setitem__(const key_type& key, const mapped_type& x) throw (std::out_of_range) {
|
void __setitem__(const key_type& key, const mapped_type& x) throw (std::out_of_range) {
|
||||||
self->insert(MultiMap::value_type(key,x));
|
self->insert(MultiMap::value_type(key,x));
|
||||||
}
|
}
|
||||||
|
|
@ -106,6 +125,7 @@
|
||||||
const MultiMap::key_type& oldkey = key;
|
const MultiMap::key_type& oldkey = key;
|
||||||
tmp = swig::from( key );
|
tmp = swig::from( key );
|
||||||
str = rb_str_buf_append( str, rb_inspect(tmp) );
|
str = rb_str_buf_append( str, rb_inspect(tmp) );
|
||||||
|
str = rb_str_cat2( str, "=>" );
|
||||||
|
|
||||||
VALUE vals = rb_ary_new();
|
VALUE vals = rb_ary_new();
|
||||||
for ( ; i != e && key == oldkey; ++i )
|
for ( ; i != e && key == oldkey; ++i )
|
||||||
|
|
@ -191,5 +211,18 @@
|
||||||
}
|
}
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(SWIG_RUBY_AUTORENAME)
|
||||||
|
|
||||||
|
%mixin std::multimap "Enumerable";
|
||||||
|
%rename("empty?") std::multimap::empty;
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
%mixin std::multimap "Enumerable";
|
||||||
|
%rename("empty?") std::multimap::empty;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
%include <std/std_multimap.i>
|
%include <std/std_multimap.i>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue