1

The Boost documentation doesn't elaborate much, but there is an (optional) KeyCompare function that can be passed to the ptree.

Anyone have a good example of using a custom KeyCompare function?

I have recently been working with a ptree that is real slow. My keys are long strings (paths), and I assuming it's the string comparisons that make it slow.

From what I can glean, the default KeyCompare is std::less(), I want to change this. I think something that just compares the hashes of the two strings.

It goes without saying (but I'll say it anyway) that I would use a different object for the key to facilitate this: Something that has (std::string+hash), rather than just a std::string. The hash would be calculated during construction.

Thanks, Rik.

ukdiveboy
  • 65
  • 7

1 Answers1

2

Found this from the boost source code: An example of a case-insensitive KeyCompare:

   template<class T>
    struct less_nocase
    {
        typedef typename T::value_type Ch;
        std::locale m_locale;
        inline bool operator()(Ch c1, Ch c2) const
        {
            return std::toupper(c1, m_locale) < std::toupper(c2, m_locale);
        }
        inline bool operator()(const T &t1, const T &t2) const
        {
            return std::lexicographical_compare(t1.begin(), t1.end(),
                                                t2.begin(), t2.end(), *this);
        }
    };

Then all you need to do is pass it in to the basic_ptree class:

typedef basic_ptree<std::string, std::string,
                        less_nocase<std::string> >  iptree;
Richard
  • 2,994
  • 1
  • 19
  • 31