Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::Convex_Hull_Trick< T > Class Template Reference

Convex Hull Trick for minimum queries. More...

#include <DP_Optimizations.H>

Collaboration diagram for Aleph::Convex_Hull_Trick< T >:
[legend]

Classes

struct  Line
 Affine line y = m*x + b. More...
 

Public Member Functions

 Convex_Hull_Trick ()=default
 Construct an empty hull.
 
 Convex_Hull_Trick (const Convex_Hull_Trick &other)=default
 Copy the lines and the query cursor of another hull.
 
 Convex_Hull_Trick (Convex_Hull_Trick &&other) noexcept
 Take the lines of another hull, which is left empty, as clear() leaves it.
 
Convex_Hull_Trick & operator= (const Convex_Hull_Trick &other)=default
 Copy the lines and the query cursor of another hull.
 
Convex_Hull_Trick & operator= (Convex_Hull_Trick &&other) noexcept
 Exchange this hull with another, which is left with the previous lines and cursor of this one.
 
void swap (Convex_Hull_Trick &other) noexcept
 Exchange in O(1) the lines and the cursors of this hull and another.
 
size_t size () const noexcept
 
bool is_empty () const noexcept
 
void clear ()
 
void reset_query_cursor () noexcept
 
void add_line (const T slope, const T intercept)
 Insert a new line; slopes must be non-increasing.
 
T query (const T x) const
 Query minimum value at arbitrary x (O(log n)).
 
T query_monotone (const T x)
 Query minimum with non-decreasing x (amortized O(1)).
 

Static Private Member Functions

static long double intersection_x (const Line &a, const Line &b) noexcept
 

Private Attributes

Array< Line > lines_
 
Array< long double > starts_
 
size_t cursor_ = 0
 

Detailed Description

template<typename T>
class Aleph::Convex_Hull_Trick< T >

Convex Hull Trick for minimum queries.

Supports insertion of lines with non-increasing slope and minimum queries over x. Query can be done in O(log n) for arbitrary x or amortized O(1) for non-decreasing x via query_monotone().

Template Parameters
TNumeric type for slopes and coordinates.
Examples
dp_optimizations_example.cc.

Definition at line 370 of file DP_Optimizations.H.

Constructor & Destructor Documentation

◆ Convex_Hull_Trick() [1/3]

template<typename T >
Aleph::Convex_Hull_Trick< T >::Convex_Hull_Trick ( )
default

Construct an empty hull.

◆ Convex_Hull_Trick() [2/3]

template<typename T >
Aleph::Convex_Hull_Trick< T >::Convex_Hull_Trick ( const Convex_Hull_Trick< T > &  other)
default

Copy the lines and the query cursor of another hull.

Parameters
otherhull to copy.

◆ Convex_Hull_Trick() [3/3]

template<typename T >
Aleph::Convex_Hull_Trick< T >::Convex_Hull_Trick ( Convex_Hull_Trick< T > &&  other)
inlinenoexcept

Take the lines of another hull, which is left empty, as clear() leaves it.

The implicit move took the lines but copied the cursor of the monotone queries.

Parameters
otherhull to move from.

Definition at line 417 of file DP_Optimizations.H.

Member Function Documentation

◆ add_line()

◆ clear()

◆ intersection_x()

template<typename T >
static long double Aleph::Convex_Hull_Trick< T >::intersection_x ( const Line &  a,
const Line &  b 
)
inlinestaticprivatenoexcept

Definition at line 394 of file DP_Optimizations.H.

Referenced by Aleph::Convex_Hull_Trick< T >::add_line().

◆ is_empty()

template<typename T >
bool Aleph::Convex_Hull_Trick< T >::is_empty ( ) const
inlinenoexcept

Definition at line 460 of file DP_Optimizations.H.

References Aleph::Convex_Hull_Trick< T >::lines_.

◆ operator=() [1/2]

template<typename T >
Convex_Hull_Trick & Aleph::Convex_Hull_Trick< T >::operator= ( const Convex_Hull_Trick< T > &  other)
default

Copy the lines and the query cursor of another hull.

Parameters
otherhull to copy.
Returns
this hull.

◆ operator=() [2/2]

template<typename T >
Convex_Hull_Trick & Aleph::Convex_Hull_Trick< T >::operator= ( Convex_Hull_Trick< T > &&  other)
inlinenoexcept

Exchange this hull with another, which is left with the previous lines and cursor of this one.

The implicit assignment exchanged the lines but not the cursors: query_monotone() of the source then read beyond the lines that it got.

Parameters
otherhull to move from.
Returns
this hull.

Definition at line 438 of file DP_Optimizations.H.

References Aleph::Convex_Hull_Trick< T >::swap().

◆ query()

template<typename T >
T Aleph::Convex_Hull_Trick< T >::query ( const T  x) const
inline

Query minimum value at arbitrary x (O(log n)).

Examples
dp_optimizations_example.cc.

Definition at line 531 of file DP_Optimizations.H.

References ah_runtime_error_if, Aleph::Convex_Hull_Trick< T >::lines_, and Aleph::Convex_Hull_Trick< T >::starts_.

Referenced by TEST(), and TEST().

◆ query_monotone()

template<typename T >
T Aleph::Convex_Hull_Trick< T >::query_monotone ( const T  x)
inline

Query minimum with non-decreasing x (amortized O(1)).

Definition at line 549 of file DP_Optimizations.H.

References ah_runtime_error_if, Aleph::and, Aleph::Convex_Hull_Trick< T >::cursor_, Aleph::Convex_Hull_Trick< T >::lines_, and Aleph::Convex_Hull_Trick< T >::starts_.

Referenced by TEST().

◆ reset_query_cursor()

template<typename T >
void Aleph::Convex_Hull_Trick< T >::reset_query_cursor ( )
inlinenoexcept

Definition at line 476 of file DP_Optimizations.H.

References Aleph::Convex_Hull_Trick< T >::cursor_.

Referenced by TEST().

◆ size()

template<typename T >
size_t Aleph::Convex_Hull_Trick< T >::size ( ) const
inlinenoexcept

Definition at line 455 of file DP_Optimizations.H.

References Aleph::Convex_Hull_Trick< T >::lines_.

Referenced by TEST().

◆ swap()

template<typename T >
void Aleph::Convex_Hull_Trick< T >::swap ( Convex_Hull_Trick< T > &  other)
inlinenoexcept

Exchange in O(1) the lines and the cursors of this hull and another.

Parameters
otherhull to exchange with.

Definition at line 448 of file DP_Optimizations.H.

References Aleph::Convex_Hull_Trick< T >::cursor_, Aleph::Convex_Hull_Trick< T >::lines_, Aleph::Convex_Hull_Trick< T >::starts_, and Aleph::Array< T >::swap().

Referenced by Aleph::Convex_Hull_Trick< T >::operator=().

Member Data Documentation

◆ cursor_

◆ lines_

◆ starts_


The documentation for this class was generated from the following file: