DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Data Structures | Public Types | Public Member Functions | Static Public Member Functions | Data Fields | Private Member Functions | Private Attributes | Static Private Attributes
DGtal::SternBrocot< TInteger, TQuotient > Class Template Reference

#include <SternBrocot.h>

Collaboration diagram for DGtal::SternBrocot< TInteger, TQuotient >:
Collaboration graph
[legend]

Data Structures

class  Fraction
 This fraction is a model of CPositiveIrreducibleFraction. More...
struct  Node

Public Types

typedef TInteger Integer
typedef TQuotient Quotient
typedef SternBrocot< Integer,
Quotient
Self

Public Member Functions

 BOOST_CONCEPT_ASSERT ((CInteger< Integer >))
 BOOST_CONCEPT_ASSERT ((CSignedInteger< Quotient >))
 ~SternBrocot ()
bool isValid () const

Static Public Member Functions

static SternBrocotinstance ()
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

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

Private Attributes

NodemyZeroOverOne
NodemyOneOverZero
NodemyOneOverOne

Static Private Attributes

static SternBrocotsingleton = 0

Detailed Description

template<typename TInteger, typename TQuotient = int32_t>
class DGtal::SternBrocot< TInteger, TQuotient >

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 'SternBrocot'

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

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).

Definition at line 78 of file SternBrocot.h.


Member Typedef Documentation

template<typename TInteger, typename TQuotient = int32_t>
typedef TInteger DGtal::SternBrocot< TInteger, TQuotient >::Integer

Definition at line 81 of file SternBrocot.h.

template<typename TInteger, typename TQuotient = int32_t>
typedef TQuotient DGtal::SternBrocot< TInteger, TQuotient >::Quotient

Definition at line 82 of file SternBrocot.h.

template<typename TInteger, typename TQuotient = int32_t>
typedef SternBrocot<Integer,Quotient> DGtal::SternBrocot< TInteger, TQuotient >::Self

Definition at line 83 of file SternBrocot.h.


Constructor & Destructor Documentation

template<typename TInteger , typename TQuotient >
DGtal::SternBrocot< TInteger, TQuotient >::~SternBrocot ( )
inline
template<typename TInteger , typename TQuotient >
DGtal::SternBrocot< TInteger, TQuotient >::SternBrocot ( )
inlineprivate

Constructor. Hidden since singleton class.

Definition at line 542 of file SternBrocot.ih.

References DGtal::SternBrocot< TInteger, TQuotient >::Node::ascendantLeft, DGtal::SternBrocot< TInteger, TQuotient >::Node::descendantLeft, DGtal::SternBrocot< TInteger, TQuotient >::Node::descendantRight, DGtal::SternBrocot< TInteger, TQuotient >::Node::inverse, DGtal::SternBrocot< TInteger, TQuotient >::myOneOverOne, DGtal::SternBrocot< TInteger, TQuotient >::myOneOverZero, DGtal::SternBrocot< TInteger, TQuotient >::myZeroOverOne, and DGtal::SternBrocot< TInteger, TQuotient >::nbFractions.

Referenced by DGtal::SternBrocot< TInteger, TQuotient >::instance().

template<typename TInteger, typename TQuotient = int32_t>
DGtal::SternBrocot< TInteger, TQuotient >::SternBrocot ( const SternBrocot< TInteger, TQuotient > &  other)
private

Copy constructor.

Parameters:
otherthe object to clone. Forbidden by default.

Member Function Documentation

template<typename TInteger, typename TQuotient = int32_t>
DGtal::SternBrocot< TInteger, TQuotient >::BOOST_CONCEPT_ASSERT ( (CInteger< Integer >)  )
template<typename TInteger, typename TQuotient = int32_t>
DGtal::SternBrocot< TInteger, TQuotient >::BOOST_CONCEPT_ASSERT ( (CSignedInteger< Quotient >)  )
template<typename TInteger , typename TQuotient >
void DGtal::SternBrocot< TInteger, TQuotient >::display ( std::ostream &  out,
const Fraction f 
)
inlinestatic

Writes/Displays the fraction on an output stream.

Parameters:
outthe output stream where the object is written.
fthe fraction to display.

Writes/Displays the object on an output stream.

Parameters:
outthe output stream where the object is written.

Definition at line 609 of file SternBrocot.ih.

References DGtal::SternBrocot< TInteger, TQuotient >::Fraction::getCFrac(), DGtal::SternBrocot< TInteger, TQuotient >::Fraction::k(), DGtal::SternBrocot< TInteger, TQuotient >::Fraction::null(), DGtal::SternBrocot< TInteger, TQuotient >::Fraction::p(), DGtal::SternBrocot< TInteger, TQuotient >::Fraction::q(), and DGtal::SternBrocot< TInteger, TQuotient >::Fraction::u().

