compact_set.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 TCompactSet class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #pragma once
  14. #include "compact_vector.h"
  15. #include <util/system/yassert.h>
  16. #include <cstddef>
  17. #include <iterator>
  18. #include <set>
  19. #include <type_traits>
  20. namespace NYT {
  21. /// TCompactSet - This maintains a set of unique values, optimizing for the case
  22. /// when the set is small (less than N). In this case, the set can be
  23. /// maintained with no mallocs. If the set gets large, we expand to using an
  24. /// std::set to maintain reasonable lookup times.
  25. ///
  26. /// Note that any modification of the set may invalidate *all* iterators.
  27. template <typename T, unsigned N, typename C = std::less<T>>
  28. class TCompactSet
  29. {
  30. private:
  31. /// Use a CompactVector to hold the elements here (even though it will never
  32. /// reach its 'large' stage) to avoid calling the default ctors of elements
  33. /// we will never use.
  34. TCompactVector<T, N> Vector;
  35. std::set<T, C> Set;
  36. using TSetConstIterator = typename std::set<T, C>::const_iterator;
  37. using TVectorConstIterator = typename TCompactVector<T, N>::const_iterator;
  38. public:
  39. class const_iterator;
  40. using size_type = std::size_t;
  41. TCompactSet() {}
  42. [[nodiscard]] bool empty() const;
  43. size_type size() const;
  44. const T& front() const;
  45. /// count - Return true if the element is in the set.
  46. size_type count(const T& v) const;
  47. /// insert - Insert an element into the set if it isn't already there.
  48. std::pair<const_iterator, bool> insert(const T& v);
  49. template <typename TIter>
  50. void insert(TIter i, TIter e);
  51. bool erase(const T& v);
  52. void clear();
  53. const_iterator begin() const;
  54. const_iterator cbegin() const;
  55. const_iterator end() const;
  56. const_iterator cend() const;
  57. private:
  58. bool IsSmall() const;
  59. };
  60. } // namespace NYT
  61. #define COMPACT_SET_INL_H_
  62. #include "compact_set-inl.h"
  63. #undef COMPACT_SET_INL_H_