os/ossrv/ossrv_pub/boost_apis/boost/detail/algorithm.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// (C) Copyright Jeremy Siek 2001.
sl@0
     2
// Distributed under the Boost Software License, Version 1.0. (See accompany-
sl@0
     3
// ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
sl@0
     4
sl@0
     5
/*
sl@0
     6
 *
sl@0
     7
 * Copyright (c) 1994
sl@0
     8
 * Hewlett-Packard Company
sl@0
     9
 *
sl@0
    10
 * Permission to use, copy, modify, distribute and sell this software
sl@0
    11
 * and its documentation for any purpose is hereby granted without fee,
sl@0
    12
 * provided that the above copyright notice appear in all copies and
sl@0
    13
 * that both that copyright notice and this permission notice appear
sl@0
    14
 * in supporting documentation.  Hewlett-Packard Company makes no
sl@0
    15
 * representations about the suitability of this software for any
sl@0
    16
 * purpose.  It is provided "as is" without express or implied warranty.
sl@0
    17
 *
sl@0
    18
 *
sl@0
    19
 * Copyright (c) 1996
sl@0
    20
 * Silicon Graphics Computer Systems, Inc.
sl@0
    21
 *
sl@0
    22
 * Permission to use, copy, modify, distribute and sell this software
sl@0
    23
 * and its documentation for any purpose is hereby granted without fee,
sl@0
    24
 * provided that the above copyright notice appear in all copies and
sl@0
    25
 * that both that copyright notice and this permission notice appear
sl@0
    26
 * in supporting documentation.  Silicon Graphics makes no
sl@0
    27
 * representations about the suitability of this software for any
sl@0
    28
 * purpose.  It is provided "as is" without express or implied warranty.
sl@0
    29
 */
sl@0
    30
sl@0
    31
#ifndef BOOST_ALGORITHM_HPP
sl@0
    32
# define BOOST_ALGORITHM_HPP
sl@0
    33
# include <boost/detail/iterator.hpp>
sl@0
    34
// Algorithms on sequences
sl@0
    35
//
sl@0
    36
// The functions in this file have not yet gone through formal
sl@0
    37
// review, and are subject to change. This is a work in progress.
sl@0
    38
// They have been checked into the detail directory because
sl@0
    39
// there are some graph algorithms that use these functions.
sl@0
    40
sl@0
    41
#include <algorithm>
sl@0
    42
#include <vector>
sl@0
    43
sl@0
    44
