OKlibrary  0.2.1.6
SequenceOperations.hpp
Go to the documentation of this file.
00001 // Oliver Kullmann, 12.6.2005 (Swansea)
00002 /* Copyright 2005 - 2007 Oliver Kullmann
00003 This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
00004 it and/or modify it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation and included in this library; either version 3 of the
00006 License, or any later version. */
00007 
00017 #ifndef SEQUENCEOPERATIONS_iKnB547
00018 #define SEQUENCEOPERATIONS_iKnB547
00019 
00020 #include <functional>
00021 #include <numeric>
00022 
00023 #include <boost/range/functions.hpp>
00024 #include <boost/range/metafunctions.hpp>
00025 #include <boost/range/iterator_range.hpp>
00026 #include <boost/iterator/transform_iterator.hpp>
00027 
00028 namespace OKlib {
00029   namespace SetAlgorithms {
00030 
00031     template <class Range>
00032     struct Size : std::unary_function<const Range&, typename boost::range_size<Range>::type> {
00033       typename boost::range_size<Range>::type operator() (const Range& r) const {
00034         using boost::distance;
00035         return distance(r);
00036       }
00037     };
00038     // ToDo: moving to a module for function objects?
00039 
00040     template <typename InputIterator_sets>
00041     struct Sum_sizes : std::binary_function<InputIterator_sets, InputIterator_sets, typename boost::range_size<typename InputIterator_sets::value_type>::type> {
00042       typename boost::range_size<typename InputIterator_sets::value_type>::type operator() (const InputIterator_sets begin, const InputIterator_sets end) const {
00043         typedef typename boost::range_size<typename InputIterator_sets::value_type>::type size_type;
00044         typedef typename InputIterator_sets::value_type Range;
00045         typedef Size<Range> size_functor;
00046         typedef boost::transform_iterator<size_functor, InputIterator_sets> transform_iterator;
00047         return std::accumulate(transform_iterator(begin), transform_iterator(end), size_type(0));
00048       }
00049     };
00050     // ToDo: moving to a module for function objects?
00051 
00052     template <typename InputIterator_sets>
00053     inline typename boost::range_size<typename InputIterator_sets::value_type>::type sum_sizes(const InputIterator_sets begin, const InputIterator_sets end) {
00054       return Sum_sizes<InputIterator_sets>()(begin, end);
00055     }
00056 
00057     // -----------------------------------------------------------------------------------------------------------------------------------
00058 
00059   }
00060 
00061 }
00062 
00063 #endif