proj home

Files   Classes   Functions   Hierarchy  

snakeindex.h

Go to the documentation of this file.
00001 #ifndef SNAKEINDEX_H
00002 #define SNAKEINDEX_H
00003 
00004 #include <typedefs.h>
00005 
00021 template< typename T = int >
00022 class snakeindex
00023 {
00025   T dcurrent;
00027   T current;
00028 public:
00029 
00031   T const rows;
00033   T const columns;
00035   T const length;
00036 
00040   snakeindex
00041   (
00042     T const _columns, 
00043     T const _length, 
00044     bool forward=true
00045   );
00046 
00048   void reset();
00050   boolc operator !() const 
00051     { return (current>=0)&&(current<length); }
00053   void operator ++ ()
00054     { current += dcurrent; }
00056   void pos(T & row, T & col) const;
00058   void pos(T & k2) const;
00059 
00060 };
00061 
00062 //---------------------------------------------------------
00063 // Implementation
00064 
00065 
00066 template< typename T >
00067 void snakeindex<T>::pos(T & k2) const
00068 {
00069   T row;
00070   T col;
00071   pos(row,col);
00072   k2 = row*columns+col;
00073 }
00074 
00075 template< typename T >
00076 void snakeindex<T>::pos(T & row, T & col) const
00077 {
00078   row = (current - (current % columns))/columns;
00079   col = current-row*columns;
00080   if ((row % 2)==1)
00081     col = columns-1-col;
00082 }
00083 
00084 template< typename T >
00085 void snakeindex<T>::reset()
00086 {
00087   if (dcurrent>0)
00088     current=0;
00089   else
00090     current=length-1;
00091 }
00092 
00093 
00094 template< typename T >
00095 snakeindex<T>::snakeindex
00096 (
00097   T const _columns, 
00098   T const _length,
00099   boolc forward
00100 )
00101   : rows(_length % _columns), columns(_columns), length(_length)
00102 {
00103   if (forward)
00104     dcurrent=1;
00105   else
00106     dcurrent=-1;
00107 
00108 }
00109 
00110 
00111 #endif
00112 

Generated on Fri Mar 4 00:49:31 2011 for Chelton Evans Source by  doxygen 1.5.8