37# include <gtest/gtest.h>
48using namespace testing;
54 int operator()(
const int a,
const int b)
const noexcept
56 return std::gcd(a, b);
60 template <
typename T,
class Op>
61 T fold_range(
const std::vector<T> & values,
62 const size_t l,
const size_t r, Op op)
65 for (
size_t i =
l + 1; i <=
r; ++i)
66 acc = op(acc, values[i]);
74 Bits
operator+(
const Bits & o)
const noexcept {
return {mask | o.mask}; }
80 int operator()(
const int a,
const int b)
const noexcept {
return a + b; }
83 template <
class T,
class Op>
89inline constexpr bool Aleph::is_known_non_idempotent_op<My_Sum, int> =
true;
93static_assert(not buildable<int, std::plus<int>>
and not buildable<int, std::plus<>>);
94static_assert(not buildable<long, Aleph::plus<long>>
and not buildable<double, std::multiplies<double>>);
95static_assert(not buildable<int, std::minus<int>>
and not buildable<unsigned, std::bit_xor<unsigned>>);
96static_assert(not buildable<int, Aleph::modulus<int>>
and not buildable<int, My_Sum>);
97static_assert(not SparseTableOp<std::plus<int>,
int>);
100static_assert(buildable<int, Min_Op<int>>
and buildable<int, Max_Op<int>>
and buildable<int, Gcd_Op>);
101static_assert(buildable<int, std::bit_and<int>>
and buildable<int, std::bit_or<>>);
102static_assert(buildable<bool, std::plus<bool>>
and buildable<Bits, std::plus<Bits>>);
103constexpr auto min_lambda = [](
const int a,
const int b) {
return std::min(a, b); };
104static_assert(IdempotentOp<
decltype(
min_lambda),
int>);
106TEST(SparseTable, EmptyConstructionAndErrors)
110 EXPECT_TRUE(st.is_empty());
111 EXPECT_EQ(st.size(), 0U);
112 EXPECT_EQ(st.num_levels(), 0U);
114 EXPECT_THROW(st.get(0), std::out_of_range);
115 EXPECT_THROW(st.query(0, 0), std::out_of_range);
118TEST(SparseTable, UniformValueConstruction)
122 EXPECT_EQ(st.
size(), 16U);
124 EXPECT_EQ(st.
query(0, 15), 7);
125 EXPECT_EQ(st.
query(5, 11), 7);
127 for (
size_t i = 0; i < st.
size(); ++i)
128 EXPECT_EQ(st.
get(i), 7);
131TEST(SparseTable, MinAndMaxAgainstBruteForce)
133 const std::vector<int> values = {9, 4, 7, 1, 8, 2, 6, 3, 5, 0};
137 for (
size_t l = 0;
l < values.
size(); ++
l)
138 for (
size_t r =
l;
r < values.size(); ++
r)
140 const int expected_min = *std::min_element(values.begin() +
l,
142 const int expected_max = *std::max_element(values.begin() +
l,
144 EXPECT_EQ(mn.
query(
l,
r), expected_min);
145 EXPECT_EQ(mx.
query(
l,
r), expected_max);
149TEST(SparseTable, ConstructorsFromAllContainers)
151 const std::vector<int> values = {5, 3, 7, 1, 9, 2, 8, 4, 6};
155 for (
const int x : values)
160 for (
const int x : values)
166 for (
size_t l = 0;
l < values.
size(); ++
l)
167 for (
size_t r =
l;
r < values.size(); ++
r)
169 const int expected = *std::min_element(values.begin() +
l,
178TEST(SparseTable, CustomAssociativeIdempotentOperation)
180 const std::vector<int> values = {12, 18, 24, 36, 60, 48, 30, 90, 15, 45};
183 for (
size_t l = 0;
l < values.
size(); ++
l)
184 for (
size_t r =
l;
r < values.size(); ++
r)
185 EXPECT_EQ(st.query(
l,
r), fold_range(values,
l,
r,
Gcd_Op{}));
188TEST(SparseTable, ClassTypePlusStillAccepted)
190 const std::vector<Bits> values = {{1}, {2}, {4}, {8}, {16}};
193 for (
size_t l = 0;
l < values.
size(); ++
l)
194 for (
size_t r =
l;
r < values.size(); ++
r)
195 EXPECT_EQ(st.
query(
l,
r), fold_range(values,
l,
r, std::plus<Bits>{}));
198TEST(SparseTable, ValuesCopyMoveAndSwap)
200 const std::vector<int> base = {8, 6, 7, 5, 3, 0, 9};
204 ASSERT_EQ(vals.
size(), base.size());
205 for (
size_t i = 0; i < base.size(); ++i)
206 EXPECT_EQ(vals(i), base[i]);
209 EXPECT_EQ(
copy.query(1, 5), 0);
212 EXPECT_EQ(moved.
query(2, 6), 0);
217 EXPECT_EQ(moved.
size(), 3U);
218 EXPECT_EQ(moved.
query(0, 2), 50);
219 EXPECT_EQ(other.
size(), base.size());
220 EXPECT_EQ(other.
query(0, 6), 0);
223TEST(SparseTable, BoundsChecking)
227 EXPECT_THROW(st.
get(3), std::out_of_range);
228 EXPECT_THROW(st.
query(0, 3), std::out_of_range);
229 EXPECT_THROW(st.
query(2, 1), std::out_of_range);
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
size_t size() const noexcept
Return the number of items of the list, in constant time.
Sparse Table over an arbitrary associative and idempotent binary operation.
T get(const size_t i) const
Retrieve the value a[i] in O(1).
Array< T > values() const
Reconstruct all original values into an Array.
constexpr size_t size() const noexcept
Number of logical elements.
constexpr bool is_empty() const noexcept
True if the table contains no elements.
void swap(Gen_Sparse_Table &other) noexcept(std::is_nothrow_swappable_v< Op >)
Swap this table with other in O(1).
T query(const size_t l, const size_t r) const
Range query over [l, r] in O(1).
Minimal std::expected-style result type for C++20.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
Main namespace for Aleph-w library functions.
Gen_MultiPolynomial< C, M > operator+(const C &s, const Gen_MultiPolynomial< C, M > &p)
Scalar + polynomial (commutative).
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
Sparse Table for range maximum queries.
Sparse Table for range minimum queries.
int operator()(const int &a, const int &b) const noexcept
Sparse Table for static range queries in O(1).
constexpr auto min_lambda