diff -r 000000000000 -r 6474c204b198 xpcom/string/public/nsAlgorithm.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xpcom/string/public/nsAlgorithm.h Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,76 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* 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/. */ + +#ifndef nsAlgorithm_h___ +#define nsAlgorithm_h___ + +#include "nsCharTraits.h" // for |nsCharSourceTraits|, |nsCharSinkTraits| + +template +inline +T +NS_ROUNDUP( const T& a, const T& b ) + { + return ((a + (b - 1)) / b) * b; + } + +// 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. +template +inline +const T& +XPCOM_MIN( const T& a, const T& b ) + { + return b < a ? b : a; + } + +// Must return b when a == b in case a is -0 +template +inline +const T& +XPCOM_MAX( const T& a, const T& b ) + { + return a > b ? a : b; + } + +namespace mozilla { + +template +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"); + return XPCOM_MIN(XPCOM_MAX(a, min), max); + } + +} + +template +inline +uint32_t +NS_COUNT( InputIterator& first, const InputIterator& last, const T& value ) + { + uint32_t result = 0; + for ( ; first != last; ++first ) + if ( *first == value ) + ++result; + return result; + } + +template +inline +OutputIterator& +copy_string( const InputIterator& first, const InputIterator& last, OutputIterator& result ) + { + typedef nsCharSourceTraits source_traits; + typedef nsCharSinkTraits sink_traits; + + sink_traits::write(result, source_traits::read(first), source_traits::readable_distance(first, last)); + return result; + } + +#endif // !defined(nsAlgorithm_h___)