A stl-compliant N-dimensional grid class

View versions (1)

Interface

Overview

A N-dimensional grid class. It has the same interface and time complexity as vectors do. Having said that, construction can be slightly different and the time complexity for functions that are linear for the vector are actually O(N^dimensions) because each linear time function performs the same linear time function on each element, and so forth.

Example 1

#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include <codecogs\array\Grid.hpp>
#include <ctime>
using namespace std;
int main(int argc, char *argv[])
{
    std::vector<int> v;
    v.push_back(7); //first dimension
    v.push_back(6); //second dimension
    v.push_back(5); //third dimension
    //non-default ctor
    Grid<int, 3> g3d(v);
    Grid<int, 2> g2d(g3d.front());
    Grid<int, 1> g1d(g2d.back());
    Grid<int, 3> cube3d(3, g3d.front()); //3 in length
    //all 2d dimensions are the same as g3d.front()
    //dimension size checking
    cout << "First dimension: " << g1d.size() << endl;
    cout << "Second dimension: " << g2d.size() << endl;
    cout << "Third dimension: " << g3d.size() << endl;
    try //bounds checking
    {
        int x = g1d.at(9);
    }
    catch(const std::out_of_range& e)
    {
        cout << "Bounds checking test: " << e.what() << endl;
    }
    srand(time(NULL));
    for(int x = 0; x < 7; x++)
    {
        for(int y = 0; y < 6; y++)
        {
            for(int z = 0; z < 5; z++)
            {
                //operator[]
                //oppositly sorted
                g3d[z][y][x] = (100 - x) + (100 - y) + (100 - z);
            }
        }
    }
   cout << "Unsorted" << endl;
   //operators
   cout << (g3d[0] < g3d[1]) << endl;
   cout << (g3d[1] < g3d[0]) << endl;
   cout << (g3d[0] == g3d[0]) << endl;
   cout << (g3d[0] != g3d[0]) << endl;
   //begin/end/rbegin/rend
   sort(g3d.begin(), g3d.end());
   cout << "Sorted" << endl;
   cout << (g3d[0] < g3d[1]) << endl;
   cout << (g3d[1] < g3d[0]) << endl;
   cout << (g3d[0] == g3d[0]) << endl;
   cout << (g3d[0] != g3d[0]) << endl;
   return 0;
}

Output:

First dimension: 7
Second dimension: 6
Third dimension: 5
Bounds checking test: vector::_M_range_check
Unsorted
0
1
1
0
Sorted
1
0
1
0
GPL Licence — free for non commercial use. See Licence details.

DECLARATION

operator[]

A N-dimensional grid class.

FUNCTION

operator==

Author

Sam Schetterer (June 2007)

FUNCTION

operator!=

FUNCTION

operator<

FUNCTION

operator>

FUNCTION

operator>=

FUNCTION

operator<=

DECLARATION

operator[]

Author

Sam Schetterer (June 2007)