aboutsummaryrefslogtreecommitdiff
path: root/paludis/util/hashes.hh
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2011-01-22 23:56:08 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2011-01-22 23:56:08 +0000
commit62a3e9ef98827094e0daa27418c4a08677ba1458 (patch)
tree2c40236b20fe26376523e0ce35658513f621ce66 /paludis/util/hashes.hh
parentc05f56916407d1730903472718fed9576bd214c9 (diff)
downloadpaludis-62a3e9ef98827094e0daa27418c4a08677ba1458.tar.gz
paludis-62a3e9ef98827094e0daa27418c4a08677ba1458.tar.xz
Support hashing tuples
Diffstat (limited to 'paludis/util/hashes.hh')
-rw-r--r--paludis/util/hashes.hh40
1 files changed, 39 insertions, 1 deletions
diff --git a/paludis/util/hashes.hh b/paludis/util/hashes.hh
index c77681979..5d74eed79 100644
--- a/paludis/util/hashes.hh
+++ b/paludis/util/hashes.hh
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2008, 2010 Ciaran McCreesh
+ * Copyright (c) 2008, 2010, 2011 Ciaran McCreesh
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -24,6 +24,7 @@
#include <paludis/util/wrapped_value-fwd.hh>
#include <paludis/util/fs_path-fwd.hh>
#include <cstddef>
+#include <tuple>
#include <utility>
#include <string>
#include <type_traits>
@@ -83,6 +84,43 @@ namespace paludis
}
};
+ template <unsigned n_, typename... Keys_>
+ std::size_t single_tuple_hash(const std::tuple<Keys_...> & p)
+ {
+ return Hash<typename std::tuple_element<n_, std::tuple<Keys_...> >::type>()(std::get<n_>(p));
+ }
+
+ struct FinishedHashingTuple
+ {
+ };
+
+ struct NotFinishedHashingTuple
+ {
+ };
+
+ template <unsigned n_, typename... Keys_>
+ std::size_t accumulate_tuple_hash(const std::tuple<Keys_...> &, std::size_t v, const FinishedHashingTuple &)
+ {
+ return v;
+ }
+
+ template <unsigned n_, typename... Keys_>
+ std::size_t accumulate_tuple_hash(const std::tuple<Keys_...> & p, std::size_t v, const NotFinishedHashingTuple &)
+ {
+ return accumulate_tuple_hash<n_ + 1, Keys_...>(p, (v << 8) ^ single_tuple_hash<n_>(p),
+ typename std::conditional<std::tuple_size<std::tuple<Keys_...> >::value == n_ + 1,
+ FinishedHashingTuple, NotFinishedHashingTuple>::type());
+ }
+
+ template <typename... Keys_>
+ struct Hash<std::tuple<Keys_...> >
+ {
+ std::size_t operator() (const std::tuple<Keys_...> & p) const
+ {
+ return accumulate_tuple_hash<0, Keys_...>(p, 0, NotFinishedHashingTuple());
+ }
+ };
+
template <typename Tag_>
struct Hash<WrappedValue<Tag_> >
{