OLD | NEW |
---|---|
(Empty) | |
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 | |
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 | |
19 namespace 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. | |
26 template <typename T, T M> | |
27 inline 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 | |
36 template <typename T> | |
37 inline 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. | |
51 template <typename T, T M> | |
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 | |
58 template <typename T> | |
59 inline 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 | |
65 template <typename T, typename M> | |
66 struct SeqNumComp_; | |
tommi
2016/03/21 16:13:13
why the postfix underscore? if it's meant to dist
philipel
2016/03/21 16:19:28
Done.
| |
67 | |
68 template <typename T, T M> | |
69 struct SeqNumComp_<T, std::integral_constant<T, M>> { | |
70 bool operator()(T a, T b) const { | |
71 return AheadOf<T, M>(a, b); | |
72 } | |
73 }; | |
74 | |
75 template <typename T> | |
76 struct SeqNumComp_<T, std::integral_constant<T, T(0)>> { | |
77 bool operator()(T a, T b) const { | |
78 return AheadOf<T>(a, b); | |
79 } | |
80 }; | |
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). | |
86 template <typename T, T M = 0> | |
87 struct AscendingSeqNumComp : | |
88 private SeqNumComp_<T, std::integral_constant<T, M>> { | |
89 bool operator()(T a, T b) const { | |
90 return SeqNumComp_<T, std::integral_constant<T, M>>::operator()(a, b); | |
91 } | |
92 }; | |
93 | |
94 // Comparator used to compare sequence numbers in a continuous fashion. | |
95 // | |
96 // WARNING! If used to sort sequence numbers of length M then the interval | |
97 // covered by the sequence numbers may not be larger than floor(M/2). | |
98 template <typename T, T M = 0> | |
99 struct DescendingSeqNumComp : | |
100 private SeqNumComp_<T, std::integral_constant<T, M>> { | |
101 bool operator()(T a, T b) const { | |
102 return SeqNumComp_<T, std::integral_constant<T, M>>::operator()(b, a); | |
103 } | |
104 }; | |
105 | |
106 } // namespace webrtc | |
107 | |
108 #endif // WEBRTC_MODULES_VIDEO_CODING_SEQUENCE_NUMBER_UTIL_H_ | |
OLD | NEW |