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