// multiArray.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <boost/multi_array.hpp>
#include <boost/array.hpp>
#include <boost/pool/pool.hpp>
#include <boost/pool/pool_alloc.hpp>
#include <boost/pool/detail/mutex.hpp>
#include <boost/multi_array/extent_gen.hpp>
#include <boost/multi_array/view.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/lambda/lambda.hpp>
void use_multiArray()
{
const int nDim = 3;
int xDim = 2, yDim = 3, zDim = 4;
boost::multi_array<double, nDim> md(boost::extents[xDim][yDim][zDim]);
unsigned nDimSize = md.num_dimensions();
assert(nDimSize == 3);
for (int i=0; i<xDim; ++i)
{
for (int j=0; j<yDim; ++j)
{
for (int k=0; k<zDim; ++k)
{
md[i][j][k] = 2012.1314;
}
}
}
boost::array<std::size_t, 3> idx = {1, 2, 3};
md(idx) = 1314.520;
double * ptr = md.data();
int elementsCnt = md.num_elements();
//boost::ref_array<double> refArray(ptr, md.num_elements());
//std::cout<<md<<std::endl;
}
void multi_array_sub_view()
{
typedef boost::fast_pool_allocator<double, boost::default_user_allocator_new_delete, boost::details::pool::null_mutex> fastallocator;
boost::multi_array<double, 2, boost::fast_pool_allocator<double>> md(boost::extents[2][3]);
//boost::multi_array::extent_gen<2> extentgen;
//extentgen = extentgen[3][5];
boost::detail::multi_array::extent_gen<2> exgen = boost::detail::multi_array::extent_gen<0>()[5][6];
typedef boost::multi_array<int,2, boost::fast_pool_allocator<int>> FastPoolMultiArray;
FastPoolMultiArray md2(boost::detail::multi_array::extent_gen<0>::extents()[5][6]);
double *elements = md.data();
for (int i=0; i<md.num_elements(); ++i)
{
elements[i] = 20+i;
}
boost::array<unsigned, 2> index = {0, 2};
md(index) = 300;
//for (int *intPtr = md2.data(), i=0; i<md2.num_elements(); ++i )
//{
// intPtr[i] = 1+i;
//}
int seftAdd = 0;
//注意boost::lambda::constant和
//boost::lambda::var
//boost::lambda::ref_constant的区别
//如果是var可以作为变量在lambda表达式中运算
//constant的不行。变量不会做自加操作。
std::for_each(md2.data(), md2.data()+md2.num_elements(),
boost::lambda::_1 = boost::lambda::var(seftAdd)++);
std::for_each(md2.data(), md2.data()+md2.num_elements(),
(std::cout<<boost::lambda::_1<<","));
//boost::multi_array_types的类型定义在multi_arary/base.hpp中定义
//索引在multi_array/types.hpp中定义
//索引的四种写法
boost::detail::multi_array::index_gen<2, 2> r;
r = boost::multi_array_types::index_gen()[boost::multi_array_types::index_range(0, 2)][boost::multi_array_types::index_range(0,3)];
boost::detail::multi_array::index_gen<0,0>::indices()[boost::multi_array_types::index_range(0, 2)][boost::multi_array_types::index_range(0,2)];
boost::detail::multi_array::index_gen<0,0>()[boost::multi_array_types::index_range(0, 2)][boost::multi_array_types::index_range(0,2)];
boost::indices[boost::multi_array_types::index_range(0, 2)][boost::multi_array_types::index_range(0,2)];
BOOST_AUTO(view, md2[r]);
BOOST_AUTO(it, view.begin());
BOOST_AUTO(iend, view.end());
std::cout<<*view.shape()<<std::endl;
std::cout<<"view elements:"<<view.num_elements()<<std::endl;
for (int i=0; i<2; ++i)
{
for (int j=0; j<3; ++j)
{
std::cout<<"view["<<i<<"]["<<j<<"]="<<view[i][j]<<std::endl;
}
}
//for (; it != iend; ++it)
//{
// std::cout<<*it<<std::endl;
// *it = 1000;
//}
}
int _tmain(int argc, _TCHAR* argv[])
{
use_multiArray();
multi_array_sub_view();
getchar();
return 0;
}
//made by davidsu33
//2014-6-20
#include "stdafx.h"
#include <boost/multi_array.hpp>
#include <boost/array.hpp>
#include <boost/pool/pool.hpp>
#include <boost/pool/pool_alloc.hpp>
#include <boost/pool/detail/mutex.hpp>
#include <boost/multi_array/extent_gen.hpp>
#include <boost/multi_array/view.hpp>
#include <boost/typeof/typeof.hpp>
#include <boost/lambda/lambda.hpp>
void use_multiArray()
{
const int nDim = 3;
int xDim = 2, yDim = 3, zDim = 4;
boost::multi_array<double, nDim> md(boost::extents[xDim][yDim][zDim]);
unsigned nDimSize = md.num_dimensions();
assert(nDimSize == 3);
for (int i=0; i<xDim; ++i)
{
for (int j=0; j<yDim; ++j)
{
for (int k=0; k<zDim; ++k)
{
md[i][j][k] = 2012.1314;
}
}
}
boost::array<std::size_t, 3> idx = {1, 2, 3};
md(idx) = 1314.520;
double * ptr = md.data();
int elementsCnt = md.num_elements();
//boost::ref_array<double> refArray(ptr, md.num_elements());
//std::cout<<md<<std::endl;
}
void multi_array_sub_view()
{
typedef boost::fast_pool_allocator<double, boost::default_user_allocator_new_delete, boost::details::pool::null_mutex> fastallocator;
boost::multi_array<double, 2, boost::fast_pool_allocator<double>> md(boost::extents[2][3]);
//boost::multi_array::extent_gen<2> extentgen;
//extentgen = extentgen[3][5];
boost::detail::multi_array::extent_gen<2> exgen = boost::detail::multi_array::extent_gen<0>()[5][6];
typedef boost::multi_array<int,2, boost::fast_pool_allocator<int>> FastPoolMultiArray;
FastPoolMultiArray md2(boost::detail::multi_array::extent_gen<0>::extents()[5][6]);
double *elements = md.data();
for (int i=0; i<md.num_elements(); ++i)
{
elements[i] = 20+i;
}
boost::array<unsigned, 2> index = {0, 2};
md(index) = 300;
//for (int *intPtr = md2.data(), i=0; i<md2.num_elements(); ++i )
//{
// intPtr[i] = 1+i;
//}
int seftAdd = 0;
//注意boost::lambda::constant和
//boost::lambda::var
//boost::lambda::ref_constant的区别
//如果是var可以作为变量在lambda表达式中运算
//constant的不行。变量不会做自加操作。
std::for_each(md2.data(), md2.data()+md2.num_elements(),
boost::lambda::_1 = boost::lambda::var(seftAdd)++);
std::for_each(md2.data(), md2.data()+md2.num_elements(),
(std::cout<<boost::lambda::_1<<","));
//boost::multi_array_types的类型定义在multi_arary/base.hpp中定义
//索引在multi_array/types.hpp中定义
//索引的四种写法
boost::detail::multi_array::index_gen<2, 2> r;
r = boost::multi_array_types::index_gen()[boost::multi_array_types::index_range(0, 2)][boost::multi_array_types::index_range(0,3)];
boost::detail::multi_array::index_gen<0,0>::indices()[boost::multi_array_types::index_range(0, 2)][boost::multi_array_types::index_range(0,2)];
boost::detail::multi_array::index_gen<0,0>()[boost::multi_array_types::index_range(0, 2)][boost::multi_array_types::index_range(0,2)];
boost::indices[boost::multi_array_types::index_range(0, 2)][boost::multi_array_types::index_range(0,2)];
BOOST_AUTO(view, md2[r]);
BOOST_AUTO(it, view.begin());
BOOST_AUTO(iend, view.end());
std::cout<<*view.shape()<<std::endl;
std::cout<<"view elements:"<<view.num_elements()<<std::endl;
for (int i=0; i<2; ++i)
{
for (int j=0; j<3; ++j)
{
std::cout<<"view["<<i<<"]["<<j<<"]="<<view[i][j]<<std::endl;
}
}
//for (; it != iend; ++it)
//{
// std::cout<<*it<<std::endl;
// *it = 1000;
//}
}
int _tmain(int argc, _TCHAR* argv[])
{
use_multiArray();
multi_array_sub_view();
getchar();
return 0;
}