OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 // | 53 // |
54 // ForwardDiff(y, x) == 251 | 54 // ForwardDiff(y, x) == 251 |
55 // | 55 // |
56 // 252 253 254 255 0 1 2 3 | 56 // 252 253 254 255 0 1 2 3 |
57 // ################################################# | 57 // ################################################# |
58 // | | x | | | | | y | | | 58 // | | x | | | | | y | | |
59 // ################################################# | 59 // ################################################# |
60 // -->-----> |----->--- | 60 // -->-----> |----->--- |
61 // | 61 // |
62 template <typename T, T M> | 62 template <typename T, T M> |
63 inline T ForwardDiff(T a, T b) { | 63 inline typename std::enable_if<(M > 0), T>::type ForwardDiff(T a, T b) { |
64 static_assert(std::is_unsigned<T>::value, | 64 static_assert(std::is_unsigned<T>::value, |
65 "Type must be an unsigned integer."); | 65 "Type must be an unsigned integer."); |
66 RTC_DCHECK_LT(a, M); | 66 RTC_DCHECK_LT(a, M); |
67 RTC_DCHECK_LT(b, M); | 67 RTC_DCHECK_LT(b, M); |
68 return a <= b ? b - a : M - (a - b); | 68 return a <= b ? b - a : M - (a - b); |
69 } | 69 } |
70 | 70 |
71 template <typename T> | 71 template <typename T, T M> |
72 inline T ForwardDiff(T a, T b) { | 72 inline typename std::enable_if<(M == 0), T>::type ForwardDiff(T a, T b) { |
terelius
2017/07/10 21:20:23
Nice generalization, but are there unit tests for
philipel
2017/07/11 11:08:30
Yes, there were already unit tests for this case a
terelius
2017/07/11 12:38:17
Acknowledged.
| |
73 static_assert(std::is_unsigned<T>::value, | 73 static_assert(std::is_unsigned<T>::value, |
74 "Type must be an unsigned integer."); | 74 "Type must be an unsigned integer."); |
75 return b - a; | 75 return b - a; |
76 } | 76 } |
77 | 77 |
78 template <typename T, typename M = void> | |
79 inline T ForwardDiff(T a, T b) { | |
80 return ForwardDiff<T, 0>(a, b); | |
81 } | |
82 | |
78 // Calculates the reverse difference between two wrapping numbers. | 83 // Calculates the reverse difference between two wrapping numbers. |
79 // | 84 // |
80 // Example: | 85 // Example: |
81 // uint8_t x = 253; | 86 // uint8_t x = 253; |
82 // uint8_t y = 2; | 87 // uint8_t y = 2; |
83 // | 88 // |
84 // ReverseDiff(y, x) == 5 | 89 // ReverseDiff(y, x) == 5 |
85 // | 90 // |
86 // 252 253 254 255 0 1 2 3 | 91 // 252 253 254 255 0 1 2 3 |
87 // ################################################# | 92 // ################################################# |
88 // | | x | | | | | y | | | 93 // | | x | | | | | y | | |
89 // ################################################# | 94 // ################################################# |
90 // <-----<-----<-----<-----<-----| | 95 // <-----<-----<-----<-----<-----| |
91 // | 96 // |
92 // ReverseDiff(x, y) == 251 | 97 // ReverseDiff(x, y) == 251 |
93 // | 98 // |
94 // 252 253 254 255 0 1 2 3 | 99 // 252 253 254 255 0 1 2 3 |
95 // ################################################# | 100 // ################################################# |
96 // | | x | | | | | y | | | 101 // | | x | | | | | y | | |
97 // ################################################# | 102 // ################################################# |
98 // ---<-----| |<-----<-- | 103 // ---<-----| |<-----<-- |
99 // | 104 // |
100 template <typename T, T M> | 105 template <typename T, T M> |
101 inline T ReverseDiff(T a, T b) { | 106 inline typename std::enable_if<(M > 0), T>::type ReverseDiff(T a, T b) { |
102 static_assert(std::is_unsigned<T>::value, | 107 static_assert(std::is_unsigned<T>::value, |
103 "Type must be an unsigned integer."); | 108 "Type must be an unsigned integer."); |
104 RTC_DCHECK_LT(a, M); | 109 RTC_DCHECK_LT(a, M); |
105 RTC_DCHECK_LT(b, M); | 110 RTC_DCHECK_LT(b, M); |
106 return b <= a ? a - b : M - (b - a); | 111 return b <= a ? a - b : M - (b - a); |
107 } | 112 } |
108 | 113 |
109 template <typename T> | 114 template <typename T, T M> |
110 inline T ReverseDiff(T a, T b) { | 115 inline typename std::enable_if<(M == 0), T>::type ReverseDiff(T a, T b) { |
111 static_assert(std::is_unsigned<T>::value, | 116 static_assert(std::is_unsigned<T>::value, |
112 "Type must be an unsigned integer."); | 117 "Type must be an unsigned integer."); |
113 return a - b; | 118 return a - b; |
114 } | 119 } |
115 | 120 |
121 template <typename T, typename M = void> | |
122 inline T ReverseDiff(T a, T b) { | |
123 return ReverseDiff<T, 0>(a, b); | |
124 } | |
125 | |
116 // Calculates the minimum distance between to wrapping numbers. | 126 // Calculates the minimum distance between to wrapping numbers. |
117 // | 127 // |
118 // The minimum distance is defined as min(ForwardDiff(a, b), ReverseDiff(a, b)) | 128 // The minimum distance is defined as min(ForwardDiff(a, b), ReverseDiff(a, b)) |
119 template <typename T, T M> | 129 template <typename T, T M = 0> |
120 inline T MinDiff(T a, T b) { | 130 inline T MinDiff(T a, T b) { |
121 static_assert(std::is_unsigned<T>::value, | 131 static_assert(std::is_unsigned<T>::value, |
122 "Type must be an unsigned integer."); | 132 "Type must be an unsigned integer."); |
123 return std::min(ForwardDiff<T, M>(a, b), ReverseDiff<T, M>(a, b)); | 133 return std::min(ForwardDiff<T, M>(a, b), ReverseDiff<T, M>(a, b)); |
124 } | 134 } |
125 | 135 |
126 template <typename T> | |
127 inline T MinDiff(T a, T b) { | |
128 static_assert(std::is_unsigned<T>::value, | |
129 "Type must be an unsigned integer."); | |
130 return std::min(ForwardDiff(a, b), ReverseDiff(a, b)); | |
131 } | |
132 | |
133 } // namespace webrtc | 136 } // namespace webrtc |
134 | 137 |
135 #endif // WEBRTC_RTC_BASE_MOD_OPS_H_ | 138 #endif // WEBRTC_RTC_BASE_MOD_OPS_H_ |
OLD | NEW |