diff options
| author | Josh Blum <josh@joshknows.com> | 2010-06-14 10:44:50 -0700 | 
|---|---|---|
| committer | Josh Blum <josh@joshknows.com> | 2010-06-14 10:44:50 -0700 | 
| commit | 0c6fbdfe3738e2d15b369fbd28d51ee0115628f9 (patch) | |
| tree | 16e6c263f55957887ec57da055f27c2cb65f1b23 | |
| parent | 126444c564d30d65116be5f772bdf47594d62884 (diff) | |
| download | uhd-0c6fbdfe3738e2d15b369fbd28d51ee0115628f9.tar.gz uhd-0c6fbdfe3738e2d15b369fbd28d51ee0115628f9.tar.bz2 uhd-0c6fbdfe3738e2d15b369fbd28d51ee0115628f9.zip | |
work on algorithms and documentation
| -rw-r--r-- | host/include/uhd/utils/algorithm.hpp | 72 | ||||
| -rw-r--r-- | host/lib/device.cpp | 8 | ||||
| -rw-r--r-- | host/lib/usrp/tune_helper.cpp | 2 | 
3 files changed, 68 insertions, 14 deletions
| diff --git a/host/include/uhd/utils/algorithm.hpp b/host/include/uhd/utils/algorithm.hpp index 146b56c63..08977a69f 100644 --- a/host/include/uhd/utils/algorithm.hpp +++ b/host/include/uhd/utils/algorithm.hpp @@ -19,31 +19,89 @@  #define INCLUDED_UHD_UTILS_ALGORITHM_HPP  #include <algorithm> -#include <boost/range/functions.hpp> +#include <boost/range/begin.hpp> +#include <boost/range/end.hpp> +#include <boost/range/size.hpp> -/*! - * Useful templated functions and classes that I like to pretend are part of stl +/*! \file algorithm.hpp + * Useful templated functions and classes that I like to pretend are part of stl. + * Many of the range wrapper functions come with recent versions of boost (1.43).   */  namespace std{ +    /*! +     * A wrapper around std::copy that takes ranges instead of iterators. +     * +     * Copy the elements of the source range into the destination range. +     * The destination range should be at least as large as the source range. +     * +     * \param src the range of elements to copy from +     * \param dst the range of elements to be filled +     */      template<typename RangeSrc, typename RangeDst> inline      void copy(const RangeSrc &src, RangeDst &dst){          std::copy(boost::begin(src), boost::end(src), boost::begin(dst));      } +    /*! +     * A wrapper around std::sort that takes a range instead of an iterator. +     * +     * The elements are sorted into ascending order using the less-than operator. +     * +     * \param range the range of elements to be sorted +     */ +    template<typename Range> inline void sort(Range &range){ +        return std::sort(boost::begin(range), boost::end(range)); +    } + +    /*! +     * A wrapper around std::sort that takes a range instead of an iterator. +     * +     * The elements are sorted into ascending order using the less-than operator. +     * This wrapper sorts the elements non-destructively into a new range. +     * Based on the builtin python function sorted(...) +     * +     * \param range the range of elements to be sorted +     * \return a new range with the elements sorted +     */ +    template<typename Range> inline Range sorted(const Range &range){ +        Range srange(range); std::sort(srange); return srange; +    } + +    /*! +     * Is the value found within the elements in this range? +     * +     * Uses std::find to search the iterable for an element. +     * +     * \param range the elements to search through +     * \param value the match to look for in the range +     * \return true when the value is found in the range +     */      template<typename Range, typename T> inline      bool has(const Range &range, const T &value){          return boost::end(range) != std::find(boost::begin(range), boost::end(range), value);      } -    template<typename T> inline T signum(T n){ +    /*! +     * A templated signum implementation. +     * \param n the comparable to process +     * \return -1 when n negative, +1 when n positive, otherwise 0 +     */ +    template<typename T> inline int signum(T n){          if (n < 0) return -1; -        if (n > 0) return 1; +        if (n > 0) return +1;          return 0;      } -    template<typename T> inline T clip(T val, T minVal, T maxVal){ -        return std::min(std::max(val, minVal), maxVal); +    /*! +     * A templated clip implementation. +     * \param val the value to clip between an upper and lower limit +     * \param bound1 the upper or lower bound +     * \param bound2 the upper or lower bound +     * \return the value clipped at the bounds +     */ +    template<typename T> inline T clip(T val, T bound1, T bound2){ +        return std::min(std::max(val, std::min(bound1, bound2)), std::max(bound1, bound2));      }  }//namespace std diff --git a/host/lib/device.cpp b/host/lib/device.cpp index f139ecb20..431595c4f 100644 --- a/host/lib/device.cpp +++ b/host/lib/device.cpp @@ -19,13 +19,13 @@  #include <uhd/types/dict.hpp>  #include <uhd/utils/assert.hpp>  #include <uhd/utils/static.hpp> +#include <uhd/utils/algorithm.hpp>  #include <boost/foreach.hpp>  #include <boost/format.hpp>  #include <boost/weak_ptr.hpp>  #include <boost/functional/hash.hpp>  #include <boost/tuple/tuple.hpp>  #include <stdexcept> -#include <algorithm>  using namespace uhd; @@ -41,13 +41,9 @@ using namespace uhd;  static size_t hash_device_addr(      const device_addr_t &dev_addr  ){ -    //sort the keys of the device address -    std::vector<std::string> keys = dev_addr.keys(); -    std::sort(keys.begin(), keys.end()); -      //combine the hashes of sorted keys/value pairs      size_t hash = 0; -    BOOST_FOREACH(const std::string &key, keys){ +    BOOST_FOREACH(const std::string &key, std::sorted(dev_addr.keys())){          boost::hash_combine(hash, key);          boost::hash_combine(hash, dev_addr[key]);      } diff --git a/host/lib/usrp/tune_helper.cpp b/host/lib/usrp/tune_helper.cpp index a7d695b4e..082c39f6d 100644 --- a/host/lib/usrp/tune_helper.cpp +++ b/host/lib/usrp/tune_helper.cpp @@ -46,7 +46,7 @@ static tune_result_t tune_xx_subdev_and_dxc(      // Calculate the DDC setting that will downconvert the baseband from the      // daughterboard to our target frequency.      double delta_freq = target_freq - actual_inter_freq; -    double delta_sign = std::signum(delta_freq); +    int delta_sign = std::signum(delta_freq);      delta_freq *= delta_sign;      delta_freq = std::fmod(delta_freq, dxc_sample_rate);      bool inverted = delta_freq > dxc_sample_rate/2.0; | 
