diff options
author | Nicolas Catania <niko@google.com> | 2010-02-02 08:41:37 -0800 |
---|---|---|
committer | Nicolas Catania <niko@google.com> | 2010-02-02 08:45:17 -0800 |
commit | 6943930994c640cbb24773ddb8df99de8a5d7e16 (patch) | |
tree | 10e541a1067314131b5c51e9adfdfa94f10875e3 /include/vector | |
parent | 74a6fdea77d52a17be4bc38831fe02a31cefbf34 (diff) | |
download | astl-6943930994c640cbb24773ddb8df99de8a5d7e16.tar.gz |
Implementation of vector::erase.
Added std::copy, needed to shift the element down during an erase call.
Squashed commit of the following:
commit e534509bd709350e585f722e525eb2b63ade5831
Author: Nicolas Catania <niko@google.com>
Date: Mon Feb 1 10:38:21 2010 -0800
implementation of std::copy
commit f94dad514c54c66da85d9492452610285b5ee446
Author: Nicolas Catania <niko@google.com>
Date: Sun Jan 31 14:58:15 2010 -0800
Added support for erase.
Erase element and erase range of elements.
Diffstat (limited to 'include/vector')
-rw-r--r-- | include/vector | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/include/vector b/include/vector index bc9bbdd..051da1d 100644 --- a/include/vector +++ b/include/vector @@ -147,6 +147,18 @@ class vector // the internal buffer: you need to call reserve() to recover it. void pop_back(); + // Remove the element pointed by the iterator. + // Expensive since the remaining elts must be shifted around. + // @param pos Iterator pointing to the elt to be removed. + // @return An iterator pointing to the next elt or end(). + iterator erase(iterator pos); + + // Remove a range of elements [first, last) + // @param first Iterator pointing to the first element to be removed. + // @param last Iterator pointing to one past the last element to be removed. + // @return An iterator pointing to the elt next to 'last' or end(). + iterator erase(iterator first, iterator last); + // Empty the vector on return. Release the internal buffer. Length // and capacity are both 0 on return. If you want to keep the // internal buffer around for reuse, call 'resize'/'erase' instead. @@ -328,6 +340,37 @@ void vector<_T>::pop_back() } template<typename _T> +typename vector<_T>::iterator +vector<_T>::erase(iterator pos) { + if (mLength) { + std::copy(pos + 1, end(), pos); + --mLength; + if (!is_pod<value_type>::value) { + end()->~_T(); + } + } + return pos; +} + +template<typename _T> +typename vector<_T>::iterator +vector<_T>::erase(iterator first, iterator last) { + difference_type len = std::distance(first, last); + if (len > 0) { + last = std::copy(last, end(), first); + + if (!is_pod<value_type>::value) { + while (last != end()) { + last->~_T(); + ++last; + } + } + mLength -= len; + } + return first; +} + +template<typename _T> void vector<_T>::clear() { if(mBegin) |