cfc::array 一个多维数组类(4)
size_type index2,
size_type index3)
{
return pdata[
index1 * dims[1] *dims[2] +
index2 * dims[2] +
index3
];
}
protected:
array_3d_base() {dim_count=3;}
};
}
#endif file://_ARRAY_BASE_H_
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////
// arrays.h, array_1d,array_2d,array_3d的实现
#ifndef _ARRAYS_H_
#define _ARRAYS_H_
#include "array_base.h"
namespace cfc
{
template <typename T, int dim1>
class array_1d : public array_1d_base<T>
{
public:
CFC_STATIC_CONST(size_type, static_size = dim1);
const_reference operator[](size_type index)const
{
return data[index];
}
reference operator[](size_type index)
{
return data[index];
}
////////////////////////////////////////////////////////////
//
//
const_reference operator()(size_type index)const
{
return data[index];
}
reference operator()(size_type index)
{
return data[index];
}
protected:
array_1d()
{
pdata = &data[0];
dims[0] = dim1;
}
protected:
T data[dim1];
};
///////////////////////////////////////////////////////////////////
// 2d
template <typename T, int dim1, int dim2>
class array_2d : public array_2d_base<T>
{
#ifndef _MSC_VER file://MSC doesn't support ref to a template array
private:
typedef T (&index_type)[dim2];
typedef T const (&const_index_type)[dim2];
public:
const_index_type operator[](size_type index)const
{
return data[index];
}
index_type operator[](size_type index)
{
return data[index];
}
#endif
public:
CFC_STATIC_CONST(size_type, static_size = dim1 * dim2);
const_reference operator()(size_type index1, size_type index
2)coonst
{
return data[index1][index2];
}
reference operator()(size_type index1, size_type index2)
{
return data[index1][index2];
}
protected:
array_2d()
{
pdata = &data[0][0];
dims[0] = dim1;
dims[1] = dim2;
}
private:
T data[dim1][dim2];
};
///////////////////////////////////////////////////////////////////
// 3d
template <typename T, int dim1, int dim2, int dim3>
class array_3d : public array_3d_base<T>
{
#ifndef _MSC_VER
private:
typedef T (&index_type)[dim2][dim3];
typedef T const (&const_index_type)[dim2][dim3];
public:
const_index_type operator[](size_type index)const
{
return data[index];
}
index_type operator[](size_type index)
{
return data[index];
}
#endif
public:
CFC_STATIC_CONST(size_type, static_size = dim1 * dim2 * dim3
);
const_reference operator()(
size_type in
- 最新评论
