|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef nsAlgorithm_h___ |
|
7 #define nsAlgorithm_h___ |
|
8 |
|
9 #include "nsCharTraits.h" // for |nsCharSourceTraits|, |nsCharSinkTraits| |
|
10 |
|
11 template <class T> |
|
12 inline |
|
13 T |
|
14 NS_ROUNDUP( const T& a, const T& b ) |
|
15 { |
|
16 return ((a + (b - 1)) / b) * b; |
|
17 } |
|
18 |
|
19 // We use these instead of std::min/max because we can't include the algorithm |
|
20 // header in all of XPCOM because the stl wrappers will error out when included |
|
21 // in parts of XPCOM. These functions should never be used outside of XPCOM. |
|
22 template <class T> |
|
23 inline |
|
24 const T& |
|
25 XPCOM_MIN( const T& a, const T& b ) |
|
26 { |
|
27 return b < a ? b : a; |
|
28 } |
|
29 |
|
30 // Must return b when a == b in case a is -0 |
|
31 template <class T> |
|
32 inline |
|
33 const T& |
|
34 XPCOM_MAX( const T& a, const T& b ) |
|
35 { |
|
36 return a > b ? a : b; |
|
37 } |
|
38 |
|
39 namespace mozilla { |
|
40 |
|
41 template <class T> |
|
42 inline |
|
43 const T& |
|
44 clamped( const T& a, const T& min, const T& max ) |
|
45 { |
|
46 NS_ABORT_IF_FALSE(max >= min, "clamped(): max must be greater than or equal to min"); |
|
47 return XPCOM_MIN(XPCOM_MAX(a, min), max); |
|
48 } |
|
49 |
|
50 } |
|
51 |
|
52 template <class InputIterator, class T> |
|
53 inline |
|
54 uint32_t |
|
55 NS_COUNT( InputIterator& first, const InputIterator& last, const T& value ) |
|
56 { |
|
57 uint32_t result = 0; |
|
58 for ( ; first != last; ++first ) |
|
59 if ( *first == value ) |
|
60 ++result; |
|
61 return result; |
|
62 } |
|
63 |
|
64 template <class InputIterator, class OutputIterator> |
|
65 inline |
|
66 OutputIterator& |
|
67 copy_string( const InputIterator& first, const InputIterator& last, OutputIterator& result ) |
|
68 { |
|
69 typedef nsCharSourceTraits<InputIterator> source_traits; |
|
70 typedef nsCharSinkTraits<OutputIterator> sink_traits; |
|
71 |
|
72 sink_traits::write(result, source_traits::read(first), source_traits::readable_distance(first, last)); |
|
73 return result; |
|
74 } |
|
75 |
|
76 #endif // !defined(nsAlgorithm_h___) |