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 private:
00026   // Map - Used to handle the correspondence of entry to ID.
00027   std::map<T, unsigned> Map;
00028 
00029   // Vector - ID ordered vector of entries. Entries can be indexed by ID - 1.
00030   //
00031   std::vector<T> Vector;
00032 
00033 public:
00034   /// insert - Append entry to the vector if it doesn't already exist.  Returns
00035   /// the entry's index + 1 to be used as a unique ID.
00036   unsigned insert(const T &Entry) {
00037     // Check if the entry is already in the map.
00038     unsigned &Val = Map[Entry];
00039 
00040     // See if entry exists, if so return prior ID.
00041     if (Val) return Val;
00042 
00043     // Compute ID for entry.
00044     Val = static_cast<unsigned>(Vector.size()) + 1;
00045 
00046     // Insert in vector.
00047     Vector.push_back(Entry);
00048     return Val;
00049   }
00050 
00051   /// idFor - return the ID for an existing entry.  Returns 0 if the entry is
00052   /// not found.
00053   unsigned idFor(const T &Entry) const {
00054     // Search for entry in the map.
00055     typename std::map<T, unsigned>::const_iterator MI = Map.find(Entry);
00056 
00057     // See if entry exists, if so return ID.
00058     if (MI != Map.end()) return MI->second;
00059 
00060     // No luck.
00061     return 0;
00062   }
00063 
00064   /// operator[] - Returns a reference to the entry with the specified ID.
00065   ///
00066   const T &operator[](unsigned ID) const {
00067     assert(ID-1 < size() && "ID is 0 or out of range!");
00068     return Vector[ID - 1];
00069   }
00070 
00071   /// size - Returns the number of entries in the vector.
00072   ///
00073   size_t size() const { return Vector.size(); }
00074 
00075   /// empty - Returns true if the vector is empty.
00076   ///
00077   bool empty() const { return Vector.empty(); }
00078 
00079   /// reset - Clears all the entries.
00080   ///
00081   void reset() {
00082     Map.clear();
00083     Vector.resize(0, 0);
00084   }
00085 };
00086 
00087 } // End of namespace llvm
00088 
00089 #endif // LLVM_ADT_UNIQUEVECTOR_H