DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
LightSternBrocot.h
1 
17 #pragma once
18 
33 #if defined(LightSternBrocot_RECURSES)
34 #error Recursive header files inclusion detected in LightSternBrocot.h
35 #else // defined(LightSternBrocot_RECURSES)
36 
37 #define LightSternBrocot_RECURSES
38 
39 #if !defined LightSternBrocot_h
40 
41 #define LightSternBrocot_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/CSignedInteger.h"
52 #include "DGtal/kernel/NumberTraits.h"
54 
55 namespace DGtal
56 {
57 
59  // template class LightSternBrocot
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 
118 
119  struct Node;
120  typedef typename TMap:: template Rebinder<Quotient, Node*>::Type MapQuotientToNode;
121 
122  public:
123 
137  struct Node {
138 
149  Node( Integer p1, Integer q1, Quotient u1, Quotient k1,
150  Node* ascendant );
151 
170 
172  inline bool even() const {
174  }
176  inline bool odd() const {
177  return NumberTraits<Quotient>::odd( k );
178  }
180  inline bool isSameDepthLeft() const {
181  return odd();
182  }
183 
184  };
185 
195  class Fraction {
196  public:
197  typedef TInteger Integer;
198  typedef TQuotient Quotient;
200  typedef typename SternBrocotTree::Fraction Self;
202  typedef std::pair<Quotient, Quotient> Value;
203  typedef std::vector<Quotient> CFracSequence;
205 
206  // --------------------- std types ------------------------------
207  typedef Value value_type;
209  typedef const value_type & const_reference;
210 
211 
212  private:
217  bool mySup1;
218 
219  public:
229  Fraction( Integer aP, Integer aQ,
231 
239  Fraction( Node* sb_node = 0, bool sup1 = false );
240 
245  Fraction( const Self & other );
246 
252  Self& operator=( const Self & other );
253 
255  bool null() const;
257  Integer p() const;
259  Integer q() const;
261  Quotient u() const;
263  Quotient k() const;
264 
267  bool isSup1() const { return mySup1; }
269  Quotient trueK() const { return myNode->k; }
270 
271  protected:
274  Fraction next( Quotient v ) const;
277  Fraction next1( Quotient v ) const;
278  public:
279 
281  Fraction left() const;
283  Fraction right() const;
285  bool even() const;
287  bool odd() const;
293  Fraction ancestor() const;
297  bool isAncestorDirect() const;
298 
303  Fraction father() const;
304 
312  Fraction father( Quotient m ) const;
318  Fraction previousPartial() const;
323  Fraction inverse() const;
330  Fraction partial( Quotient kp ) const;
337  Fraction reduced( Quotient i ) const;
338 
355  void push_back( const std::pair<Quotient, Quotient> & quotient );
356 
367  void pushBack( const std::pair<Quotient, Quotient> & quotient );
368 
376  void getSplit( Fraction & f1, Fraction & f2 ) const;
377 
389  void getSplitBerstel( Fraction & f1, Quotient & nb1,
390  Fraction & f2, Quotient & nb2 ) const;
391 
396  void getCFrac( std::vector<Quotient> & quotients ) const;
397 
403  bool equals( Integer p1, Integer q1 ) const;
404 
410  bool lessThan( Integer p1, Integer q1 ) const;
411 
417  bool moreThan( Integer p1, Integer q1 ) const;
418 
423  bool operator==( const Fraction & other ) const;
424 
429  bool operator!=( const Fraction & other ) const;
430 
435  bool operator<( const Fraction & other ) const;
436 
441  bool operator>( const Fraction & other ) const;
442 
447  void selfDisplay ( std::ostream & out ) const;
448 
453  ConstIterator begin() const;
454 
459  ConstIterator end() const;
460 
461  };
462 
463 
464 
465  // ----------------------- Standard services ------------------------------
466  public:
471 
475  static LightSternBrocot & instance();
476 
478  static Fraction zeroOverOne();
479 
481  static Fraction oneOverZero();
482 
496  static Fraction fraction( Integer p, Integer q,
497  Fraction ancestor = zeroOverOne() );
498 
499  // ----------------------- Interface --------------------------------------
500  public:
501 
507  static void display ( std::ostream & out, const Fraction & f );
508 
513  bool isValid() const;
514 
517 
518  // ------------------------- Protected Datas ------------------------------
519  protected:
520  // ------------------------- Private Datas --------------------------------
521  private:
522 
525 
526 
527  // ------------------------- Datas ----------------------------------------
528  private:
529 
533 
534  // ------------------------- Hidden services ------------------------------
535  protected:
536 
537  private:
542 
543 
549  LightSternBrocot ( const LightSternBrocot & other );
550 
557  LightSternBrocot & operator= ( const LightSternBrocot & other );
558 
559  // ------------------------- Internals ------------------------------------
560  private:
561 
562  }; // end of class LightSternBrocot
563 
564 
571  // template <typename TInteger, typename TQuotient, typename TMap>
572  // std::ostream&
573  // operator<< ( std::ostream & out,
574  // const typename LightSternBrocot<TInteger, TQuotient, TMap>::Fraction & object );
575 
576 } // namespace DGtal
577 
578 
580 // Includes inline functions.
581 #include "DGtal/arithmetic/LightSternBrocot.ih"
582 
583 // //
585 
586 #endif // !defined LightSternBrocot_h
587 
588 #undef LightSternBrocot_RECURSES
589 #endif // else defined(LightSternBrocot_RECURSES)