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
|
// Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
// https://github.com/Dobiasd/FunctionalPlus
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#include <fplus/container_common.hpp>
#include <fplus/composition.hpp>
#include <fplus/generate.hpp>
#include <fplus/maybe.hpp>
#include <algorithm>
namespace fplus
{
// API search type: find_first_by : ((a -> Bool), [a]) -> Maybe a
// fwd bind count: 1
// Returns the first element fulfilling the predicate.
// find_first_by(is_even, [1, 3, 4, 6, 9]) == Just(4)
// find_first_by(is_even, [1, 3, 5, 7, 9]) == Nothing
template <typename Container, typename UnaryPredicate,
typename T = typename Container::value_type>
maybe<T> find_first_by(UnaryPredicate pred, const Container& xs)
{
internal::check_unary_predicate_for_container<UnaryPredicate, Container>();
auto it = std::find_if(std::begin(xs), std::end(xs), pred);
if (it == std::end(xs))
return nothing<T>();
return just<T>(*it);
}
// API search type: find_last_by : ((a -> Bool), [a]) -> Maybe a
// fwd bind count: 1
// Returns the last element fulfilling the predicate.
// find_last_by(is_even, [1, 3, 4, 6, 9]) == Just(6)
// find_last_by(is_even, [1, 3, 5, 7, 9]) == Nothing
template <typename Container, typename UnaryPredicate,
typename T = typename Container::value_type>
maybe<T> find_last_by(UnaryPredicate pred, const Container& xs)
{
internal::check_unary_predicate_for_container<UnaryPredicate, Container>();
return find_first_by(pred, reverse(xs));
}
// API search type: find_first_idx_by : ((a -> Bool), [a]) -> Maybe Int
// fwd bind count: 1
// Returns the index of the first element fulfilling the predicate.
// find_first_idx_by(is_even, [1, 3, 4, 6, 9]) == Just(2)
// find_first_idx_by(is_even, [1, 3, 5, 7, 9]) == Nothing
template <typename Container, typename UnaryPredicate>
maybe<std::size_t> find_first_idx_by
(UnaryPredicate pred, const Container& xs)
{
internal::check_unary_predicate_for_container<UnaryPredicate, Container>();
auto it = std::find_if(std::begin(xs), std::end(xs), pred);
if (it == std::end(xs))
return nothing<std::size_t>();
return static_cast<std::size_t>(std::distance(std::begin(xs), it));
}
// API search type: find_last_idx_by : ((a -> Bool), [a]) -> Maybe Int
// fwd bind count: 1
// Returns the index of the last element fulfilling the predicate.
// find_last_idx_by(is_even, [1, 3, 4, 6, 9]) == Just(3)
// find_last_idx_by(is_even, [1, 3, 5, 7, 9]) == Nothing
template <typename Container, typename UnaryPredicate>
maybe<std::size_t> find_last_idx_by
(UnaryPredicate pred, const Container& xs)
{
internal::check_unary_predicate_for_container<UnaryPredicate, Container>();
auto calcRevIdx = [&](std::size_t idx)
{
return size_of_cont(xs) - (idx + 1);
};
return lift_maybe(calcRevIdx, find_first_idx_by(pred, reverse(xs)));
}
// API search type: find_first_idx : (a, [a]) -> Maybe Int
// fwd bind count: 1
// Returns the index of the first element equal to x.
// find_first_idx(4, [1, 3, 4, 4, 9]) == Just(2)
// find_first_idx(4, [1, 3, 5, 7, 9]) == Nothing
template <typename Container>
maybe<std::size_t> find_first_idx
(const typename Container::value_type& x, const Container& xs)
{
return find_first_idx_by(is_equal_to(x), xs);
}
// API search type: find_last_idx : (a, [a]) -> Maybe Int
// fwd bind count: 1
// Returns the index of the last element equal to x.
// find_last_idx(4, [1, 3, 4, 4, 9]) == Just(3)
// find_last_idx(4, [1, 3, 5, 7, 9]) == Nothing
template <typename Container>
maybe<std::size_t> find_last_idx
(const typename Container::value_type& x, const Container& xs)
{
return find_last_idx_by(is_equal_to(x), xs);
}
// API search type: find_all_idxs_by : ((a -> Bool), [a]) -> [Int]
// fwd bind count: 1
// Returns the indices off all elements fulfilling the predicate.
// find_all_idxs_by(is_even, [1, 3, 4, 6, 9]) == [2, 3]
template <typename ContainerOut = std::vector<std::size_t>,
typename UnaryPredicate, typename Container>
ContainerOut find_all_idxs_by(UnaryPredicate p, const Container& xs)
{
internal::check_unary_predicate_for_container<UnaryPredicate, Container>();
std::size_t idx = 0;
ContainerOut result;
auto itOut = internal::get_back_inserter(result);
for (const auto& x : xs)
{
if (internal::invoke(p, x))
*itOut = idx;
++idx;
}
return result;
}
// API search type: find_all_idxs_of : (a, [a]) -> [Int]
// fwd bind count: 1
// Returns the indices off all elements equal to x.
// find_all_idxs_of(4, [1, 3, 4, 4, 9]) == [2, 3]
template <typename ContainerOut = std::vector<std::size_t>,
typename Container,
typename T = typename Container::value_type>
ContainerOut find_all_idxs_of
(const T& x, const Container& xs)
{
return find_all_idxs_by(is_equal_to(x), xs);
}
// API search type: find_all_instances_of_token : ([a], [a]) -> [Int]
// fwd bind count: 1
// Returns the starting indices of all segments matching token.
// find_all_instances_of_token("haha", "oh, hahaha!") == [4, 6]
template <typename ContainerOut =
std::vector<std::size_t>, typename Container>
ContainerOut find_all_instances_of_token(const Container& token,
const Container& xs)
{
if (size_of_cont(token) > size_of_cont(xs))
return ContainerOut();
auto itInBegin = std::begin(xs);
auto itInEnd = itInBegin;
internal::advance_iterator(itInEnd, size_of_cont(token));
std::size_t idx = 0;
ContainerOut result;
auto outIt = internal::get_back_inserter(result);
std::size_t last_possible_idx = size_of_cont(xs) - size_of_cont(token);
auto check_and_push = [&]()
{
if (std::equal(itInBegin, itInEnd, std::begin(token)))
{
*outIt = idx;
}
};
while (idx != last_possible_idx)
{
check_and_push();
++itInBegin;
++itInEnd;
++idx;
}
check_and_push();
return result;
}
// API search type: find_all_instances_of_token_non_overlapping : ([a], [a]) -> [Int]
// fwd bind count: 1
// Returns the starting indices
// of all non-overlapping segments matching token.
// find_all_instances_of_token_non_overlapping("haha", "oh, hahaha!") == [4]
template <typename ContainerOut = std::vector<std::size_t>, typename Container>
ContainerOut find_all_instances_of_token_non_overlapping
(const Container& token, const Container& xs)
{
auto overlapping_instances = find_all_instances_of_token<ContainerOut>(
token, xs);
ContainerOut result;
auto outIt = internal::get_back_inserter(result);
std::size_t token_size = size_of_cont(token);
for (const auto idx : overlapping_instances)
{
if (result.empty() || result.back() + token_size <= idx)
{
*outIt = idx;
}
}
return result;
}
// API search type: find_first_instance_of_token : ([a], [a]) -> Maybe Int
// fwd bind count: 1
// Returns the index of the first segment matching token.
// find_first_instance_of_token("haha", "oh, hahaha!") == just 4
template <typename Container>
maybe<std::size_t> find_first_instance_of_token
(const Container& token, const Container& xs)
{
if (size_of_cont(token) > size_of_cont(xs))
return nothing<std::size_t>();
auto itInBegin = std::begin(xs);
auto itInEnd = itInBegin;
internal::advance_iterator(itInEnd, size_of_cont(token));
std::size_t idx = 0;
std::size_t last_possible_idx = size_of_cont(xs) - size_of_cont(token);
while (idx != last_possible_idx)
{
if (std::equal(itInBegin, itInEnd, std::begin(token)))
{
return just(idx);
}
++itInBegin;
++itInEnd;
++idx;
}
if (std::equal(itInBegin, itInEnd, std::begin(token)))
{
return just(idx);
}
return nothing<std::size_t>();
}
} // namespace fplus
|