sequence.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #pragma once
  2. #include "numeric.h"
  3. #include <util/generic/hash.h>
  4. #include <util/generic/array_ref.h>
  5. template <typename ElementHash = void>
  6. class TRangeHash {
  7. private:
  8. template <typename ElementType>
  9. using TBase = std::conditional_t<
  10. !std::is_void<ElementHash>::value,
  11. ElementHash,
  12. THash<ElementType>>;
  13. public:
  14. template <typename Range>
  15. size_t operator()(const Range& range) const {
  16. size_t accumulated = 0;
  17. for (const auto& element : range) {
  18. accumulated = CombineHashes(accumulated, TBase<typename Range::value_type>()(element));
  19. }
  20. return accumulated;
  21. }
  22. };
  23. using TSimpleRangeHash = TRangeHash<>;
  24. template <typename RegionHash = void>
  25. class TContiguousHash {
  26. private:
  27. template <typename ElementType>
  28. using TBase = std::conditional_t<
  29. !std::is_void<RegionHash>::value,
  30. RegionHash,
  31. TRangeHash<ElementType>>;
  32. public:
  33. template <typename ContainerType>
  34. auto operator()(const ContainerType& container) const {
  35. return operator()(MakeArrayRef(container));
  36. }
  37. template <typename ElementType>
  38. auto operator()(const TArrayRef<ElementType>& data) const {
  39. return TBase<ElementType>()(data);
  40. }
  41. };