LLVM API Documentation

UniqueVector.h
Go to the documentation of this file.
00001 //===-- llvm/ADT/UniqueVector.h ---------------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_ADT_UNIQUEVECTOR_H
00011 #define LLVM_ADT_UNIQUEVECTOR_H
00012 
00013 #include <cassert>
00014 #include <map>
00015 #include <vector>
00016 
00017 namespace llvm {
00018 
00019 //===----------------------------------------------------------------------===//
00020 /// UniqueVector - This class produces a sequential ID number (base 1) for each
00021 /// unique entry that is added.  T is the type of entries in the vector. This
00022 /// class should have an implementation of operator== and of operator<.
00023 /// Entries can be fetched using operator[] with the entry ID.
00024 template<class T> class UniqueVector {
00025 public:
00026   typedef typename std::vector<T> VectorType;
00027   typedef typename VectorType::iterator iterator;
00028   typedef typename VectorType::const_iterator const_iterator;
00029 
00030 private:
00031   // Map - Used to handle the correspondence of entry to ID.
00032   std::map<T, unsigned> Map;
00033 
00034   // Vector - ID ordered vector of entries. Entries can be indexed by ID - 1.
00035   //
00036   VectorType Vector;
00037 
00038 public:
00039   /// insert - Append entry to the vector if it doesn't already exist.  Returns
00040   /// the entry's index + 1 to be used as a unique ID.
00041   unsigned insert(const T &Entry) {
00042     // Check if the entry is already in the map.
00043     unsigned &Val = Map[Entry];
00044 
00045     // See if entry exists, if so return prior ID.
00046     if (Val) return Val;
00047 
00048     // Compute ID for entry.
00049     Val = static_cast<unsigned>(Vector.size()) + 1;
00050 
00051     // Insert in vector.
00052     Vector.push_back(Entry);
00053     return Val;
00054   }
00055 
00056   /// idFor - return the ID for an existing entry.  Returns 0 if the entry is
00057   /// not found.
00058   unsigned idFor(const T &Entry) const {
00059     // Search for entry in the map.
00060     typename std::map<T, unsigned>::const_iterator MI = Map.find(Entry);
00061 
00062     // See if entry exists, if so return ID.
00063     if (MI != Map.end()) return MI->second;
00064 
00065     // No luck.
00066     return 0;
00067   }
00068 
00069   /// operator[] - Returns a reference to the entry with the specified ID.
00070   ///
00071   const T &operator[](unsigned ID) const {
00072     assert(ID-1 < size() && "ID is 0 or out of range!");
00073     return Vector[ID - 1];
00074   }
00075 
00076   /// \brief Return an iterator to the start of the vector.
00077   iterator begin() { return Vector.begin(); }
00078 
00079   /// \brief Return an iterator to the start of the vector.
00080   const_iterator begin() const { return Vector.begin(); }
00081 
00082   /// \brief Return an iterator to the end of the vector.
00083   iterator end() { return Vector.end(); }
00084 
00085   /// \brief Return an iterator to the end of the vector.
00086   const_iterator end() const { return Vector.end(); }
00087 
00088   /// size - Returns the number of entries in the vector.
00089   ///
00090   size_t size() const { return Vector.size(); }
00091 
00092   /// empty - Returns true if the vector is empty.
00093   ///
00094   bool empty() const { return Vector.empty(); }
00095 
00096   /// reset - Clears all the entries.
00097   ///
00098   void reset() {
00099     Map.clear();
00100     Vector.resize(0, 0);
00101   }
00102 };
00103 
00104 } // End of namespace llvm
00105 
00106 #endif // LLVM_ADT_UNIQUEVECTOR_H