2014-05-05 21:30:46 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2000-05-20 23:57:03 +04:00
|
|
|
|
|
|
|
#ifndef nsAlgorithm_h___
|
|
|
|
#define nsAlgorithm_h___
|
|
|
|
|
2014-03-17 20:20:47 +04:00
|
|
|
#include "nsCharTraits.h" // for |nsCharSourceTraits|, |nsCharSinkTraits|
|
2000-09-09 08:39:25 +04:00
|
|
|
|
2011-06-02 16:56:50 +04:00
|
|
|
template <class T>
|
2014-05-27 11:15:35 +04:00
|
|
|
inline T
|
|
|
|
NS_ROUNDUP(const T& aA, const T& aB)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
return ((aA + (aB - 1)) / aB) * aB;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2011-06-02 16:56:50 +04:00
|
|
|
|
2013-01-15 20:26:58 +04:00
|
|
|
// We use these instead of std::min/max because we can't include the algorithm
|
|
|
|
// header in all of XPCOM because the stl wrappers will error out when included
|
|
|
|
// in parts of XPCOM. These functions should never be used outside of XPCOM.
|
2000-05-20 23:57:03 +04:00
|
|
|
template <class T>
|
2014-05-27 11:15:35 +04:00
|
|
|
inline const T&
|
|
|
|
XPCOM_MIN(const T& aA, const T& aB)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
return aB < aA ? aB : aA;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2000-05-20 23:57:03 +04:00
|
|
|
|
2011-08-27 04:06:03 +04:00
|
|
|
// Must return b when a == b in case a is -0
|
2000-05-20 23:57:03 +04:00
|
|
|
template <class T>
|
2014-05-27 11:15:35 +04:00
|
|
|
inline const T&
|
|
|
|
XPCOM_MAX(const T& aA, const T& aB)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2014-05-27 11:15:35 +04:00
|
|
|
return aA > aB ? aA : aB;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2000-05-20 23:57:03 +04:00
|
|
|
|
2011-10-28 22:33:28 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
template <class T>
|
2014-05-27 11:15:35 +04:00
|
|
|
inline const T&
|
|
|
|
clamped(const T& aA, const T& aMin, const T& aMax)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aMax >= aMin,
|
|
|
|
"clamped(): aMax must be greater than or equal to aMin");
|
2014-05-27 11:15:35 +04:00
|
|
|
return XPCOM_MIN(XPCOM_MAX(aA, aMin), aMax);
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2011-10-28 22:33:28 +04:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|
2011-10-28 22:33:28 +04:00
|
|
|
|
2000-05-20 23:57:03 +04:00
|
|
|
template <class InputIterator, class T>
|
2014-05-27 11:15:35 +04:00
|
|
|
inline uint32_t
|
|
|
|
NS_COUNT(InputIterator& aFirst, const InputIterator& aLast, const T& aValue)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
uint32_t result = 0;
|
2014-05-27 11:15:35 +04:00
|
|
|
for (; aFirst != aLast; ++aFirst)
|
|
|
|
if (*aFirst == aValue) {
|
2014-05-05 21:30:46 +04:00
|
|
|
++result;
|
2014-05-27 11:15:35 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
return result;
|
|
|
|
}
|
2000-05-20 23:57:03 +04:00
|
|
|
|
|
|
|
template <class InputIterator, class OutputIterator>
|
2014-05-27 11:15:35 +04:00
|
|
|
inline OutputIterator&
|
|
|
|
copy_string(const InputIterator& aFirst, const InputIterator& aLast,
|
|
|
|
OutputIterator& aResult)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
typedef nsCharSourceTraits<InputIterator> source_traits;
|
|
|
|
typedef nsCharSinkTraits<OutputIterator> sink_traits;
|
2000-05-20 23:57:03 +04:00
|
|
|
|
2014-05-27 11:15:35 +04:00
|
|
|
sink_traits::write(aResult, source_traits::read(aFirst),
|
|
|
|
source_traits::readable_distance(aFirst, aLast));
|
|
|
|
return aResult;
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2000-05-20 23:57:03 +04:00
|
|
|
|
|
|
|
#endif // !defined(nsAlgorithm_h___)
|