2007-01-23 01:59:15 +01:00
|
|
|
//===- llvm/ADT/SmallSet.h - 'Normally small' sets --------------*- 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.
|
2007-01-23 01:59:15 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the SmallSet class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_SMALLSET_H
|
|
|
|
#define LLVM_ADT_SMALLSET_H
|
|
|
|
|
2007-01-27 09:20:15 +01:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2012-12-03 18:02:12 +01:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2007-01-27 09:14:53 +01:00
|
|
|
#include <set>
|
2007-01-23 01:59:15 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
/// SmallSet - This maintains a set of unique values, optimizing for the case
|
|
|
|
/// when the set is small (less than N). In this case, the set can be
|
2007-01-27 09:14:53 +01:00
|
|
|
/// maintained with no mallocs. If the set gets large, we expand to using an
|
|
|
|
/// std::set to maintain reasonable lookup times.
|
2007-01-23 01:59:15 +01:00
|
|
|
///
|
2007-01-27 09:14:53 +01:00
|
|
|
/// Note that this set does not provide a way to iterate over members in the
|
|
|
|
/// set.
|
2012-01-05 18:35:07 +01:00
|
|
|
template <typename T, unsigned N, typename C = std::less<T> >
|
2007-01-23 01:59:15 +01:00
|
|
|
class SmallSet {
|
2007-01-27 09:14:53 +01:00
|
|
|
/// Use a SmallVector to hold the elements here (even though it will never
|
2009-09-15 17:08:33 +02:00
|
|
|
/// reach its 'large' stage) to avoid calling the default ctors of elements
|
2007-01-27 09:14:53 +01:00
|
|
|
/// we will never use.
|
2007-01-23 01:59:15 +01:00
|
|
|
SmallVector<T, N> Vector;
|
2012-01-05 18:35:07 +01:00
|
|
|
std::set<T, C> Set;
|
2007-01-27 09:14:53 +01:00
|
|
|
typedef typename SmallVector<T, N>::const_iterator VIterator;
|
2007-01-23 01:59:15 +01:00
|
|
|
typedef typename SmallVector<T, N>::iterator mutable_iterator;
|
|
|
|
public:
|
2014-06-20 12:26:56 +02:00
|
|
|
typedef size_t size_type;
|
2007-01-23 01:59:15 +01:00
|
|
|
SmallSet() {}
|
|
|
|
|
2013-11-24 00:06:20 +01:00
|
|
|
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const {
|
|
|
|
return Vector.empty() && Set.empty();
|
|
|
|
}
|
|
|
|
|
2014-06-20 12:26:56 +02:00
|
|
|
size_type size() const {
|
2007-01-27 09:14:53 +01:00
|
|
|
return isSmall() ? Vector.size() : Set.size();
|
2007-01-23 02:16:19 +01:00
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2013-12-22 13:04:23 +01:00
|
|
|
/// count - Return 1 if the element is in the set, 0 otherwise.
|
2014-06-20 12:26:56 +02:00
|
|
|
size_type count(const T &V) const {
|
2007-01-27 09:14:53 +01:00
|
|
|
if (isSmall()) {
|
|
|
|
// Since the collection is small, just do a linear search.
|
2013-12-22 13:04:23 +01:00
|
|
|
return vfind(V) == Vector.end() ? 0 : 1;
|
2007-01-27 09:14:53 +01:00
|
|
|
} else {
|
|
|
|
return Set.count(V);
|
|
|
|
}
|
2007-01-23 01:59:15 +01:00
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2007-01-23 01:59:15 +01:00
|
|
|
/// insert - Insert an element into the set if it isn't already there.
|
2013-04-04 06:54:21 +02:00
|
|
|
/// Returns true if the element is inserted (it was not in the set before).
|
2007-01-27 03:13:58 +01:00
|
|
|
bool insert(const T &V) {
|
2007-01-27 09:14:53 +01:00
|
|
|
if (!isSmall())
|
|
|
|
return Set.insert(V).second;
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2007-01-27 09:14:53 +01:00
|
|
|
VIterator I = vfind(V);
|
|
|
|
if (I != Vector.end()) // Don't reinsert if it already exists.
|
2007-01-27 03:13:58 +01:00
|
|
|
return false;
|
2007-01-27 09:14:53 +01:00
|
|
|
if (Vector.size() < N) {
|
|
|
|
Vector.push_back(V);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, grow from vector to set.
|
|
|
|
while (!Vector.empty()) {
|
|
|
|
Set.insert(Vector.back());
|
|
|
|
Vector.pop_back();
|
|
|
|
}
|
|
|
|
Set.insert(V);
|
2007-01-27 03:13:58 +01:00
|
|
|
return true;
|
2007-01-23 01:59:15 +01:00
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2009-05-04 04:19:15 +02:00
|
|
|
template <typename IterT>
|
|
|
|
void insert(IterT I, IterT E) {
|
|
|
|
for (; I != E; ++I)
|
|
|
|
insert(*I);
|
|
|
|
}
|
|
|
|
|
2007-01-27 09:14:53 +01:00
|
|
|
bool erase(const T &V) {
|
|
|
|
if (!isSmall())
|
2007-02-04 01:12:12 +01:00
|
|
|
return Set.erase(V);
|
2007-01-23 01:59:15 +01:00
|
|
|
for (mutable_iterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
|
|
|
|
if (*I == V) {
|
|
|
|
Vector.erase(I);
|
2007-01-27 09:14:53 +01:00
|
|
|
return true;
|
2007-01-23 01:59:15 +01:00
|
|
|
}
|
2007-01-27 09:14:53 +01:00
|
|
|
return false;
|
2007-01-23 01:59:15 +01:00
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2007-01-23 01:59:15 +01:00
|
|
|
void clear() {
|
|
|
|
Vector.clear();
|
2007-01-27 09:14:53 +01:00
|
|
|
Set.clear();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
bool isSmall() const { return Set.empty(); }
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2007-01-27 09:14:53 +01:00
|
|
|
VIterator vfind(const T &V) const {
|
|
|
|
for (VIterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
|
|
|
|
if (*I == V)
|
|
|
|
return I;
|
|
|
|
return Vector.end();
|
2007-01-23 01:59:15 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-01-27 09:20:15 +01:00
|
|
|
/// If this set is of pointer values, transparently switch over to using
|
|
|
|
/// SmallPtrSet for performance.
|
|
|
|
template <typename PointeeType, unsigned N>
|
|
|
|
class SmallSet<PointeeType*, N> : public SmallPtrSet<PointeeType*, N> {};
|
2007-01-23 01:59:15 +01:00
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|