Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compiler warnings #1423

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compat/glibcxx_sanity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ bool sanity_test_range_fmt()
{
std::string test;
try {
test.at(1);
(void) test.at(1);
} catch (const std::out_of_range&) {
return true;
} catch (...) {
Expand Down
155 changes: 133 additions & 22 deletions src/immer/algorithm.hpp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we've got a separate PR for immer library update. I think the changes made to the immer directory shouldn't be in this PR if we are to apply this one after #1432

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#pragma once

#include <algorithm>
#include <cassert>
#include <functional>
#include <numeric>
#include <type_traits>

Expand Down Expand Up @@ -47,8 +49,8 @@ template <typename Iterator, typename Fn>
void for_each_chunk(const Iterator& first, const Iterator& last, Fn&& fn)
{
assert(&first.impl() == &last.impl());
first.impl().for_each_chunk(first.index(), last.index(),
std::forward<Fn>(fn));
first.impl().for_each_chunk(
first.index(), last.index(), std::forward<Fn>(fn));
}

template <typename T, typename Fn>
Expand Down Expand Up @@ -81,8 +83,8 @@ template <typename Iterator, typename Fn>
bool for_each_chunk_p(const Iterator& first, const Iterator& last, Fn&& fn)
{
assert(&first.impl() == &last.impl());
return first.impl().for_each_chunk_p(first.index(), last.index(),
std::forward<Fn>(fn));
return first.impl().for_each_chunk_p(
first.index(), last.index(), std::forward<Fn>(fn));
}

template <typename T, typename Fn>
Expand All @@ -91,23 +93,43 @@ bool for_each_chunk_p(const T* first, const T* last, Fn&& fn)
return std::forward<Fn>(fn)(first, last);
}

namespace detail {

template <class Iter, class T>
T accumulate_move(Iter first, Iter last, T init)
{
for (; first != last; ++first)
init = std::move(init) + *first;
return init;
}

template <class Iter, class T, class Fn>
T accumulate_move(Iter first, Iter last, T init, Fn op)
{
for (; first != last; ++first)
init = op(std::move(init), *first);
return init;
}

} // namespace detail

/*!
* Equivalent of `std::accumulate` applied to the range `r`.
*/
template <typename Range, typename T>
T accumulate(Range&& r, T init)
{
for_each_chunk(r, [&] (auto first, auto last) {
init = std::accumulate(first, last, init);
for_each_chunk(r, [&](auto first, auto last) {
init = detail::accumulate_move(first, last, init);
});
return init;
}

template <typename Range, typename T, typename Fn>
T accumulate(Range&& r, T init, Fn fn)
{
for_each_chunk(r, [&] (auto first, auto last) {
init = std::accumulate(first, last, init, fn);
for_each_chunk(r, [&](auto first, auto last) {
init = detail::accumulate_move(first, last, init, fn);
});
return init;
}
Expand All @@ -119,17 +141,17 @@ T accumulate(Range&& r, T init, Fn fn)
template <typename Iterator, typename T>
T accumulate(Iterator first, Iterator last, T init)
{
for_each_chunk(first, last, [&] (auto first, auto last) {
init = std::accumulate(first, last, init);
for_each_chunk(first, last, [&](auto first, auto last) {
init = detail::accumulate_move(first, last, init);
});
return init;
}

template <typename Iterator, typename T, typename Fn>
T accumulate(Iterator first, Iterator last, T init, Fn fn)
{
for_each_chunk(first, last, [&] (auto first, auto last) {
init = std::accumulate(first, last, init, fn);
for_each_chunk(first, last, [&](auto first, auto last) {
init = detail::accumulate_move(first, last, init, fn);
});
return init;
}
Expand All @@ -140,7 +162,7 @@ T accumulate(Iterator first, Iterator last, T init, Fn fn)
template <typename Range, typename Fn>
Fn&& for_each(Range&& r, Fn&& fn)
{
for_each_chunk(r, [&] (auto first, auto last) {
for_each_chunk(r, [&](auto first, auto last) {
for (; first != last; ++first)
fn(*first);
});
Expand All @@ -154,7 +176,7 @@ Fn&& for_each(Range&& r, Fn&& fn)
template <typename Iterator, typename Fn>
Fn&& for_each(Iterator first, Iterator last, Fn&& fn)
{
for_each_chunk(first, last, [&] (auto first, auto last) {
for_each_chunk(first, last, [&](auto first, auto last) {
for (; first != last; ++first)
fn(*first);
});
Expand All @@ -167,9 +189,8 @@ Fn&& for_each(Iterator first, Iterator last, Fn&& fn)
template <typename Range, typename OutIter>
OutIter copy(Range&& r, OutIter out)
{
for_each_chunk(r, [&] (auto first, auto last) {
out = std::copy(first, last, out);
});
for_each_chunk(
r, [&](auto first, auto last) { out = std::copy(first, last, out); });
return out;
}

Expand All @@ -180,7 +201,7 @@ OutIter copy(Range&& r, OutIter out)
template <typename InIter, typename OutIter>
OutIter copy(InIter first, InIter last, OutIter out)
{
for_each_chunk(first, last, [&] (auto first, auto last) {
for_each_chunk(first, last, [&](auto first, auto last) {
out = std::copy(first, last, out);
});
return out;
Expand All @@ -192,9 +213,8 @@ OutIter copy(InIter first, InIter last, OutIter out)
template <typename Range, typename Pred>
bool all_of(Range&& r, Pred p)
{
return for_each_chunk_p(r, [&] (auto first, auto last) {
return std::all_of(first, last, p);
});
return for_each_chunk_p(
r, [&](auto first, auto last) { return std::all_of(first, last, p); });
}

/*!
Expand All @@ -204,11 +224,102 @@ bool all_of(Range&& r, Pred p)
template <typename Iter, typename Pred>
bool all_of(Iter first, Iter last, Pred p)
{
return for_each_chunk_p(first, last, [&] (auto first, auto last) {
return for_each_chunk_p(first, last, [&](auto first, auto last) {
return std::all_of(first, last, p);
});
}

/*!
* Object that can be used to process changes as computed by the @a diff
* algorithm.
*
* @tparam AddedFn Unary function that is be called whenever an added element is
* found. It is called with the added element as argument.
*
* @tparam RemovedFn Unary function that is called whenever a removed element is
* found. It is called with the removed element as argument.
*
* @tparam ChangedFn Unary function that is called whenever a changed element is
* found. It is called with the changed element as argument.
*/
template <class AddedFn, class RemovedFn, class ChangedFn>
struct differ
{
AddedFn added;
RemovedFn removed;
ChangedFn changed;
};

/*!
* Produces a @a differ object with `added`, `removed` and `changed` functions.
*/
template <class AddedFn, class RemovedFn, class ChangedFn>
auto make_differ(AddedFn&& added, RemovedFn&& removed, ChangedFn&& changed)
-> differ<std::decay_t<AddedFn>,
std::decay_t<RemovedFn>,
std::decay_t<ChangedFn>>
{
return {std::forward<AddedFn>(added),
std::forward<RemovedFn>(removed),
std::forward<ChangedFn>(changed)};
}

/*!
* Produces a @a differ object with `added` and `removed` functions and no
* `changed` function.
*/
template <class AddedFn, class RemovedFn>
auto make_differ(AddedFn&& added, RemovedFn&& removed)
{
return make_differ(std::forward<AddedFn>(added),
std::forward<RemovedFn>(removed),
[](auto&&...) {});
}

/*!
* Compute the differences between `a` and `b`.
*
* Changes detected are notified via the differ object, which should support the
* following expressions:
*
* - `differ.added(x)`, invoked when element `x` is found in `b` but not in
* `a`.
*
* - `differ.removed(x)`, invoked when element `x` is found in `a` but not in
* `b`.
*
* - `differ.changed(x, y)`, invoked when element `x` and `y` from `a` and `b`
* share the same key but map to a different value.
*
* This method leverages structural sharing to offer a complexity @f$ O(|diff|)
* @f$ when `b` is derived from `a` by performing @f$ |diff| @f$ updates. This
* is, this function can detect changes in effectively constant time per update,
* as oposed to the @f$ O(|a|+|b|) @f$ complexity of a trivial implementation.
*
* @rst
*
* .. note:: This method is only implemented for ``map`` and ``set``. When sets
* are diffed, the ``changed`` function is never called.
*
* @endrst
*/
template <typename T, typename Differ>
void diff(const T& a, const T& b, Differ&& differ)
{
a.impl().template diff<std::equal_to<typename T::value_type>>(
b.impl(), std::forward<Differ>(differ));
}

/*!
* Compute the differences between `a` and `b` using the callbacks in `fns` as
* differ. Equivalent to `diff(a, b, make_differ(fns)...)`.
*/
template <typename T, typename... Fns>
void diff(const T& a, const T& b, Fns&&... fns)
{
diff(a, b, make_differ(std::forward<Fns>(fns)...));
}

/** @} */ // group: algorithm

} // namespace immer
Loading
Loading