sequence_number_util.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_NUMERICS_SEQUENCE_NUMBER_UTIL_H_
  11. #define RTC_BASE_NUMERICS_SEQUENCE_NUMBER_UTIL_H_
  12. #include <stdint.h>
  13. #include <limits>
  14. #include <type_traits>
  15. #include "absl/types/optional.h"
  16. #include "rtc_base/checks.h"
  17. #include "rtc_base/numerics/mod_ops.h"
  18. namespace webrtc {
  19. // Test if the sequence number |a| is ahead or at sequence number |b|.
  20. //
  21. // If |M| is an even number and the two sequence numbers are at max distance
  22. // from each other, then the sequence number with the highest value is
  23. // considered to be ahead.
  24. template <typename T, T M>
  25. inline typename std::enable_if<(M > 0), bool>::type AheadOrAt(T a, T b) {
  26. static_assert(std::is_unsigned<T>::value,
  27. "Type must be an unsigned integer.");
  28. const T maxDist = M / 2;
  29. if (!(M & 1) && MinDiff<T, M>(a, b) == maxDist)
  30. return b < a;
  31. return ForwardDiff<T, M>(b, a) <= maxDist;
  32. }
  33. template <typename T, T M>
  34. inline typename std::enable_if<(M == 0), bool>::type AheadOrAt(T a, T b) {
  35. static_assert(std::is_unsigned<T>::value,
  36. "Type must be an unsigned integer.");
  37. const T maxDist = std::numeric_limits<T>::max() / 2 + T(1);
  38. if (a - b == maxDist)
  39. return b < a;
  40. return ForwardDiff(b, a) < maxDist;
  41. }
  42. template <typename T>
  43. inline bool AheadOrAt(T a, T b) {
  44. return AheadOrAt<T, 0>(a, b);
  45. }
  46. // Test if the sequence number |a| is ahead of sequence number |b|.
  47. //
  48. // If |M| is an even number and the two sequence numbers are at max distance
  49. // from each other, then the sequence number with the highest value is
  50. // considered to be ahead.
  51. template <typename T, T M = 0>
  52. inline bool AheadOf(T a, T b) {
  53. static_assert(std::is_unsigned<T>::value,
  54. "Type must be an unsigned integer.");
  55. return a != b && AheadOrAt<T, M>(a, b);
  56. }
  57. // Comparator used to compare sequence numbers in a continuous fashion.
  58. //
  59. // WARNING! If used to sort sequence numbers of length M then the interval
  60. // covered by the sequence numbers may not be larger than floor(M/2).
  61. template <typename T, T M = 0>
  62. struct AscendingSeqNumComp {
  63. bool operator()(T a, T b) const { return AheadOf<T, M>(a, b); }
  64. };
  65. // Comparator used to compare sequence numbers in a continuous fashion.
  66. //
  67. // WARNING! If used to sort sequence numbers of length M then the interval
  68. // covered by the sequence numbers may not be larger than floor(M/2).
  69. template <typename T, T M = 0>
  70. struct DescendingSeqNumComp {
  71. bool operator()(T a, T b) const { return AheadOf<T, M>(b, a); }
  72. };
  73. // A sequence number unwrapper where the first unwrapped value equals the
  74. // first value being unwrapped.
  75. template <typename T, T M = 0>
  76. class SeqNumUnwrapper {
  77. static_assert(
  78. std::is_unsigned<T>::value &&
  79. std::numeric_limits<T>::max() < std::numeric_limits<int64_t>::max(),
  80. "Type unwrapped must be an unsigned integer smaller than int64_t.");
  81. public:
  82. int64_t Unwrap(T value) {
  83. if (!last_value_) {
  84. last_unwrapped_ = {value};
  85. } else {
  86. last_unwrapped_ += ForwardDiff<T, M>(*last_value_, value);
  87. if (!AheadOrAt<T, M>(value, *last_value_)) {
  88. constexpr int64_t kBackwardAdjustment =
  89. M == 0 ? int64_t{std::numeric_limits<T>::max()} + 1 : M;
  90. last_unwrapped_ -= kBackwardAdjustment;
  91. }
  92. }
  93. last_value_ = value;
  94. return last_unwrapped_;
  95. }
  96. private:
  97. int64_t last_unwrapped_ = 0;
  98. absl::optional<T> last_value_;
  99. };
  100. } // namespace webrtc
  101. #endif // RTC_BASE_NUMERICS_SEQUENCE_NUMBER_UTIL_H_