/* Copyright (c) 1997-2006
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
*/

#ifndef _POLYMAKE_FIBONACCI_H
#define _POLYMAKE_FIBONACCI_H "$Project: polymake $$Id: Fibonacci.h,v 1.3 2006/02/21 13:58:54 gawrilow Exp $"

#ifndef _POLYMAKE_ITERATORS_H
# include <iterators.h>
#endif
#include <limits>

namespace pm
{
   template <typename Number=int> class FibonacciNumbers;

   // An infinite source of Fibonacci numbers - as iterator
   template <typename Number=int>
   class Fibonacci_iterator {
   public:
      typedef forward_iterator_tag iterator_category;
      typedef Number value_type;
      typedef const Number& reference;
      typedef const Number* pointer;
      typedef ptrdiff_t difference_type;
      typedef Fibonacci_iterator iterator;
      typedef iterator const_iterator;

      Fibonacci_iterator() : prev(0), cur(1) { }

      reference operator* () const { return cur; }
      pointer operator-> () const { return &cur; }

      iterator& operator++ () { const Number p=prev; prev=cur; cur+=p; return *this; }
      const iterator operator++ (int) { iterator copy(*this); operator++(); return copy; }

      bool operator== (const iterator& it) const { return prev==it.prev && cur==it.cur; }
      bool operator!= (const iterator& it) const { return !operator==(it); }

      void reset() { prev=0; cur=1; }
   protected:
      Number prev, cur;

      Fibonacci_iterator(bool at_end) : prev(0), cur(0) { }
      friend class FibonacciNumbers<Number>;
   };


   // An infinite source of Fibonacci numbers - as container
   template <typename Number>
   class FibonacciNumbers {
   public:
      typedef Number value_type;
      typedef const Number& reference;
      typedef reference const_reference;
      typedef Fibonacci_iterator<Number> iterator;
      typedef iterator const_iterator;

      iterator begin() const { return iterator(); }
      iterator end() const { return iterator(true); }
      reference front() const { static Number one(1); return one; }
      size_t size() const { return std::numeric_limits<size_t>::max(); }
      bool empty() const { return false; }
   };

   template <typename Number>
   struct check_iterator_feature<Fibonacci_iterator<Number>, unlimited> : True { };

   template <typename Number>
   struct check_iterator_feature<Fibonacci_iterator<Number>, resettable> : True { };

   template <typename Number>
   struct spec_object_traits< FibonacciNumbers<Number> > : spec_object_traits<is_container> { };

   template <typename Number> inline
   Fibonacci_iterator<Number> fibonacci_numbers() { return Fibonacci_iterator<Number>(); }

   inline
   Fibonacci_iterator<> fibonacci_numbers() { return Fibonacci_iterator<>(); }

   template <typename Number> inline
   Number fibonacci_number(int i)
   {
      Fibonacci_iterator<Number> it;
      while (--i>=0) ++it;
      return *it;
   }

   inline int fibonacci_number(int i) { return fibonacci_number<int>(i); }
}

namespace polymake {
   using pm::Fibonacci_iterator;
   using pm::FibonacciNumbers;
   using pm::fibonacci_numbers;
   using pm::fibonacci_number;
}

#endif // _POLYMAKE_FIBONACCI_H


// Local Variables:
// mode:C++
// c-basic-offset:3
// End:
