blob: 828e68a76842bfcf02b7f0fa1b2a2850ac1b6982 [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
11#ifndef WEBRTC_MODULES_VIDEO_CODING_SEQUENCE_NUMBER_UTIL_H_
12#define WEBRTC_MODULES_VIDEO_CODING_SEQUENCE_NUMBER_UTIL_H_
13
14#include <limits>
15#include <type_traits>
16
17#include "webrtc/base/mod_ops.h"
18
19namespace webrtc {
20
21// Test if the sequence number |a| is ahead or at sequence number |b|.
22//
23// If |M| is an even number and the two sequence numbers are at max distance
24// from each other, then the sequence number with the highest value is
25// considered to be ahead.
26template <typename T, T M>
27inline bool AheadOrAt(T a, T b) {
28 static_assert(std::is_unsigned<T>::value,
29 "Type must be an unsigned integer.");
30 const T maxDist = M / 2;
31 if (!(M & 1) && MinDiff<T, M>(a, b) == maxDist)
32 return b < a;
33 return ForwardDiff<T, M>(b, a) <= maxDist;
34}
35
36template <typename T>
37inline bool AheadOrAt(T a, T b) {
38 static_assert(std::is_unsigned<T>::value,
39 "Type must be an unsigned integer.");
40 const T maxDist = std::numeric_limits<T>::max() / 2 + T(1);
41 if (a - b == maxDist)
42 return b < a;
43 return ForwardDiff(b, a) < maxDist;
44}
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.
51template <typename T, T M>
52inline 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
58template <typename T>
59inline bool AheadOf(T a, T b) {
60 static_assert(std::is_unsigned<T>::value,
61 "Type must be an unsigned integer.");
62 return a != b && AheadOrAt(a, b);
63}
64
65namespace internal {
66
67template <typename T, typename M>
68struct SeqNumComp;
69
70template <typename T, T M>
71struct SeqNumComp<T, std::integral_constant<T, M>> {
72 bool operator()(T a, T b) const { return AheadOf<T, M>(a, b); }
73};
74
75template <typename T>
76struct SeqNumComp<T, std::integral_constant<T, T(0)>> {
77 bool operator()(T a, T b) const { return AheadOf<T>(a, b); }
78};
79
80} // namespace internal
81
82// Comparator used to compare sequence numbers in a continuous fashion.
83//
84// WARNING! If used to sort sequence numbers of length M then the interval
85// covered by the sequence numbers may not be larger than floor(M/2).
86template <typename T, T M = 0>
87struct AscendingSeqNumComp
88 : private internal::SeqNumComp<T, std::integral_constant<T, M>> {
89 bool operator()(T a, T b) const {
90 return internal::SeqNumComp<T, std::integral_constant<T, M>>::operator()(a,
91 b);
92 }
93};
94
95// Comparator used to compare sequence numbers in a continuous fashion.
96//
97// WARNING! If used to sort sequence numbers of length M then the interval
98// covered by the sequence numbers may not be larger than floor(M/2).
99template <typename T, T M = 0>
100struct DescendingSeqNumComp
101 : private internal::SeqNumComp<T, std::integral_constant<T, M>> {
102 bool operator()(T a, T b) const {
103 return internal::SeqNumComp<T, std::integral_constant<T, M>>::operator()(b,
104 a);
105 }
106};
107
108} // namespace webrtc
109
110#endif // WEBRTC_MODULES_VIDEO_CODING_SEQUENCE_NUMBER_UTIL_H_