Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r82056 - sandbox/variadic_templates/sandbox/array_dyn
From: cppljevans_at_[hidden]
Date: 2012-12-17 13:57:58


Author: cppljevans
Date: 2012-12-17 13:57:58 EST (Mon, 17 Dec 2012)
New Revision: 82056
URL: http://svn.boost.org/trac/boost/changeset/82056

Log:
Now print_array handles empty axes
Text files modified:
   sandbox/variadic_templates/sandbox/array_dyn/array_dyn.cpp | 40 ++++++++++++++++++-----
   sandbox/variadic_templates/sandbox/array_dyn/array_dyn.hpp | 67 ++++++++++++++++++++++++++++-----------
   2 files changed, 79 insertions(+), 28 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-12-17 13:57:58 EST (Mon, 17 Dec 2012)
@@ -20,7 +20,25 @@
       }
       sout<<"}";
       return sout;
- }
+ }
+
+ template
+ < typename Arr
+ >
+ typename Arr::data_t::value_type
+assign_iota
+ ( Arr&arr
+ , typename Arr::data_t::value_type start=0
+ )
+ {
+ unsigned n=arr.my_data.size();
+ typedef typename Arr::data_t::value_type value_t;
+ for( unsigned i=0; i<n; ++i)
+ {
+ arr.my_data[i]=start+value_t(i);
+ }
+ return start+value_t(n);
+ }
 
 int main(void)
 {
@@ -78,26 +96,30 @@
         unsigned dir_op=dir_fwd;
         std::cout<<"********reshape demo***********\n";
         std::cout<<"dir_op="<<dir_op<<"\n";
- std::vector<unsigned> shape({0,3});
+ std::vector<unsigned> shape({3,0});
         std::cout<<"shape="<<shape<<"\n";
         array_dyn<int> ai(dirs(dir_op),shape);
         unsigned const size=ai.my_data.size();
         std::cout<<"my_data.size()="<<size<<"\n";
- unsigned const value0=1000;
- for( unsigned i=0; i<size; ++i)
- {
- ai.my_data[i]=value0+i;
- }
+ assign_iota(ai);
+ std::cout<<"ai=\n";
+ std::cout<<ai<<".\n";
+ shape[1]=1;
+ std::cout<<"shape="<<shape<<"\n";
+ ai.reshape(shape);
+ assign_iota(ai);
         std::cout<<"ai=\n";
         std::cout<<ai<<".\n";
- shape[0]=1;
+ shape[1]=5;
         std::cout<<"shape="<<shape<<"\n";
         ai.reshape(shape);
+ assign_iota(ai);
         std::cout<<"ai=\n";
         std::cout<<ai<<".\n";
- shape[0]=5;
+ shape[0]=0;
         std::cout<<"shape="<<shape<<"\n";
         ai.reshape(shape);
+ assign_iota(ai);
         std::cout<<"ai=\n";
         std::cout<<ai<<".\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-12-17 13:57:58 EST (Mon, 17 Dec 2012)
@@ -196,23 +196,30 @@
         index_t const axis_size=my_array.size(axis_now);
         bool const is_leaf=axis_now==my_axis_end;
         index_t const axis_stride=my_array.stride(axis_now);
- for(index_t i=0; i<axis_size; ++i)
+ if(0 == axis_size)
         {
- a_viz.visit_pre(is_leaf, axis_now, i);
- if(is_leaf)
- {
- a_viz.visit_node(my_array.my_data[offset]);
- }
- else
+ a_viz.visit_empty(is_leaf, axis_now);
+ }
+ else
+ {//0 < axis_size
+ for(index_t i=0; i<axis_size; ++i)
             {
- this->accept_off_ax
- ( a_viz
- , offset
- , axis_now+my_axis_dir
- );
+ a_viz.visit_pre(is_leaf, axis_now, i);
+ if(is_leaf)
+ {
+ a_viz.visit_node(my_array.my_data[offset]);
+ }
+ else
+ {
+ this->accept_off_ax
+ ( a_viz
+ , offset
+ , axis_now+my_axis_dir
+ );
+ }
+ offset+=axis_stride;
             }
- offset+=axis_stride;
- }
+ }//0 < axis_size
         a_viz.visit_post(is_leaf, axis_now);
     }
 };
@@ -222,31 +229,53 @@
>
   struct
 print_array
+ /**@brief
+ * an instance of Array Visitor (i.e. argument to array_host<Array>::accept)
+ * where Array is array_dyn<T> for some T.
+ */
 {
       std::ostream&
     my_sout
+ /**@brief
+ * Where the array_dyn<T> is to be printed.
+ */
+ ;
+ unsigned
+ my_indent
+ /**@brief
+ * Margin indentation increment.
+ * Each successive axis of the array_dyn<T> is printed
+ * at offset that's a multiple of my_indent.
+ * For example, at axis, Iaxis, indentation is at my_indent*Iaxis.
+ */
     ;
         typedef typename
       array_dyn<T>::index_t
     index_t
     ;
- print_array(std::ostream& a_sout)
+ print_array(std::ostream& a_sout, unsigned a_indent=2)
     : my_sout(a_sout)
+ , my_indent(a_indent)
     {}
     
+ void visit_empty( bool is_leaf, index_t axis)
+ {
+ my_sout<<std::left<<std::setw(my_indent)<<"{";
+ }
+
     void visit_pre( bool is_leaf, index_t axis, index_t index)
     {
         if(index==0)
         {
- my_sout<<"{ ";
+ my_sout<<std::left<<std::setw(my_indent)<<"{";
         }
         else
         {
             if(!is_leaf)
             {
- my_sout<<std::setw(2*axis)<<"";
+ my_sout<<std::setw(my_indent*axis)<<"";
             }
- my_sout<<", ";
+ my_sout<<std::left<<std::setw(my_indent)<<",";
         }
     }
     
@@ -259,7 +288,7 @@
     {
         if(!is_leaf)
         {
- my_sout<<std::setw(2*axis)<<"";
+ my_sout<<std::setw(my_indent*axis)<<"";
         }
         my_sout<<"}\n";
     }


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