xpcom/string/public/nsAlgorithm.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial