|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Disjoint Sparse Table applied to real-world scenarios. More...
#include <cstdio>#include <iostream>#include <iomanip>#include <numeric>#include <cassert>#include <tpl_disjoint_sparse_table.H>Go to the source code of this file.
Classes | |
| struct | Xor_Op |
Functions | |
| void | scenario_sales_analytics () |
| void | scenario_probability () |
| void | scenario_xor_checksum () |
| void | scenario_construction () |
| void | scenario_parlay_betting () |
| int | main () |
Disjoint Sparse Table applied to real-world scenarios.
This example demonstrates the Disjoint Sparse Table variants through realistic scenarios, highlighting the key advantage over the classical Sparse Table: support for non-idempotent associative operations like addition, multiplication, and XOR.
A retail chain records daily revenue for each store across a quarter. After the quarter ends, the finance team needs to answer many queries of the form "what was the total revenue between day l and day r?" Because sum is associative but NOT idempotent (sum(a, a) = 2a != a), a classical Sparse Table cannot handle this. The Disjoint Sparse Table answers each query in O(1).
A reliability engineer models a series system of n components. The probability that a subsystem spanning components [l, r] works is the product of individual reliabilities. Multiplication is associative but not idempotent, making the Disjoint Sparse Table ideal.
A data integrity tool needs range-XOR queries on a block of data. XOR is associative and forms an abelian group. This scenario illustrates using a custom functor with the generic table.
A sportsbook offers a card of 12 consecutive match-ups. A bettor can build a "parlay" (accumulator) by picking any contiguous run of matches; the combined payout multiplier is the product of the individual decimal odds. Product is associative but NOT idempotent (odds * odds = odds² ≠ odds), so a classical Sparse Table cannot handle this, but the Disjoint Sparse Table answers every parlay combination in O(1).
Shows construction from Array, vector, DynList, and initializer_list.
g++ -std=c++20 -I.. -o disjoint_sparse_table_example \ disjoint_sparse_table_example.cc ./disjoint_sparse_table_example
Definition in file disjoint_sparse_table_example.cc.
| int main | ( | ) |
Definition at line 444 of file disjoint_sparse_table_example.cc.
References scenario_construction(), scenario_parlay_betting(), scenario_probability(), scenario_sales_analytics(), and scenario_xor_checksum().
| void scenario_construction | ( | ) |
Definition at line 286 of file disjoint_sparse_table_example.cc.
References Aleph::Array< T >::append(), Aleph::DynList< T >::append(), l, Aleph::maps(), and Aleph::HTList::size().
Referenced by main().
| void scenario_parlay_betting | ( | ) |
Definition at line 348 of file disjoint_sparse_table_example.cc.
References abs(), l, Aleph::maps(), N, and Aleph::HTList::size().
Referenced by main().
| void scenario_probability | ( | ) |
Definition at line 154 of file disjoint_sparse_table_example.cc.
References abs(), l, Aleph::maps(), and Aleph::HTList::size().
Referenced by main().
| void scenario_sales_analytics | ( | ) |
Definition at line 98 of file disjoint_sparse_table_example.cc.
References l, Aleph::maps(), and Aleph::HTList::size().
Referenced by main().
| void scenario_xor_checksum | ( | ) |
Definition at line 222 of file disjoint_sparse_table_example.cc.
References l, Aleph::maps(), and Aleph::HTList::size().
Referenced by main().