Ruby STL container negative indexing support improved
Using negative indexes to set values works the same as Ruby arrays, eg %template(IntVector) std::vector<int>; iv = IntVector.new([1,2,3,4]) iv[-4] = 9 # => [1,2,3,9] iv[-5] = 9 # => IndexError
This commit is contained in:
parent
19c24a4f60
commit
fe09f05beb
2 changed files with 20 additions and 7 deletions
|
|
@ -80,6 +80,21 @@ iv[1,3] = [6,7,8,9]
|
||||||
#__setitem__ needs fixing
|
#__setitem__ needs fixing
|
||||||
#swig_assert_equal(iv.to_s, '16789', binding)
|
#swig_assert_equal(iv.to_s, '16789', binding)
|
||||||
|
|
||||||
|
iv = IntVector.new([1,2,3,4])
|
||||||
|
|
||||||
|
iv[-1] = 9
|
||||||
|
iv[-4] = 6
|
||||||
|
swig_assert_equal(iv.to_s, '6239', binding)
|
||||||
|
|
||||||
|
begin
|
||||||
|
iv[-5] = 99
|
||||||
|
raise "exception missed"
|
||||||
|
rescue IndexError
|
||||||
|
end
|
||||||
|
|
||||||
|
iv[6] = 5
|
||||||
|
swig_assert_equal(iv.to_s, '6239555', binding)
|
||||||
|
|
||||||
dv = DoubleVector.new(10)
|
dv = DoubleVector.new(10)
|
||||||
|
|
||||||
swig_assert( "dv.respond_to? :each_with_index", binding )
|
swig_assert( "dv.respond_to? :each_with_index", binding )
|
||||||
|
|
|
||||||
|
|
@ -825,14 +825,12 @@ namespace swig
|
||||||
return swig::from< Sequence* >( swig::getslice(self, s, e+1) );
|
return swig::from< Sequence* >( swig::getslice(self, s, e+1) );
|
||||||
}
|
}
|
||||||
|
|
||||||
VALUE __setitem__(difference_type i, const value_type& x) throw (std::invalid_argument)
|
VALUE __setitem__(difference_type i, const value_type& x) throw (std::invalid_argument, std::out_of_range)
|
||||||
{
|
{
|
||||||
std::size_t len = $self->size();
|
if ( i >= static_cast<Sequence::difference_type>( $self->size()) )
|
||||||
if ( i < 0 ) i = len - i;
|
|
||||||
else if ( static_cast<std::size_t>(i) >= len )
|
|
||||||
swig::resize( $self, i+1, x );
|
swig::resize( $self, i+1, x );
|
||||||
else
|
else
|
||||||
*(swig::getpos(self,i)) = x;
|
*(swig::getpos($self, i)) = x;
|
||||||
|
|
||||||
return swig::from< Sequence::value_type >( x );
|
return swig::from< Sequence::value_type >( x );
|
||||||
}
|
}
|
||||||
|
|
@ -850,7 +848,7 @@ namespace swig
|
||||||
}
|
}
|
||||||
|
|
||||||
VALUE r = Qnil;
|
VALUE r = Qnil;
|
||||||
swig::setslice(self, i, j, v);
|
swig::setslice($self, i, j, v);
|
||||||
r = swig::from< const Sequence* >( &v );
|
r = swig::from< const Sequence* >( &v );
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue