DGtal  0.9.4beta
Data Structures | Public Types | Public Member Functions | Static Public Member Functions | Data Fields | Private Member Functions | Private Attributes | Static Private Attributes
DGtal::LightSternBrocot< TInteger, TQuotient, TMap > Class Template Reference

#include <DGtal/arithmetic/LightSternBrocot.h>

Collaboration diagram for DGtal::LightSternBrocot< TInteger, TQuotient, TMap >:
[legend]

Data Structures

class  Fraction
 
struct  Node
 

Public Types

typedef TInteger Integer
 
typedef TQuotient Quotient
 
typedef TMap Map
 
typedef LightSternBrocot< TInteger, TQuotient, TMap > Self
 
typedef TMap::template Rebinder< Quotient, Node * >::Type MapQuotientToNode
 

Public Member Functions

 BOOST_CONCEPT_ASSERT ((concepts::CInteger< Integer >))
 
 ~LightSternBrocot ()
 
bool isValid () const
 

Static Public Member Functions

static LightSternBrocotinstance ()
 
static Fraction zeroOverOne ()
 
static Fraction oneOverZero ()
 
static Fraction fraction (Integer p, Integer q, Fraction ancestor=zeroOverOne())
 
static void display (std::ostream &out, const Fraction &f)
 

Data Fields

Quotient nbFractions
 

Private Member Functions

 LightSternBrocot ()
 
 LightSternBrocot (const LightSternBrocot &other)
 
LightSternBrocotoperator= (const LightSternBrocot &other)
 
template<>
LightSternBrocot< DGtal::int32_t, DGtal::int32_t > * singleton
 
template<>
LightSternBrocot< DGtal::int64_t, DGtal::int32_t > * singleton
 
template<>
LightSternBrocot< DGtal::int64_t, DGtal::int64_t > * singleton
 

Private Attributes

NodemyZeroOverOne
 
NodemyOneOverZero
 
NodemyOneOverOne
 

Static Private Attributes

static LightSternBrocotsingleton = 0
 

Detailed Description

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
class DGtal::LightSternBrocot< TInteger, TQuotient, TMap >

Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it progressively and to navigate within fractions in O(1) time for most operations. It is well known that the structure of this tree is a coding of the continued fraction representation of fractions.

Description of template class 'LightSternBrocot'

There are two main differences with the class SternBrocot. The first one is that inverses are not stored. With this optimization, there are twice less nodes and each node is lighter. The second one lies in the access to the children of a node. Here, a map type M is provided so that a node [u_0; u_1, ..., u_n] can access its child node [u_0; u_1, ..., u_n, k] in the time of the operation M::operator[].

In this representation, the fraction 1/1 has depth 1, like 1/2, 1/3, etc. Furthermore, each fraction has an ancestor, which is the reduced partial of order 1 of the fraction. Be careful the ancestor of an ancestor is not the reduced of order 2. Each node [u_0; u_1, ..., u_n] has two sets of children: the nodes [u_0; u_1, ..., u_n, k], for k >= 2, and the nodes [u_0; u_1, ..., u_n - 1, 1, k], for k >= 2. A disadvantage of this representation is that to obtain the father of something like [...,u_k, 1, ..., 1, u_n ], one has to go up the tree till u_k, to go back down on the other side.

In practice, although this class has supposedly a better complexity than SternBrocot, it is 1% slower for integers smaller than 10^9 and 5% slower for integers smaller than 10^4. Note however that it takes like 6 times less memory (and asymptotically less when the number of computations tends toward infinity).

This class is not to be instantiated, since it is useless to duplicate it. Use static method LightSternBrocot::fraction to obtain your fractions.

Template Parameters
TIntegerthe integral type chosen for the fractions.
TQuotientthe integral type chosen for the quotients/coefficients or depth (may be "smaller" than TInteger, since they are generally much smaller than the fraction itself).
TMapthe rebinder type for defining an association TQuotient -> LighterSternBrocot::Node*. For instance, StdMapRebinder is fine.

Definition at line 107 of file LightSternBrocot.h.

Member Typedef Documentation

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
typedef TInteger DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Integer

Definition at line 110 of file LightSternBrocot.h.

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
typedef TMap DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Map

Definition at line 112 of file LightSternBrocot.h.

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
typedef TMap:: template Rebinder<Quotient, Node*>::Type DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::MapQuotientToNode

Definition at line 117 of file LightSternBrocot.h.

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
typedef TQuotient DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Quotient

Definition at line 111 of file LightSternBrocot.h.

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
typedef LightSternBrocot<TInteger,TQuotient,TMap> DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Self

Definition at line 113 of file LightSternBrocot.h.

Constructor & Destructor Documentation

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::~LightSternBrocot ( )

Destructor.

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::LightSternBrocot ( )
private

Constructor. Hidden since singleton class.

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::LightSternBrocot ( const LightSternBrocot< TInteger, TQuotient, TMap > &  other)
private

Copy constructor.

Parameters
otherthe object to clone. Forbidden by default.

Member Function Documentation

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::BOOST_CONCEPT_ASSERT ( (concepts::CInteger< Integer >)  )
template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
static void DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::display ( std::ostream &  out,
const Fraction f 
)
static

Writes/Displays the fraction on an output stream.

Parameters
outthe output stream where the object is written.
fthe fraction to display.
template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
static Fraction DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::fraction ( Integer  p,
Integer  q,
Fraction  ancestor = zeroOverOne() 
)
static

Creates the fraction p/q.

Parameters
pthe numerator (>=0)
qthe denominator (>=0)
ancestor(optional) unused in this representation.
Returns
the corresponding fraction in the Stern-Brocot tree.

NB: Complexity is bounded by n where n is the depth of the continued fraction of p/q.

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
static LightSternBrocot& DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::instance ( )
static
Returns
the (only) instance of LightSternBrocot.
template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
bool DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::isValid ( ) const

Checks the validity/consistency of the object.

Returns
'true' if the object is valid, 'false' otherwise.
template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
static Fraction DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::oneOverZero ( )
static

The fraction 1/0

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
LightSternBrocot& DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::operator= ( const LightSternBrocot< TInteger, TQuotient, TMap > &  other)
private

Assignment.

Parameters
otherthe object to copy.
Returns
a reference on 'this'. Forbidden by default.

Definition at line 57 of file LightSternBrocot.cpp.

Definition at line 61 of file LightSternBrocot.cpp.

Definition at line 65 of file LightSternBrocot.cpp.

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
static Fraction DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::zeroOverOne ( )
static

The fraction 0/1

Field Documentation

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
Node* DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::myOneOverOne
private

Definition at line 530 of file LightSternBrocot.h.

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
Node* DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::myOneOverZero
private

Definition at line 529 of file LightSternBrocot.h.

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
Node* DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::myZeroOverOne
private

Definition at line 528 of file LightSternBrocot.h.

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
Quotient DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::nbFractions

The total number of fractions in the current tree.

Definition at line 514 of file LightSternBrocot.h.

template<typename TInteger, typename TQuotient, typename TMap = StdMapRebinder>
DGtal::LightSternBrocot< TInteger, TQuotient, TMap > * DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::singleton = 0
staticprivate

Singleton class.

Definition at line 522 of file LightSternBrocot.h.


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