/* Copyright (c) 1997-2004
   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.
*/

namespace pm {

   template <bool left_to_right, typename number_type>
   MultiDimCounter<left_to_right, number_type>&
   MultiDimCounter<left_to_right, number_type>::operator++ ()
   {
      const int last = my_limits.size()-1;

      if (left_to_right)
	 for (int digit = 0; (++my_counter[digit]) >= my_limits[digit]; ++digit)
	    if (digit < last) {
	       my_counter[digit] = my_start[digit];
	    } else {
	       my_at_end = true;  break;
	    }

      else
	 for (int digit = last; (++my_counter[digit]) >= my_limits[digit]; --digit)
	    if (digit > 0) {
	       my_counter[digit] = my_start[digit];
	    } else {
	       my_at_end = true;  break;
	    }

      return *this;
   }
   
   template <bool left_to_right, typename number_type>
   MultiDimCounter<left_to_right, number_type>&
   MultiDimCounter<left_to_right, number_type>::operator-- ()
   {
      const int last = my_limits.size()-1;
      
      if (left_to_right)
	 for (int digit = 0; (--my_counter[digit]) < my_start[digit]; ++digit)
	    if (digit < last) {
	       my_counter[digit] = my_limits[digit]-1;
	    } else {
	       my_at_end = true;  break;
	    }
	 
      else
	 for (int digit = last; (--my_counter[digit]) < my_start[digit]; --digit)
	    if (digit > 0) {
	       my_counter[digit] = my_limits[digit]-1;
	    } else {
	       my_at_end = true;  break;
	    }

      return *this;
   }
   
} // end namespace pm

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