Line data Source code
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/. */
6 :
7 : #ifndef nsAlgorithm_h___
8 : #define nsAlgorithm_h___
9 :
10 : #include "nsCharTraits.h" // for |nsCharSourceTraits|, |nsCharSinkTraits|
11 :
12 : template <class T>
13 : inline T
14 2371 : NS_ROUNDUP(const T& aA, const T& aB)
15 : {
16 2371 : return ((aA + (aB - 1)) / aB) * aB;
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 const T&
24 785805 : XPCOM_MIN(const T& aA, const T& aB)
25 : {
26 785805 : return aB < aA ? aB : aA;
27 : }
28 :
29 : // Must return b when a == b in case a is -0
30 : template <class T>
31 : inline const T&
32 9239 : XPCOM_MAX(const T& aA, const T& aB)
33 : {
34 9239 : return aA > aB ? aA : aB;
35 : }
36 :
37 : namespace mozilla {
38 :
39 : template <class T>
40 : inline const T&
41 1293 : clamped(const T& aA, const T& aMin, const T& aMax)
42 : {
43 1293 : MOZ_ASSERT(aMax >= aMin,
44 : "clamped(): aMax must be greater than or equal to aMin");
45 1293 : return XPCOM_MIN(XPCOM_MAX(aA, aMin), aMax);
46 : }
47 :
48 : } // namespace mozilla
49 :
50 : template <class InputIterator, class T>
51 : inline uint32_t
52 14 : NS_COUNT(InputIterator& aFirst, const InputIterator& aLast, const T& aValue)
53 : {
54 14 : uint32_t result = 0;
55 246 : for (; aFirst != aLast; ++aFirst)
56 116 : if (*aFirst == aValue) {
57 7 : ++result;
58 : }
59 14 : return result;
60 : }
61 :
62 : template <class InputIterator, class OutputIterator>
63 : inline OutputIterator&
64 31896 : copy_string(const InputIterator& aFirst, const InputIterator& aLast,
65 : OutputIterator& aResult)
66 : {
67 : typedef nsCharSourceTraits<InputIterator> source_traits;
68 : typedef nsCharSinkTraits<OutputIterator> sink_traits;
69 :
70 31896 : sink_traits::write(aResult, source_traits::read(aFirst),
71 : source_traits::readable_distance(aFirst, aLast));
72 31896 : return aResult;
73 : }
74 :
75 : #endif // !defined(nsAlgorithm_h___)
|