DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SternBrocot.h
1 
17 #pragma once
18 
33 #if defined(SternBrocot_RECURSES)
34 #error Recursive header files inclusion detected in SternBrocot.h
35 #else // defined(SternBrocot_RECURSES)
36 
37 #define SternBrocot_RECURSES
38 
39 #if !defined SternBrocot_h
40 
41 #define SternBrocot_h
42 
44 // Inclusions
45 #include <iostream>
46 #include <vector>
47 #include "DGtal/base/Common.h"
48 #include "DGtal/base/InputIteratorWithRankOnSequence.h"
49 #include "DGtal/kernel/CInteger.h"
50 #include "DGtal/kernel/CSignedInteger.h"
51 #include "DGtal/kernel/NumberTraits.h"
53 
54 namespace DGtal
55 {
56 
58  // template class SternBrocot
77  template <typename TInteger, typename TQuotient = int32_t>
79  {
80  public:
81  typedef TInteger Integer;
82  typedef TQuotient Quotient;
84 
87 
88  public:
89 
103  struct Node {
104 
118  Node( Integer p1, Integer q1, Quotient u1, Quotient k1,
119  Node* ascendant_left1, Node* ascendant_right1,
120  Node* descendant_left1, Node* descendant_right1,
121  Node* inverse1 );
122 
141  };
142 
151  class Fraction {
152  public:
153  typedef TInteger Integer;
154  typedef TQuotient Quotient;
156  typedef typename SternBrocotTree::Fraction Self;
158  typedef std::pair<Quotient, Quotient> Value;
159  typedef std::vector<Quotient> CFracSequence;
161 
162  // --------------------- std types ------------------------------
163  typedef Value value_type;
165  typedef const value_type & const_reference;
166 
167  private:
169 
170  public:
186  Fraction( Integer aP, Integer aQ,
187  Fraction ancestor = SternBrocotTree::zeroOverOne() );
188 
193  Fraction( Node* sb_node = 0 );
194 
199  Fraction( const Self & other );
200 
206  Self& operator=( const Self & other );
207 
209  bool null() const;
211  Integer p() const;
213  Integer q() const;
215  Quotient u() const;
217  Quotient k() const;
219  Fraction left() const;
221  Fraction right() const;
223  bool even() const;
225  bool odd() const;
230  Fraction father() const;
238  Fraction father( Quotient m ) const;
244  Fraction previousPartial() const;
249  Fraction inverse() const;
256  Fraction partial( Quotient kp ) const;
263  Fraction reduced( Quotient i ) const;
264 
281  void push_back( const std::pair<Quotient, Quotient> & quotient );
282 
293  void pushBack( const std::pair<Quotient, Quotient> & quotient );
294 
302  void getSplit( Fraction & f1, Fraction & f2 ) const;
303 
315  void getSplitBerstel( Fraction & f1, Quotient & nb1,
316  Fraction & f2, Quotient & nb2 ) const;
317 
322  void getCFrac( std::vector<Quotient> & quotients ) const;
323 
329  bool equals( Integer p1, Integer q1 ) const;
330 
336  bool lessThan( Integer p1, Integer q1 ) const;
337 
343  bool moreThan( Integer p1, Integer q1 ) const;
344 
349  bool operator==( const Fraction & other ) const;
350 
355  bool operator!=( const Fraction & other ) const;
356 
361  bool operator<( const Fraction & other ) const;
362 
367  bool operator>( const Fraction & other ) const;
368 
373  void selfDisplay ( std::ostream & out ) const;
374 
379  ConstIterator begin() const;
380 
385  ConstIterator end() const;
386 
387  };
388 
389 
390 
391  // ----------------------- Standard services ------------------------------
392  public:
393 
397  ~SternBrocot();
398 
402  static SternBrocot & instance();
403 
405  static Fraction zeroOverOne();
406 
408  static Fraction oneOverZero();
409 
425  static Fraction fraction( Integer p, Integer q,
426  Fraction ancestor = zeroOverOne() );
427 
428  // ----------------------- Interface --------------------------------------
429  public:
430 
436  static void display ( std::ostream & out, const Fraction & f );
437 
442  bool isValid() const;
443 
446 
447  // ------------------------- Protected Datas ------------------------------
448  private:
449  // ------------------------- Private Datas --------------------------------
450  private:
453 
457 
458  // ------------------------- Hidden services ------------------------------
459  private:
460 
464  SternBrocot();
465 
471  SternBrocot ( const SternBrocot & other );
472 
479  SternBrocot & operator= ( const SternBrocot & other );
480 
481  // ------------------------- Internals ------------------------------------
482  private:
483 
484  }; // end of class SternBrocot
485 
486 
493  // template <typename TInteger, typename TQuotient>
494  // std::ostream&
495  // operator<< ( std::ostream & out,
496  // const typename SternBrocot<TInteger, TQuotient>::Fraction & object );
497 
498 } // namespace DGtal
499 
500 
502 // Includes inline functions.
503 #include "DGtal/arithmetic/SternBrocot.ih"
504 
505 // //
507 
508 #endif // !defined SternBrocot_h
509 
510 #undef SternBrocot_RECURSES
511 #endif // else defined(SternBrocot_RECURSES)