3 * Copyright (C) 2007 Tim Moore
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #ifndef VECTORARRAYADAPTERHXX
23 #define VECTORARRAYADAPTERHXX 1
25 // #define SG_CHECK_VECTOR_ACCESS 1
28 template <typename Vector>
29 class VectorArrayAdapter {
32 * Adapter that provides 2D array access to the elements of a
33 * std::vector in row major order.
35 * @param rowStride distance from an element to the corresponding
36 * element in the next row.
37 * @param baseOffset offset of the first element of the array from the
38 * beginning of the vector.
39 * @param rowOffset offset of the first element in a row from the
40 * actual beginning of the row.
42 VectorArrayAdapter(Vector& v, int rowStride, int baseOffset = 0,
44 _v(v), _rowStride(rowStride), _baseOffset(baseOffset),
48 #ifdef SG_CHECK_VECTOR_ACCESS
49 typename Vector::value_type& operator() (int i, int j)
51 return _v.at(_baseOffset + i * _rowStride + _rowOffset + j);
53 const typename Vector::value_type& operator() (int i, int j) const
55 return _v.at(_baseOffset + i * _rowStride + _rowOffset + j);
58 typename Vector::value_type& operator() (int i, int j)
60 return _v[_baseOffset + i * _rowStride + _rowOffset + j];
62 const typename Vector::value_type& operator() (int i, int j) const
64 return _v[_baseOffset + i * _rowStride + _rowOffset + j];
70 const int _baseOffset;