1
// rak - Rakshasa's toolbox
2
// Copyright (C) 2005-2007, Jari Sundell
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.
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.
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
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
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.
32
// Contact: Jari Sundell <jaris@ifi.uio.no>
35
// 3185 Skoppum, NORWAY
37
#ifndef RAK_ALGORITHM_H
38
#define RAK_ALGORITHM_H
45
template <typename _InputIter, typename _Function>
47
for_each_pre(_InputIter __first, _InputIter __last, _Function __f) {
50
while (__first != __last) {
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;
70
if (__middle2 != __last) {
74
} else if (__middle1 == __first) {
81
if (__middle1 != __first) {
85
} else if (__middle2 == __last) {
91
return std::make_pair(__middle1, __middle2);
94
template <typename _InputIter, typename _Distance>
96
advance_forward(_InputIter __first, _InputIter __last, _Distance __distance) {
97
while (__first != __last && __distance != 0) {
105
template <typename _InputIter, typename _Distance>
107
advance_backward(_InputIter __first, _InputIter __last, _Distance __distance) {
108
while (__first != __last && __distance != 0) {
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);
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) {
130
typename std::iterator_traits<_InputIter1>::difference_type __n = 0;
132
for ( ;__first1 != __last1 && __first2 != __last2; ++__first1, ++__first2, ++__n)
133
if (*__first1 != *__first2)
139
template <typename _Return, typename _InputIter, typename _Ftor>
141
make_base(_InputIter __first, _InputIter __last, _Ftor __ftor) {
142
if (__first == __last)
145
_Return __base = __ftor(*__first++);
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());
151
if (__pos < (typename std::iterator_traits<_InputIter>::difference_type)__base.size())
152
__base.resize(__pos);