ivl 679
ivl/details/array_nd/impl/array_nd.hpp
00001 /* This file is part of the ivl C++ library <http://image.ntua.gr/ivl>.
00002    A C++ template library extending syntax towards mathematical notation.
00003 
00004    Copyright (C) 2012 Yannis Avrithis <iavr@image.ntua.gr>
00005    Copyright (C) 2012 Kimon Kontosis <kimonas@image.ntua.gr>
00006 
00007    ivl is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU Lesser General Public License 
00009    version 3 as published by the Free Software Foundation.
00010 
00011    Alternatively, you can redistribute it and/or modify it under the terms 
00012    of the GNU General Public License version 2 as published by the Free 
00013    Software Foundation.
00014 
00015    ivl is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00018    See the GNU General Public License for more details.
00019 
00020    You should have received a copy of the GNU General Public License 
00021    and a copy of the GNU Lesser General Public License along 
00022    with ivl. If not, see <http://www.gnu.org/licenses/>. */
00023 
00024 #ifndef IVL_ARRAY_ND_DETAILS_ARRAY_ND_BASE_HPP
00025 #define IVL_ARRAY_ND_DETAILS_ARRAY_ND_BASE_HPP
00026 
00027 namespace ivl {
00028 
00029 template<class T, class S> class array_nd;
00030 //template<class T> class gslice_array;
00031 //template<class T, class S, bool C> class sub_array;
00032 
00033 typedef array_nd<bool, mem> bool_array_nd;
00034 
00035 //print an array_nd
00036 //template<class T>
00037 //std::ostream& print(std::ostream& os, const array_nd<T, array_nd<T, types::term> >& a);
00038 
00039 
00040 #include "specialization/array_nd_class.hpp"
00041 #include "specialization/array_nd_2d_class.hpp"
00042 #include "specialization/array_nd_image_class.hpp"
00043 //#include "specialization/force_class.hpp"
00044 #include "specialization/wrap_array_nd_class.hpp"
00045 #include "specialization/subarray_class.hpp"
00046 #include "specialization/catarray_class.hpp"
00047 #include "specialization/unary_elem_func_nd_class.hpp"
00048 #include "specialization/binary_elem_func_nd_class.hpp"
00049 #include "specialization/binary_elem_func_ptr_nd_class.hpp"
00050 
00051 
00052 // --------------------------------------------------------
00053 // Print functions
00054 
00055 namespace array_nd_details {
00056 
00057 template<class T, class D>
00058 std::ostream& print_array_nd (std::ostream& os,
00059                         const array_nd<T, D>& a, bool float_point = false)
00060 {
00061         std::ios::fmtflags saveflags = os.flags(); // save formatting flags
00062 
00063         if(float_point)
00064                 os << std::fixed << std::setprecision(4);
00065 
00066         if (a.ndims() == 0) {
00067                 os << "[unassigned]";
00068                 return os;
00069         }
00070 
00071         if (a.ndims() == 1) {
00072                 os << "[";
00073                 for (size_t j = 0; j < a.length(); ++j) {
00074                         print_nd_val(os, a(ivl::idx(j)));
00075                 }
00076                 os << "]";
00077 
00078                 os.flags(saveflags) ; // restore formatting flags
00079                 return os;
00080         }
00081 
00082         array<size_t, tiny> id(a.ndims(), size_t(0));//todo:here!
00083         bool stop = false;
00084 
00085         do {
00086                 size_t j, k;
00087 
00088                 if (a.ndims() > 2) {
00089                         // print coordinates
00090                         os << std::endl << "[";
00091                         for (j = a.ndims() - 1; j > 1; j--)
00092                                 os << (j < a.ndims() - 1 ? ", " : "") << id[j];
00093                         os << "]" << std::endl;
00094                 }
00095 
00096                 // print current 2-d block
00097                 for (k = 0; k < a.size_nd()[0]; ++k) {
00098                         for (j = 0; j < a.size_nd()[1]; ++j) {
00099                                 id[0] = k;
00100                                 id[1] = j;
00101                                 print_nd_val(os, a(id));
00102                         }
00103                         os << std::endl;
00104                 }
00105 
00106                 // increment indices
00107                 if(a.ndims() > 2)
00108                 {
00109                         for (j = 2; j < a.ndims(); j++)
00110                                 if(++id[j] >= a.size_nd(j)) {
00111                                         if(j == a.ndims() - 1)
00112                                                 stop = true;
00113                                         else {
00114                                                 id[j] = 0;
00115                                         }
00116                                 } else
00117                                         break;
00118 
00119                 }
00120                 else
00121                         stop  = true;
00122 
00123         } while(!stop);
00124 
00125         os.flags(saveflags) ; // restore formatting flags
00126         return os;
00127 }
00128 
00129 template<class T>
00130 inline
00131 void print_nd_val(std::ostream& os, const T& val)
00132 {
00133         os << std::setw(6) << val;
00134 }
00135 
00136 template<>
00137 inline
00138 void print_nd_val<float>(std::ostream& os, const float& val)
00139 {
00140         os << std::setw(11);
00141         if(ivl::math::ivl_isnan(val))
00142                 os << "NaN";
00143         else if(!ivl::math::ivl_finite(val))
00144                 os << "Inf";
00145         else
00146                 os << val;
00147         os << " ";
00148 }
00149 
00150 template<>
00151 inline
00152 void print_nd_val<double>(std::ostream& os, const double& val)
00153 {
00154         os << std::setw(11);
00155         if(ivl::math::ivl_isnan(val))
00156                 os << "NaN";
00157         else if(!ivl::math::ivl_finite(val))
00158                 os << "Inf";
00159         else
00160                 os << val;
00161         os << " ";
00162 }
00163 
00164 } /* namespace array_nd_details */
00165 
00166 template <class T, class D>
00167 inline
00168 std::ostream& print(std::ostream& os, const array_nd<T, D>& in)
00169 {
00170         return array_nd_details::print_array_nd(os, in);
00171 }
00172 
00173 template<class D>
00174 inline
00175 std::ostream& print(std::ostream& os, const array_nd<double, D>& in)
00176 {
00177         return array_nd_details::print_array_nd(os, in, true);
00178 }
00179 
00180 template <class D>
00181 inline
00182 std::ostream& print(std::ostream& os, const array_nd<float, D>& in)
00183 {
00184         return array_nd_details::print_array_nd(os, in, true);
00185 }
00186 
00187 template <class T, class D>
00188 std::ostream& array_common_base<array_nd<T, D> >::
00189         print(std::ostream& os) const
00190 {
00191         return ivl::print(os, to_array_nd());
00192 }
00193 
00194 } // namespace ivl
00195 
00196 #endif // IVL_ARRAY_ND_DETAILS_ARRAY_ND_BASE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations