commit ecc9f2f1b0bb13a01b9f7414274a0d828add784a
parent 6b59a59f039abc01dabadeb071c9478053ee5c3e
Author: Michael Savage <mikejsavage@gmail.com>
Date: Mon, 4 May 2020 01:43:21 +0300
Fix array insert
Diffstat:
1 file changed, 9 insertions(+), 11 deletions(-)
diff --git a/src/array.h b/src/array.h
@@ -28,10 +28,10 @@ public:
}
void insert( const T & x, size_t pos ) {
+ ASSERT( pos <= n );
+
extend( 1 );
- for( size_t i = pos + 1; i < n; i++ ) {
- elems[ i ] = elems[ i - 1 ];
- }
+ memmove( elems + pos + 1, elems + pos, ( n - pos - 1 ) * sizeof( T ) );
elems[ pos ] = x;
}
@@ -44,10 +44,8 @@ public:
if( n == 0 || pos >= n )
return false;
- n--;
- for( size_t i = pos; i < n; i++ ) {
- elems[ i ] = elems[ i + 1 ];
- }
+ resize( n - 1 );
+ memmove( elems + pos, elems + pos + 1, ( n - pos ) * sizeof( T ) );
return true;
}
@@ -83,22 +81,22 @@ public:
}
T & operator[]( size_t i ) {
- assert( i < n );
+ ASSERT( i < n );
return elems[ i ];
}
const T & operator[]( size_t i ) const {
- assert( i < n );
+ ASSERT( i < n );
return elems[ i ];
}
T & top() {
- assert( n > 0 );
+ ASSERT( n > 0 );
return elems[ n - 1 ];
}
const T & top() const {
- assert( n > 0 );
+ ASSERT( n > 0 );
return elems[ n - 1 ];
}