|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r79992 - sandbox/variadic_templates/sandbox/array_dyn
From: cppljevans_at_[hidden]
Date: 2012-08-12 20:34:52
Author: cppljevans
Date: 2012-08-12 20:34:51 EDT (Sun, 12 Aug 2012)
New Revision: 79992
URL: http://svn.boost.org/trac/boost/changeset/79992
Log:
WHAT:
Replace variadic templates, such as:
template
< typename... Indices
>
index_t
offset_at_indices
( Indices...const& a_indices
)const
to:
template
< typename Indices
>
index_t
offset_at_indices
( Indices const& a_indices
)const
where Indices is some type of stl container type with
begin/end member functions.
WHY:
Avoid annoying warning messages, such as:
warning: narrowing conversion of 'a_size#0' from 'int' to 'unsigned int'
from g++ compiler.
Text files modified:
sandbox/variadic_templates/sandbox/array_dyn/array_dyn.cpp | 24 +++--
sandbox/variadic_templates/sandbox/array_dyn/array_dyn.hpp | 74 ++++++++++++++---
sandbox/variadic_templates/sandbox/array_dyn/box_domain.hpp | 161 +++++++++++++++++++++++++++------------
3 files changed, 183 insertions(+), 76 deletions(-)
Modified: sandbox/variadic_templates/sandbox/array_dyn/array_dyn.cpp
==============================================================================
--- sandbox/variadic_templates/sandbox/array_dyn/array_dyn.cpp (original)
+++ sandbox/variadic_templates/sandbox/array_dyn/array_dyn.cpp 2012-08-12 20:34:51 EDT (Sun, 12 Aug 2012)
@@ -9,7 +9,7 @@
{
std::cout<<"*************************\n";
std::cout<<"dir_op="<<dir_op<<"\n";
- array_dyn<int> ai(dirs(dir_op),2,3,4);
+ array_dyn<int> ai(dirs(dir_op),{2,3,4});
unsigned const size=ai.my_data.size();
std::cout<<"my_data.size()="<<size<<"\n";
unsigned const value0=1000;
@@ -25,16 +25,18 @@
std::cout<<"stride("<<i<<")="<<ai.stride(i)<<"\n";
std::cout<<" size("<<i<<")="<<ai.size(i)<<"\n";
}
- std::cout<<"ai.offset_at_indices(1,0,0)="<<ai.offset_at_indices(1,0,0)<<"\n";
- std::cout<<"ai(1,0,0)="<<ai(1,0,0)<<"\n";
- std::cout<<"ai.offset_at_indices(0,1,0)="<<ai.offset_at_indices(0,1,0)<<"\n";
- std::cout<<"ai(0,1,0)="<<ai(0,1,0)<<"\n";
- std::cout<<"ai.offset_at_indices(0,0,1)="<<ai.offset_at_indices(0,0,1)<<"\n";
- std::cout<<"ai(0,0,1)="<<ai(0,0,1)<<"\n";
- std::cout<<"ai.ok_indices(1,2,3)="<<ai.ok_indices(1,2,3)<<"\n";
- std::cout<<"ai.ok_indices(1,2,4)="<<ai.ok_indices(1,2,4)<<"\n";
- std::cout<<"ai.ok_indices(1,2,3,0)="<<ai.ok_indices(1,2,3,0)<<"\n";
- unsigned offset=ai.offset_at_indices(1,2,3);
+ std::cout<<"ai.offset_at_indices(1,0,0)="<<ai.offset_at_indices({1,0,0})<<"\n";
+ std::cout<<"ai(1,0,0)="<<ai({1,0,0})<<"\n";
+ std::cout<<"ai.offset_at_indices(0,1,0)="<<ai.offset_at_indices({0,1,0})<<"\n";
+ std::cout<<"ai(0,1,0)="<<ai({0,1,0})<<"\n";
+ std::cout<<"ai.offset_at_indices(0,0,1)="<<ai.offset_at_indices({0,0,1})<<"\n";
+ std::cout<<"ai(0,0,1)="<<ai({0,0,1})<<"\n";
+ std::cout<<"ai.ok_indices(1,2,3)="<<ai.ok_indices({1,2,3})<<"\n";
+ typedef std::vector<unsigned> indices_t;
+ std::cout<<"ai.ok_indices(indices_t({0,0,0}))="<<ai.ok_indices(indices_t({0,0,0}))<<"\n";
+ std::cout<<"ai.ok_indices(1,2,4)="<<ai.ok_indices({1,2,4})<<"\n";
+ std::cout<<"ai.ok_indices(1,2,3,0)="<<ai.ok_indices({1,2,3,0})<<"\n";
+ unsigned offset=ai.offset_at_indices({1,2,3});
std::cout<<"ai.offset_at_indices(1,2,3)="<<offset<<"\n";
std::vector<unsigned> indices(ai.indices_at_offset(offset));
std::cout<<"indices_at_offset("<<offset<<")=\n";
Modified: sandbox/variadic_templates/sandbox/array_dyn/array_dyn.hpp
==============================================================================
--- sandbox/variadic_templates/sandbox/array_dyn/array_dyn.hpp (original)
+++ sandbox/variadic_templates/sandbox/array_dyn/array_dyn.hpp 2012-08-12 20:34:51 EDT (Sun, 12 Aug 2012)
@@ -1,5 +1,13 @@
#ifndef ARRAY_DYN_HPP_INCLUDED
#define ARRAY_DYN_HPP_INCLUDED
+// (C) Copyright Larry Evans 2012.
+//
+// Permission to copy, use, modify, sell and distribute this software
+// is granted provided this copyright notice appears in all copies.
+// This software is provided "as is" without express or implied
+// warranty, and with no claim as to its suitability for any purpose.
+//
+//====================================================================
#include <iostream>
#include <iomanip>
@@ -22,8 +30,8 @@
box_domain<>
super_t
;
- typedef typename
- super_t::index_t
+ using typename
+ super_t::
index_t
;
std::vector<T>
@@ -32,41 +40,79 @@
* data in the array.
*/
;
+
+ //[Template interface:
+ // This is more general than the
+ // Initializer_list interface below.
template
- < typename... Size
+ < typename Sizes
>
- array_dyn( dirs a_dir, Size... a_size)
- : super_t( a_dir, a_size...)
+ array_dyn( dirs a_dir, Sizes const& a_sizes={})
+ : super_t( a_dir, a_sizes)
, my_data( super_t::size())
{
}
template
- < typename... Size
+ < typename Sizes
>
void
- reshape( Size... a_size)
+ reshape( Sizes const& a_sizes)
{
- my_data.resize(super_t::reshape(a_size...));
+ my_data.resize(super_t::reshape(a_sizes));
}
template
- < typename... Index
+ < typename Indices
>
T&
- operator()( Index... a_index)
+ operator()( Indices const& a_indices)
{
- return my_data[offset_at_indices(a_index...)];
+ return my_data[offset_at_indices(a_indices)];
}
template
- < typename... Index
+ < typename Indices
>
T const&
- operator()( Index... a_index)const
+ operator()( Indices const& a_indices)const
+ {
+ return my_data[offset_at_indices(a_indices)];
+ }
+
+ //]
+ //[Initializer_list interface:
+ // Similar to above Template interface, but using
+ // init_list_t(see below) instead of templates.
+ // This makes interfaces slightly more user friendly.
+ using typename
+ super_t::
+ init_list_t
+ ;
+ array_dyn( dirs a_dir, init_list_t const& a_sizes={})
+ : super_t( a_dir, a_sizes)
+ , my_data( super_t::size())
+ {
+ }
+
+ void
+ reshape( init_list_t const& a_sizes)
+ {
+ this->template reshape<init_list_t>(a_sizes);
+ }
+
+ T&
+ operator()( init_list_t const& a_indices)
+ {
+ return this->template operator()<init_list_t>(a_indices);
+ }
+
+ T const&
+ operator()( init_list_t const& a_indices)const
{
- return my_data[offset_at_indices(a_index...)];
+ return this->template operator()<init_list_t>(a_indices);
}
+ //]
};
Modified: sandbox/variadic_templates/sandbox/array_dyn/box_domain.hpp
==============================================================================
--- sandbox/variadic_templates/sandbox/array_dyn/box_domain.hpp (original)
+++ sandbox/variadic_templates/sandbox/array_dyn/box_domain.hpp 2012-08-12 20:34:51 EDT (Sun, 12 Aug 2012)
@@ -1,8 +1,17 @@
#ifndef BOX_DOMAIN_HPP_INCLUDED
#define BOX_DOMAIN_HPP_INCLUDED
+// (C) Copyright Larry Evans 2012.
+//
+// Permission to copy, use, modify, sell and distribute this software
+// is granted provided this copyright notice appears in all copies.
+// This software is provided "as is" without express or implied
+// warranty, and with no claim as to its suitability for any purpose.
+//
+//====================================================================
#include <vector>
#include <numeric>
#include <algorithm>
+#include <iterator>
enum dirs //directions(used to flag order of processing an ordered sequence).
{ dir_fwd //forward direction.
@@ -79,15 +88,16 @@
( sizes_beg
, sizes_end
, ++strides
- , std::multiplies<typename InpIter::value_type>()
+ , std::multiplies<index_t>()
);
}
+
template
- < typename... Sizes
+ < typename Sizes
>
index_t
init_strides
- ( Sizes... a_size
+ ( Sizes const& a_sizes
)
/**@brief
* Calculates strides of the array with shape, a_size...
@@ -97,24 +107,33 @@
* product of a_size...
*/
{
- strides_t const sizes({a_size...});
index_t result;
if(my_dir==dir_fwd)
{
auto
it_v=init_iter_strides
- ( sizes.begin()
- , sizes.end()
+ ( a_sizes.begin()
+ , a_sizes.end()
, my_strides.begin()
);
result=*(--it_v);
}
else
{
+ typedef typename
+ Sizes::const_iterator
+ fwd_iter;
+ typedef
+ std::reverse_iterator<fwd_iter>
+ rev_iter;
+ rev_iter
+ rbeg(a_sizes.end());
+ rev_iter
+ rend(a_sizes.begin());
auto
it_v=init_iter_strides
- ( sizes.rbegin()
- , sizes.rend()
+ ( rbeg
+ , rend
, my_strides.rbegin()
);
result=*(--it_v);
@@ -122,16 +141,6 @@
return result;
}
- template
- < typename... Size
- >
- box_domain( dirs a_dir, Size... a_size)
- : my_strides( sizeof...(Size)+1)
- , my_dir(a_dir)
- {
- init_strides( a_size...);
- }
-
index_t
rank()const
{
@@ -168,29 +177,6 @@
}
template
- < typename... Index
- >
- index_t
- offset_at_indices
- ( Index... a_index
- )const
- /**@brief
- * The offset of element in an array
- * corresponding to indices, a_index...
- */
- {
- strides_t const indices({a_index...});
- index_t const offset
- = std::inner_product
- ( indices.begin()
- , indices.end()
- , my_strides.begin()+my_dir
- , index_t(0)
- );
- return offset;
- }
-
- template
< typename InpIter
, typename OutIter
>
@@ -248,36 +234,109 @@
return indices;
}
+ //[Template interface:
+ // This is more general than the
+ // Initializer_list interface shown farther below.
+
+ template
+ < typename Sizes
+ >
+ box_domain( dirs a_dir, Sizes const& a_sizes)
+ : my_strides( a_sizes.size()+1)
+ , my_dir(a_dir)
+ {
+ init_strides( a_sizes);
+ }
+
template
- < typename... Size
+ < typename Indices
>
index_t
- reshape( Size... a_size)
+ offset_at_indices
+ ( Indices const& a_indices
+ )const
+ /**@brief
+ * The offset of element in an array
+ * corresponding to indices, a_index...
+ */
+ {
+ index_t const offset
+ = std::inner_product
+ ( a_indices.begin()
+ , a_indices.end()
+ , my_strides.begin()+my_dir
+ , index_t(0)
+ );
+ return offset;
+ }
+
+ template
+ < typename Indices
+ >
+ index_t
+ reshape( Indices const& a_sizes)
{
- index_t const rankp1=sizeof...(a_size)+1;
+ index_t const rankp1=a_sizes.size()+1;
my_strides.resize(rankp1);
- return init_strides( a_size...);
+ return init_strides( a_sizes);
}
template
- < typename... Index
+ < typename Indices
>
bool
- ok_indices( Index... a_index)
+ ok_indices( Indices const& a_indices)
/**@brief
* Is a_index... a valid argument to
* offset_at_indices?
*/
{
- std::vector<index_t> const indices({a_index...});
- unsigned n=indices.size();
+ unsigned n=a_indices.size();
bool result= n<=rank();
- for( unsigned i=0; i<n && result; ++i)
+ auto index_iter=a_indices.begin();
+ for( unsigned i=0; i<n && result; ++i,++index_iter)
{
- result = 0<=indices[i] && indices[i]<size(i);
+ index_t index_i=*index_iter;
+ result = 0<=index_i && index_i<size(i);
}
return result;
}
+
+ //]Template interface:
+ //[Initializer_list interface:
+ typedef
+ std::initializer_list<index_t>
+ init_list_t
+ ;
+
+ box_domain( dirs a_dir, init_list_t const& a_sizes)
+ : my_strides( a_sizes.size()+1)
+ , my_dir(a_dir)
+ {
+ init_strides( a_sizes);
+ }
+
+ index_t
+ offset_at_indices
+ ( init_list_t const& a_indices
+ )const
+ {
+ return this->template offset_at_indices<init_list_t>(a_indices);
+ }
+
+ index_t
+ reshape( init_list_t const& a_sizes)
+ {
+ return this->template reshape<init_list_t>( a_sizes);
+ }
+
+ bool
+ ok_indices( init_list_t const& a_indices)
+ {
+ return this->template ok_indices<init_list_t>(a_indices);
+ }
+
+ //]Initializer_list interface:
};
#endif
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk