Bumping manifests a=b2g-bump
[gecko.git] / xpcom / string / nsAlgorithm.h
blobb269393a9f844a84eeff494deb7f4716bf045b99
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|
12 template <class T>
13 inline T
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.
22 template <class T>
23 inline const T&
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
30 template <class T>
31 inline const T&
32 XPCOM_MAX(const T& aA, const T& aB)
34 return aA > aB ? aA : aB;
37 namespace mozilla {
39 template <class T>
40 inline const T&
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>
51 inline uint32_t
52 NS_COUNT(InputIterator& aFirst, const InputIterator& aLast, const T& aValue)
54 uint32_t result = 0;
55 for (; aFirst != aLast; ++aFirst)
56 if (*aFirst == aValue) {
57 ++result;
59 return result;
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));
72 return aResult;
75 #endif // !defined(nsAlgorithm_h___)