Add back() and pop_back() methods to SetVector

Move clear to the end of the class
Add assertions

llvm-svn: 15203
This commit is contained in:
Chris Lattner 2004-07-25 11:07:02 +00:00
parent 3e3fe9ff5a
commit 6bc4b6c0dd

View File

@ -19,6 +19,7 @@
#include <set>
#include <vector>
#include <cassert>
namespace llvm {
@ -44,16 +45,10 @@ public:
/// @brief Initialize a SetVector with a range of elements
template<typename It>
SetVector( It Start, It End ) {
SetVector(It Start, It End) {
insert(Start, End);
}
/// @brief Completely clear the SetVector
void clear() {
set_.clear();
vector_.clear();
}
/// @brief Determine if the SetVector is empty or not.
bool empty() const {
return vector_.empty();
@ -84,35 +79,54 @@ public:
return vector_.end();
}
/// @brief Return the last element of the SetVector.
const T &back() const {
assert(!empty() && "Cannot call back() on empty SetVector!");
return vector_.back();
}
/// @brief Index into the SetVector.
const_reference operator[](size_type n) const {
return vector_[n];
assert(n < vector_.size() && "SetVector access out of range!");
return vector_[n];
}
/// @returns true iff the element was inserted into the SetVector.
/// @brief Insert a new element into the SetVector.
bool insert( const value_type& X ) {
bool insert(const value_type &X) {
bool result = set_.insert(X).second;
if ( result ) {
if (result)
vector_.push_back(X);
}
return result;
}
/// @brief Insert a range of elements into the SetVector.
template<typename It>
void insert( It Start, It End ) {
void insert(It Start, It End) {
for (; Start != End; ++Start)
if ( set_.insert(*Start).second )
if (set_.insert(*Start).second)
vector_.push_back(*Start);
}
/// @returns 0 if the element is not in the SetVector, 1 if it is.
/// @brief Count the number of elements of a given key in the SetVector.
size_type count( const key_type& key ) const {
size_type count(const key_type &key) const {
return set_.count(key);
}
/// @brief Completely clear the SetVector
void clear() {
set_.clear();
vector_.clear();
}
/// @brief Remove the last element of the SetVector.
void pop_back() {
assert(!empty() && "Cannot remove an element from an empty SetVector!");
set_.erase(back());
vector_.pop_back();
}
private:
set_type set_; ///< The set.
vector_type vector_; ///< The vector.