namespace boost {
sl@0
    45
sl@0
    46
  template <typename Iter1, typename Iter2>
sl@0
    47
  Iter1 begin(const std::pair<Iter1, Iter2>& p) { return p.first; }
sl@0
    48
sl@0
    49
  template <typename Iter1, typename Iter2>
sl@0
    50
  Iter2 end(const std::pair<Iter1, Iter2>& p) { return p.second; }
sl@0
    51
sl@0
    52
  template <typename Iter1, typename Iter2>
sl@0
    53
  typename boost::detail::iterator_traits<Iter1>::difference_type
sl@0
    54
  size(const std::pair<Iter1, Iter2>& p) {
sl@0
    55
    return std::distance(p.first, p.second);
sl@0
    56
  }
sl@0
    57
sl@0
    58
#if 0
sl@0
    59
  // These seem to interfere with the std::pair overloads :(
sl@0
    60
  template <typename Container>
sl@0
    61
  typename Container::iterator
sl@0
    62
  begin(Container& c) { return c.begin(); }
sl@0
    63
sl@0
    64
  template <typename Container>
sl@0
    65
  typename Container::const_iterator
sl@0
    66
  begin(const Container& c) { return c.begin(); }
sl@0
    67
sl@0
    68
  template <typename Container>
sl@0
    69
  typename Container::iterator
sl@0
    70
  end(Container& c) { return c.end(); }
sl@0
    71
sl@0
    72
  template <typename Container>
sl@0
    73
  typename Container::const_iterator
sl@0
    74
  end(const Container& c) { return c.end(); }
sl@0
    75
sl@0
    76
  template <typename Container>
sl@0
    77
  typename Container::size_type
sl@0
    78
  size(const Container& c) { return c.size(); }
sl@0
    79
#else
sl@0
    80
  template <typename T>
sl@0
    81
  typename std::vector<T>::iterator
sl@0
    82
  begin(std::vector<T>& c) { return c.begin(); }
sl@0
    83
sl@0
    84
  template <typename T>
sl@0
    85
  typename std::vector<T>::const_iterator
sl@0
    86
  begin(const std::vector<T>& c) { return c.begin(); }
sl@0
    87
sl@0
    88
  template <typename T>
sl@0
    89
  typename std::vector<T>::iterator
sl@0
    90
  end(std::vector<T>& c) { return c.end(); }
sl@0
    91
sl@0
    92
  template <typename T>
sl@0
    93
  typename std::vector<T>::const_iterator
sl@0
    94
  end(const std::vector<T>& c) { return c.end(); }
sl@0
    95
sl@0
    96
  template <typename T>
sl@0
    97
  typename std::vector<T>::size_type
sl@0
    98
  size(const std::vector<T>& c) { return c.size(); }
sl@0
    99
#endif
sl@0
   100
  
sl@0
   101
  template <class ForwardIterator, class T>
sl@0
   102
  void iota(ForwardIterator first, ForwardIterator last, T value)
sl@0
   103
  {
sl@0
   104
    for (; first != last; ++first, ++value)
sl@0
   105
      *first = value;
sl@0
   106
  }
sl@0
   107
  template <typename Container, typename T>
sl@0
   108
  void iota(Container& c, const T& value)
sl@0
   109
  {
sl@0
   110
    iota(begin(c), end(c), value);
sl@0
   111
  }
sl@0
   112
 
sl@0
   113
  // Also do version with 2nd container?
sl@0
   114
  template <typename Container, typename OutIter>
sl@0
   115
  OutIter copy(const Container& c, OutIter result) {
sl@0
   116
    return std::copy(begin(c), end(c), result);
sl@0
   117
  }
sl@0
   118
sl@0
   119
  template <typename Container1, typename Container2>
sl@0
   120
  bool equal(const Container1& c1, const Container2& c2)
sl@0
   121
  {
sl@0
   122
    if (size(c1) != size(c2))
sl@0
   123
      return false;
sl@0
   124
    return std::equal(begin(c1), end(c1), begin(c2));
sl@0
   125
  }
sl@0
   126
sl@0
   127
  template <typename Container>
sl@0
   128
  void sort(Container& c) { std::sort(begin(c), end(c)); }
sl@0
   129
sl@0
   130
  template <typename Container, typename Predicate>
sl@0
   131
  void sort(Container& c, const Predicate& p) { 
sl@0
   132
    std::sort(begin(c), end(c), p);
sl@0
   133
  }
sl@0
   134
sl@0
   135
  template <typename Container>
sl@0
   136
  void stable_sort(Container& c) { std::stable_sort(begin(c), end(c)); }
sl@0
   137
sl@0
   138
  template <typename Container, typename Predicate>
sl@0
   139
  void stable_sort(Container& c, const Predicate& p) { 
sl@0
   140
    std::stable_sort(begin(c), end(c), p);
sl@0
   141
  }
sl@0
   142
sl@0
   143
  template <typename InputIterator, typename Predicate>
sl@0
   144
  bool any_if(InputIterator first, InputIterator last, Predicate p)
sl@0
   145
  {
sl@0
   146
    return std::find_if(first, last, p) != last;
sl@0
   147
  }
sl@0
   148
  template <typename Container, typename Predicate>
sl@0
   149
  bool any_if(const Container& c, Predicate p)
sl@0
   150
  {
sl@0
   151
    return any_if(begin(c), end(c), p);
sl@0
   152
  }
sl@0
   153
sl@0
   154
  template <typename InputIterator, typename T>
sl@0
   155
  bool contains(InputIterator first, InputIterator last, T value)
sl@0
   156
  {
sl@0
   157
    return std::find(first, last, value) != last;
sl@0
   158
  }
sl@0
   159
  template <typename Container, typename T>
sl@0
   160
  bool contains(const Container& c, const T& value)
sl@0
   161
  {
sl@0
   162
    return contains(begin(c), end(c), value);
sl@0
   163
  }
sl@0
   164
sl@0
   165
  template <typename InputIterator, typename Predicate>
sl@0
   166
  bool all(InputIterator first, InputIterator last, Predicate p)
sl@0
   167
  {
sl@0
   168
    for (; first != last; ++first)
sl@0
   169
      if (!p(*first))
sl@0
   170
        return false;
sl@0
   171
    return true;
sl@0
   172
  }
sl@0
   173
  template <typename Container, typename Predicate>
sl@0
   174
  bool all(const Container& c, Predicate p)
sl@0
   175
  {
sl@0
   176
    return all(begin(c), end(c), p);
sl@0
   177
  }
sl@0
   178
sl@0
   179
  template <typename Container, typename T>
sl@0
   180
  std::size_t count(const Container& c, const T& value)
sl@0
   181
  {
sl@0
   182
    return std::count(begin(c), end(c), value);
sl@0
   183
  }
sl@0
   184
sl@0
   185
  template <typename Container, typename Predicate>
sl@0
   186
  std::size_t count_if(const Container& c, Predicate p)
sl@0
   187
  {
sl@0
   188
    return std::count_if(begin(c), end(c), p);
sl@0
   189
  }
sl@0
   190
sl@0
   191
  template <typename ForwardIterator>
sl@0
   192
  bool is_sorted(ForwardIterator first, ForwardIterator last)
sl@0
   193
  {
sl@0
   194
    if (first == last)
sl@0
   195
      return true;
sl@0
   196
sl@0
   197
    ForwardIterator next = first;
sl@0
   198
    for (++next; next != last; first = next, ++next) {
sl@0
   199
      if (*next < *first)
sl@0
   200
        return false;
sl@0
   201
    }
sl@0
   202
sl@0
   203
    return true;
sl@0
   204
  }
sl@0
   205
sl@0
   206
  template <typename ForwardIterator, typename StrictWeakOrdering>
sl@0
   207
  bool is_sorted(ForwardIterator first, ForwardIterator last,
sl@0
   208
                 StrictWeakOrdering comp)
sl@0
   209
  {
sl@0
   210
    if (first == last)
sl@0
   211
      return true;
sl@0
   212
sl@0
   213
    ForwardIterator next = first;
sl@0
   214
    for (++next; next != last; first = next, ++next) {
sl@0
   215
      if (comp(*next, *first))
sl@0
   216
        return false;
sl@0
   217
    }
sl@0
   218
sl@0
   219
    return true;
sl@0
   220
  }
sl@0
   221
sl@0
   222
  template <typename Container>
sl@0
   223
  bool is_sorted(const Container& c)
sl@0
   224
  {
sl@0
   225
    return is_sorted(begin(c), end(c));
sl@0
   226
  }
sl@0
   227
sl@0
   228
  template <typename Container, typename StrictWeakOrdering>
sl@0
   229
  bool is_sorted(const Container& c, StrictWeakOrdering comp)
sl@0
   230
  {
sl@0
   231
    return is_sorted(begin(c), end(c), comp);
sl@0
   232
  }
sl@0
   233
sl@0
   234
} // namespace boost
sl@0
   235
sl@0
   236
#endif // BOOST_ALGORITHM_HPP