ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::BalancedParentheses Class Reference

a static sequence of balanced parentheses with a range min-max tree More...

#include <ADS/succinct/BalancedParentheses.h>

Public member functions

 BalancedParentheses (void)
 the empty sequence
 BalancedParentheses (BitVector b)
 the parentheses \( b \), 1 for an open one and 0 for a closed one, and the range min-max tree on them
std::size_t size (void) const
 the number of parentheses
bool operator[] (std::size_t i) const
 whether the place \( i \) has an open parenthesis
std::int64_t excess (std::size_t i) const
 \( E( i ) \), the open parentheses minus the closed ones in \( [ 0 , i ] \), as \( 2 \, \mathrm{rank}_1( i + 1 ) - ( i + 1 ) \)
std::size_t findClose (std::size_t i) const
 the closed parenthesis of the open one at \( i \)
std::size_t forwardSearch (std::size_t i, std::int64_t d) const
 the first \( j > i \) with \( E( j ) - E( i ) = d < 0 \), nil if none: the end of a run of parentheses that goes \( -d \) levels below the one at \( i \), e.g., of a subtree of the code by the degrees in depth [see SuccinctPatriciaTrie.h]
std::size_t findOpen (std::size_t j) const
 the open parenthesis of the closed one at \( j \)
std::size_t enclose (std::size_t i) const
 the open parenthesis of the pair that encloses the open one at \( i \), nil if none
const CompactRankSelect & bits (void) const
 the parentheses with their rank and select
std::size_t treeBits (void) const
 the bits of the range min-max tree
template<class A>
void persist (A &a)
 the members written to, or read from, the archive \( a \) [see Persistence.h]

Static public attributes

static constexpr std::size_t nil = std::size_t( -1 )
 no place
static constexpr std::string_view persistName = "BalancedParentheses"
 the name of the structure in a file [see Persistence.h]

Detailed description

a static sequence of balanced parentheses with a range min-max tree

Constructor and destructor documentation

◆ BalancedParentheses() [1/2]

ADS::BalancedParentheses::BalancedParentheses ( void )

the empty sequence

◆ BalancedParentheses() [2/2]

ADS::BalancedParentheses::BalancedParentheses ( BitVector b)
explicit

the parentheses \( b \), 1 for an open one and 0 for a closed one, and the range min-max tree on them

In pseudocode (the range min-max tree of Navarro and Sadakane, beyond the book), with \( \ell \) leaves of 512 bits, \( g \) the least power of 2 not below the groups of 16 leaves, and the node \( v \) of the tree with the children \( 2 v \) and \( 2 v + 1 \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{build}( b ) \cr 2 & \quad \textbf{for}\ l \gets 0\ \textbf{to}\ \ell - 1\ \textbf{do} \cr 3 & \qquad e \gets 0;\ \mu \gets 0 \cr 4 & \qquad \textbf{foreach}\ \text{byte } y \text{ of the leaf } l \ \textbf{do} \cr 5 & \qquad\quad \mu \gets \min\{ \mu , e + \mathit{min}_8[ y ] \};\ e \gets e + \mathit{exc}_8[ y ]\quad \triangleright\ \text{by tables} \cr 6 & \qquad \mathit{lexc}[ l ] \gets e;\ \mathit{lmin}[ l ] \gets \mu;\ v \gets g + \lfloor l / 16 \rfloor \cr 7 & \qquad \mathit{min}[ v ] \gets \min\{ \mathit{min}[ v ] , \mathit{exc}[ v ] + \mu \};\ \mathit{exc}[ v ] \gets \mathit{exc}[ v ] + e \cr 8 & \quad \textbf{for}\ v \gets g - 1\ \textbf{downto}\ 1\ \textbf{do} \cr 9 & \qquad \mathit{exc}[ v ] \gets \mathit{exc}[ 2 v ] + \mathit{exc}[ 2 v + 1 ] \cr 10 & \qquad \mathit{min}[ v ] \gets \min\{ \mathit{min}[ 2 v ] , \mathit{exc}[ 2 v ] + \mathit{min}[ 2 v + 1 ] \} \end{array}} \]

Precondition
\( b \) is balanced (an audit)

Member function documentation

◆ bits()

const CompactRankSelect & ADS::BalancedParentheses::bits ( void ) const
nodiscard

the parentheses with their rank and select

◆ enclose()

std::size_t ADS::BalancedParentheses::enclose ( std::size_t i) const
nodiscard

the open parenthesis of the pair that encloses the open one at \( i \), nil if none

In pseudocode (Navarro and Sadakane, beyond the book), by the backward search of findOpen() with the target 2 levels below:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{enclose}( i ) \cr 2 & \quad \textbf{if}\ \text{no } k < i \text{ has } E( k ) - E( i ) \le -2\ \textbf{then return nil}\quad \triangleright\ \text{the outer } \text{level} \cr 3 & \quad \textbf{return}\ k + 1 \text{ for the last such } k \end{array}} \]

Precondition
the place \( i \) has an open parenthesis

◆ excess()

std::int64_t ADS::BalancedParentheses::excess ( std::size_t i) const
nodiscard

\( E( i ) \), the open parentheses minus the closed ones in \( [ 0 , i ] \), as \( 2 \, \mathrm{rank}_1( i + 1 ) - ( i + 1 ) \)

