/usr/include/KF5/libkleo/stl_util.h is in libkf5libkleo-dev 4:17.12.3-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | /****************************************************************************
** Copyright (C) 2001-2007 Klarälvdalens Datakonsult AB. All rights reserved.
**
** This file is part of the KD Tools library.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding valid commercial KD Tools licenses may use this file in
** accordance with the KD Tools Commercial License Agreement provided with
** the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** Contact info@klaralvdalens-datakonsult.se if any conditions of this
** licensing are not clear to you.
**
**********************************************************************/
#ifndef __KDTOOLSCORE_STL_UTIL_H__
#define __KDTOOLSCORE_STL_UTIL_H__
#include <algorithm>
#include <numeric>
#include <utility>
#include <iterator>
#include <functional>
namespace kdtools
{
template<typename _Iterator, typename UnaryPredicate>
struct filter_iterator
{
using value_type = typename std::iterator_traits<_Iterator>::value_type;
using reference = typename std::iterator_traits<_Iterator>::reference;
using pointer = typename std::iterator_traits<_Iterator>::pointer;
using difference_type = typename std::iterator_traits<_Iterator>::difference_type;
filter_iterator(UnaryPredicate pred, _Iterator it, _Iterator last) : it(it), last(last), pred(pred) {}
template<typename _OtherIter>
filter_iterator(const filter_iterator<_OtherIter, UnaryPredicate> &other) : it(other.it), last(other.last), pred(other.pred) {}
filter_iterator &operator++() { while (++it != last && !pred(*it)){} return *this; }
filter_iterator operator++(int) { auto retval = *this; while(++it != last && !pred(*it)){} return retval; }
bool operator==(filter_iterator other) const { return it == other.it; }
bool operator!=(filter_iterator other) const { return it != other.it; }
typename _Iterator::reference operator*() const { return *it; }
private:
_Iterator it, last;
UnaryPredicate pred;
};
template<typename _Iterator, typename UnaryPredicate>
filter_iterator<typename std::decay<_Iterator>::type,
UnaryPredicate>
make_filter_iterator(UnaryPredicate &&pred, _Iterator &&it, _Iterator &&last)
{
return filter_iterator<typename std::decay<_Iterator>::type,
UnaryPredicate>(
std::forward<UnaryPredicate>(pred),
std::forward<_Iterator>(it),
std::forward<_Iterator>(last));
}
template <typename InputIterator, typename OutputIterator, typename UnaryPredicate>
OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator dest, UnaryPredicate pred)
{
while (first != last) {
if (pred(*first)) {
*dest = *first;
++dest;
}
++first;
}
return dest;
}
template <typename OutputIterator, typename InputIterator, typename UnaryFunction, typename UnaryPredicate>
void transform_if(InputIterator first, InputIterator last, OutputIterator dest, UnaryPredicate pred, UnaryFunction filter)
{
for (; first != last; ++first) {
if (filter(*first)) {
*dest++ = pred(*first);
}
}
}
template <typename InputIterator, typename OutputIterator, typename Predicate>
OutputIterator copy_1st_if(InputIterator first, InputIterator last, OutputIterator dest, Predicate pred)
{
const auto trans = [](typename std::iterator_traits<InputIterator>::reference v) {
return std::get<0>(v);
};
kdtools::transform_if(first, last, dest, trans,
[&pred, &trans](typename std::iterator_traits<InputIterator>::reference v) {
return pred(trans(v));
});
return dest;
}
template <typename InputIterator, typename OutputIterator, typename Predicate>
OutputIterator copy_2nd_if(InputIterator first, InputIterator last, OutputIterator dest, Predicate pred)
{
const auto trans = [](typename std::iterator_traits<InputIterator>::reference v) {
return std::get<1>(v);
};
kdtools::transform_if(first, last, dest, trans,
[&pred, &trans](typename std::iterator_traits<InputIterator>::reference v) {
return pred(trans(v));
});
return dest;
}
template <typename OutputIterator, typename InputIterator, typename UnaryFunction>
OutputIterator transform_1st(InputIterator first, InputIterator last, OutputIterator dest, UnaryFunction func)
{
return std::transform(first, last, dest,
[func](typename std::iterator_traits<InputIterator>::reference v) {
return func(std::get<0>(v));
});
}
template <typename OutputIterator, typename InputIterator, typename UnaryFunction>
OutputIterator transform_2nd(InputIterator first, InputIterator last, OutputIterator dest, UnaryFunction func)
{
return std::transform(first, last, dest,
[func](typename std::iterator_traits<InputIterator>::reference v) {
return func(std::get<1>(v));
});
}
template <typename Value, typename InputIterator, typename UnaryPredicate>
Value accumulate_if(InputIterator first, InputIterator last, UnaryPredicate filter, const Value &value = Value())
{
return std::accumulate(make_filter_iterator(filter, first, last),
make_filter_iterator(filter, last, last), value);
}
template <typename Value, typename InputIterator, typename UnaryPredicate, typename BinaryOperation>
Value accumulate_if(InputIterator first, InputIterator last, UnaryPredicate filter, const Value &value, BinaryOperation op)
{
return std::accumulate(make_filter_iterator(filter, first, last),
make_filter_iterator(filter, last, last), value, op);
}
template <typename Value, typename InputIterator, typename UnaryFunction>
Value accumulate_transform(InputIterator first, InputIterator last, UnaryFunction map, const Value &value = Value())
{
return std::accumulate(first, last, value,
[map](Value lhs,
typename std::iterator_traits<InputIterator>::reference rhs)
{
return lhs + map(rhs);
});
}
template <typename Value, typename InputIterator, typename UnaryFunction, typename BinaryOperation>
Value accumulate_transform(InputIterator first, InputIterator last, UnaryFunction map, const Value &value, BinaryOperation op)
{
return std::accumulate(first, last, value,
[map, op](typename InputIterator::reference lhs,
typename InputIterator::reference rhs) {
return op(map(lhs), map(rhs));
});
}
template <typename Value, typename InputIterator, typename UnaryFunction, typename UnaryPredicate, typename BinaryOperation>
Value accumulate_transform_if(InputIterator first, InputIterator last, UnaryFunction map, UnaryPredicate filter, const Value &value, BinaryOperation op)
{
return accumulate_transform(make_filter_iterator(filter, first, last),
make_filter_iterator(filter, last, last),
map, value, op);
}
template <typename InputIterator, typename BinaryOperation>
BinaryOperation for_each_adjacent_pair(InputIterator first, InputIterator last, BinaryOperation op)
{
typedef typename std::iterator_traits<InputIterator>::value_type ValueType;
if (first == last) {
return op;
}
ValueType value = *first;
while (++first != last) {
ValueType tmp = *first;
op(value, tmp);
value = tmp;
}
return op;
}
template <typename InputIterator, typename OutputIterator1, typename OutputIterator2, typename UnaryPredicate>
std::pair<OutputIterator1, OutputIterator2> separate_if(InputIterator first, InputIterator last, OutputIterator1 dest1, OutputIterator2 dest2, UnaryPredicate pred)
{
while (first != last) {
if (pred(*first)) {
*dest1 = *first;
++dest1;
} else {
*dest2 = *first;
++dest2;
}
++first;
}
return std::make_pair(dest1, dest2);
}
//@{
/**
Versions of std::set_intersection optimized for ForwardIterator's
*/
template <typename ForwardIterator, typename ForwardIterator2, typename OutputIterator, typename BinaryPredicate>
OutputIterator set_intersection(ForwardIterator first1, ForwardIterator last1, ForwardIterator2 first2, ForwardIterator2 last2, OutputIterator result)
{
while (first1 != last1 && first2 != last2) {
if (*first1 < *first2) {
first1 = std::lower_bound(++first1, last1, *first2);
} else if (*first2 < *first1) {
first2 = std::lower_bound(++first2, last2, *first1);
} else {
*result = *first1;
++first1;
++first2;
++result;
}
}
return result;
}
template <typename ForwardIterator, typename ForwardIterator2, typename OutputIterator, typename BinaryPredicate>
OutputIterator set_intersection(ForwardIterator first1, ForwardIterator last1, ForwardIterator2 first2, ForwardIterator2 last2, OutputIterator result, BinaryPredicate pred)
{
while (first1 != last1 && first2 != last2) {
if (pred(*first1, *first2)) {
first1 = std::lower_bound(++first1, last1, *first2, pred);
} else if (pred(*first2, *first1)) {
first2 = std::lower_bound(++first2, last2, *first1, pred);
} else {
*result = *first1;
++first1;
++first2;
++result;
}
}
return result;
}
//@}
template <typename ForwardIterator, typename ForwardIterator2, typename BinaryPredicate>
bool set_intersects(ForwardIterator first1, ForwardIterator last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred)
{
while (first1 != last1 && first2 != last2) {
if (pred(*first1, *first2)) {
first1 = std::lower_bound(++first1, last1, *first2, pred);
} else if (pred(*first2, *first1)) {
first2 = std::lower_bound(++first2, last2, *first1, pred);
} else {
return true;
}
}
return false;
}
//@{
}
#endif /* __KDTOOLSCORE_STL_UTIL_H__ */
|