|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Offline range query engine using Mo's algorithm. More...
#include <tpl_mo_algorithm.H>
Public Types | |
| using | Item_Type = T |
| using | answer_type = typename Policy::answer_type |
Public Member Functions | |
| Gen_Mo_Algorithm (std::initializer_list< T > il, Policy p=Policy()) | |
| Construct from an initializer list. | |
| Gen_Mo_Algorithm (const Array< T > &arr, Policy p=Policy()) | |
| Construct from an Array<T>. | |
| Gen_Mo_Algorithm (Array< T > and arr, Policy p=Policy()) | |
| Construct from an Array<T> (move). | |
| Gen_Mo_Algorithm (const DynList< T > &lst, Policy p=Policy()) | |
| Construct from a DynList<T>. | |
| Gen_Mo_Algorithm (const Gen_Mo_Algorithm &)=default | |
| Gen_Mo_Algorithm (Gen_Mo_Algorithm &&) noexcept(std::is_nothrow_move_constructible_v< Array< T > > and std::is_nothrow_move_constructible_v< Policy >)=default | |
| Gen_Mo_Algorithm & | operator= (const Gen_Mo_Algorithm &)=default |
| Gen_Mo_Algorithm & | operator= (Gen_Mo_Algorithm &&) noexcept(std::is_nothrow_move_assignable_v< Array< T > > and std::is_nothrow_move_assignable_v< Policy >)=default |
| constexpr size_t | size () const noexcept |
| Number of elements in the data array. | |
| constexpr bool | is_empty () const noexcept |
| True if the data array is empty. | |
| void | swap (Gen_Mo_Algorithm &other) noexcept(noexcept(std::swap(std::declval< Policy & >(), std::declval< Policy & >()))) |
Swap with other in O(1). | |
| Array< answer_type > | solve (const Array< std::pair< size_t, size_t > > &ranges) const |
| Solve queries given as (l, r) pairs. | |
| Array< answer_type > | solve (const std::initializer_list< std::pair< size_t, size_t > > il) const |
| Solve queries given as (l, r) pairs (initializer list). | |
| Array< answer_type > | solve (Array< Mo_Query > queries) const |
| Core solver: answer all Mo_Query objects. | |
Private Attributes | |
| Array< T > | data_ |
| Policy | pol_ |
Offline range query engine using Mo's algorithm.
Given a static array of N elements and Q range queries [l, r], Gen_Mo_Algorithm answers all queries in O((N+Q) sqrt (N)) time by sorting queries into blocks and sweeping a sliding window.
The user supplies a Policy object that maintains per-window state (add/remove elements, compute the current answer).
| T | Element type stored in the array. |
| Policy | A type satisfying MoPolicy<Policy, T>. |
Definition at line 141 of file tpl_mo_algorithm.H.
| using Aleph::Gen_Mo_Algorithm< T, Policy >::answer_type = typename Policy::answer_type |
Definition at line 148 of file tpl_mo_algorithm.H.
Definition at line 147 of file tpl_mo_algorithm.H.
|
inline |
Construct from an initializer list.
| il | Initializer list used to populate the internal array. |
| p | Policy object copied/moved into the solver. |
| std::bad_alloc | If internal storage allocation fails. |
| Any | exception thrown by T copy/move construction or by Policy move construction. |
il.size(). Definition at line 161 of file tpl_mo_algorithm.H.
|
inline |
Construct from an Array<T>.
| arr | Source array copied into internal storage. |
| p | Policy object copied/moved into the solver. |
| std::bad_alloc | If internal storage allocation fails. |
| Any | exception thrown by T copy construction or by Policy move construction. |
arr.size(). Definition at line 176 of file tpl_mo_algorithm.H.
|
inline |
Construct from an Array<T> (move).
| arr | Source array moved into internal storage. |
| p | Policy object copied/moved into the solver. |
| Any | exception thrown by Array<T> move construction or by Policy move construction. |
Array<T> and Policy are nothrow-movable. Definition at line 191 of file tpl_mo_algorithm.H.
|
inline |
Construct from a DynList<T>.
| lst | Source list copied into internal contiguous storage. |
| p | Policy object copied/moved into the solver. |
| std::bad_alloc | If internal storage allocation fails. |
| Any | exception thrown by T copy construction or by Policy move construction. |
lst.size(). Definition at line 206 of file tpl_mo_algorithm.H.
References Aleph::Gen_Mo_Algorithm< T, Policy >::data_, and Aleph::divide_and_conquer_partition_dp().
|
default |
|
defaultnoexcept |
|
inlineconstexprnoexcept |
True if the data array is empty.
Definition at line 233 of file tpl_mo_algorithm.H.
References Aleph::Gen_Mo_Algorithm< T, Policy >::data_.
|
default |
|
defaultnoexcept |
|
inlineconstexprnoexcept |
Number of elements in the data array.
Definition at line 227 of file tpl_mo_algorithm.H.
References Aleph::Gen_Mo_Algorithm< T, Policy >::data_, and Aleph::Gen_Mo_Algorithm< T, Policy >::size().
Referenced by Aleph::Gen_Mo_Algorithm< T, Policy >::size().
|
inline |
Core solver: answer all Mo_Query objects.
The queries are sorted by block (snake-optimized), swept with a sliding window, and the answers are returned in original order.
Array<answer_type> indexed by original query id.| std::bad_alloc | If the answers array or policy-internal storage allocation fails. |
| std::out_of_range | If any query has r >= size(), l > r, or id >= queries.size(). |
| Any | exception thrown by Policy::init, Policy::add, Policy::remove, or Policy::answer. |
Definition at line 317 of file tpl_mo_algorithm.H.
References ah_out_of_range_error_if, Aleph::Array< T >::create(), Aleph::Gen_Mo_Algorithm< T, Policy >::data_, Aleph::divide_and_conquer_partition_dp(), l, Aleph::Mo_Query::l, Aleph::Gen_Mo_Algorithm< T, Policy >::pol_, r, and Aleph::Mo_Query::r.
|
inline |
Solve queries given as (l, r) pairs.
Convenience overload: assigns sequential ids 0, 1, 2, ...
| ranges | Array of (l, r) closed ranges (0-based, inclusive). |
Array<answer_type> indexed by original query order.| std::bad_alloc | If temporary query/answer arrays cannot be allocated. |
| std::out_of_range | If any query has r >= size(), l > r, or an invalid id (validated in core solve). |
| Any | exception thrown by Policy::init, Policy::add, Policy::remove, or Policy::answer. |
Mo_Query: O(q)Definition at line 265 of file tpl_mo_algorithm.H.
References Aleph::Array< T >::create(), Aleph::divide_and_conquer_partition_dp(), and Aleph::Gen_Mo_Algorithm< T, Policy >::solve().
Referenced by Aleph::Gen_Mo_Algorithm< T, Policy >::solve(), Aleph::Gen_Mo_Algorithm< T, Policy >::solve(), TEST(), TEST(), TEST(), TEST(), TEST(), and TEST().
|
inline |
Solve queries given as (l, r) pairs (initializer list).
| il | Initializer list of (l, r) closed ranges. |
Array<answer_type> indexed by original query order.| std::bad_alloc | If temporary ranges/query/answer arrays cannot be allocated. |
| std::out_of_range | Propagated from solve(const Array<...>&) when query bounds are invalid. |
| Any | exception propagated from the underlying policy. |
Array from initializer list: O(q)Definition at line 289 of file tpl_mo_algorithm.H.
References Aleph::divide_and_conquer_partition_dp(), and Aleph::Gen_Mo_Algorithm< T, Policy >::solve().
|
inlinenoexcept |
Swap with other in O(1).
Definition at line 239 of file tpl_mo_algorithm.H.
References Aleph::Gen_Mo_Algorithm< T, Policy >::data_, Aleph::divide_and_conquer_partition_dp(), and Aleph::Gen_Mo_Algorithm< T, Policy >::pol_.
Referenced by TEST().
Definition at line 143 of file tpl_mo_algorithm.H.
Referenced by Aleph::Gen_Mo_Algorithm< T, Policy >::Gen_Mo_Algorithm(), Aleph::Gen_Mo_Algorithm< T, Policy >::is_empty(), Aleph::Gen_Mo_Algorithm< T, Policy >::size(), Aleph::Gen_Mo_Algorithm< T, Policy >::solve(), and Aleph::Gen_Mo_Algorithm< T, Policy >::swap().
|
mutableprivate |
Definition at line 144 of file tpl_mo_algorithm.H.
Referenced by Aleph::Gen_Mo_Algorithm< T, Policy >::solve(), and Aleph::Gen_Mo_Algorithm< T, Policy >::swap().