DGtal
0.6.devel
|
#include <LightSternBrocot.h>
Data Structures | |
class | Fraction |
This fraction is a model of CPositiveIrreducibleFraction. More... | |
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 ((CInteger< Integer >)) | |
BOOST_CONCEPT_ASSERT ((CSignedInteger< Quotient >)) | |
~LightSternBrocot () | |
bool | isValid () const |
Static Public Member Functions | |
static LightSternBrocot & | instance () |
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) | |
LightSternBrocot & | operator= (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 | |
Node * | myZeroOverOne |
Node * | myOneOverZero |
Node * | myOneOverOne |
Static Private Attributes | |
static LightSternBrocot * | singleton = 0 |
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.
TInteger | the integral type chosen for the fractions. |
TQuotient | the integral type chosen for the quotients/coefficients or depth (may be "smaller" than TInteger, since they are generally much smaller than the fraction itself). |
TMap | the rebinder type for defining an association TQuotient -> LighterSternBrocot::Node*. For instance, StdMapRebinder is fine. |
Definition at line 108 of file LightSternBrocot.h.
typedef TInteger DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Integer |
Definition at line 111 of file LightSternBrocot.h.
typedef TMap DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Map |
Definition at line 113 of file LightSternBrocot.h.
typedef TMap:: template Rebinder<Quotient, Node*>::Type DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::MapQuotientToNode |
Definition at line 119 of file LightSternBrocot.h.
typedef TQuotient DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Quotient |
Definition at line 112 of file LightSternBrocot.h.
typedef LightSternBrocot<TInteger,TQuotient,TMap> DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Self |
Definition at line 114 of file LightSternBrocot.h.
|
inline |
Destructor.
Definition at line 631 of file LightSternBrocot.ih.
References DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::myOneOverOne, DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::myOneOverZero, and DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::myZeroOverOne.
|
inlineprivate |
Constructor. Hidden since singleton class.
Definition at line 640 of file LightSternBrocot.ih.
References DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Node::ascendant, DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Node::descendant, DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Node::descendant2, DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::myOneOverOne, DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::myOneOverZero, DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::myZeroOverOne, and DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::nbFractions.
Referenced by DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::instance().
|
private |
Copy constructor.
other | the object to clone. Forbidden by default. |
DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::BOOST_CONCEPT_ASSERT | ( | (CInteger< Integer >) | ) |
DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::BOOST_CONCEPT_ASSERT | ( | (CSignedInteger< Quotient >) | ) |
|
inlinestatic |
Writes/Displays the fraction on an output stream.
out | the output stream where the object is written. |
f | the fraction to display. |
Writes/Displays the object on an output stream.
out | the output stream where the object is written. |
Definition at line 724 of file LightSternBrocot.ih.
References DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::getCFrac(), DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::k(), DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::null(), DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::p(), DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::q(), and DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::u().
|
inlinestatic |
Creates the fraction p/q.
p | the numerator (>=0) |
q | the denominator (>=0) |
ancestor | (optional) unused in this representation. |
NB: Complexity is bounded by n where n is the depth of the continued fraction of p/q.
Definition at line 768 of file LightSternBrocot.ih.
|
inlinestatic |
Definition at line 690 of file LightSternBrocot.ih.
References DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::LightSternBrocot(), and DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::singleton().
Referenced by DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::k(), DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::next(), DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::next1(), DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::oneOverZero(), and DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::zeroOverOne().
|
inline |
Checks the validity/consistency of the object.
Definition at line 756 of file LightSternBrocot.ih.
|
inlinestatic |
The fraction 1/0
Definition at line 709 of file LightSternBrocot.ih.
References DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::instance(), and DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::myZeroOverOne.
Referenced by DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::Fraction().
|
private |
Assignment.
other | the object to copy. |
Referenced by DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::Fraction().
|
private |
Definition at line 57 of file LightSternBrocot.cpp.
Referenced by DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::instance().
|
private |
Definition at line 61 of file LightSternBrocot.cpp.
|
private |
Definition at line 65 of file LightSternBrocot.cpp.
|
inlinestatic |
The fraction 0/1
Definition at line 701 of file LightSternBrocot.ih.
References DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::instance(), and DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::myZeroOverOne.
Referenced by DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::Fraction().
|
private |
|
private |
|
private |
Definition at line 530 of file LightSternBrocot.h.
Referenced by DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::k(), DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::LightSternBrocot(), DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::next(), DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::oneOverZero(), DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::zeroOverOne(), and DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::~LightSternBrocot().
Quotient DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::nbFractions |
The total number of fractions in the current tree.
Definition at line 516 of file LightSternBrocot.h.
Referenced by DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::LightSternBrocot(), DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::next(), and DGtal::LightSternBrocot< TInteger, TQuotient, TMap >::Fraction::next1().
|
staticprivate |
Singleton class.
Definition at line 524 of file LightSternBrocot.h.