compact_heap.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #pragma once
  2. #include "compact_vector.h"
  3. namespace NYT {
  4. ////////////////////////////////////////////////////////////////////////////////
  5. //! A heap structure optimized for storing elements inline
  6. //! and with little memory overhead. See TCompactVector.
  7. /*!
  8. * When inline, uses linear search for selecting minimum
  9. * instead of storing heap.
  10. */
  11. template <class T, size_t N, class C = std::less<T>>
  12. class TCompactHeap
  13. {
  14. public:
  15. static_assert(N <= 8, "TCompactHeap is not optimal for large N");
  16. explicit TCompactHeap(C comparator = C()) noexcept;
  17. using value_type = T;
  18. using const_reference = const T&;
  19. using const_iterator = const T*;
  20. using difference_type = ptrdiff_t;
  21. using size_type = size_t;
  22. void push(T value);
  23. void pop();
  24. const_reference get_min() const;
  25. value_type extract_min();
  26. const_iterator begin() const;
  27. const_iterator end() const;
  28. void swap(TCompactHeap<T, N, C>& other);
  29. size_t size() const;
  30. size_t capacity() const;
  31. size_t max_size() const;
  32. bool empty() const;
  33. void shrink_to_small();
  34. private:
  35. TCompactVector<T, N> Heap_;
  36. class TReverseComparator
  37. {
  38. public:
  39. explicit TReverseComparator(C underlying);
  40. bool operator()(const T& lhs, const T& rhs) const;
  41. private:
  42. C Underlying_;
  43. };
  44. TReverseComparator Comparator_;
  45. bool IsInline() const;
  46. };
  47. ////////////////////////////////////////////////////////////////////////////////
  48. } // namespace NYT
  49. #define COMPACT_HEAP_INL_H_
  50. #include "compact_heap-inl.h"
  51. #undef COMPACT_HEAP_INL_H_