contrib/mul/mbl/mbl_data_collector.txx
Go to the documentation of this file.
00001 // This is mul/mbl/mbl_data_collector.txx
00002 #ifndef mbl_data_collector_txx_
00003 #define mbl_data_collector_txx_
00004 //:
00005 //  \file
00006 
00007 #include "mbl_data_collector.h"
00008 
00009 #include <vcl_cassert.h>
00010 #include <vcl_algorithm.h>
00011 
00012 //=======================================================================
00013 // Dflt ctor
00014 //=======================================================================
00015 
00016 template<class T>
00017 mbl_data_collector<T>::mbl_data_collector()
00018 {
00019 }
00020 
00021 //=======================================================================
00022 // Destructor
00023 //=======================================================================
00024 
00025 template<class T>
00026 mbl_data_collector<T>::~mbl_data_collector()
00027 {
00028 }
00029 
00030 
00031 //: Copy all the data from a mbl_data_wrapper<T> into a mbl_data_collector<T>
00032 template<class T>
00033 unsigned long mbl_data_collector_copy_all(mbl_data_collector<T> &dest, mbl_data_wrapper<T> &src)
00034 {
00035   if (src.size() == 0) return 0;
00036   unsigned long i = 1;
00037 
00038   src.reset();
00039   dest.record(src.current());
00040   while (src.next())
00041   {
00042     dest.record(src.current());
00043     i++;
00044   }
00045   return i;
00046 }
00047 
00048 //: Merge all the data from the two mbl_data_wrapper-s into one mbl_data_collector<T>
00049 template<class T>
00050 unsigned long mbl_data_collector_merge_all(mbl_data_collector<T> &dest,
00051                                            mbl_data_wrapper<T > &src0,
00052                                            mbl_data_wrapper<T > &src1,
00053                                            vcl_vector<unsigned> *order /*=0*/)
00054 {
00055   unsigned long n0 = src0.size();
00056   unsigned long n1 = src1.size();
00057 
00058 // Deal with special cases
00059 
00060   if (n0 == 0L)
00061   {
00062     if (order)
00063     {
00064       order->resize(n1);
00065       vcl_fill(order->begin(), order->end(), 1);
00066     }
00067     return mbl_data_collector_copy_all(dest, src1);
00068   }
00069   if (n1 == 0L)
00070   {
00071     if (order)
00072     {
00073       order->resize(n0);
00074       vcl_fill(order->begin(), order->end(), 0);
00075     }
00076     return mbl_data_collector_copy_all(dest, src0);
00077   }
00078 
00079   if (order)
00080   {
00081     order->clear();
00082     order->reserve(n0+n1);
00083   }
00084 
00085   long remainder=0;
00086   src0.reset();
00087   src1.reset();
00088   for (unsigned long i=0; i < n0+n1; i++)
00089   {
00090     if (remainder <= 0L)
00091     {
00092       // Take from src0;
00093       dest.record(src0.current());
00094       remainder += n1;
00095       src0.next();
00096       if (order) order->push_back(0);
00097     }
00098     else
00099     {
00100       // Take from src1;
00101       dest.record(src1.current());
00102       remainder -= n0;
00103       src1.next();
00104       if (order) order->push_back(1);
00105     }
00106   }
00107 
00108   assert ((!src0.next()) && (!src1.next())); // check that we have reached the end.
00109   return n0 + n1;
00110 }
00111 
00112 #define MBL_DATA_COLLECTOR_INSTANTIATE(T) \
00113 template class mbl_data_collector<T >; \
00114 template unsigned long mbl_data_collector_copy_all(mbl_data_collector<T > &dest, \
00115                                     mbl_data_wrapper<T > &src);\
00116 template unsigned long mbl_data_collector_merge_all(mbl_data_collector<T > &dest,\
00117                                     mbl_data_wrapper<T > &src1,\
00118                                     mbl_data_wrapper<T > &src2,\
00119                                     vcl_vector<unsigned > *order)
00120 
00121 #endif // mbl_data_collector_txx_