blob: 96a4974ec5e56ffbe3792318167f6c3c180fb5a6 [file] [log] [blame]
philipel2cb73412016-03-22 10:03:43 +01001/*
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
Bjorn Tereliusa194e582017-10-25 13:07:09 +020011#ifndef RTC_BASE_NUMERICS_SEQUENCE_NUMBER_UTIL_H_
12#define RTC_BASE_NUMERICS_SEQUENCE_NUMBER_UTIL_H_
philipel2cb73412016-03-22 10:03:43 +010013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
philipel2cb73412016-03-22 10:03:43 +010016#include <limits>
17#include <type_traits>
18
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020019#include "absl/types/optional.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "rtc_base/checks.h"
Karl Wiberg65c39222017-11-22 12:25:14 +010021#include "rtc_base/numerics/mod_ops.h"
philipel2cb73412016-03-22 10:03:43 +010022
23namespace webrtc {
24
25// Test if the sequence number |a| is ahead or at sequence number |b|.
26//
27// If |M| is an even number and the two sequence numbers are at max distance
28// from each other, then the sequence number with the highest value is
29// considered to be ahead.
30template <typename T, T M>
philipel7956c0f2017-07-26 07:48:15 -070031inline typename std::enable_if<(M > 0), bool>::type AheadOrAt(T a, T b) {
philipel2cb73412016-03-22 10:03:43 +010032 static_assert(std::is_unsigned<T>::value,
33 "Type must be an unsigned integer.");
34 const T maxDist = M / 2;
35 if (!(M & 1) && MinDiff<T, M>(a, b) == maxDist)
36 return b < a;
37 return ForwardDiff<T, M>(b, a) <= maxDist;
38}
39
philipel7956c0f2017-07-26 07:48:15 -070040template <typename T, T M>
41inline typename std::enable_if<(M == 0), bool>::type AheadOrAt(T a, T b) {
philipel2cb73412016-03-22 10:03:43 +010042 static_assert(std::is_unsigned<T>::value,
43 "Type must be an unsigned integer.");
44 const T maxDist = std::numeric_limits<T>::max() / 2 + T(1);
45 if (a - b == maxDist)
46 return b < a;
47 return ForwardDiff(b, a) < maxDist;
48}
49
philipel7956c0f2017-07-26 07:48:15 -070050template <typename T>
51inline bool AheadOrAt(T a, T b) {
52 return AheadOrAt<T, 0>(a, b);
53}
54
philipel2cb73412016-03-22 10:03:43 +010055// Test if the sequence number |a| is ahead of sequence number |b|.
56//
57// If |M| is an even number and the two sequence numbers are at max distance
58// from each other, then the sequence number with the highest value is
59// considered to be ahead.
philipel7956c0f2017-07-26 07:48:15 -070060template <typename T, T M = 0>
philipel2cb73412016-03-22 10:03:43 +010061inline bool AheadOf(T a, T b) {
62 static_assert(std::is_unsigned<T>::value,
63 "Type must be an unsigned integer.");
64 return a != b && AheadOrAt<T, M>(a, b);
65}
66
philipel7956c0f2017-07-26 07:48:15 -070067// Comparator used to compare sequence numbers in a continuous fashion.
68//
69// WARNING! If used to sort sequence numbers of length M then the interval
70// covered by the sequence numbers may not be larger than floor(M/2).
71template <typename T, T M = 0>
72struct AscendingSeqNumComp {
philipel2cb73412016-03-22 10:03:43 +010073 bool operator()(T a, T b) const { return AheadOf<T, M>(a, b); }
74};
75
philipel2cb73412016-03-22 10:03:43 +010076// Comparator used to compare sequence numbers in a continuous fashion.
77//
78// WARNING! If used to sort sequence numbers of length M then the interval
79// covered by the sequence numbers may not be larger than floor(M/2).
80template <typename T, T M = 0>
philipel7956c0f2017-07-26 07:48:15 -070081struct DescendingSeqNumComp {
82 bool operator()(T a, T b) const { return AheadOf<T, M>(b, a); }
philipel2cb73412016-03-22 10:03:43 +010083};
84
Philip Eliasson1f850a62019-03-19 12:15:00 +000085// A sequence number unwrapper where the first unwrapped value equals the
86// first value being unwrapped.
philipel2cb73412016-03-22 10:03:43 +010087template <typename T, T M = 0>
philipel7956c0f2017-07-26 07:48:15 -070088class SeqNumUnwrapper {
89 static_assert(
90 std::is_unsigned<T>::value &&
Philip Eliasson1f850a62019-03-19 12:15:00 +000091 std::numeric_limits<T>::max() < std::numeric_limits<int64_t>::max(),
92 "Type unwrapped must be an unsigned integer smaller than int64_t.");
philipel7956c0f2017-07-26 07:48:15 -070093
94 public:
Philip Eliasson1f850a62019-03-19 12:15:00 +000095 int64_t Unwrap(T value) {
96 if (!last_value_) {
97 last_unwrapped_ = {value};
Philip Eliassonb5207b42019-03-14 18:14:14 +000098 } else {
Philip Eliasson1f850a62019-03-19 12:15:00 +000099 last_unwrapped_ += ForwardDiff<T, M>(*last_value_, value);
100
101 if (!AheadOrAt<T, M>(value, *last_value_)) {
102 constexpr int64_t kBackwardAdjustment =
103 M == 0 ? int64_t{std::numeric_limits<T>::max()} + 1 : M;
104 last_unwrapped_ -= kBackwardAdjustment;
105 }
philipel7956c0f2017-07-26 07:48:15 -0700106 }
107
Philip Eliasson1f850a62019-03-19 12:15:00 +0000108 last_value_ = value;
philipel7956c0f2017-07-26 07:48:15 -0700109 return last_unwrapped_;
philipel2cb73412016-03-22 10:03:43 +0100110 }
philipel7956c0f2017-07-26 07:48:15 -0700111
112 private:
Philip Eliasson1f850a62019-03-19 12:15:00 +0000113 int64_t last_unwrapped_ = 0;
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200114 absl::optional<T> last_value_;
philipel2cb73412016-03-22 10:03:43 +0100115};
116
117} // namespace webrtc
118
Bjorn Tereliusa194e582017-10-25 13:07:09 +0200119#endif // RTC_BASE_NUMERICS_SEQUENCE_NUMBER_UTIL_H_