DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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)
36 
37 #define LighterSternBrocot_RECURSES
38 
39 #if !defined LighterSternBrocot_h
40 
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/CSignedInteger.h"
52 #include "DGtal/kernel/NumberTraits.h"
54 
55 namespace DGtal
56 {
57 
59  // 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 
118 
119  struct Node;
120  typedef typename TMap:: template Rebinder<Quotient, Node*>::Type MapQuotientToNode;
121 
122  public:
123 
140  struct Node {
141 
151  Node( Integer p1, Integer q1, Quotient u1, Quotient k1,
152  Node* _origin );
153 
156  Node* child( Quotient v );
157 
162  Node* origin() const;
163 
170  Node* ancestor() const;
175  Node* father() const;
176 
191 
192 
194  inline bool even() const {
196  }
198  inline bool odd() const {
199  return NumberTraits<Quotient>::odd( k );
200  }
202  inline bool isSameDepthLeft() const {
203  return odd();
204  }
205 
206  };
207 
217  class Fraction {
218  public:
219  typedef TInteger Integer;
220  typedef TQuotient Quotient;
222  typedef typename SternBrocotTree::Fraction Self;
224  typedef std::pair<Quotient, Quotient> Value;
225  typedef std::vector<Quotient> CFracSequence;
227 
228  // --------------------- std types ------------------------------
229  typedef Value value_type;
231  typedef const value_type & const_reference;
232 
233  private:
238  bool mySup1;
239 
240  public:
250  Fraction( Integer aP, Integer aQ,
252 
260  Fraction( Node* sb_node = 0, bool sup1 = false );
261 
266  Fraction( const Self & other );
267 
273  Self& operator=( const Self & other );
274 
276  bool null() const;
278  Integer p() const;
280  Integer q() const;
282  Quotient u() const;
284  Quotient k() const;
285 
288  bool isSup1() const { return mySup1; }
290  Quotient trueK() const { return myNode->k; }
291 
292  protected:
295  Fraction child( Quotient v ) const;
300  Fraction origin() const;
303  Fraction next( Quotient v ) const;
304 
305  public:
306 
308  Fraction left() const;
310  Fraction right() const;
312  bool even() const;
314  bool odd() const;
315 
322  Fraction ancestor() const;
326  bool isAncestorDirect() const;
331  Fraction father() const;
336  Fraction father( Quotient m ) const;
342  Fraction previousPartial() const;
347  Fraction inverse() const;
354  Fraction partial( Quotient kp ) const;
361  Fraction reduced( Quotient i ) const;
362 
379  void push_back( const std::pair<Quotient, Quotient> & quotient );
380 
391  void pushBack( const std::pair<Quotient, Quotient> & quotient );
392 
400  void getSplit( Fraction & f1, Fraction & f2 ) const;
401 
413  void getSplitBerstel( Fraction & f1, Quotient & nb1,
414  Fraction & f2, Quotient & nb2 ) const;
415 
420  void getCFrac( std::vector<Quotient> & quotients ) const;
421 
427  bool equals( Integer p1, Integer q1 ) const;
428 
434  bool lessThan( Integer p1, Integer q1 ) const;
435 
441  bool moreThan( Integer p1, Integer q1 ) const;
442 
447  bool operator==( const Fraction & other ) const;
448 
453  bool operator!=( const Fraction & other ) const;
454 
459  bool operator<( const Fraction & other ) const;
460 
465  bool operator>( const Fraction & other ) const;
466 
471  void selfDisplay ( std::ostream & out ) const;
472 
477  ConstIterator begin() const;
478 
483  ConstIterator end() const;
484 
485  };
486 
487 
488 
489  // ----------------------- Standard services ------------------------------
490  public:
495 
499  static LighterSternBrocot & instance();
500 
502  static Fraction zeroOverOne();
503 
505  static Fraction oneOverZero();
506 
508  static Fraction oneOverOne();
509 
521  static Fraction fraction( Integer p, Integer q,
522  Fraction ancestor = oneOverZero() );
523 
524  // ----------------------- Interface --------------------------------------
525  public:
526 
532  static void display ( std::ostream & out, const Fraction & f );
533 
538  bool isValid() const;
539 
542 
543  // ------------------------- Protected Datas ------------------------------
544  protected:
545  // ------------------------- Private Datas --------------------------------
546  private:
547 
550 
553 
554  // ------------------------- Hidden services ------------------------------
555  protected:
556 
557  private:
558 
563 
569  LighterSternBrocot ( const LighterSternBrocot & other );
570 
578 
579  // ------------------------- Internals ------------------------------------
580  private:
581 
582  }; // end of class LighterSternBrocot
583 
584 
591  // template <typename TInteger, typename TQuotient, typename TMap>
592  // std::ostream&
593  // operator<< ( std::ostream & out,
594  // const typename LighterSternBrocot<TInteger, TQuotient, TMap>::Fraction & object );
595 
596 } // namespace DGtal
597 
598 
600 // Includes inline functions.
601 #include "DGtal/arithmetic/LighterSternBrocot.ih"
602 
603 // //
605 
606 #endif // !defined LighterSternBrocot_h
607 
608 #undef LighterSternBrocot_RECURSES
609 #endif // else defined(LighterSternBrocot_RECURSES)