Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.3 KiB

  1. //===- llvm/ADT/SmallSet.h - 'Normally small' sets --------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the SmallSet class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ADT_SMALLSET_H
  14. #define LLVM_ADT_SMALLSET_H
  15. #include "llvm/ADT/SmallPtrSet.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include <set>
  18. namespace llvm {
  19. /// SmallSet - This maintains a set of unique values, optimizing for the case
  20. /// when the set is small (less than N). In this case, the set can be
  21. /// maintained with no mallocs. If the set gets large, we expand to using an
  22. /// std::set to maintain reasonable lookup times.
  23. ///
  24. /// Note that this set does not provide a way to iterate over members in the
  25. /// set.
  26. template <typename T, unsigned N, typename C = std::less<T> >
  27. class SmallSet {
  28. /// Use a SmallVector to hold the elements here (even though it will never
  29. /// reach its 'large' stage) to avoid calling the default ctors of elements
  30. /// we will never use.
  31. SmallVector<T, N> Vector;
  32. std::set<T, C> Set;
  33. typedef typename SmallVector<T, N>::const_iterator VIterator;
  34. typedef typename SmallVector<T, N>::iterator mutable_iterator;
  35. public:
  36. SmallSet() {}
  37. bool empty() const { return Vector.empty() && Set.empty(); }
  38. unsigned size() const {
  39. return isSmall() ? Vector.size() : Set.size();
  40. }
  41. /// count - Return true if the element is in the set.
  42. bool count(const T &V) const {
  43. if (isSmall()) {
  44. // Since the collection is small, just do a linear search.
  45. return vfind(V) != Vector.end();
  46. } else {
  47. return Set.count(V);
  48. }
  49. }
  50. /// insert - Insert an element into the set if it isn't already there.
  51. /// Returns true if the element is inserted (it was not in the set before).
  52. bool insert(const T &V) {
  53. if (!isSmall())
  54. return Set.insert(V).second;
  55. VIterator I = vfind(V);
  56. if (I != Vector.end()) // Don't reinsert if it already exists.
  57. return false;
  58. if (Vector.size() < N) {
  59. Vector.push_back(V);
  60. return true;
  61. }
  62. // Otherwise, grow from vector to set.
  63. while (!Vector.empty()) {
  64. Set.insert(Vector.back());
  65. Vector.pop_back();
  66. }
  67. Set.insert(V);
  68. return true;
  69. }
  70. template <typename IterT>
  71. void insert(IterT I, IterT E) {
  72. for (; I != E; ++I)
  73. insert(*I);
  74. }
  75. bool erase(const T &V) {
  76. if (!isSmall())
  77. return Set.erase(V);
  78. for (mutable_iterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
  79. if (*I == V) {
  80. Vector.erase(I);
  81. return true;
  82. }
  83. return false;
  84. }
  85. void clear() {
  86. Vector.clear();
  87. Set.clear();
  88. }
  89. private:
  90. bool isSmall() const { return Set.empty(); }
  91. VIterator vfind(const T &V) const {
  92. for (VIterator I = Vector.begin(), E = Vector.end(); I != E; ++I)
  93. if (*I == V)
  94. return I;
  95. return Vector.end();
  96. }
  97. };
  98. /// If this set is of pointer values, transparently switch over to using
  99. /// SmallPtrSet for performance.
  100. template <typename PointeeType, unsigned N>
  101. class SmallSet<PointeeType*, N> : public SmallPtrSet<PointeeType*, N> {};
  102. } // end namespace llvm
  103. #endif