~ubuntu-branches/ubuntu/wily/libtorrent/wily-proposed

« back to all changes in this revision

Viewing changes to rak/algorithm.h

  • Committer: Bazaar Package Importer
  • Author(s): Rogério Brito
  • Date: 2011-03-20 01:06:18 UTC
  • mfrom: (1.1.13 upstream) (4.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110320010618-g3wyylccqzqko73c
Tags: 0.12.7-5
* Use Steinar's "real" patch for IPv6. Addresses #490277, #618275,
  and Closes: #617791.
* Adapt libtorrent-0.12.6-ipv6-07.patch. It FTBFS otherwise.
* Add proper attibution to the IPv6 patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// rak - Rakshasa's toolbox
 
2
// Copyright (C) 2005-2007, Jari Sundell
 
3
//
 
4
// This program is free software; you can redistribute it and/or modify
 
5
// it under the terms of the GNU General Public License as published by
 
6
// the Free Software Foundation; either version 2 of the License, or
 
7
// (at your option) any later version.
 
8
// 
 
9
// This program is distributed in the hope that it will be useful,
 
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
// GNU General Public License for more details.
 
13
// 
 
14
// You should have received a copy of the GNU General Public License
 
15
// along with this program; if not, write to the Free Software
 
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
//
 
18
// In addition, as a special exception, the copyright holders give
 
19
// permission to link the code of portions of this program with the
 
20
// OpenSSL library under certain conditions as described in each
 
21
// individual source file, and distribute linked combinations
 
22
// including the two.
 
23
//
 
24
// You must obey the GNU General Public License in all respects for
 
25
// all of the code used other than OpenSSL.  If you modify file(s)
 
26
// with this exception, you may extend this exception to your version
 
27
// of the file(s), but you are not obligated to do so.  If you do not
 
28
// wish to do so, delete this exception statement from your version.
 
29
// If you delete this exception statement from all source files in the
 
30
// program, then also delete it here.
 
31
//
 
32
// Contact:  Jari Sundell <jaris@ifi.uio.no>
 
33
//
 
34
//           Skomakerveien 33
 
35
//           3185 Skoppum, NORWAY
 
36
 
 
37
#ifndef RAK_ALGORITHM_H
 
38
#define RAK_ALGORITHM_H
 
39
 
 
40
#include <algorithm>
 
41
#include <functional>
 
42
 
 
43
namespace rak {
 
44
 
 
45
template <typename _InputIter, typename _Function>
 
46
_Function
 
47
for_each_pre(_InputIter __first, _InputIter __last, _Function __f) {
 
48
  _InputIter __tmp;
 
49
 
 
50
  while (__first != __last) {
 
51
    __tmp = __first++;
 
52
    
 
53
    __f(*__tmp);
 
54
  }
 
55
 
 
56
  return __f;
 
57
}
 
58
 
 
59
// Return a range with a distance of no more than __distance and
 
60
// between __first and __last, centered on __middle1.
 
61
template <typename _InputIter, typename _Distance>
 
62
std::pair<_InputIter, _InputIter>
 
63
advance_bidirectional(_InputIter __first, _InputIter __middle1, _InputIter __last, _Distance __distance) {
 
64
  _InputIter __middle2 = __middle1;
 
65
 
 
66
  do {
 
67
    if (!__distance)
 
68
      break;
 
69
 
 
70
    if (__middle2 != __last) {
 
71
      ++__middle2;
 
72
      --__distance;
 
73
 
 
74
    } else if (__middle1 == __first) {
 
75
      break;
 
76
    }
 
77
 
 
78
    if (!__distance)
 
79
      break;
 
80
 
 
81
    if (__middle1 != __first) {
 
82
      --__middle1;
 
83
      --__distance;
 
84
 
 
85
    } else if (__middle2 == __last) {
 
86
      break;
 
87
    }
 
88
 
 
89
  } while (true);
 
90
 
 
91
  return std::make_pair(__middle1, __middle2);
 
92
}
 
93
 
 
94
template <typename _InputIter, typename _Distance>
 
95
_InputIter
 
96
advance_forward(_InputIter __first, _InputIter __last, _Distance __distance) {
 
97
  while (__first != __last && __distance != 0) {
 
98
    __first++;
 
99
    __distance--;
 
100
  }
 
101
 
 
102
  return __first;
 
103
}
 
104
 
 
105
template <typename _InputIter, typename _Distance>
 
106
_InputIter
 
107
advance_backward(_InputIter __first, _InputIter __last, _Distance __distance) {
 
108
  while (__first != __last && __distance != 0) {
 
109
    __first--;
 
110
    __distance--;
 
111
  }
 
112
 
 
113
  return __first;
 
114
}
 
115
 
 
116
template <typename _Value>
 
117
struct compare_base : public std::binary_function<_Value, _Value, bool> {
 
118
  bool operator () (const _Value& complete, const _Value& base) const {
 
119
    return !complete.compare(0, base.size(), base);
 
120
  }
 
121
};
 
122
 
 
123
// Count the number of elements from the start of the containers to
 
124
// the first inequal element.
 
125
template <typename _InputIter1, typename _InputIter2>
 
126
typename std::iterator_traits<_InputIter1>::difference_type
 
127
count_base(_InputIter1 __first1, _InputIter1 __last1,
 
128
           _InputIter2 __first2, _InputIter2 __last2) {
 
129
 
 
130
  typename std::iterator_traits<_InputIter1>::difference_type __n = 0;
 
131
 
 
132
  for ( ;__first1 != __last1 && __first2 != __last2; ++__first1, ++__first2, ++__n)
 
133
    if (*__first1 != *__first2)
 
134
      return __n;
 
135
 
 
136
  return __n;
 
137
}
 
138
 
 
139
template <typename _Return, typename _InputIter, typename _Ftor>
 
140
_Return
 
141
make_base(_InputIter __first, _InputIter __last, _Ftor __ftor) {
 
142
  if (__first == __last)
 
143
    return "";
 
144
 
 
145
  _Return __base = __ftor(*__first++);
 
146
 
 
147
  for ( ;__first != __last; ++__first) {
 
148
    typename std::iterator_traits<_InputIter>::difference_type __pos = count_base(__base.begin(), __base.end(),
 
149
                                                                                  __ftor(*__first).begin(), __ftor(*__first).end());
 
150
 
 
151
    if (__pos < (typename std::iterator_traits<_InputIter>::difference_type)__base.size())
 
152
      __base.resize(__pos);
 
153
  }
 
154
 
 
155
  return __base;
 
156
}
 
157
 
 
158
}
 
159
 
 
160
#endif