Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsAlgorithm_h___ |
michael@0 | 7 | #define nsAlgorithm_h___ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsCharTraits.h" // for |nsCharSourceTraits|, |nsCharSinkTraits| |
michael@0 | 10 | |
michael@0 | 11 | template <class T> |
michael@0 | 12 | inline |
michael@0 | 13 | T |
michael@0 | 14 | NS_ROUNDUP( const T& a, const T& b ) |
michael@0 | 15 | { |
michael@0 | 16 | return ((a + (b - 1)) / b) * b; |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | // We use these instead of std::min/max because we can't include the algorithm |
michael@0 | 20 | // header in all of XPCOM because the stl wrappers will error out when included |
michael@0 | 21 | // in parts of XPCOM. These functions should never be used outside of XPCOM. |
michael@0 | 22 | template <class T> |
michael@0 | 23 | inline |
michael@0 | 24 | const T& |
michael@0 | 25 | XPCOM_MIN( const T& a, const T& b ) |
michael@0 | 26 | { |
michael@0 | 27 | return b < a ? b : a; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | // Must return b when a == b in case a is -0 |
michael@0 | 31 | template <class T> |
michael@0 | 32 | inline |
michael@0 | 33 | const T& |
michael@0 | 34 | XPCOM_MAX( const T& a, const T& b ) |
michael@0 | 35 | { |
michael@0 | 36 | return a > b ? a : b; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | namespace mozilla { |
michael@0 | 40 | |
michael@0 | 41 | template <class T> |
michael@0 | 42 | inline |
michael@0 | 43 | const T& |
michael@0 | 44 | clamped( const T& a, const T& min, const T& max ) |
michael@0 | 45 | { |
michael@0 | 46 | NS_ABORT_IF_FALSE(max >= min, "clamped(): max must be greater than or equal to min"); |
michael@0 | 47 | return XPCOM_MIN(XPCOM_MAX(a, min), max); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | template <class InputIterator, class T> |
michael@0 | 53 | inline |
michael@0 | 54 | uint32_t |
michael@0 | 55 | NS_COUNT( InputIterator& first, const InputIterator& last, const T& value ) |
michael@0 | 56 | { |
michael@0 | 57 | uint32_t result = 0; |
michael@0 | 58 | for ( ; first != last; ++first ) |
michael@0 | 59 | if ( *first == value ) |
michael@0 | 60 | ++result; |
michael@0 | 61 | return result; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | template <class InputIterator, class OutputIterator> |
michael@0 | 65 | inline |
michael@0 | 66 | OutputIterator& |
michael@0 | 67 | copy_string( const InputIterator& first, const InputIterator& last, OutputIterator& result ) |
michael@0 | 68 | { |
michael@0 | 69 | typedef nsCharSourceTraits<InputIterator> source_traits; |
michael@0 | 70 | typedef nsCharSinkTraits<OutputIterator> sink_traits; |
michael@0 | 71 | |
michael@0 | 72 | sink_traits::write(result, source_traits::read(first), source_traits::readable_distance(first, last)); |
michael@0 | 73 | return result; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | #endif // !defined(nsAlgorithm_h___) |