Reduce memory consumption and force (somewhat) access to entries via ID.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@25393 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jim Laskey 2006-01-17 16:29:58 +00:00
parent 57c517d30c
commit e3150024b4

View File

@ -18,17 +18,17 @@ namespace llvm {
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// UniqueVector - This class produces a sequential ID number (base 1) for each /// 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 /// unique entry that is added. T is the type of entries in the vector. This
/// of the entries (indexed by ID - 1.) T is the type of entries in the vector. /// class should have an implementation of operator== and of operator<.
/// This class should have an implementation of operator== and of operator<. /// Entries can be fetched using operator[] with the entry ID.
template<class T> class UniqueVector { template<class T> class UniqueVector {
private: private:
// Map - Used to handle the correspondence of entry to ID. // Map - Used to handle the correspondence of entry to ID.
typename std::map<T, unsigned> Map; std::map<T, unsigned> Map;
// Vector - ID ordered vector of entries. Entries can be indexed by ID - 1. // Vector - ID ordered vector of entries. Entries can be indexed by ID - 1.
// //
typename std::vector<T> Vector; std::vector<const T *> Vector;
public: public:
/// insert - Append entry to the vector if it doesn't already exist. Returns /// insert - Append entry to the vector if it doesn't already exist. Returns
@ -44,25 +44,25 @@ public:
unsigned ID = Vector.size() + 1; unsigned ID = Vector.size() + 1;
// Insert in map. // Insert in map.
Map.insert(MI, std::make_pair(Entry, ID)); MI = Map.insert(MI, std::make_pair(Entry, ID));
// Insert in vector. // Insert in vector.
Vector.push_back(Entry); Vector.push_back(&MI->first);
return ID; return ID;
} }
/// operator[] - Returns a reference to the entry with the specified ID. /// 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 { return *Vector[ID - 1]; }
/// size - Returns the number of entries in the vector. /// size - Returns the number of entries in the vector.
/// ///
size_t size() const { return Vector.size(); } 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<T> &getVector() const { return Vector; } bool empty() const { return Vector.empty(); }
}; };
} // End of namespace llvm } // End of namespace llvm