tuple_hash.hh

00001 // This file is a part of Aurelia.
00002 // Copyright (C) 2010  Valentin David
00003 // Copyright (C) 2010  University of Bergen
00004 //
00005 // This program is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 #ifndef __TUPLE_HASH_HH
00018 # define __TUPLE_HASH_HH
00019 
00020 # include <tuple>
00021 # include "mhash.hh"
00022 
00023 namespace aurelia {
00024   template <typename T, size_t N = 0>
00025   struct get_hash {
00026   };
00027 
00028   template <typename... T, size_t N>
00029   struct get_hash<std::tuple<T...>, N> {
00030     static void make(size_t& hash, const std::tuple<T...>& t) {
00031       get_hash<std::tuple<T...>, N-1>::make(hash, t);
00032       hash = (hash << 11) | (hash >> 21);
00033       hash ^= mhash<typename std::tuple_element<N, std::tuple<T...> >::type>()
00034         (std::get<N>(t));
00035     }
00036   };
00037 
00038   template <typename... T>
00039   struct get_hash<std::tuple<T...>, 0u> {
00040     static void make(size_t& hash, const std::tuple<T...>& t) {
00041       hash = mhash<typename std::tuple_element<0, std::tuple<T...> >::type>()
00042         (std::get<0>(t));
00043     }
00044   };
00045 
00046 }
00047 
00048 namespace std {
00049   template <typename... T>
00050   struct hash<tuple<T...> >: public unary_function<tuple<T...>, size_t> {
00051     size_t operator()(const tuple<T...>& t) const {
00052       size_t hash = 0;
00053       aurelia::get_hash<tuple<T...>, tuple_size<tuple<T...> >::value-1>::make(hash, t);
00054       return hash;
00055     }
00056   };
00057 
00058   template <>
00059   struct hash<tuple<> >: public unary_function<tuple<>, size_t> {
00060     size_t operator()(const tuple<>& ) const {
00061       return 0;
00062     }
00063   };
00064 }
00065 
00066 #endif