OverSim
HashFunc.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
24 #ifndef __HASHFUNC
25 #define __HASHFUNC
26 
27 #include <oversim_mapset.h>
28 #include <oversim_byteswap.h>
29 
30 #include <IPvXAddress.h>
31 #include <TransportAddress.h>
32 
33 #if defined(HAVE_GCC_TR1) || defined(HAVE_MSVC_TR1)
34 namespace std { namespace tr1 {
35 #else
36 namespace __gnu_cxx {
37 #endif
38 
42 template<> struct hash<IPvXAddress> : std::unary_function<IPvXAddress, std::size_t>
43 {
50  std::size_t operator()(const IPvXAddress& addr) const
51  {
52  if (addr.isIPv6()) {
53  return ((bswap_32(addr.get6().words()[0])) ^
54  (bswap_32(addr.get6().words()[1])) ^
55  (bswap_32(addr.get6().words()[2])) ^
56  (bswap_32(addr.get6().words()[3])));
57  } else {
58  return bswap_32(addr.get4().getInt());
59  }
60  }
61 };
62 
63 
67 template<> struct hash<TransportAddress> : std::unary_function<TransportAddress, std::size_t>
68 {
75  std::size_t operator()(const TransportAddress& addr) const
76  {
77  if (addr.getIp().isIPv6()) {
78  return (((bswap_32(addr.getIp().get6().words()[0])) ^
79  (bswap_32(addr.getIp().get6().words()[1])) ^
80  (bswap_32(addr.getIp().get6().words()[2])) ^
81  (bswap_32(addr.getIp().get6().words()[3]))) ^
82  addr.getPort());
83  } else {
84  return ((bswap_32(addr.getIp().get4().getInt())) ^
85  addr.getPort());
86  }
87  }
88 };
89 
90 }
91 #if defined(HAVE_GCC_TR1) || defined(HAVE_MSVC_TR1)
92 }
93 #endif
94 
95 #endif