2004-09-01 22:55:40 +00:00
|
|
|
//===- llvm/ADT/SetVector.h - Set with insert order iteration ---*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2004-07-08 19:36:21 +00:00
|
|
|
// 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.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2004-07-08 19:36:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2005-04-21 20:19:05 +00:00
|
|
|
// This file implements a set that has insertion order iteration
|
2004-07-08 19:36:21 +00:00
|
|
|
// characteristics. This is useful for keeping a set of things that need to be
|
|
|
|
// visited later but in a deterministic order (insertion order). The interface
|
|
|
|
// is purposefully minimal.
|
|
|
|
//
|
2007-02-03 23:56:03 +00:00
|
|
|
// This file defines SetVector and SmallSetVector, which performs no allocations
|
|
|
|
// if the SetVector has less than a certain number of elements.
|
|
|
|
//
|
2004-07-08 19:36:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#ifndef LLVM_ADT_SETVECTOR_H
|
|
|
|
#define LLVM_ADT_SETVECTOR_H
|
2004-07-08 19:36:21 +00:00
|
|
|
|
2007-02-03 23:56:03 +00:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2004-09-11 04:25:58 +00:00
|
|
|
#include <algorithm>
|
2009-02-20 22:20:18 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <vector>
|
2004-07-08 19:36:21 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief A vector that has set insertion semantics.
|
|
|
|
///
|
2007-02-03 23:56:03 +00:00
|
|
|
/// This adapter class provides a way to keep a set of things that also has the
|
2004-07-08 19:36:21 +00:00
|
|
|
/// property of a deterministic iteration order. The order of iteration is the
|
|
|
|
/// order of insertion.
|
2007-02-03 23:56:03 +00:00
|
|
|
template <typename T, typename Vector = std::vector<T>,
|
2007-02-04 00:30:40 +00:00
|
|
|
typename Set = SmallSet<T, 16> >
|
2004-07-08 19:36:21 +00:00
|
|
|
class SetVector {
|
|
|
|
public:
|
|
|
|
typedef T value_type;
|
|
|
|
typedef T key_type;
|
|
|
|
typedef T& reference;
|
|
|
|
typedef const T& const_reference;
|
2007-02-03 23:56:03 +00:00
|
|
|
typedef Set set_type;
|
|
|
|
typedef Vector vector_type;
|
2007-01-17 02:22:18 +00:00
|
|
|
typedef typename vector_type::const_iterator iterator;
|
2004-07-08 19:36:21 +00:00
|
|
|
typedef typename vector_type::const_iterator const_iterator;
|
|
|
|
typedef typename vector_type::size_type size_type;
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Construct an empty SetVector
|
2004-07-15 08:18:31 +00:00
|
|
|
SetVector() {}
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Initialize a SetVector with a range of elements
|
2004-07-15 08:18:31 +00:00
|
|
|
template<typename It>
|
2004-07-25 11:07:02 +00:00
|
|
|
SetVector(It Start, It End) {
|
2004-07-15 08:18:31 +00:00
|
|
|
insert(Start, End);
|
|
|
|
}
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Determine if the SetVector is empty or not.
|
2004-07-08 19:36:21 +00:00
|
|
|
bool empty() const {
|
|
|
|
return vector_.empty();
|
|
|
|
}
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Determine the number of elements in the SetVector.
|
2004-07-08 19:36:21 +00:00
|
|
|
size_type size() const {
|
|
|
|
return vector_.size();
|
|
|
|
}
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Get an iterator to the beginning of the SetVector.
|
2004-07-08 19:36:21 +00:00
|
|
|
iterator begin() {
|
|
|
|
return vector_.begin();
|
|
|
|
}
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Get a const_iterator to the beginning of the SetVector.
|
2004-07-08 19:36:21 +00:00
|
|
|
const_iterator begin() const {
|
|
|
|
return vector_.begin();
|
|
|
|
}
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Get an iterator to the end of the SetVector.
|
2004-07-08 19:36:21 +00:00
|
|
|
iterator end() {
|
|
|
|
return vector_.end();
|
|
|
|
}
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Get a const_iterator to the end of the SetVector.
|
2004-07-08 19:36:21 +00:00
|
|
|
const_iterator end() const {
|
|
|
|
return vector_.end();
|
|
|
|
}
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Return the last element of the SetVector.
|
2004-07-25 11:07:02 +00:00
|
|
|
const T &back() const {
|
|
|
|
assert(!empty() && "Cannot call back() on empty SetVector!");
|
|
|
|
return vector_.back();
|
|
|
|
}
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Index into the SetVector.
|
2004-07-08 19:36:21 +00:00
|
|
|
const_reference operator[](size_type n) const {
|
2004-07-25 11:07:02 +00:00
|
|
|
assert(n < vector_.size() && "SetVector access out of range!");
|
|
|
|
return vector_[n];
|
2004-07-08 19:36:21 +00:00
|
|
|
}
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Insert a new element into the SetVector.
|
|
|
|
/// \returns true iff the element was inserted into the SetVector.
|
2004-07-25 11:07:02 +00:00
|
|
|
bool insert(const value_type &X) {
|
2014-11-19 07:49:26 +00:00
|
|
|
bool result = set_.insert(X).second;
|
2004-07-25 11:07:02 +00:00
|
|
|
if (result)
|
2004-07-08 19:36:21 +00:00
|
|
|
vector_.push_back(X);
|
2004-07-08 21:50:33 +00:00
|
|
|
return result;
|
2004-07-08 19:36:21 +00:00
|
|
|
}
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Insert a range of elements into the SetVector.
|
2004-07-15 08:18:31 +00:00
|
|
|
template<typename It>
|
2004-07-25 11:07:02 +00:00
|
|
|
void insert(It Start, It End) {
|
2004-07-15 08:18:31 +00:00
|
|
|
for (; Start != End; ++Start)
|
2014-11-19 07:49:26 +00:00
|
|
|
if (set_.insert(*Start).second)
|
2004-07-15 08:18:31 +00:00
|
|
|
vector_.push_back(*Start);
|
|
|
|
}
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Remove an item from the set vector.
|
2010-09-22 23:20:04 +00:00
|
|
|
bool remove(const value_type& X) {
|
2007-02-04 00:30:40 +00:00
|
|
|
if (set_.erase(X)) {
|
2007-01-17 02:22:18 +00:00
|
|
|
typename vector_type::iterator I =
|
|
|
|
std::find(vector_.begin(), vector_.end(), X);
|
2004-09-11 20:38:25 +00:00
|
|
|
assert(I != vector_.end() && "Corrupted SetVector instances!");
|
|
|
|
vector_.erase(I);
|
2010-09-22 23:20:04 +00:00
|
|
|
return true;
|
2004-09-11 04:25:58 +00:00
|
|
|
}
|
2010-09-22 23:20:04 +00:00
|
|
|
return false;
|
2004-09-11 04:25:58 +00:00
|
|
|
}
|
|
|
|
|
2012-10-02 22:46:45 +00:00
|
|
|
/// \brief Remove items from the set vector based on a predicate function.
|
|
|
|
///
|
|
|
|
/// This is intended to be equivalent to the following code, if we could
|
|
|
|
/// write it:
|
|
|
|
///
|
|
|
|
/// \code
|
|
|
|
/// V.erase(std::remove_if(V.begin(), V.end(), P), V.end());
|
|
|
|
/// \endcode
|
|
|
|
///
|
|
|
|
/// However, SetVector doesn't expose non-const iterators, making any
|
|
|
|
/// algorithm like remove_if impossible to use.
|
|
|
|
///
|
|
|
|
/// \returns true if any element is removed.
|
|
|
|
template <typename UnaryPredicate>
|
|
|
|
bool remove_if(UnaryPredicate P) {
|
2012-10-03 01:04:07 +00:00
|
|
|
typename vector_type::iterator I
|
|
|
|
= std::remove_if(vector_.begin(), vector_.end(),
|
|
|
|
TestAndEraseFromSet<UnaryPredicate>(P, set_));
|
|
|
|
if (I == vector_.end())
|
2012-10-02 22:46:45 +00:00
|
|
|
return false;
|
2012-10-03 01:04:07 +00:00
|
|
|
vector_.erase(I, vector_.end());
|
2012-10-02 22:46:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-09-11 04:25:58 +00:00
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Count the number of elements of a given key in the SetVector.
|
|
|
|
/// \returns 0 if the element is not in the SetVector, 1 if it is.
|
2004-07-25 11:07:02 +00:00
|
|
|
size_type count(const key_type &key) const {
|
2004-07-08 19:36:21 +00:00
|
|
|
return set_.count(key);
|
|
|
|
}
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Completely clear the SetVector
|
2004-07-25 11:07:02 +00:00
|
|
|
void clear() {
|
|
|
|
set_.clear();
|
|
|
|
vector_.clear();
|
|
|
|
}
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Remove the last element of the SetVector.
|
2004-07-25 11:07:02 +00:00
|
|
|
void pop_back() {
|
|
|
|
assert(!empty() && "Cannot remove an element from an empty SetVector!");
|
|
|
|
set_.erase(back());
|
|
|
|
vector_.pop_back();
|
|
|
|
}
|
2011-12-20 00:03:41 +00:00
|
|
|
|
2013-08-19 22:12:00 +00:00
|
|
|
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val() {
|
2011-12-20 00:03:41 +00:00
|
|
|
T Ret = back();
|
|
|
|
pop_back();
|
|
|
|
return Ret;
|
|
|
|
}
|
2004-07-25 11:07:02 +00:00
|
|
|
|
2010-06-05 00:26:02 +00:00
|
|
|
bool operator==(const SetVector &that) const {
|
|
|
|
return vector_ == that.vector_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const SetVector &that) const {
|
|
|
|
return vector_ != that.vector_;
|
|
|
|
}
|
|
|
|
|
2004-07-08 19:36:21 +00:00
|
|
|
private:
|
2012-10-03 01:04:07 +00:00
|
|
|
/// \brief A wrapper predicate designed for use with std::remove_if.
|
|
|
|
///
|
|
|
|
/// This predicate wraps a predicate suitable for use with std::remove_if to
|
|
|
|
/// call set_.erase(x) on each element which is slated for removal.
|
|
|
|
template <typename UnaryPredicate>
|
|
|
|
class TestAndEraseFromSet {
|
|
|
|
UnaryPredicate P;
|
|
|
|
set_type &set_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TestAndEraseFromSet(UnaryPredicate P, set_type &set_) : P(P), set_(set_) {}
|
|
|
|
|
2014-03-03 19:28:52 +00:00
|
|
|
template <typename ArgumentT>
|
|
|
|
bool operator()(const ArgumentT &Arg) {
|
2012-10-03 01:04:07 +00:00
|
|
|
if (P(Arg)) {
|
|
|
|
set_.erase(Arg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2004-07-08 19:36:21 +00:00
|
|
|
set_type set_; ///< The set.
|
|
|
|
vector_type vector_; ///< The vector.
|
|
|
|
};
|
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief A SetVector that performs no allocations if smaller than
|
2007-02-03 23:56:03 +00:00
|
|
|
/// a certain size.
|
|
|
|
template <typename T, unsigned N>
|
|
|
|
class SmallSetVector : public SetVector<T, SmallVector<T, N>, SmallSet<T, N> > {
|
2007-02-04 00:30:40 +00:00
|
|
|
public:
|
2007-02-03 23:56:03 +00:00
|
|
|
SmallSetVector() {}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2012-10-02 22:46:40 +00:00
|
|
|
/// \brief Initialize a SmallSetVector with a range of elements
|
2007-02-03 23:56:03 +00:00
|
|
|
template<typename It>
|
|
|
|
SmallSetVector(It Start, It End) {
|
|
|
|
this->insert(Start, End);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2004-07-08 19:36:21 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
// vim: sw=2 ai
|
|
|
|
#endif
|