File: replace.hpp

package info (click to toggle)
libfplus 0.2.13-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,904 kB
  • sloc: cpp: 27,543; javascript: 634; sh: 105; python: 103; makefile: 6
file content (117 lines) | stat: -rw-r--r-- 3,886 bytes parent folder | download | duplicates (3)
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
// 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/compare.hpp>
#include <fplus/split.hpp>

namespace fplus
{

namespace internal
{

template <typename UnaryPredicate, typename T, typename Container>
Container replace_if(internal::reuse_container_t,
    UnaryPredicate p, const T& dest, Container&& xs)
{
    std::replace_if(std::begin(xs), std::end(xs), p, dest);
    return std::forward<Container>(xs);
}

template <typename UnaryPredicate, typename T, typename Container>
Container replace_if(internal::create_new_container_t,
    UnaryPredicate p, const T& dest, const Container& xs)
{
    Container ys = xs;
    return replace_if(internal::reuse_container_t(),
        p, dest, std::move(ys));
}

} // namespace internal

// API search type: replace_if : ((a -> Bool), a, [a]) -> [a]
// fwd bind count: 2
// Replace every element fulfilling a predicate with a specific value.
// replace_if(is_even, 0, [1, 3, 4, 6, 7]) == [1, 3, 0, 0, 7]
template <typename UnaryPredicate, typename Container,
    typename ContainerOut = internal::remove_const_and_ref_t<Container>>
ContainerOut replace_if(UnaryPredicate p,
    const typename ContainerOut::value_type& dest, Container&& xs)
{
    return internal::replace_if(internal::can_reuse_v<Container>{},
        p, dest, std::forward<Container>(xs));
}

namespace internal
{

template <typename Container,
        typename T = typename Container::value_type>
Container replace_elem_at_idx(internal::reuse_container_t,
    std::size_t idx, const T& dest, Container&& xs)
{
    assert(idx < xs.size());
    auto it = std::begin(xs);
    advance_iterator(it, idx);
    *it = dest;
    return std::forward<Container>(xs);
}

template <typename Container,
        typename T = typename Container::value_type>
Container replace_elem_at_idx(internal::create_new_container_t,
    std::size_t idx, const T& dest, const Container& xs)
{
    Container ys = xs;
    return replace_elem_at_idx(internal::reuse_container_t(),
        idx, dest, std::move(ys));
}

} // namespace internal

// API search type: replace_elem_at_idx : (Int, a, [a]) -> [a]
// fwd bind count: 2
// Replace the element at a specific index.
// replace_elem_at_idx(2, 0, [1, 3, 4, 4, 7]) == [1, 3, 0, 4, 7]
template <typename Container,
        typename ContainerOut = internal::remove_const_and_ref_t<Container>,
        typename T = typename ContainerOut::value_type>
ContainerOut replace_elem_at_idx(std::size_t idx, const T& dest,
    Container&& xs)
{
    return internal::replace_elem_at_idx(internal::can_reuse_v<Container>{},
        idx, dest, std::forward<Container>(xs));
}

// API search type: replace_elems : (a, a, [a]) -> [a]
// fwd bind count: 2
// Replace all elements matching source with dest.
// replace_elems(4, 0, [1, 3, 4, 4, 7]) == [1, 3, 0, 0, 7]
template <typename Container,
        typename ContainerOut = internal::remove_const_and_ref_t<Container>,
        typename T = typename ContainerOut::value_type>
ContainerOut replace_elems(const T& source, const T& dest, Container&& xs)
{
    return replace_if(bind_1st_of_2(is_equal<T>, source), dest, xs);
}

// API search type: replace_tokens : ([a], [a], [a]) -> [a]
// fwd bind count: 2
// Replace all segments matching source with dest.
// replace_tokens("haha", "hihi", "oh, hahaha!") == "oh, hihiha!"
// replace_tokens("haha", "o", "oh, hahaha!") == "oh, oha!"
template <typename Container>
Container replace_tokens
        (const Container& source, const Container& dest, const Container& xs)
{
    auto splitted = split_by_token(source, true, xs);
    return join(dest, splitted);
}

} // namespace fplus