2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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___
|
|
|
|
|
|
|
|
#ifndef nsCharTraits_h___
|
|
|
|
#include "nsCharTraits.h"
|
|
|
|
// for |nsCharSourceTraits|, |nsCharSinkTraits|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef nsDebug_h___
|
|
|
|
#include "nsDebug.h"
|
|
|
|
// for NS_ASSERTION
|
|
|
|
#endif
|
|
|
|
|
2011-06-02 05:56:50 -07:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline
|
|
|
|
T
|
|
|
|
NS_ROUNDUP( const T& a, const T& b )
|
|
|
|
{
|
|
|
|
return ((a + (b - 1)) / b) * b;
|
|
|
|
}
|
|
|
|
|
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>
|
|
|
|
inline
|
|
|
|
const T&
|
2013-01-15 04:22:03 -08:00
|
|
|
XPCOM_MIN( const T& a, const T& b )
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return b < a ? b : a;
|
|
|
|
}
|
|
|
|
|
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>
|
|
|
|
inline
|
|
|
|
const T&
|
2013-01-15 04:22:03 -08:00
|
|
|
XPCOM_MAX( const T& a, const T& b )
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return a > b ? a : b;
|
|
|
|
}
|
|
|
|
|
2012-12-05 14:26:28 -08:00
|
|
|
#if defined(_MSC_VER) && (_MSC_VER < 1600)
|
|
|
|
namespace std {
|
|
|
|
inline
|
|
|
|
long long
|
|
|
|
abs( const long long& a )
|
|
|
|
{
|
|
|
|
return a < 0 ? -a : a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-28 11:33:28 -07:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline
|
|
|
|
const T&
|
|
|
|
clamped( const T& a, const T& min, const T& max )
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(max >= min, "clamped(): max must be greater than or equal to min");
|
2013-01-15 04:22:03 -08:00
|
|
|
return XPCOM_MIN(XPCOM_MAX(a, min), max);
|
2011-10-28 11:33:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
template <class InputIterator, class T>
|
|
|
|
inline
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_COUNT( InputIterator& first, const InputIterator& last, const T& value )
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t result = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
for ( ; first != last; ++first )
|
|
|
|
if ( *first == value )
|
|
|
|
++result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class InputIterator, class OutputIterator>
|
|
|
|
inline
|
|
|
|
OutputIterator&
|
2008-01-03 16:07:06 -08:00
|
|
|
copy_string( const InputIterator& first, const InputIterator& last, OutputIterator& result )
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
typedef nsCharSourceTraits<InputIterator> source_traits;
|
|
|
|
typedef nsCharSinkTraits<OutputIterator> sink_traits;
|
|
|
|
|
2008-01-03 16:07:06 -08:00
|
|
|
sink_traits::write(result, source_traits::read(first), source_traits::readable_distance(first, last));
|
2007-03-22 10:30:00 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !defined(nsAlgorithm_h___)
|