DGtal  1.5.beta
LighterSternBrocot.h
1 
17 #pragma once
18 
33 #if defined(LighterSternBrocot_RECURSES)
34 #error Recursive header files inclusion detected in LighterSternBrocot.h
35 #else // defined(LighterSternBrocot_RECURSES)
37 #define LighterSternBrocot_RECURSES
38 
39 #if !defined LighterSternBrocot_h
41 #define LighterSternBrocot_h
42 
44 // Inclusions
45 #include <iostream>
46 #include <vector>
47 #include "DGtal/base/Common.h"
48 #include "DGtal/base/StdRebinders.h"
49 #include "DGtal/base/InputIteratorWithRankOnSequence.h"
50 #include "DGtal/kernel/CInteger.h"
51 #include "DGtal/kernel/NumberTraits.h"
53 
54 namespace DGtal
55 {
56 
58  // template class LighterSternBrocot
106  template <typename TInteger, typename TQuotient,
107  typename TMap = StdMapRebinder >
109  {
110  public:
111  typedef TInteger Integer;
112  typedef TQuotient Quotient;
113  typedef TMap Map;
115 
117 
118  struct Node;
119  typedef typename TMap:: template Rebinder<Quotient, Node*>::Type MapQuotientToNode;
120 
121  public:
122 
139  struct Node {
140 
151  Node* anOrigin );
152 
156 
161  Node* origin() const;
162 
169  Node* ancestor() const;
174  Node* father() const;
175 
190 
191 
193  inline bool even() const {
195  }
197  inline bool odd() const {
198  return NumberTraits<Quotient>::odd( k );
199  }
201  inline bool isSameDepthLeft() const {
202  return odd();
203  }
204 
205  };
206 
216  class Fraction {
217  public:
218  typedef TInteger Integer;
219  typedef TQuotient Quotient;
221  typedef typename SternBrocotTree::Fraction Self;
223  typedef std::pair<Quotient, Quotient> Value;
224  typedef std::vector<Quotient> CFracSequence;
226 
227  // --------------------- std types ------------------------------
228  typedef Value value_type;
230  typedef const value_type & const_reference;
231 
232  private:
237  bool mySup1;
238 
239  public:
251 
259  Fraction( Node* sb_node = 0, bool sup1 = false );
260 
265  Fraction( const Self & other );
266 
272  Self& operator=( const Self & other );
273 
275  bool null() const;
277  Integer p() const;
279  Integer q() const;
281  Quotient u() const;
283  Quotient k() const;
284 
287  bool isSup1() const { return mySup1; }
289  Quotient trueK() const { return myNode->k; }
290 
291  protected:
294  Fraction child( Quotient v ) const;
299  Fraction origin() const;
302  Fraction next( Quotient v ) const;
303 
304  public:
305 
307  Fraction left() const;
309  Fraction right() const;
311  bool even() const;
313  bool odd() const;
314 
325  bool isAncestorDirect() const;
330  Fraction father() const;
346  Fraction inverse() const;
353  Fraction partial( Quotient kp ) const;
361 
378  void push_back( const std::pair<Quotient, Quotient> & quotient );
379 
390  void pushBack( const std::pair<Quotient, Quotient> & quotient );
391 
399  void getSplit( Fraction & f1, Fraction & f2 ) const;
400 
412  void getSplitBerstel( Fraction & f1, Quotient & nb1,
413  Fraction & f2, Quotient & nb2 ) const;
414 
419  void getCFrac( std::vector<Quotient> & quotients ) const;
420 
426  bool equals( Integer p1, Integer q1 ) const;
427 
433  bool lessThan( Integer p1, Integer q1 ) const;
434 
440  bool moreThan( Integer p1, Integer q1 ) const;
441 
446  bool operator==( const Fraction & other ) const;
447 
452  bool operator!=( const Fraction & other ) const;
453 
458  bool operator<( const Fraction & other ) const;
459 
464  bool operator>( const Fraction & other ) const;
465 
470  void selfDisplay ( std::ostream & out ) const;
471 
477 
483 
484  };
485 
486 
487 
488  // ----------------------- Standard services ------------------------------
489  public:
494 
499 
502 
505 
508 
521  Fraction ancestor = oneOverZero() );
522 
523  // ----------------------- Interface --------------------------------------
524  public:
525 
531  static void display ( std::ostream & out, const Fraction & f );
532 
537  bool isValid() const;
538 
541 
542  // ------------------------- Protected Datas ------------------------------
543  protected:
544  // ------------------------- Private Datas --------------------------------
545  private:
546 
549 
552 
553  // ------------------------- Hidden services ------------------------------
554  protected:
555 
556  private:
557 
562 
569 
577 
578  // ------------------------- Internals ------------------------------------
579  private:
580 
581  }; // end of class LighterSternBrocot
582 
583 
590  // template <typename TInteger, typename TQuotient, typename TMap>
591  // std::ostream&
592  // operator<< ( std::ostream & out,
593  // const typename LighterSternBrocot<TInteger, TQuotient, TMap>::Fraction & object );
594 
595 } // namespace DGtal
596 
597 
599 // Includes inline functions.
600 #include "DGtal/arithmetic/LighterSternBrocot.ih"
601 
602 // //
604 
605 #endif // !defined LighterSternBrocot_h
606 
607 #undef LighterSternBrocot_RECURSES
608 #endif // else defined(LighterSternBrocot_RECURSES)
Aim: Useful to create an iterator that returns a pair (value,rank) when visiting a sequence....
This fraction is a model of CPositiveIrreducibleFraction.
void getSplit(Fraction &f1, Fraction &f2) const
LighterSternBrocot< TInteger, TQuotient, TMap > SternBrocotTree
Self & operator=(const Self &other)
bool operator==(const Fraction &other) const
bool mySup1
When 'true', the fraction is greater or equal than 1/1 (to its right).
bool operator!=(const Fraction &other) const
NumberTraits< Integer >::UnsignedVersion UnsignedInteger
Fraction partial(Quotient kp) const
bool operator<(const Fraction &other) const
Fraction father(Quotient m) const
Fraction child(Quotient v) const
Fraction next(Quotient v) const
void pushBack(const std::pair< Quotient, Quotient > &quotient)
void selfDisplay(std::ostream &out) const
std::pair< Quotient, Quotient > Value
Fraction(Integer aP, Integer aQ, Fraction start=SternBrocotTree::zeroOverOne())
bool lessThan(Integer p1, Integer q1) const
void getSplitBerstel(Fraction &f1, Quotient &nb1, Fraction &f2, Quotient &nb2) const
Fraction(Node *sb_node=0, bool sup1=false)
void getCFrac(std::vector< Quotient > &quotients) const
void push_back(const std::pair< Quotient, Quotient > &quotient)
bool moreThan(Integer p1, Integer q1) const
Fraction reduced(Quotient i) const
InputIteratorWithRankOnSequence< CFracSequence, Quotient > ConstIterator
bool equals(Integer p1, Integer q1) const
bool operator>(const Fraction &other) const
Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it pr...
Quotient nbFractions
The total number of fractions in the current tree.
static LighterSternBrocot & instance()
static Fraction fraction(Integer p, Integer q, Fraction ancestor=oneOverZero())
BOOST_CONCEPT_ASSERT((concepts::CInteger< Integer >))
static Fraction zeroOverOne()
static LighterSternBrocot * singleton
Singleton class.
static void display(std::ostream &out, const Fraction &f)
TMap::template Rebinder< Quotient, Node * >::Type MapQuotientToNode
static Fraction oneOverOne()
LighterSternBrocot(const LighterSternBrocot &other)
static Fraction oneOverZero()
LighterSternBrocot & operator=(const LighterSternBrocot &other)
LighterSternBrocot< TInteger, TQuotient, TMap > Self
DGtal is the top-level namespace which contains all DGtal functions and types.
Node(Integer p1, Integer q1, Quotient u1, Quotient k1, Node *anOrigin)
Node * myOrigin
A pointer to the origin node [u_0,...,u_{n-1},1].
Quotient u
the quotient (last coefficient of its continued fraction).
Quotient k
the depth (1+number of coefficients of its continued fraction).
std::decay< T >::type UnsignedVersion
Alias to the unsigned version of the number type.
Definition: NumberTraits.h:90
static bool even(ParamType aT)
Check the parity of a number.
Definition: NumberTraits.h:173
static bool odd(ParamType aT)
Check the parity of a number.
Definition: NumberTraits.h:183
Aim: Concept checking for Integer Numbers. More precisely, this concept is a refinement of both CEucl...
Definition: CInteger.h:88