X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FADT%2FUniqueVector.h;h=a9cb2f5709eb2dc6cd5211aa1643ec7bc8aab8bd;hb=1b44cbf5acedf967dcc20417151e462dd2a9b67b;hp=d00de388e24363714d33f82ff5bc5379b975f78f;hpb=67218e9f276a22ee284c9206af85f2f1ae9e9094;p=oota-llvm.git diff --git a/include/llvm/ADT/UniqueVector.h b/include/llvm/ADT/UniqueVector.h index d00de388e24..a9cb2f5709e 100644 --- a/include/llvm/ADT/UniqueVector.h +++ b/include/llvm/ADT/UniqueVector.h @@ -1,16 +1,16 @@ - //===-- llvm/ADT/UniqueVector.h ---------------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // -// This file was developed by James M. Laskey and is distributed under -// the University of Illinois Open Source License. See LICENSE.TXT for details. +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// #ifndef LLVM_ADT_UNIQUEVECTOR_H #define LLVM_ADT_UNIQUEVECTOR_H +#include #include #include @@ -18,53 +18,89 @@ namespace llvm { //===----------------------------------------------------------------------===// /// UniqueVector - This class produces a sequential ID number (base 1) for each -/// unique entry that is added. This class also provides an ID ordered vector -/// of the entries (indexed by ID - 1.) T is the type of entries in the vector. -/// This class should have an implementation of operator== and of operator<. +/// unique entry that is added. T is the type of entries in the vector. This +/// class should have an implementation of operator== and of operator<. +/// Entries can be fetched using operator[] with the entry ID. template class UniqueVector { +public: + typedef typename std::vector VectorType; + typedef typename VectorType::iterator iterator; + typedef typename VectorType::const_iterator const_iterator; + private: // Map - Used to handle the correspondence of entry to ID. - typename std::map Map; + std::map Map; // Vector - ID ordered vector of entries. Entries can be indexed by ID - 1. // - typename std::vector Vector; - + VectorType Vector; + public: /// insert - Append entry to the vector if it doesn't already exist. Returns /// the entry's index + 1 to be used as a unique ID. unsigned insert(const T &Entry) { // Check if the entry is already in the map. - typename std::map::iterator MI = Map.lower_bound(Entry); - + unsigned &Val = Map[Entry]; + // See if entry exists, if so return prior ID. - if (MI != Map.end() && MI->first == Entry) return MI->second; + if (Val) return Val; // Compute ID for entry. - unsigned ID = Vector.size() + 1; - - // Insert in map. - Map.insert(MI, std::make_pair(Entry, ID)); - + Val = static_cast(Vector.size()) + 1; + // Insert in vector. Vector.push_back(Entry); + return Val; + } + + /// idFor - return the ID for an existing entry. Returns 0 if the entry is + /// not found. + unsigned idFor(const T &Entry) const { + // Search for entry in the map. + typename std::map::const_iterator MI = Map.find(Entry); + + // See if entry exists, if so return ID. + if (MI != Map.end()) return MI->second; - return ID; + // No luck. + return 0; } - + /// operator[] - Returns a reference to the entry with the specified ID. - /// - const T &operator[](unsigned ID) const { return Vector[ID - 1]; } - + /// + const T &operator[](unsigned ID) const { + assert(ID-1 < size() && "ID is 0 or out of range!"); + return Vector[ID - 1]; + } + + /// \brief Return an iterator to the start of the vector. + iterator begin() { return Vector.begin(); } + + /// \brief Return an iterator to the start of the vector. + const_iterator begin() const { return Vector.begin(); } + + /// \brief Return an iterator to the end of the vector. + iterator end() { return Vector.end(); } + + /// \brief Return an iterator to the end of the vector. + const_iterator end() const { return Vector.end(); } + /// size - Returns the number of entries in the vector. /// size_t size() const { return Vector.size(); } - - /// getVector - Return the ID ordered vector of entries. + + /// empty - Returns true if the vector is empty. /// - const typename std::vector &getVector() const { return Vector; } + bool empty() const { return Vector.empty(); } + + /// reset - Clears all the entries. + /// + void reset() { + Map.clear(); + Vector.resize(0, 0); + } }; } // End of namespace llvm -#endif // LLVM_ADT_UNIQUEVECTOR_H \ No newline at end of file +#endif // LLVM_ADT_UNIQUEVECTOR_H