ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BinaryCounter.h

The binary counter of \( k \) bits of the amortized analysis. More...

Files

file  ADS/algorithm/BinaryCounter.h
 The binary counter of \( k \) bits of the amortized analysis: \( A[ 0 ] \) is the least significant bit, and add() increments the counter modulo \( 2^k \).

Functions

std::size_t ADS::add (std::vector< int > &A)
 the increment of the counter \( A \) of \( k \) bits modulo \( 2^k \), returning the number of bits it has changed, i.e., its actual cost

Detailed description

The binary counter of \( k \) bits of the amortized analysis.

The definitions, the invariants and the costs of the area are in Theory of other algorithms.

Function documentation

◆ add()

std::size_t ADS::add ( std::vector< int > & A)
inline

the increment of the counter \( A \) of \( k \) bits modulo \( 2^k \), returning the number of bits it has changed, i.e., its actual cost

In pseudocode (section 4.2.1), the carry running over the ones:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{add}( A[ 0 .. k - 1 ] )\quad \triangleright\ A[ 0 ] \text{ the lowest bit; returns the bits changed} \cr 2 & \quad i \gets 0 \cr 3 & \quad \textbf{while}\ i < k\ \textbf{and}\ A[ i ] = 1\ \textbf{do} \ A[ i ] \gets 0;\ i \gets i + 1\quad \triangleright \ \text{the carry moves up} \cr 4 & \quad \textbf{if}\ i = k\ \textbf{then return}\ k\quad \triangleright\ \text{an overflow: all zeros} \cr 5 & \quad A[ i ] \gets 1;\ \textbf{return}\ i + 1 \end{array}} \]

Precondition
the items of \( A \) are bits (audit)
Postcondition
at most \( k \) bits changed