1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/string/public/nsAlgorithm.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,76 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsAlgorithm_h___ 1.10 +#define nsAlgorithm_h___ 1.11 + 1.12 +#include "nsCharTraits.h" // for |nsCharSourceTraits|, |nsCharSinkTraits| 1.13 + 1.14 +template <class T> 1.15 +inline 1.16 +T 1.17 +NS_ROUNDUP( const T& a, const T& b ) 1.18 + { 1.19 + return ((a + (b - 1)) / b) * b; 1.20 + } 1.21 + 1.22 +// We use these instead of std::min/max because we can't include the algorithm 1.23 +// header in all of XPCOM because the stl wrappers will error out when included 1.24 +// in parts of XPCOM. These functions should never be used outside of XPCOM. 1.25 +template <class T> 1.26 +inline 1.27 +const T& 1.28 +XPCOM_MIN( const T& a, const T& b ) 1.29 + { 1.30 + return b < a ? b : a; 1.31 + } 1.32 + 1.33 +// Must return b when a == b in case a is -0 1.34 +template <class T> 1.35 +inline 1.36 +const T& 1.37 +XPCOM_MAX( const T& a, const T& b ) 1.38 + { 1.39 + return a > b ? a : b; 1.40 + } 1.41 + 1.42 +namespace mozilla { 1.43 + 1.44 +template <class T> 1.45 +inline 1.46 +const T& 1.47 +clamped( const T& a, const T& min, const T& max ) 1.48 + { 1.49 + NS_ABORT_IF_FALSE(max >= min, "clamped(): max must be greater than or equal to min"); 1.50 + return XPCOM_MIN(XPCOM_MAX(a, min), max); 1.51 + } 1.52 + 1.53 +} 1.54 + 1.55 +template <class InputIterator, class T> 1.56 +inline 1.57 +uint32_t 1.58 +NS_COUNT( InputIterator& first, const InputIterator& last, const T& value ) 1.59 + { 1.60 + uint32_t result = 0; 1.61 + for ( ; first != last; ++first ) 1.62 + if ( *first == value ) 1.63 + ++result; 1.64 + return result; 1.65 + } 1.66 + 1.67 +template <class InputIterator, class OutputIterator> 1.68 +inline 1.69 +OutputIterator& 1.70 +copy_string( const InputIterator& first, const InputIterator& last, OutputIterator& result ) 1.71 + { 1.72 + typedef nsCharSourceTraits<InputIterator> source_traits; 1.73 + typedef nsCharSinkTraits<OutputIterator> sink_traits; 1.74 + 1.75 + sink_traits::write(result, source_traits::read(first), source_traits::readable_distance(first, last)); 1.76 + return result; 1.77 + } 1.78 + 1.79 +#endif // !defined(nsAlgorithm_h___)