proj home

Files   Classes   Functions   Hierarchy  

snakeindex< T > Class Template Reference

Indexing a 2D matrix with a snake pattern. More...

#include <snakeindex.h>

Collaboration diagram for snakeindex< T >:

List of all members.

Public Member Functions

 snakeindex (T const _columns, T const _length, bool forward=true)
 Pass in the number of columns and length of the matrix, configure the iterator forwards or backwards.
void reset ()
 Reset the iterator.
boolc operator! () const
 Is the iterator valid?
void operator++ ()
 Increment the iterator.
void pos (T &row, T &col) const
 The current position as a 2D indexed point.
void pos (T &k2) const
 The current position as an index from the start.

Public Attributes

T const rows
 The number of rows.
T const columns
 The number of columns.
T const length
 The length of the 2D matrix.


Detailed Description

template<typename T = int>
class snakeindex< T >

Indexing a 2D matrix with a snake pattern.

Two combinations of itterating a 2D matrix are made.

To get the other two combinations, take a N by M matrix and consecutively label the elements from 0, then transpose it so it is now an M by N matrix.

Now construct a M by N snakeindex iterator and the other two combinations are realized by indexing on the transposed matrix.

Definition at line 22 of file snakeindex.h.


Constructor & Destructor Documentation

template<typename T = int>
snakeindex< T >::snakeindex ( T const   _columns,
T const   _length,
bool  forward = true 
)

Pass in the number of columns and length of the matrix, configure the iterator forwards or backwards.


Member Function Documentation

template<typename T = int>
boolc snakeindex< T >::operator! (  )  const [inline]

Is the iterator valid?

Definition at line 50 of file snakeindex.h.

References snakeindex< T >::length.

00051     { return (current>=0)&&(current<length); }

template<typename T = int>
void snakeindex< T >::operator++ (  )  [inline]

Increment the iterator.

Definition at line 53 of file snakeindex.h.

00054     { current += dcurrent; }

template<typename T >
void snakeindex< T >::pos ( T k2  )  const [inline]

The current position as an index from the start.

Definition at line 67 of file snakeindex.h.

References snakeindex< T >::columns, and snakeindex< T >::pos().

00068 {
00069   T row;
00070   T col;
00071   pos(row,col);
00072   k2 = row*columns+col;
00073 }

template<typename T >
void snakeindex< T >::pos ( T row,
T col 
) const [inline]

The current position as a 2D indexed point.

Definition at line 76 of file snakeindex.h.

References snakeindex< T >::columns.

Referenced by snakeindex< T >::pos(), snakeindextest::test01(), snakeindextest::test02(), and snakeindextest::test03().

00077 {
00078   row = (current - (current % columns))/columns;
00079   col = current-row*columns;
00080   if ((row % 2)==1)
00081     col = columns-1-col;
00082 }

template<typename T >
void snakeindex< T >::reset (  )  [inline]

Reset the iterator.

Definition at line 85 of file snakeindex.h.

References snakeindex< T >::length.

Referenced by snakeindextest::test01(), snakeindextest::test02(), and snakeindextest::test03().

00086 {
00087   if (dcurrent>0)
00088     current=0;
00089   else
00090     current=length-1;
00091 }


Member Data Documentation

template<typename T = int>
T const snakeindex< T >::columns

The number of columns.

Definition at line 33 of file snakeindex.h.

Referenced by snakeindex< T >::pos().

template<typename T = int>
T const snakeindex< T >::length

The length of the 2D matrix.

Definition at line 35 of file snakeindex.h.

Referenced by snakeindex< T >::operator!(), and snakeindex< T >::reset().

template<typename T = int>
T const snakeindex< T >::rows

The number of rows.

Definition at line 31 of file snakeindex.h.


The documentation for this class was generated from the following file:

Generated on Fri Mar 4 00:50:17 2011 for Chelton Evans Source by  doxygen 1.5.8