Precondition
\( i < \mathrm{size}() \)

◆ findClose()

std::size_t ADS::BalancedParentheses::findClose ( std::size_t i) const
nodiscard

the closed parenthesis of the open one at \( i \)

In pseudocode (the forward search of Navarro and Sadakane, beyond the book), the first \( j > i \) with \( E( j ) - E( i ) \le d = -1 \), \( e \) the excess relative to \( E( i ) \) and each range skipped whole when its minimum stays above \( d \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{findClose}( i ) \cr 2 & \quad d \gets -1;\ e \gets 0;\ l \gets \text{the leaf of } i \cr 3 & \quad \text{scan the leaf } l \text{ from } i + 1 \text{, a byte } \text{at a time;}\ \textbf{if}\ \text{some } j \text{ has } e \le d \ \textbf{then return}\ j \cr 4 & \quad \textbf{foreach}\ \text{next leaf } l \text{ of its group} \ \textbf{do} \cr 5 & \qquad \textbf{if}\ e + \mathit{lmin}[ l ] \le d \ \textbf{then return}\ \text{the place found by the scan of } l \cr 6 & \qquad e \gets e + \mathit{lexc}[ l ] \cr 7 & \quad v \gets \text{the leaf of the tree for the group} \cr 8 & \quad \textbf{while}\ v > 1\ \textbf{and not}\ ( v \text{ is a left } \text{child}\ \textbf{and}\ e + \mathit{min}[ v + 1 ] \le d ) \ \textbf{do} \cr 9 & \qquad \textbf{if}\ v \text{ is a left child }\ \textbf{then}\ e \gets e + \mathit{exc}[ v + 1 ] \cr 10 & \qquad v \gets \lfloor v / 2 \rfloor \cr 11 & \quad \textbf{if}\ v = 1\ \textbf{then return nil} \cr 12 & \quad v \gets v + 1\quad \triangleright\ \text{the right sibling} \text{ that reaches } d \cr 13 & \quad \textbf{while}\ v \text{ is not a leaf }\ \textbf{do}\quad \triangleright\ \text{down to the leftmost range that reaches } d \cr 14 & \qquad \textbf{if}\ e + \mathit{min}[ 2 v ] \le d\ \textbf{then}\ v \gets 2 v\ \textbf{else}\ e \gets e + \mathit{exc}[ 2 v ];\ v \gets 2 v + 1 \cr 15 & \quad l \gets \text{the first leaf of the group } v \text{ with } e + \mathit{lmin}[ l ] \le d \text{, adding the } \mathit{lexc} \text{ of those before} \cr 16 & \quad \textbf{return}\ \text{the place found by the scan of } l \end{array}} \]

Precondition
the place \( i \) has an open parenthesis
Postcondition
the first place after \( i \) with the excess \( E( i ) - 1 \)

◆ findOpen()

std::size_t ADS::BalancedParentheses::findOpen ( std::size_t j) const
nodiscard

the open parenthesis of the closed one at \( j \)

In pseudocode (the backward search of Navarro and Sadakane, beyond the book), the mirror of the forward search of findClose(), on the ranges to the left and with the excess taken off:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{findOpen}( j ) \cr 2 & \quad k \gets \text{the last } k < j \text{ with } E( k ) - E( j ) \le 0 \text{, } E( -1 ) = 0 \text{, by the leaf of } j \text{, its} \text{ group and the tree} \cr 3 & \quad \textbf{return}\ k + 1 \end{array}} \]

Precondition
the place \( j \) has a closed parenthesis
Postcondition
findClose() of it is \( j \)

◆ forwardSearch()

std::size_t ADS::BalancedParentheses::forwardSearch ( std::size_t i,
std::int64_t d ) const
nodiscard

the first \( j > i \) with \( E( j ) - E( i ) = d < 0 \), nil if none: the end of a run of parentheses that goes \( -d \) levels below the one at \( i \), e.g., of a subtree of the code by the degrees in depth [see SuccinctPatriciaTrie.h]

In pseudocode (the forward search of Navarro and Sadakane, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{forwardSearch}( i , d ) \cr 2 & \quad \textbf{return}\ \text{the place found by } \mathrm{findClose}( i ) \text{ with the target } d \text{ for } -1 \end{array}} \]

Precondition
\( i < \mathrm{size}() \) and \( d < 0 \)

◆ operator[]()

bool ADS::BalancedParentheses::operator[] ( std::size_t i) const
nodiscard

whether the place \( i \) has an open parenthesis

Precondition
\( i < \mathrm{size}() \)

◆ persist()

template<class A>
void ADS::BalancedParentheses::persist ( A & a)

the members written to, or read from, the archive \( a \) [see Persistence.h]

◆ size()

std::size_t ADS::BalancedParentheses::size ( void ) const
nodiscard

the number of parentheses

◆ treeBits()

std::size_t ADS::BalancedParentheses::treeBits ( void ) const
nodiscard

the bits of the range min-max tree

Member data documentation

◆ nil

std::size_t ADS::BalancedParentheses::nil = std::size_t( -1 )
staticconstexpr

no place

◆ persistName

std::string_view ADS::BalancedParentheses::persistName = "BalancedParentheses"
staticconstexpr

the name of the structure in a file [see Persistence.h]


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