2006-01-16 23:29:43 +00:00
|
|
|
//===-- llvm/ADT/UniqueVector.h ---------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-01-16 23:29:43 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_UNIQUEVECTOR_H
|
|
|
|
#define LLVM_ADT_UNIQUEVECTOR_H
|
|
|
|
|
2007-02-06 14:59:28 +00:00
|
|
|
#include <cassert>
|
2006-01-16 23:29:43 +00:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// UniqueVector - This class produces a sequential ID number (base 1) for each
|
2006-01-17 16:29:58 +00:00
|
|
|
/// 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<.
|
2009-01-09 19:25:42 +00:00
|
|
|
/// Entries can be fetched using operator[] with the entry ID.
|
2006-01-16 23:29:43 +00:00
|
|
|
template<class T> class UniqueVector {
|
|
|
|
private:
|
|
|
|
// Map - Used to handle the correspondence of entry to ID.
|
2006-01-17 16:29:58 +00:00
|
|
|
std::map<T, unsigned> Map;
|
2006-01-16 23:29:43 +00:00
|
|
|
|
|
|
|
// Vector - ID ordered vector of entries. Entries can be indexed by ID - 1.
|
|
|
|
//
|
2007-02-05 23:24:48 +00:00
|
|
|
std::vector<T> Vector;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-01-16 23:29:43 +00:00
|
|
|
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.
|
2007-02-05 23:24:48 +00:00
|
|
|
unsigned &Val = Map[Entry];
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-01-16 23:29:43 +00:00
|
|
|
// See if entry exists, if so return prior ID.
|
2007-02-05 23:24:48 +00:00
|
|
|
if (Val) return Val;
|
2006-01-16 23:29:43 +00:00
|
|
|
|
|
|
|
// Compute ID for entry.
|
2008-05-05 18:30:58 +00:00
|
|
|
Val = static_cast<unsigned>(Vector.size()) + 1;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-01-16 23:29:43 +00:00
|
|
|
// Insert in vector.
|
2007-02-05 23:24:48 +00:00
|
|
|
Vector.push_back(Entry);
|
|
|
|
return Val;
|
2006-01-16 23:29:43 +00:00
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-01-26 20:09:35 +00:00
|
|
|
/// 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.
|
2007-02-05 23:18:32 +00:00
|
|
|
typename std::map<T, unsigned>::const_iterator MI = Map.find(Entry);
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-01-26 20:09:35 +00:00
|
|
|
// See if entry exists, if so return ID.
|
2006-01-26 20:30:51 +00:00
|
|
|
if (MI != Map.end()) return MI->second;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-01-26 20:09:35 +00:00
|
|
|
// No luck.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-01-16 23:29:43 +00:00
|
|
|
/// operator[] - Returns a reference to the entry with the specified ID.
|
2006-01-17 16:29:58 +00:00
|
|
|
///
|
2007-02-05 23:24:48 +00:00
|
|
|
const T &operator[](unsigned ID) const {
|
|
|
|
assert(ID-1 < size() && "ID is 0 or out of range!");
|
|
|
|
return Vector[ID - 1];
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-01-16 23:29:43 +00:00
|
|
|
/// size - Returns the number of entries in the vector.
|
|
|
|
///
|
|
|
|
size_t size() const { return Vector.size(); }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-01-17 16:29:58 +00:00
|
|
|
/// empty - Returns true if the vector is empty.
|
2006-01-16 23:29:43 +00:00
|
|
|
///
|
2006-01-17 16:29:58 +00:00
|
|
|
bool empty() const { return Vector.empty(); }
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2006-01-26 20:09:35 +00:00
|
|
|
/// reset - Clears all the entries.
|
|
|
|
///
|
|
|
|
void reset() {
|
|
|
|
Map.clear();
|
|
|
|
Vector.resize(0, 0);
|
|
|
|
}
|
2006-01-16 23:29:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End of namespace llvm
|
|
|
|
|
2006-01-17 19:21:01 +00:00
|
|
|
#endif // LLVM_ADT_UNIQUEVECTOR_H
|