#pragma once
#include <algorithm>
#include <cassert>
#include <limits>
#include <map>
#include <string>
#include <sstream>
#include <utility>
#include <cstdint>
#include "custom_containers.h"
#define RANGE_ASSERT(b) …
namespace sparse_container {
template <typename Index>
struct range { … };
template <typename Range>
class range_view { … };
template <typename Range>
std::string string_range(const Range &range) { … }
template <typename Range>
std::string string_range_hex(const Range &range) { … }
struct insert_range_no_split_bounds { … };
struct insert_range_split_bounds { … };
struct split_op_keep_both { … };
struct split_op_keep_lower { … };
struct split_op_keep_upper { … };
enum class value_precedence { … };
template <typename Iterator, typename Map, typename Range>
Iterator split(Iterator in, Map &map, const Range &range);
template <typename Key, typename T, typename RangeKey = range<Key>, typename ImplMap = std::map<RangeKey, T>>
class range_map {
public:
protected:
using MapKey = RangeKey;
ImplMap impl_map_;
using ImplIterator = typename ImplMap::iterator;
using ImplConstIterator = typename ImplMap::const_iterator;
public:
using mapped_type = typename ImplMap::mapped_type;
using value_type = typename ImplMap::value_type;
using key_type = typename ImplMap::key_type;
using index_type = typename key_type::index_type;
using size_type = typename ImplMap::size_type;
protected:
template <typename ThisType>
using ConstCorrectImplIterator = decltype(std::declval<ThisType>().impl_begin());
template <typename ThisType, typename WrappedIterator = ConstCorrectImplIterator<ThisType>>
static WrappedIterator lower_bound_impl(ThisType &that, const key_type &key) { … }
ImplIterator lower_bound_impl(const key_type &key) { … }
ImplConstIterator lower_bound_impl(const key_type &key) const { … }
template <typename ThisType, typename WrappedIterator = ConstCorrectImplIterator<ThisType>>
static WrappedIterator upper_bound_impl(ThisType &that, const key_type &key) { … }
ImplIterator upper_bound_impl(const key_type &key) { … }
ImplConstIterator upper_bound_impl(const key_type &key) const { … }
ImplIterator impl_find(const key_type &key) { … }
ImplConstIterator impl_find(const key_type &key) const { … }
bool impl_not_found(const key_type &key) const { … }
ImplIterator impl_end() { … }
ImplConstIterator impl_end() const { … }
ImplIterator impl_begin() { … }
ImplConstIterator impl_begin() const { … }
inline bool at_impl_end(const ImplIterator &pos) { … }
inline bool at_impl_end(const ImplConstIterator &pos) const { … }
inline bool at_impl_begin(const ImplIterator &pos) { … }
inline bool at_impl_begin(const ImplConstIterator &pos) const { … }
ImplIterator impl_erase(const ImplIterator &pos) { … }
template <typename Value>
ImplIterator impl_insert(const ImplIterator &hint, Value &&value) { … }
ImplIterator impl_insert(const ImplIterator &hint, const key_type &key, const mapped_type &value) { … }
ImplIterator impl_insert(const ImplIterator &hint, const index_type &begin, const index_type &end, const mapped_type &value) { … }
template <typename SplitOp>
ImplIterator split_impl(const ImplIterator &split_it, const index_type &index, const SplitOp &) { … }
range<ImplIterator> infill_and_split(const key_type &bounds, const mapped_type &value, ImplIterator lower, bool split_bounds) { … }
template <typename TouchOp>
ImplIterator impl_erase_range(const key_type &bounds, ImplIterator lower, const TouchOp &touch_mapped_value) { … }
template <typename ValueType, typename WrappedIterator_>
struct iterator_impl {
public:
friend class range_map;
using WrappedIterator = WrappedIterator_;
private:
WrappedIterator pos_;
iterator_impl(const WrappedIterator &pos) : pos_(pos) {}
public:
iterator_impl() : iterator_impl(WrappedIterator()){};
iterator_impl(const iterator_impl &other) : pos_(other.pos_){};
iterator_impl &operator=(const iterator_impl &rhs) {
pos_ = rhs.pos_;
return *this;
}
inline bool operator==(const iterator_impl &rhs) const { return pos_ == rhs.pos_; }
inline bool operator!=(const iterator_impl &rhs) const { return pos_ != rhs.pos_; }
ValueType &operator*() const { return *pos_; }
ValueType *operator->() const { return &*pos_; }
iterator_impl &operator++() {
++pos_;
return *this;
}
iterator_impl &operator--() {
--pos_;
return *this;
}
const WrappedIterator &get_pos() const { return pos_; };
};
public:
using iterator = iterator_impl<value_type, ImplIterator>;
class const_iterator : public iterator_impl<const value_type, ImplConstIterator> {
using Base = iterator_impl<const value_type, ImplConstIterator>;
friend range_map;
public:
const_iterator &operator=(const const_iterator &other) {
Base::operator=(other);
return *this;
}
const_iterator(const const_iterator &other) : Base(other){};
const_iterator(const iterator &it) : Base(ImplConstIterator(it.get_pos())) {}
const_iterator() : Base() {}
private:
const_iterator(const ImplConstIterator &pos) : Base(pos) {}
};
protected:
inline bool at_end(const iterator &it) { … }
inline bool at_end(const const_iterator &it) const { … }
inline bool at_begin(const iterator &it) { … }
template <typename That, typename Iterator>
static bool is_contiguous_impl(That *const that, const key_type &range, const Iterator &lower) { … }
public:
iterator end() { … }
const_iterator end() const { … }
iterator begin() { … }
const_iterator begin() const { … }
const_iterator cbegin() const { … }
const_iterator cend() const { … }
iterator erase(const iterator &pos) { … }
iterator erase(range<iterator> bounds) { … }
iterator erase(iterator first, iterator last) { … }
template <typename TouchOp>
iterator erase_range_or_touch(const key_type &bounds, const TouchOp &touch_mapped_value) { … }
iterator erase_range(const key_type &bounds) { … }
void clear() { … }
iterator find(const key_type &key) { … }
const_iterator find(const key_type &key) const { … }
iterator find(const index_type &index) { … }
const_iterator find(const index_type &index) const { … }
iterator lower_bound(const key_type &key) { … }
const_iterator lower_bound(const key_type &key) const { … }
iterator upper_bound(const key_type &key) { … }
const_iterator upper_bound(const key_type &key) const { … }
range<iterator> bounds(const key_type &key) { … }
range<const_iterator> cbounds(const key_type &key) const { … }
range<const_iterator> bounds(const key_type &key) const { … }
using insert_pair = std::pair<iterator, bool>;
insert_pair insert(const value_type &value) { … };
iterator insert(const_iterator hint, const value_type &value) { … }
template <typename MergeOp>
iterator split_and_merge_insert(const value_type &value, const MergeOp &merge_op) { … }
template <typename SplitOp>
iterator split(const iterator whole_it, const index_type &index, const SplitOp &split_op) { … }
template <typename Value>
iterator overwrite_range(const iterator &lower, Value &&value) { … }
template <typename Value>
iterator overwrite_range(Value &&value) { … }
bool empty() const { … }
size_type size() const { … }
ImplMap &get_implementation_map() { … }
const ImplMap &get_implementation_map() const { … }
};
const_correct_iterator;
template <typename Key, typename T, typename RangeKey = range<Key>, size_t N = 64, typename SmallIndex = uint8_t>
class small_range_map { … };
template <typename Map>
class cached_lower_bound_impl { … };
template <typename CachedLowerBound, typename MappedType = typename CachedLowerBound::mapped_type>
const MappedType &evaluate(const CachedLowerBound &clb, const MappedType &default_value) { … }
template <typename Iterator, typename Map, typename Range>
Iterator split(Iterator in, Map &map, const Range &range) { … }
template <typename RangeMap, typename InfillUpdateOps, typename Iterator = typename RangeMap::iterator>
Iterator infill_update_range(RangeMap &map, Iterator pos, const typename RangeMap::key_type &range, const InfillUpdateOps &ops) { … }
template <typename RangeMap, typename InfillUpdateOps>
void infill_update_range(RangeMap &map, const typename RangeMap::key_type &range, const InfillUpdateOps &ops) { … }
template <typename RangeMap, typename RangeGen, typename InfillUpdateOps>
void infill_update_rangegen(RangeMap &map, RangeGen &range_gen, const InfillUpdateOps &ops) { … }
template <typename MapA, typename MapB = MapA, typename KeyType = typename MapA::key_type>
class parallel_iterator { … };
template <typename DstRangeMap, typename SrcRangeMap, typename Updater,
typename SourceIterator = typename SrcRangeMap::const_iterator>
bool splice(DstRangeMap &to, const SrcRangeMap &from, SourceIterator begin, SourceIterator end, const Updater &updater) { … }
template <typename DstRangeMap, typename SrcRangeMap, typename Updater>
bool splice(DstRangeMap &to, const SrcRangeMap &from, const Updater &updater) { … }
template <typename T>
struct update_prefer_source { … };
template <typename T>
struct update_prefer_dest { … };
template <typename RangeMap, typename SourceIterator = typename RangeMap::const_iterator>
bool splice(RangeMap &to, const RangeMap &from, value_precedence arbiter, [[maybe_unused]] SourceIterator begin,
[[maybe_unused]] SourceIterator end) { … }
template <typename RangeMap>
bool splice(RangeMap &to, const RangeMap &from, value_precedence arbiter) { … }
template <typename Map, typename Range = typename Map::key_type, typename MapValue = typename Map::mapped_type>
bool update_range_value(Map &map, const Range &range, MapValue &&value, value_precedence precedence) { … }
template <typename RangeMap>
void consolidate(RangeMap &map) { … }
}
static inline sparse_container::range<int64_t> GetRangeIntersection(int64_t x, uint64_t x_size, int64_t y, uint64_t y_size) { … }