Changeset View
Changeset View
Standalone View
Standalone View
src/client/clientutil.hpp
| Show First 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | namespace Kazv | ||||
| } | } | ||||
| template<class Context> | template<class Context> | ||||
| EventInterface &getEventEmitter(Context &&ctx) | EventInterface &getEventEmitter(Context &&ctx) | ||||
| { | { | ||||
| return lager::get<EventInterface &>(std::forward<Context>(ctx)); | return lager::get<EventInterface &>(std::forward<Context>(ctx)); | ||||
| } | } | ||||
| namespace | /** | ||||
| { | * Merge `addon` into the sorted container `base`. | ||||
| template<class ImmerT> | * | ||||
| struct ImmerIterator | * This function will look into every item `x` in `addon`. If `exists(x)`, | ||||
| { | * nothing is done. Otherwise, it does a binary search in `base`. If | ||||
| using value_type = typename ImmerT::value_type; | * there is an item `k` where `keyOf(k) == keyOf(x)`, nothing is done. | ||||
| using reference = typename ImmerT::reference; | * Otherwise, `x` is inserted into `base`, keeping the sort order. | ||||
| using pointer = const value_type *; | * | ||||
| using difference_type = long int; | * This function invokes at most `O(|addon|*log(|base|))` comparisons; each | ||||
| using iterator_category = std::random_access_iterator_tag; | * comparison invokes exactly 2 `keyOf`s. | ||||
| * | |||||
| ImmerIterator(const ImmerT &container, std::size_t index) | * This function invokes at most `|addon|` insertions of `base`. | ||||
| : m_container(std::ref(container)) | * | ||||
| , m_index(index) | * This function invokes `exists` exactly `|addon|` times. | ||||
| {} | * | ||||
| * @param base An immer sequence container of some type T. `base` must be sorted | |||||
| ImmerIterator &operator+=(difference_type d) { | * in strict ascending order in terms of `keyOf`. I.e., For any two items x, y: `keyOf(x) < keyOf(y)` iff x is before y. | ||||
| m_index += d; | * @param addon A range of the same type T. | ||||
| return *this; | * @param exists A function taking T and producing a boolean value. | ||||
| } | * @param keyOf A key function for the sorting of `base`. It must be a strong order. | ||||
| * | |||||
| ImmerIterator &operator-=(difference_type d) { | */ | ||||
| m_index -= d; | |||||
| return *this; | |||||
| } | |||||
| difference_type operator-(ImmerIterator b) const { | |||||
| return index() - b.index(); | |||||
| } | |||||
| ImmerIterator &operator++() { | |||||
| return *this += 1; | |||||
| } | |||||
| ImmerIterator operator++(int) { | |||||
| auto tmp = *this; | |||||
| *this += 1; | |||||
| return tmp; | |||||
| } | |||||
| ImmerIterator &operator--() { | |||||
| return *this -= 1; | |||||
| } | |||||
| ImmerIterator operator--(int) { | |||||
| auto tmp = *this; | |||||
| *this -= 1; | |||||
| return tmp; | |||||
| } | |||||
| reference &operator*() const { | |||||
| return m_container.get().at(m_index); | |||||
| } | |||||
| reference operator[](difference_type d) const; | |||||
| std::size_t index() const { return m_index; } | |||||
| private: | |||||
| std::reference_wrapper<const ImmerT> m_container; | |||||
| std::size_t m_index; | |||||
| }; | |||||
| template<class ImmerT> | |||||
| auto ImmerIterator<ImmerT>::operator[](difference_type d) const -> reference | |||||
| { | |||||
| return *(*this + d); | |||||
| } | |||||
| template<class ImmerT> | |||||
| auto operator+(ImmerIterator<ImmerT> a, long int d) | |||||
| { | |||||
| return a += d; | |||||
| }; | |||||
| template<class ImmerT> | |||||
| auto operator+(long int d, ImmerIterator<ImmerT> a) | |||||
| { | |||||
| return a += d; | |||||
| }; | |||||
| template<class ImmerT> | |||||
| auto operator-(ImmerIterator<ImmerT> a, long int d) | |||||
| { | |||||
| return a -= d; | |||||
| }; | |||||
| template<class ImmerT> | |||||
| auto immerBegin(const ImmerT &c) | |||||
| { | |||||
| return ImmerIterator<ImmerT>(c, 0); | |||||
| } | |||||
| template<class ImmerT> | |||||
| auto immerEnd(const ImmerT &c) | |||||
| { | |||||
| return ImmerIterator<ImmerT>(c, c.size()); | |||||
| } | |||||
| } | |||||
| template<class ImmerT1, class RangeT2, class Pred, class Func> | template<class ImmerT1, class RangeT2, class Pred, class Func> | ||||
| ImmerT1 sortedUniqueMerge(ImmerT1 base, RangeT2 addon, Pred exists, Func keyOf) | ImmerT1 sortedUniqueMerge(ImmerT1 base, RangeT2 addon, Pred exists, Func keyOf) | ||||
| { | { | ||||
| auto needToAdd = intoImmer(ImmerT1{}, | |||||
| zug::filter([=](auto a) { | |||||
| return !exists(a); | |||||
| }), | |||||
| addon); | |||||
| auto cmp = [=](auto a, auto b) { | auto cmp = [=](auto a, auto b) { | ||||
| return keyOf(a) < keyOf(b); | return keyOf(a) < keyOf(b); | ||||
| }; | }; | ||||
| for (auto item : needToAdd) { | for (auto item : addon) { | ||||
| auto it = std::upper_bound(immerBegin(base), immerEnd(base), item, cmp); | if (exists(item)) { | ||||
| continue; | |||||
| } | |||||
| // https://en.cppreference.com/w/cpp/algorithm/upper_bound.html | |||||
| // *(it-1) <= item < *it | |||||
| auto it = std::upper_bound(base.begin(), base.end(), item, cmp); | |||||
| // If `it` is not the first iterator, and `*(it-1) == item`, | |||||
| // then `item` is already in the list. Otherwise, we are guaranteed | |||||
| // that `*(it-1)` either does not exist, or `*(it-1) < item`. | |||||
| // In these cases, `item` is not in the list and we will need | |||||
| // to add them. | |||||
| if (it.index() != 0 && keyOf(item) == keyOf(*(it - 1))) { | |||||
| continue; | |||||
| } | |||||
| auto index = it.index(); | auto index = it.index(); | ||||
| base = std::move(base).insert(index, item); | base = std::move(base).insert(index, item); | ||||
| } | } | ||||
| return base; | return base; | ||||
| } | } | ||||
| std::string increaseTxnId(std::string cur); | std::string increaseTxnId(std::string cur); | ||||
| Show All 24 Lines | |||||