X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FADT%2FSetVector.h;h=f8b37c591838d4cd34c40febd4b751c44b5504da;hb=76c8710697c1d3dae98f97ef646610f96b830ea5;hp=a6e3f850fd270a87ff4bcdd6933f990fd2e76cc7;hpb=800473c8df8f0c9b566c9216bf124495451cb573;p=oota-llvm.git diff --git a/include/llvm/ADT/SetVector.h b/include/llvm/ADT/SetVector.h index a6e3f850fd2..f8b37c59183 100644 --- a/include/llvm/ADT/SetVector.h +++ b/include/llvm/ADT/SetVector.h @@ -1,34 +1,35 @@ -//===- SetVector.h - A set with insertion order iteration -------*- C++ -*-===// -// +//===- llvm/ADT/SetVector.h - Set with insert order iteration ---*- C++ -*-===// +// // The LLVM Compiler Infrastructure // // This file was developed by Reid Spencer and is distributed under // the University of Illinois Open Source License. See LICENSE.TXT for details. -// +// //===----------------------------------------------------------------------===// // -// This file implements a set that has insertion order iteration +// This file implements a set that has insertion order iteration // characteristics. This is useful for keeping a set of things that need to be // visited later but in a deterministic order (insertion order). The interface // is purposefully minimal. // //===----------------------------------------------------------------------===// -#ifndef SUPPORT_SETVECTOR_H -#define SUPPORT_SETVECTOR_H +#ifndef LLVM_ADT_SETVECTOR_H +#define LLVM_ADT_SETVECTOR_H #include #include +#include +#include namespace llvm { -/// This class provides a way to keep a set of things that also has the +/// This class provides a way to keep a set of things that also has the /// property of a deterministic iteration order. The order of iteration is the /// order of insertion. -/// @breif A vector that has set insertion semantics. +/// @brief A vector that has set insertion semantics. template class SetVector { - public: typedef T value_type; typedef T key_type; @@ -40,10 +41,13 @@ public: typedef typename vector_type::const_iterator const_iterator; typedef typename vector_type::size_type size_type; - /// @brief Completely clear the SetVector - void clear() { - set_.clear(); - vector_.clear(); + /// @brief Construct an empty SetVector + SetVector() {} + + /// @brief Initialize a SetVector with a range of elements + template + SetVector(It Start, It End) { + insert(Start, End); } /// @brief Determine if the SetVector is empty or not. @@ -76,27 +80,64 @@ 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 + void insert(It Start, It End) { + for (; Start != End; ++Start) + if (set_.insert(*Start).second) + vector_.push_back(*Start); + } + + /// @brief Remove an item from the set vector. + void remove(const value_type& X) { + if (0 < set_.erase(X)) { + iterator I = std::find(vector_.begin(),vector_.end(),X); + assert(I != vector_.end() && "Corrupted SetVector instances!"); + vector_.erase(I); + } + } + + /// @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.