2014-05-05 10:30:46 -07: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 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef nsAlgorithm_h___
|
|
|
|
#define nsAlgorithm_h___
|
|
|
|
|
2014-03-17 09:20:47 -07:00
|
|
|
#include "nsCharTraits.h" // for |nsCharSourceTraits|, |nsCharSinkTraits|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-02 05:56:50 -07:00
|
|
|
template <class T>
|
2014-05-27 00:15:35 -07:00
|
|
|
inline T
|
|
|
|
NS_ROUNDUP(const T& aA, const T& aB)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
return ((aA + (aB - 1)) / aB) * aB;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2011-06-02 05:56:50 -07:00
|
|
|
|
2013-01-15 08:26:58 -08: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.
|
2007-03-22 10:30:00 -07:00
|
|
|
template <class T>
|
2014-05-27 00:15:35 -07:00
|
|
|
inline const T&
|
|
|
|
XPCOM_MIN(const T& aA, const T& aB)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
return aB < aA ? aB : aA;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-08-26 17:06:03 -07:00
|
|
|
// Must return b when a == b in case a is -0
|
2007-03-22 10:30:00 -07:00
|
|
|
template <class T>
|
2014-05-27 00:15:35 -07:00
|
|
|
inline const T&
|
|
|
|
XPCOM_MAX(const T& aA, const T& aB)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2014-05-27 00:15:35 -07:00
|
|
|
return aA > aB ? aA : aB;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-28 11:33:28 -07:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
template <class T>
|
2014-05-27 00:15:35 -07:00
|
|
|
inline const T&
|
|
|
|
clamped(const T& aA, const T& aMin, const T& aMax)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
2015-02-09 14:34:50 -08:00
|
|
|
MOZ_ASSERT(aMax >= aMin,
|
|
|
|
"clamped(): aMax must be greater than or equal to aMin");
|
2014-05-27 00:15:35 -07:00
|
|
|
return XPCOM_MIN(XPCOM_MAX(aA, aMin), aMax);
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2011-10-28 11:33:28 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
template <class InputIterator, class T>
|
2014-05-27 00:15:35 -07:00
|
|
|
inline uint32_t
|
|
|
|
NS_COUNT(InputIterator& aFirst, const InputIterator& aLast, const T& aValue)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
uint32_t result = 0;
|
2014-05-27 00:15:35 -07:00
|
|
|
for (; aFirst != aLast; ++aFirst)
|
|
|
|
if (*aFirst == aValue) {
|
2014-05-05 10:30:46 -07:00
|
|
|
++result;
|
2014-05-27 00:15:35 -07:00
|
|
|
}
|
2014-05-05 10:30:46 -07:00
|
|
|
return result;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
template <class InputIterator, class OutputIterator>
|
2014-05-27 00:15:35 -07:00
|
|
|
inline OutputIterator&
|
|
|
|
copy_string(const InputIterator& aFirst, const InputIterator& aLast,
|
|
|
|
OutputIterator& aResult)
|
2014-05-05 10:30:46 -07:00
|
|
|
{
|
|
|
|
typedef nsCharSourceTraits<InputIterator> source_traits;
|
|
|
|
typedef nsCharSinkTraits<OutputIterator> sink_traits;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-05-27 00:15:35 -07:00
|
|
|
sink_traits::write(aResult, source_traits::read(aFirst),
|
|
|
|
source_traits::readable_distance(aFirst, aLast));
|
|
|
|
return aResult;
|
2014-05-05 10:30:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#endif // !defined(nsAlgorithm_h___)
|