1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsAlgorithm_h___
8 #define nsAlgorithm_h___
10 #include "nsCharTraits.h" // for |nsCharSourceTraits|, |nsCharSinkTraits|
14 NS_ROUNDUP(const T
& aA
, const T
& aB
)
16 return ((aA
+ (aB
- 1)) / aB
) * aB
;
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.
24 XPCOM_MIN(const T
& aA
, const T
& aB
)
26 return aB
< aA
? aB
: aA
;
29 // Must return b when a == b in case a is -0
32 XPCOM_MAX(const T
& aA
, const T
& aB
)
34 return aA
> aB
? aA
: aB
;
41 clamped(const T
& aA
, const T
& aMin
, const T
& aMax
)
43 NS_ABORT_IF_FALSE(aMax
>= aMin
,
44 "clamped(): aMax must be greater than or equal to aMin");
45 return XPCOM_MIN(XPCOM_MAX(aA
, aMin
), aMax
);
50 template <class InputIterator
, class T
>
52 NS_COUNT(InputIterator
& aFirst
, const InputIterator
& aLast
, const T
& aValue
)
55 for (; aFirst
!= aLast
; ++aFirst
)
56 if (*aFirst
== aValue
) {
62 template <class InputIterator
, class OutputIterator
>
63 inline OutputIterator
&
64 copy_string(const InputIterator
& aFirst
, const InputIterator
& aLast
,
65 OutputIterator
& aResult
)
67 typedef nsCharSourceTraits
<InputIterator
> source_traits
;
68 typedef nsCharSinkTraits
<OutputIterator
> sink_traits
;
70 sink_traits::write(aResult
, source_traits::read(aFirst
),
71 source_traits::readable_distance(aFirst
, aLast
));
75 #endif // !defined(nsAlgorithm_h___)