使用boost :: hash和boost :: property_tree?

时间:2017-07-21 17:59:12

标签: boost hash boost-propertytree

我正在尝试为包含boost :: property_tree(boost::property_tree::basic_ptree<std::basic_string<char>, std::basic_string<char> >)的对象生成哈希值。在属性树的boost头文件中搜索我找不到任何已定义的hash_value函数。我想要实现的基本例子:

class MyClass{
public:
    friend std::size_t hash_value(const MyClass & obj);
private:
    boost::property_tree m_data;
}

inline std::size_t hash_value(const MyClass & obj){
    std::size_t seed = 0;
    boost::hash_combine(seed,obj.m_data);
    return seed;
}

此代码无法编译:     “没有匹配函数来调用'hash_value(const boost :: property_tree&amp;)'”

我的问题:是否在某些头文件中为boost :: property_tree定义了hash_value我还没找到。如果没有,通过遍历它来对一个property_tree进行散列的提升惯用方法是什么?

我是否应该使用ptree序列化转换为std :: string并哈希,或者手动遍历树并创建递归哈希?

1 个答案:

答案 0 :(得分:1)

为ptree专门设置hash<>

#include <boost/property_tree/ptree.hpp>
#include <boost/functional/hash.hpp>

namespace boost {
    template<typename Key, typename Data, typename KeyCompare>
    struct hash<boost::property_tree::basic_ptree<Key, Data, KeyCompare> > {
        size_t operator()(boost::property_tree::basic_ptree<Key, Data, KeyCompare> const& pt) const {
            std::size_t seed = 0;
            boost::hash_combine(seed, pt.template get_value<std::string>());
            boost::hash_range(seed, pt.begin(), pt.end());
            return seed;
        }
    };
}

这就够了!这是一个小的MyClass,它从类似json的文字中读取:

#include <boost/property_tree/json_parser.hpp>

class MyClass{
  public:
      MyClass(std::string const& json) {
          std::istringstream iss(json);
          read_json(iss, m_data);
      }
  private:
    boost::property_tree::ptree m_data;

    friend inline std::size_t hash_value(const MyClass& obj){
        std::size_t seed = 0;
        boost::hash_combine(seed, obj.m_data);
        return seed;
    }
};

现在你可以测试它了

<强> Live On Coliru

#include <iostream>

int main() {
    for (std::string const data : {
            R"({"a":[1,2,3],"b":{"nest":"hello","more":"world"}})",
            R"({"b":{"nest":"hello","more":"world"},"a":[1,2,3]})",
            R"({ })",
            R"({})",
        })
    {
        MyClass o(data);
        std::cout << "object hash: " << hash_value(o) << " " << data << "\n";
    }
}

打印:

object hash: 3573231694259656572 {"a":[1,2,3],"b":{"nest":"hello","more":"world"}}
object hash: 11176663460548092204 {"b":{"nest":"hello","more":"world"},"a":[1,2,3]}
object hash: 3864292196 { }
object hash: 3864292196 {}

注意:哈希和平等

对于许多容器,哈希&lt;&gt;假设 对应的 相等比较器。如果它们不匹配,则会获得Undefined Behaviour

您可能想要根据hash_range的有序(关联)界面定义ptree

boost::hash_range(seed, pt.ordered_begin(), pt.not_found()); // CAUTION

这样做的好处是{"a":1,"b":2}会匹配{"b":2,"a":1}

除非你知道自己在做什么,否则不要这样做。具体来说,您需要将兼容的相等比较器传递给使用此哈希的每个容器/算法。

如果您这样写,并使用如下驱动程序进行测试:

int main() {
    MyClass a{ R"({"a":[1,2,3],"b":{"nest":"hello","more":"world"}})" },
            b{R"({"b":{"nest":"hello","more":"world"},"a":[1,2,3]})" },
            c{R"({ })" },
            d{R"({})" };

    for (auto& lhs : {a,b,c,d})
    for (auto& rhs : {a,b,c,d})
    {
        std::cout << "hash: " << hash_value(lhs) << " " << hash_value(rhs) << " - equality: " << std::boolalpha << (lhs==rhs) << "\n";
        if ((hash_value(lhs) == hash_value(rhs)) != (lhs==rhs))
            std::cout << " -- MISMATCH\n";
    }
}

它将打印:

hash: 10737438301360613971 10737438301360613971 - equality: true
hash: 10737438301360613971 10737438301360613971 - equality: false
 -- MISMATCH
hash: 10737438301360613971 3864292196 - equality: false
hash: 10737438301360613971 3864292196 - equality: false
hash: 10737438301360613971 10737438301360613971 - equality: false
 -- MISMATCH
hash: 10737438301360613971 10737438301360613971 - equality: true
hash: 10737438301360613971 3864292196 - equality: false
hash: 10737438301360613971 3864292196 - equality: false
hash: 3864292196 10737438301360613971 - equality: false
hash: 3864292196 10737438301360613971 - equality: false
hash: 3864292196 3864292196 - equality: true
hash: 3864292196 3864292196 - equality: true
hash: 3864292196 10737438301360613971 - equality: false
hash: 3864292196 10737438301360613971 - equality: false
hash: 3864292196 3864292196 - equality: true
hash: 3864292196 3864292196 - equality: true

MISMATCH警告表明平等和哈希不同意。

如果您使用原始哈希(在首屏上方)运行测试驱动程序,它将打印:

hash: 3573231694259656572 3573231694259656572 - equality: true
hash: 3573231694259656572 11176663460548092204 - equality: false
hash: 3573231694259656572 3864292196 - equality: false
hash: 3573231694259656572 3864292196 - equality: false
hash: 11176663460548092204 3573231694259656572 - equality: false
hash: 11176663460548092204 11176663460548092204 - equality: true
hash: 11176663460548092204 3864292196 - equality: false
hash: 11176663460548092204 3864292196 - equality: false
hash: 3864292196 3573231694259656572 - equality: false
hash: 3864292196 11176663460548092204 - equality: false
hash: 3864292196 3864292196 - equality: true
hash: 3864292196 3864292196 - equality: true
hash: 3864292196 3573231694259656572 - equality: false
hash: 3864292196 11176663460548092204 - equality: false
hash: 3864292196 3864292196 - equality: true
hash: 3864292196 3864292196 - equality: true