Skip to content

Commit 7a68f46

Browse files
committed
irept::string_hash: irept hashing using string content
Compute hash values with respect to string content, not just string container indices (as irept::hash and irept::full_hash do).
1 parent 945c1e0 commit 7a68f46

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/util/irep.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,33 @@ std::size_t irept::hash() const
450450
return result;
451451
}
452452

453+
std::size_t irept::string_hash() const
454+
{
455+
const irept::subt &sub = get_sub();
456+
const irept::named_subt &named_sub = get_named_sub();
457+
458+
std::size_t result = hash_string(id2string(id()));
459+
460+
for(const auto &irep : sub)
461+
result = hash_combine(result, irep.string_hash());
462+
463+
std::size_t number_of_named_ireps = 0;
464+
465+
for(const auto &irep_entry : named_sub)
466+
{
467+
if(!is_comment(irep_entry.first)) // this variant ignores comments
468+
{
469+
result = hash_combine(result, hash_string(id2string(irep_entry.first)));
470+
result = hash_combine(result, irep_entry.second.string_hash());
471+
number_of_named_ireps++;
472+
}
473+
}
474+
475+
result = hash_finalize(result, sub.size() + number_of_named_ireps);
476+
477+
return result;
478+
}
479+
453480
std::size_t irept::full_hash() const
454481
{
455482
const irept::subt &sub=get_sub();

src/util/irep.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,12 @@ class irept
452452

453453
std::size_t hash() const;
454454
std::size_t full_hash() const;
455+
/// Compute hash value while hashing the actual string content instead of
456+
/// string indices. These hash values remain stable even when the string
457+
/// container is built in a different order. Note that this can be much slower
458+
/// than using `hash()` as string lookups are required, strings need to be
459+
/// iterated over, and the results are not cached.
460+
std::size_t string_hash() const;
455461

456462
bool full_eq(const irept &other) const;
457463

0 commit comments

Comments
 (0)