SMTStabilizer API
Public API documentation for SMTStabilizer
Loading...
Searching...
No Matches
node_helper.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 XiangZhang
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21#include "node_helper.h"
22
23#include <cstdint>
24namespace stabilizer::util {
25size_t hash_node(const node::Node &node) {
26 size_t seed = 0;
27 size_t h1 = node->getSort()->hash_without_name();
28 size_t h2 = static_cast<size_t>(node->getKind());
29 size_t h3 = node->getChildren().size();
30 size_t h4 = node->isConst() ? std::hash<std::string>{}(node->toString()) : 0;
31
32 hash_combine(seed, h1);
33 hash_combine(seed, h2);
34 hash_combine(seed, h3);
35 hash_combine(seed, h4);
36
37 return seed;
38}
39
40size_t hash_mix(size_t x) {
41 constexpr size_t m = 0xe9846af9b1a615d;
42 x ^= x >> 32;
43 x *= m;
44 x ^= x >> 32;
45 x *= m;
46 x ^= x >> 28;
47 return x;
48}
49
50void hash_combine(size_t &seed, const size_t &value) {
51 seed = hash_mix(seed + 0x9e3779b9 + value);
52 // boost::hash_combine(seed, value);
53}
54
55void hash_children(size_t &seed, const std::vector<size_t> &children, const std::vector<size_t> &hash_table) {
56 for (const auto &child_index : children)
57 hash_combine(seed, hash_table.at(child_index));
58 // hash_combine(seed, SIZE_MAX);
59}
60
61void hash_communative_children(size_t &seed, const std::vector<size_t> &children, const std::vector<size_t> &hash_table) {
62 const size_t s2(seed);
63 for (const auto &child : children) {
64 size_t s3(s2);
65 hash_combine(s3, hash_table.at(child));
66 seed += s3;
67 }
68 // hash_combine(seed, SIZE_MAX);
69}
70
71void hash_parents(size_t &seed, const std::vector<std::pair<size_t, size_t>> &parents, const std::vector<size_t> &hash_table) {
72 const size_t s2(seed);
73 for (const auto &[parent_index, oper_index] : parents) {
74 size_t s3(s2);
75 hash_combine(s3, hash_table.at(parent_index));
76 hash_combine(s3, oper_index);
77 seed += s3;
78 }
79 // hash_combine(seed, SIZE_MAX);
80}
81} // namespace stabilizer::util
size_t hash_node(const node::Node &node)
Compute a stable structural hash seed for a parser/node DAG node.
void hash_combine(size_t &seed, const size_t &value)
Mix one value into a hash seed.
void hash_children(size_t &seed, const std::vector< size_t > &children, const std::vector< size_t > &hash_table)
Mix child-node hashes into a parent seed in argument order.
size_t hash_mix(size_t x)
void hash_parents(size_t &seed, const std::vector< std::pair< size_t, size_t > > &parents, const std::vector< size_t > &hash_table)
Propagate a node hash contribution to parent nodes.
void hash_communative_children(size_t &seed, const std::vector< size_t > &children, const std::vector< size_t > &hash_table)
Mix child-node hashes as a commutative multiset.