ivl 679
ivl/details/mfunc/find.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_MFUNC_FIND
00025 #define IVL_MFUNC_FIND
00026 
00027 namespace ivl {
00028 
00029 static  __attribute__ ((unused))
00030 struct find_impl : public ivl_func<find_impl>
00031 {
00037         template <class T1, class S1, class T2, class S2>
00038         void operate(array<T1, S1>& indexes, sep,
00039                                         const array<T2, S2>& in, const T2& what, size_t start = 0);
00040 
00041         template <class T, class S>
00042         size_array operator()(const array<T, S>& in, const T& what,
00043                                                 size_t start = 0)
00044         {
00045                 return call<size_array>(in, what, start);
00046         }
00047 
00050         template <class T1, class S1, class S2>
00051         void operate(array<T1, S1>& indexes, sep, const array<bool, S2>& ba);
00052 
00053         template <class S>
00054         size_array operator()(const array<bool, S>& in)
00055         {
00056                 return call<size_array>(in);
00057         }
00058 
00064         template<class T1, class S1, class K1, class T2, class S2>
00065         void operate(array<array<size_t, K1>, S1>& indexes_nd, sep,
00066                 const array_nd<T2, S2>& a, const T2& what);
00067 
00068         template<class T, class S>
00069         array<array<size_t, stack<3> > > operator()(
00070                 const array_nd<T, S>& a, const T& what)
00071         {
00072                 return call<
00073                         array<array<size_t, stack<3> > > >(a, what);
00074         }
00075 
00076 
00078         /*
00079         template<class T1, class S1, class S2, class S3>
00080         void operate(array<T1, S1>& row, array<T1, S2>& col, sep,
00081                 const array_2d<bool, S3>& in);
00082         */
00083 
00084 } find;
00085 
00086 
00092 template <class T1, class S1, class T2, class S2>
00093 void find_impl::operate(array<T1, S1>& indexes, sep,
00094                                 const array<T2, S2>& in, const T2& what, size_t start)
00095 {
00096         size_array a(in.length());
00097         size_t n = 0;
00098         for (size_t i = start; i < in.length(); i++)
00099                 if (in[i] == what)
00100                         a[n++] = i;
00101 
00102         indexes = a[rng(0, n - 1)];
00103         //indexes.resize(n);
00104 
00105 
00106         // TODO: wipe these wrong parts when proved working
00107         indexes[*_] = a;
00108         indexes[rng(0, n - 1)] = a;
00109 }
00110 
00113 template <class T1, class S1, class S2>
00114 void find_impl::operate(array<T1, S1>& indexes, sep, const array<bool, S2>& ba)
00115 {
00116         const size_t len = ba.length();
00117         size_array a(len);
00118         size_t n = 0;
00119 
00120         for (size_t i = 0; i < len; i++)
00121                 if (ba[i])
00122                         a[n++] = i;
00123 
00124         indexes = a[rng(0, n - 1)];
00125 }
00126 
00132 template<class T1, class S1, class K1, class T2, class S2>
00133 void find_impl::operate(array<array<size_t, K1>, S1>& b, sep,
00134         const array_nd<T2, S2>& a, const T2& what)
00135 {
00136         b.resize(a.length());
00137         size_t n = 0;
00138 
00139         for (size_t i = 0; i < a.length(); i++)
00140                 if (a[i] == what)
00141                         b[n++] = (ind2sub(a, i));
00142 
00143         b.resize(n);
00144 }
00145 
00146 } /* namespace ivl */
00147 
00148 #endif // IVL_MFUNC_FIND
 All Classes Namespaces Files Functions Variables Typedefs Enumerations