{
if ( f.null() ) out << "[Fraction null]";
else
{
out << "[Fraction f=" << f.p()
<< "/" << f.q()
<< " u=" << f.u()
<< " k=" << f.k()
<< std::flush;
std::vector<Quotient> quotients;
if ( f.k() >= 0 )
{
f.getCFrac( quotients );
out << " [" << quotients[ 0 ];
for ( unsigned int i = 1; i < quotients.size(); ++i )
out << "," << quotients[ i ];
out << "]";
}
out << " ]";
}
}
template<typename TInteger , typename TQuotient >
DGtal::SternBrocot< TInteger, TQuotient >::Fraction DGtal::SternBrocot< TInteger, TQuotient >::fraction ( Integer  p,
Integer  q,
Fraction  ancestor = zeroOverOne() 
)
inlinestatic

Any fraction p/q. Complexity is in \( \sum_i u_i \), where u_i are the partial quotients of p/q.

Parameters:
pthe numerator (>=0)
qthe denominator (>=0)
ancestor(optional) any ancestor of p/q in the tree (for speed-up).
Returns:
the corresponding fraction in the Stern-Brocot tree.

NB: Complexity is bounded by \( 2 \sum_i u_i \), where u_i are the partial quotients of p/q.

Definition at line 652 of file SternBrocot.ih.

References DGtal::SternBrocot< TInteger, TQuotient >::Fraction::equals(), DGtal::IntegerComputer< TInteger >::gcd(), DGtal::SternBrocot< TInteger, TQuotient >::Fraction::left(), DGtal::SternBrocot< TInteger, TQuotient >::Fraction::lessThan(), DGtal::SternBrocot< TInteger, TQuotient >::oneOverZero(), DGtal::SternBrocot< TInteger, TQuotient >::Fraction::p(), DGtal::SternBrocot< TInteger, TQuotient >::Fraction::q(), and DGtal::SternBrocot< TInteger, TQuotient >::Fraction::right().

{
Integer g = ic.gcd( p, q );
{
p /= g;
q /= g;
}
// special case 1/0
&& ( q == NumberTraits<Integer>::ZERO ) ) return oneOverZero();
// other positive fractions
while ( ! ancestor.equals( p, q ) )
{
ASSERT( ( p + q ) >= ( ancestor.p() + ancestor.q() )
&& "[ImaGene::SternBrocot::fraction] bad ancestor." );
ancestor = ancestor.lessThan( p, q )
? ancestor.right()
: ancestor.left();
}
return ancestor;
}
template<typename TInteger , typename TQuotient >
DGtal::SternBrocot< TInteger, TQuotient > & DGtal::SternBrocot< TInteger, TQuotient >::instance ( )
inlinestatic
template<typename TInteger , typename TQuotient >
bool DGtal::SternBrocot< TInteger, TQuotient >::isValid ( ) const
inline

Checks the validity/consistency of the object.

Returns:
'true' if the object is valid, 'false' otherwise.

Definition at line 640 of file SternBrocot.ih.

{
return true;
}
template<typename TInteger , typename TQuotient >
DGtal::SternBrocot< TInteger, TQuotient >::Fraction DGtal::SternBrocot< TInteger, TQuotient >::oneOverZero ( )
inlinestatic
template<typename TInteger, typename TQuotient = int32_t>
SternBrocot& DGtal::SternBrocot< TInteger, TQuotient >::operator= ( const SternBrocot< TInteger, TQuotient > &  other)
private

Assignment.

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

Referenced by DGtal::SternBrocot< TInteger, TQuotient >::Fraction::Fraction(), and DGtal::SternBrocot< TInteger, TQuotient >::Fraction::pushBack().

Definition at line 57 of file SternBrocot.cpp.

Referenced by DGtal::SternBrocot< TInteger, TQuotient >::instance().

Definition at line 61 of file SternBrocot.cpp.

Definition at line 65 of file SternBrocot.cpp.

template<typename TInteger , typename TQuotient >
DGtal::SternBrocot< TInteger, TQuotient >::Fraction DGtal::SternBrocot< TInteger, TQuotient >::zeroOverOne ( )
inlinestatic

Field Documentation

template<typename TInteger, typename TQuotient = int32_t>
Node* DGtal::SternBrocot< TInteger, TQuotient >::myOneOverOne
private
template<typename TInteger, typename TQuotient = int32_t>
Node* DGtal::SternBrocot< TInteger, TQuotient >::myOneOverZero
private
template<typename TInteger, typename TQuotient = int32_t>
Node* DGtal::SternBrocot< TInteger, TQuotient >::myZeroOverOne
private
template<typename TInteger, typename TQuotient = int32_t>
Quotient DGtal::SternBrocot< TInteger, TQuotient >::nbFractions

The total number of fractions in the current tree.

Definition at line 445 of file SternBrocot.h.

Referenced by DGtal::SternBrocot< TInteger, TQuotient >::Fraction::left(), and DGtal::SternBrocot< TInteger, TQuotient >::SternBrocot().

template<typename TInteger, typename TQuotient = int32_t>
DGtal::SternBrocot< TInteger, TQuotient > * DGtal::SternBrocot< TInteger, TQuotient >::singleton = 0
staticprivate

Singleton class.

Definition at line 452 of file SternBrocot.h.


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