Skip to content

Commit

Permalink
Merged with master
Browse files Browse the repository at this point in the history
  • Loading branch information
psolstice committed Aug 21, 2024
2 parents f65c3e8 + 5a1bb0b commit bd31b27
Show file tree
Hide file tree
Showing 92 changed files with 8,576 additions and 3,959 deletions.
104 changes: 59 additions & 45 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -267,63 +267,77 @@ BITCOIN_CORE_H = \
llmq/quorums_chainlocks.h\
llmq/quorums_init.h \
llmq/quorums_signing_shares.h \
immer/atom.hpp \
immer/set.hpp \
immer/box.hpp \
immer/flex_vector_transient.hpp \
immer/transience/no_transience_policy.hpp \
immer/transience/gc_transience_policy.hpp \
immer/array_transient.hpp \
immer/map_transient.hpp \
immer/set_transient.hpp \
immer/flex_vector.hpp \
immer/refcount/enable_intrusive_ptr.hpp \
immer/refcount/no_refcount_policy.hpp \
immer/map.hpp \
immer/lock \
immer/lock/no_lock_policy.hpp \
immer/lock/spinlock_policy.hpp \
immer/heap \
immer/heap/heap_policy.hpp \
immer/heap/thread_local_free_list_heap.hpp \
immer/heap/tags.hpp \
immer/heap/split_heap.hpp \
immer/heap/with_data.hpp \
immer/heap/unsafe_free_list_heap.hpp \
immer/heap/cpp_heap.hpp \
immer/heap/malloc_heap.hpp \
immer/heap/gc_heap.hpp \
immer/heap/debug_size_heap.hpp \
immer/heap/free_list_heap.hpp \
immer/heap/identity_heap.hpp \
immer/heap/free_list_node.hpp \
immer/set.hpp \
immer/vector_transient.hpp \
immer/atom.hpp \
immer/config.hpp \
immer/experimental \
immer/experimental/dvektor.hpp \
immer/experimental/detail \
immer/experimental/detail/dvektor_impl.hpp \
immer/algorithm.hpp \
immer/table.hpp \
immer/memory_policy.hpp \
immer/refcount \
immer/refcount/unsafe_refcount_policy.hpp \
immer/refcount/no_refcount_policy.hpp \
immer/refcount/enable_intrusive_ptr.hpp \
immer/refcount/refcount_policy.hpp \
immer/memory_policy.hpp \
immer/config.hpp \
immer/array.hpp \
immer/transience \
immer/transience/gc_transience_policy.hpp \
immer/transience/no_transience_policy.hpp \
immer/flex_vector.hpp \
immer/box.hpp \
immer/vector.hpp \
immer/detail/arrays/no_capacity.hpp \
immer/detail/arrays/node.hpp \
immer/detail/arrays/with_capacity.hpp \
immer/detail/type_traits.hpp \
immer/detail/rbts/rbtree_iterator.hpp \
immer/array.hpp \
immer/set_transient.hpp \
immer/detail \
immer/detail/rbts \
immer/detail/rbts/operations.hpp \
immer/detail/rbts/rbtree.hpp \
immer/detail/rbts/rrbtree.hpp \
immer/detail/rbts/rbtree_iterator.hpp \
immer/detail/rbts/visitor.hpp \
immer/detail/rbts/rrbtree_iterator.hpp \
immer/detail/rbts/rrbtree.hpp \
immer/detail/rbts/node.hpp \
immer/detail/rbts/position.hpp \
immer/detail/rbts/bits.hpp \
immer/detail/rbts/node.hpp \
immer/detail/rbts/operations.hpp \
immer/detail/rbts/rrbtree_iterator.hpp \
immer/detail/combine_standard_layout.hpp \
immer/detail/ref_count_base.hpp \
immer/detail/hamts/champ_iterator.hpp \
immer/detail/arrays \
immer/detail/arrays/with_capacity.hpp \
immer/detail/arrays/node.hpp \
immer/detail/arrays/no_capacity.hpp \
immer/detail/util.hpp \
immer/detail/hamts \
immer/detail/hamts/champ.hpp \
immer/detail/hamts/bits.hpp \
immer/detail/hamts/champ_iterator.hpp \
immer/detail/hamts/node.hpp \
immer/detail/util.hpp \
immer/detail/hamts/bits.hpp \
immer/detail/type_traits.hpp \
immer/detail/iterator_facade.hpp \
immer/vector_transient.hpp \
immer/experimental/dvektor.hpp \
immer/experimental/detail/dvektor_impl.hpp \
immer/heap/free_list_heap.hpp \
immer/heap/tags.hpp \
immer/heap/debug_size_heap.hpp \
immer/heap/heap_policy.hpp \
immer/heap/gc_heap.hpp \
immer/heap/unsafe_free_list_heap.hpp \
immer/heap/split_heap.hpp \
immer/heap/identity_heap.hpp \
immer/heap/thread_local_free_list_heap.hpp \
immer/heap/malloc_heap.hpp \
immer/heap/cpp_heap.hpp \
immer/heap/free_list_node.hpp \
immer/heap/with_data.hpp \
immer/algorithm.hpp \
immer/array_transient.hpp \
immer/map.hpp
immer/detail/combine_standard_layout.hpp \
immer/flex_vector_transient.hpp \
immer/table_transient.hpp

obj/build.h: FORCE
@$(MKDIR_P) $(builddir)/obj
Expand Down
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
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

0 comments on commit bd31b27

Please sign in to comment.