Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7048247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:52:07+00:00 2026-05-28T02:52:07+00:00

Here is my first try at a generic histogram template function in C++ tested

  • 0

Here is my first try at a generic histogram template function in C++ tested with GCC 4.6. However, I would like to merge dense_histogram() and sparse_histogram() into one common generic function template. The problem is that the dense-specific constructor H h(n, 0) is neither defined nor relevant in the sparse version H h. Is there a way to solve this in some clever C++ regular way or statically typically using conditional compilation through Boost Type.Traits (#include <boost/type_traits.hpp>)?

#include <algorithm>
#include <limits>
#include <algorithm>
#include <vector>
#include <unordered_map>

namespace std
{

/*!
 * \em Dense Histogram of \p a.
 *
 * \tparam V is Value Type.
 * \tparam C is Count (Bin) Type.
 * \tparam H is Histogram Storage Type, typically a vector.
 *
 * \param[in] x is a set of the input data set
 */
template <class V, class C = size_t, class H = vector<C> >
inline
H dense_histogram(const V & x)
{
    typedef typename V::value_type E; // element type
    size_t n = (static_cast<C>(1)) << (8*sizeof(E)); // maximum number of possible elements for dense variant
    H h(n, 0);                       // histogram
    C bmax = 0;                      // bin max
    for_each(begin(x), end(x),  // C++11
             [&h, &bmax] (const E & e) { // value element
                 h[e]++;
                 bmax = std::max(bmax, h[e]);
             });
    return h;
}
template <class V, class H = vector<size_t> > H make_dense_histogram(const V & x) { return dense_histogram<V, size_t, H>(x); }

/*!
 * \em Sparse Histogram of \p a.
 *
 * \tparam V is Value Type.
 * \tparam C is Count (Bin) Type.
 * \tparam H is Histogram Structure Type, typically a unordered_map.
 *
 * \param[in] x is a set of the input data set
 */
template <class V, class C = size_t, class H = unordered_map<typename V::value_type, C> >
inline
H sparse_histogram(const V & x)
{
    typedef typename V::value_type E; // element type
    H h;                        // histogram
    C bmax = 0;                 // bin max
    for_each(begin(x), end(x), // C++11
             [&h,&bmax] (const E & e) { // value element
                 h[e]++;
                 bmax = std::max(bmax, h[e]);
             });
    return h;
}
template <class V, class H = unordered_map<typename V::value_type, size_t> > H make_sparse_histogram(const V & x) { return sparse_histogram<V, size_t, H>(x); }

}

run using

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:52:08+00:00Added an answer on May 28, 2026 at 2:52 am

    I think you should simply put only the common parts in a third function leaving dense_histogram and sparse_histogram to create h and call that implementation function:

    template <class V, class C = size_t, class H>
    inline void histogram_impl(const V & x, H& h) {
        typedef typename V::value_type E; // element type
        C bmax = 0;                      // bin max
        for_each(begin(x), end(x),  // C++11
                 [&h, &bmax] (const E & e) { // value element
                     h[e]++;
                     bmax = std::max(bmax, h[e]);
                 });
        return h;
    }
    template <class V, class C = size_t, class H = vector<C> >
    inline H dense_histogram(const V & x) {
        typedef typename V::value_type E; // element type
        size_t n = (static_cast<C>(1)) << (8*sizeof(E)); // maximum number of possible elements for dense variant
        H h(n, 0);                       // histogram
        histogram_impl(x, h);
        return h;
    }
    template <class V, class C = size_t, class H = unordered_map<typename V::value_type, C> >
    inline H sparse_histogram(const V & x) {
        H h;                        // histogram
        histogram_impl(x, h);
        return h;
    }
    

    However since you asked for it: As you are working on containers I would assume they have a cheap move, so you could define a creation trait to generate your container and move that into your local variable. Then you can write your own detection of an appropriate constructor like this:

    template<typename T> struct has_explicit_length_constructor{
    private:
       template<typename U>
       decltype(U(0, 0), void(), std::true_type()) test(int x);
       template<typename>
       std::false_type test(...);
      typedef decltype(test<T>(0)) constant_type;
    public:
       constexpr bool value = constant_type::value;
    };
    
    template<class H, bool B = has_explicit_length_constructor<H>::value> struct histogram_creation_trait;
    template<class H> struct histogram_creation_trait<H, true> {
      static H create()  {
        size_t n = (static_cast<C>(1)) << (8*sizeof(typename V::value_type));
        return H(n, 0);  
      }
    };
    template<class H> struct histogram_creation_trait<H, false>
    { static H create()  { return H(); } };
    
    template <class V, class C = size_t, class Ht>
    inline void histogram_impl(const V & x, H& h, Trait) {
        typedef typename V::value_type E; // element type
        C bmax = 0;                      // bin max
        H h = histogram_creation_trait<H>::create();
        for_each(begin(x), end(x),  // C++11
                 [&h, &bmax] (const E & e) { // value element
                     h[e]++;
                     bmax = std::max(bmax, h[e]);
                 });
        return h;
    }
    template <class V, class H = vector<size_t> > H make_dense_histogram(const V & x) { return histogram_impl<V, size_t, H>(x); }
    template <class V, class H = unordered_map<typename V::value_type, size_t> > H make_sparse_histogram(const V & x) { return histogram_impl<V, size_t, H>(x); }
    

    As a side not: Adding your own methods to std is UB by the standard ([namespace.std] $17.6.4.2.1 p1):

    The behavior of a C++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First here's what I'm using and trying to do: the minimal setup for this
I am new here so first of all my greetings to you I am
Here's my first question at SO. I have a internal application for my company
Here is the first part of my controller code: public class ControlMController : Controller
Here is my first attempt at validating XML with XSD. The XML file to
My first post here, so i hope this is the right area. I am
first question here. I'm developing a program in C# (.NET 3.5) that displays files
First post here... I normally develop using PHP and Symfony with Propel and ActionScript
First off here is the code! <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
First question here: it is a very short yet fundamental thing in Java that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.