michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsAlgorithm_h___ michael@0: #define nsAlgorithm_h___ michael@0: michael@0: #include "nsCharTraits.h" // for |nsCharSourceTraits|, |nsCharSinkTraits| michael@0: michael@0: template michael@0: inline michael@0: T michael@0: NS_ROUNDUP( const T& a, const T& b ) michael@0: { michael@0: return ((a + (b - 1)) / b) * b; michael@0: } michael@0: michael@0: // We use these instead of std::min/max because we can't include the algorithm michael@0: // header in all of XPCOM because the stl wrappers will error out when included michael@0: // in parts of XPCOM. These functions should never be used outside of XPCOM. michael@0: template michael@0: inline michael@0: const T& michael@0: XPCOM_MIN( const T& a, const T& b ) michael@0: { michael@0: return b < a ? b : a; michael@0: } michael@0: michael@0: // Must return b when a == b in case a is -0 michael@0: template michael@0: inline michael@0: const T& michael@0: XPCOM_MAX( const T& a, const T& b ) michael@0: { michael@0: return a > b ? a : b; michael@0: } michael@0: michael@0: namespace mozilla { michael@0: michael@0: template michael@0: inline michael@0: const T& michael@0: clamped( const T& a, const T& min, const T& max ) michael@0: { michael@0: NS_ABORT_IF_FALSE(max >= min, "clamped(): max must be greater than or equal to min"); michael@0: return XPCOM_MIN(XPCOM_MAX(a, min), max); michael@0: } michael@0: michael@0: } michael@0: michael@0: template michael@0: inline michael@0: uint32_t michael@0: NS_COUNT( InputIterator& first, const InputIterator& last, const T& value ) michael@0: { michael@0: uint32_t result = 0; michael@0: for ( ; first != last; ++first ) michael@0: if ( *first == value ) michael@0: ++result; michael@0: return result; michael@0: } michael@0: michael@0: template michael@0: inline michael@0: OutputIterator& michael@0: copy_string( const InputIterator& first, const InputIterator& last, OutputIterator& result ) michael@0: { michael@0: typedef nsCharSourceTraits source_traits; michael@0: typedef nsCharSinkTraits sink_traits; michael@0: michael@0: sink_traits::write(result, source_traits::read(first), source_traits::readable_distance(first, last)); michael@0: return result; michael@0: } michael@0: michael@0: #endif // !defined(nsAlgorithm_h___)