blob: 57efb5128def3bfa411d428e7583056edf3752f8 [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>
philipel2cb73412016-03-22 10:03:43 +010015#include <limits>
16#include <type_traits>
17
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020018#include "absl/types/optional.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "rtc_base/checks.h"
Karl Wiberg65c39222017-11-22 12:25:14 +010020#include "rtc_base/numerics/mod_ops.h"
philipel2cb73412016-03-22 10:03:43 +010021
22namespace webrtc {
23
24// Test if the sequence number |a| is ahead or at sequence number |b|.
25//
26// If |M| is an even number and the two sequence numbers are at max distance
27// from each other, then the sequence number with the highest value is
28// considered to be ahead.
29template <typename T, T M>
philipel7956c0f2017-07-26 07:48:15 -070030inline typename std::enable_if<(M > 0), bool>::type AheadOrAt(T a, T b) {
philipel2cb73412016-03-22 10:03:43 +010031 static_assert(std::is_unsigned<T>::value,
32 "Type must be an unsigned integer.");
33 const T maxDist = M / 2;
34 if (!(M & 1) && MinDiff<T, M>(a, b) == maxDist)
35 return b < a;
36 return ForwardDiff<T, M>(b, a) <= maxDist;
37}
38
philipel7956c0f2017-07-26 07:48:15 -070039template <typename T, T M>
40inline typename std::enable_if<(M == 0), bool>::type AheadOrAt(T a, T b) {
philipel2cb73412016-03-22 10:03:43 +010041 static_assert(std::is_unsigned<T>::value,
42 "Type must be an unsigned integer.");
43 const T maxDist = std::numeric_limits<T>::max() / 2 + T(1);
44 if (a - b == maxDist)
45 return b < a;
46 return ForwardDiff(b, a) < maxDist;
47}
48
philipel7956c0f2017-07-26 07:48:15 -070049template <typename T>
50inline bool AheadOrAt(T a, T b) {
51 return AheadOrAt<T, 0>(a, b);
52}
53
philipel2cb73412016-03-22 10:03:43 +010054// Test if the sequence number |a| is ahead of sequence number |b|.
55//
56// If |M| is an even number and the two sequence numbers are at max distance
57// from each other, then the sequence number with the highest value is
58// considered to be ahead.
philipel7956c0f2017-07-26 07:48:15 -070059template <typename T, T M = 0>
philipel2cb73412016-03-22 10:03:43 +010060inline bool AheadOf(T a, T b) {
61 static_assert(std::is_unsigned<T>::value,
62 "Type must be an unsigned integer.");
63 return a != b && AheadOrAt<T, M>(a, b);
64}
65
philipel7956c0f2017-07-26 07:48:15 -070066// Comparator used to compare sequence numbers in a continuous fashion.
67//
68// WARNING! If used to sort sequence numbers of length M then the interval
69// covered by the sequence numbers may not be larger than floor(M/2).
70template <typename T, T M = 0>
71struct AscendingSeqNumComp {
philipel2cb73412016-03-22 10:03:43 +010072 bool operator()(T a, T b) const { return AheadOf<T, M>(a, b); }
73};
74
philipel2cb73412016-03-22 10:03:43 +010075// Comparator used to compare sequence numbers in a continuous fashion.
76//
77// WARNING! If used to sort sequence numbers of length M then the interval
78// covered by the sequence numbers may not be larger than floor(M/2).
79template <typename T, T M = 0>
philipel7956c0f2017-07-26 07:48:15 -070080struct DescendingSeqNumComp {
81 bool operator()(T a, T b) const { return AheadOf<T, M>(b, a); }
philipel2cb73412016-03-22 10:03:43 +010082};
83
philipelb0f968a2019-03-14 10:52:37 +010084// A sequence number unwrapper where the first unwrapped value equals the
85// first value being unwrapped.
philipel2cb73412016-03-22 10:03:43 +010086template <typename T, T M = 0>
philipel7956c0f2017-07-26 07:48:15 -070087class SeqNumUnwrapper {
88 static_assert(
89 std::is_unsigned<T>::value &&
philipelb0f968a2019-03-14 10:52:37 +010090 std::numeric_limits<T>::max() < std::numeric_limits<int64_t>::max(),
91 "Type unwrapped must be an unsigned integer smaller than int64_t.");
philipel7956c0f2017-07-26 07:48:15 -070092
93 public:
philipelb0f968a2019-03-14 10:52:37 +010094 int64_t Unwrap(T value) {
95 if (!last_value_) {
96 last_unwrapped_ = {value};
philipel7956c0f2017-07-26 07:48:15 -070097 } else {
philipelb0f968a2019-03-14 10:52:37 +010098 last_unwrapped_ += ForwardDiff<T, M>(*last_value_, value);
99
100 if (!AheadOrAt<T, M>(value, *last_value_)) {
101 constexpr int64_t kBackwardAdjustment =
102 M == 0 ? int64_t{std::numeric_limits<T>::max()} + 1 : M;
103 last_unwrapped_ -= kBackwardAdjustment;
104 }
philipel7956c0f2017-07-26 07:48:15 -0700105 }
106
philipelb0f968a2019-03-14 10:52:37 +0100107 last_value_ = value;
philipel7956c0f2017-07-26 07:48:15 -0700108 return last_unwrapped_;
philipel2cb73412016-03-22 10:03:43 +0100109 }
philipel7956c0f2017-07-26 07:48:15 -0700110
111 private:
philipelb0f968a2019-03-14 10:52:37 +0100112 int64_t last_unwrapped_ = 0;
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200113 absl::optional<T> last_value_;
philipel2cb73412016-03-22 10:03:43 +0100114};
115
116} // namespace webrtc
117
Bjorn Tereliusa194e582017-10-25 13:07:09 +0200118#endif // RTC_BASE_NUMERICS_SEQUENCE_NUMBER_UTIL_H_