Skip to content

Commit da64e21

Browse files
committed
replaced std::function with templated variant
1 parent 44142ad commit da64e21

File tree

1 file changed

+49
-76
lines changed

1 file changed

+49
-76
lines changed

include/functional_vector.h

Lines changed: 49 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,11 @@ class functional_vector
9191
// }
9292
#ifdef CPP17_AVAILABLE
9393
template <typename U, typename Transform, typename = std::enable_if_t<std::is_invocable_r_v<U, Transform, T>>>
94-
functional_vector<U> map(Transform && transform) const
95-
{
9694
#else
97-
template <typename U>
98-
functional_vector<U> map(std::function<U(T)> && transform) const
99-
{
100-
using Transform = std::function<U(T)>;
95+
template <typename U, typename Transform>
10196
#endif
97+
functional_vector<U> map(Transform && transform) const
98+
{
10299
std::vector<U> transformed_vector;
103100
transformed_vector.reserve(backing_vector_.size());
104101
std::transform(backing_vector_.begin(),
@@ -141,13 +138,11 @@ class functional_vector
141138
// });
142139
#ifdef CPP17_AVAILABLE
143140
template <typename Callable, typename = std::enable_if_t<std::is_invocable_r_v<bool, Callable, T>>>
144-
bool all_of(Callable && unary_predicate) const
145-
{
146141
#else
147-
bool all_of(std::function<bool(T)> && unary_predicate) const
148-
{
149-
using Callable = std::function<bool(T)>;
142+
template <typename Callable>
150143
#endif
144+
bool all_of(Callable && unary_predicate) const
145+
{
151146
return std::all_of(cbegin(),
152147
cend(),
153148
std::forward<Callable>(unary_predicate));
@@ -182,13 +177,11 @@ class functional_vector
182177
// });
183178
#ifdef CPP17_AVAILABLE
184179
template <typename Callable, typename = std::enable_if_t<std::is_invocable_r_v<bool, Callable, T>>>
185-
bool any_of(Callable && unary_predicate) const
186-
{
187180
#else
188-
bool any_of(std::function<bool(T)> && unary_predicate) const
189-
{
190-
using Callable = std::function<bool(T)>;
181+
template <typename Callable>
191182
#endif
183+
bool any_of(Callable && unary_predicate) const
184+
{
192185
return std::any_of(cbegin(),
193186
cend(),
194187
std::forward<Callable>(unary_predicate));
@@ -223,13 +216,11 @@ class functional_vector
223216
// });
224217
#ifdef CPP17_AVAILABLE
225218
template <typename Callable, typename = std::enable_if_t<std::is_invocable_r_v<bool, Callable, T>>>
226-
bool none_of(Callable && unary_predicate) const
227-
{
228219
#else
229-
bool none_of(std::function<bool(T)> && unary_predicate) const
230-
{
231-
using Callable = std::function<bool(T)>;
220+
template <typename Callable>
232221
#endif
222+
bool none_of(Callable && unary_predicate) const
223+
{
233224
return std::none_of(cbegin(),
234225
cend(),
235226
std::forward<Callable>(unary_predicate));
@@ -271,13 +262,11 @@ class functional_vector
271262
// }
272263
#ifdef CPP17_AVAILABLE
273264
template <typename Filter, typename = std::enable_if_t<std::is_invocable_r_v<bool, Filter, T>>>
274-
functional_vector& filter(Filter && predicate_to_keep)
275-
{
276265
#else
277-
functional_vector& filter(std::function<bool(T)> && predicate_to_keep)
278-
{
279-
using Filter = std::function<bool(T)>;
266+
template <typename Filter>
280267
#endif
268+
functional_vector& filter(Filter && predicate_to_keep)
269+
{
281270
backing_vector_.erase(std::remove_if(backing_vector_.begin(),
282271
backing_vector_.end(),
283272
[predicate=std::forward<Filter>(predicate_to_keep)](const T& element) {
@@ -325,13 +314,11 @@ class functional_vector
325314
// }
326315
#ifdef CPP17_AVAILABLE
327316
template <typename Callable, typename = std::enable_if_t<std::is_invocable_r_v<bool, Callable, T>>>
328-
functional_vector filtered(Callable && predicate_to_keep) const
329-
{
330317
#else
331-
functional_vector filtered(std::function<bool(T)> && predicate_to_keep) const
332-
{
333-
using Callable = std::function<bool(T)>;
318+
template <typename Callable>
334319
#endif
320+
functional_vector filtered(Callable && predicate_to_keep) const
321+
{
335322
std::vector<T> filtered_vector;
336323
filtered_vector.reserve(backing_vector_.size());
337324
std::copy_if(backing_vector_.begin(),
@@ -538,13 +525,11 @@ class functional_vector
538525
// });
539526
#ifdef CPP17_AVAILABLE
540527
template <typename Sortable, typename = std::enable_if_t<std::is_invocable_r_v<bool, Sortable, T, T>>>
541-
functional_vector& sort(Sortable && comparison_predicate)
542-
{
543528
#else
544-
functional_vector& sort(std::function<bool(T, T)> && comparison_predicate)
545-
{
546-
using Sortable = std::function<bool(T, T)>;
529+
template <typename Sortable>
547530
#endif
531+
functional_vector& sort(Sortable && comparison_predicate)
532+
{
548533
std::sort(backing_vector_.begin(),
549534
backing_vector_.end(),
550535
std::forward<Sortable>(comparison_predicate));
@@ -632,13 +617,11 @@ class functional_vector
632617
// });
633618
#ifdef CPP17_AVAILABLE
634619
template <typename Sortable, typename = std::enable_if_t<std::is_invocable_r_v<bool, Sortable, T, T>>>
635-
functional_vector sorted(Sortable && comparison_predicate) const
636-
{
637620
#else
638-
functional_vector sorted(std::function<bool(T, T)> && comparison_predicate) const
639-
{
640-
using Sortable = std::function<bool(T, T)>;
621+
template <typename Sortable>
641622
#endif
623+
functional_vector sorted(Sortable && comparison_predicate) const
624+
{
642625
auto sorted_vector(backing_vector_);
643626
std::sort(sorted_vector.begin(),
644627
sorted_vector.end(),
@@ -709,13 +692,11 @@ class functional_vector
709692
// change the vector's contents during execution.
710693
#ifdef CPP17_AVAILABLE
711694
template <typename Callable, typename = std::enable_if_t<std::is_invocable_r_v<void, Callable, T const &>>>
712-
const functional_vector& for_each(Callable && operation) const
713-
{
714695
#else
715-
const functional_vector& for_each(std::function<void(T)> && operation) const
716-
{
717-
using Callable = std::function<void(T)>;
696+
template <typename Callable>
718697
#endif
698+
const functional_vector& for_each(Callable && operation) const
699+
{
719700
std::for_each(backing_vector_.cbegin(),
720701
backing_vector_.cend(),
721702
std::forward<Callable>(operation));
@@ -1497,10 +1478,10 @@ class functional_vector
14971478
// The iterator passed here may not necessarily be from std::vector as long as it's a valid iterable range
14981479
#ifdef CPP17_AVAILABLE
14991480
template<typename Iterator, typename = std::enable_if_t<std::is_constructible_v<T, typename std::iterator_traits<Iterator>::value_type>>>
1500-
functional_vector& insert_back_range_impl(const Iterator & vec_begin, const Iterator & vec_end)
15011481
#else
1502-
functional_vector& insert_back_range_impl(const typename std::vector<T>::const_iterator& vec_begin, const typename std::vector<T>::const_iterator& vec_end)
1482+
template<typename Iterator>
15031483
#endif
1484+
functional_vector& insert_back_range_impl(const Iterator& vec_begin, const Iterator& vec_end)
15041485
{
15051486
backing_vector_.insert(backing_vector_.end(),
15061487
vec_begin,
@@ -1510,10 +1491,10 @@ class functional_vector
15101491

15111492
#ifdef CPP17_AVAILABLE
15121493
template<typename Iterator, typename = std::enable_if_t<std::is_constructible_v<T, typename std::iterator_traits<Iterator>::value_type>>>
1513-
functional_vector& insert_front_range_impl(const Iterator& vec_begin, const Iterator& vec_end)
15141494
#else
1515-
functional_vector& insert_front_range_impl(const typename std::vector<T>::const_iterator& vec_begin, const typename std::vector<T>::const_iterator& vec_end)
1495+
template<typename Iterator>
15161496
#endif
1497+
functional_vector& insert_front_range_impl(const Iterator& vec_begin, const Iterator& vec_end)
15171498
{
15181499
backing_vector_.insert(backing_vector_.begin(),
15191500
vec_begin,
@@ -1523,10 +1504,10 @@ class functional_vector
15231504

15241505
#ifdef CPP17_AVAILABLE
15251506
template<typename Iterator, typename = std::enable_if_t<std::is_constructible_v<T, typename std::iterator_traits<Iterator>::value_type>>>
1526-
[[nodiscard]] functional_vector inserting_back_range_impl(const Iterator& vec_begin, const Iterator& vec_end) const
15271507
#else
1528-
[[nodiscard]] functional_vector inserting_back_range_impl(const typename std::vector<T>::const_iterator& vec_begin, const typename std::vector<T>::const_iterator& vec_end) const
1508+
template<typename Iterator>
15291509
#endif
1510+
[[nodiscard]] functional_vector inserting_back_range_impl(const Iterator& vec_begin, const Iterator& vec_end) const
15301511
{
15311512
auto augmented_vector(backing_vector_);
15321513
augmented_vector.reserve(augmented_vector.size() + std::distance(vec_begin, vec_end));
@@ -1538,10 +1519,10 @@ class functional_vector
15381519

15391520
#ifdef CPP17_AVAILABLE
15401521
template<typename Iterator, typename = std::enable_if_t<std::is_constructible_v<T, typename std::iterator_traits<Iterator>::value_type>>>
1541-
[[nodiscard]] functional_vector inserting_front_range_impl(const Iterator& vec_begin, const Iterator& vec_end) const
15421522
#else
1543-
[[nodiscard]] functional_vector inserting_front_range_impl(const typename std::vector<T>::const_iterator& vec_begin, const typename std::vector<T>::const_iterator& vec_end) const
1523+
template<typename Iterator>
15441524
#endif
1525+
[[nodiscard]] functional_vector inserting_front_range_impl(const Iterator& vec_begin, const Iterator& vec_end) const
15451526
{
15461527
auto augmented_vector(backing_vector_);
15471528
augmented_vector.reserve(augmented_vector.size() + std::distance(vec_begin, vec_end));
@@ -1553,7 +1534,7 @@ class functional_vector
15531534

15541535
#ifdef CPP17_AVAILABLE
15551536
template<typename Iterator, typename = std::enable_if_t<is_valid_iterator<Iterator>::value>>
1556-
[[nodiscard]] auto zip_impl( const Iterator& vec_begin, const Iterator & vec_end) const ->
1537+
[[nodiscard]] auto zip_impl( const Iterator& vec_begin, const Iterator& vec_end) const ->
15571538
functional_vector<pair<deref_type<Iterator>>>
15581539
{
15591540
using U = deref_type<Iterator>;
@@ -1577,14 +1558,12 @@ class functional_vector
15771558
#ifdef CPP17_AVAILABLE
15781559
// checks if the value of dereferencing the passed Iterators is convertible to type T
15791560
template<typename Iterator, typename = std::enable_if_t<std::is_constructible_v<T, typename std::iterator_traits<Iterator>::value_type>>>
1580-
functional_vector& insert_at_impl(int index,
1581-
const Iterator vec_begin,
1582-
const Iterator& vec_end)
15831561
#else
1584-
functional_vector& insert_at_impl(int index,
1585-
const typename std::vector<T>::const_iterator& vec_begin,
1586-
const typename std::vector<T>::const_iterator& vec_end)
1562+
template<typename Iterator>
15871563
#endif
1564+
functional_vector& insert_at_impl(int index,
1565+
const Iterator& vec_begin,
1566+
const Iterator& vec_end)
15881567
{
15891568
if (vec_begin != vec_end)
15901569
{
@@ -1598,14 +1577,12 @@ class functional_vector
15981577

15991578
#ifdef CPP17_AVAILABLE
16001579
template<typename Iterator, typename = std::enable_if_t<std::is_constructible_v<T, typename std::iterator_traits<Iterator>::value_type>>>
1580+
#else
1581+
template<typename Iterator>
1582+
#endif
16011583
[[nodiscard]] functional_vector inserting_at_impl(int index,
16021584
const Iterator& vec_begin,
16031585
const Iterator& vec_end) const
1604-
#else
1605-
[[nodiscard]] functional_vector inserting_at_impl(int index,
1606-
const typename std::vector<T>::const_iterator& vec_begin,
1607-
const typename std::vector<T>::const_iterator& vec_end) const
1608-
#endif
16091586
{
16101587
if (vec_begin == vec_end)
16111588
{
@@ -1621,14 +1598,12 @@ class functional_vector
16211598

16221599
#ifdef CPP17_AVAILABLE
16231600
template<typename Iterator, typename = std::enable_if_t<std::is_constructible_v<T, typename std::iterator_traits<Iterator>::value_type>>>
1601+
#else
1602+
template<typename Iterator>
1603+
#endif
16241604
functional_vector& replace_range_at_imp(int index,
16251605
const Iterator& vec_begin,
16261606
const Iterator& vec_end)
1627-
#else
1628-
functional_vector& replace_range_at_imp(int index,
1629-
const typename std::vector<T>::const_iterator& vec_begin,
1630-
const typename std::vector<T>::const_iterator& vec_end)
1631-
#endif
16321607
{
16331608
const auto vec_size = std::distance(vec_begin, vec_end);
16341609
assert(index + vec_size >= vec_size && index + vec_size <= size());
@@ -1640,14 +1615,12 @@ class functional_vector
16401615

16411616
#ifdef CPP17_AVAILABLE
16421617
template<typename Iterator, typename = std::enable_if_t<std::is_constructible_v<T, typename std::iterator_traits<Iterator>::value_type>>>
1618+
#else
1619+
template<typename Iterator>
1620+
#endif
16431621
[[nodiscard]] functional_vector replacing_range_at_imp(int index,
16441622
const Iterator& vec_begin,
16451623
const Iterator& vec_end) const
1646-
#else
1647-
[[nodiscard]] functional_vector replacing_range_at_imp(int index,
1648-
const typename std::vector<T>::const_iterator& vec_begin,
1649-
const typename std::vector<T>::const_iterator& vec_end) const
1650-
#endif
16511624
{
16521625
const auto vec_size = std::distance(vec_begin, vec_end);
16531626
assert(index + vec_size >= vec_size && index + vec_size <= size());

0 commit comments

Comments
 (0)