Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 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 | * Author: Ilya Moiseenko <iliamo@cs.ucla.edu> |
| 19 | */ |
| 20 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 21 | #ifndef CCNX_HASH_HELPER_H |
| 22 | #define CCNX_HASH_HELPER_H |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 23 | |
| 24 | #include <string> |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 25 | #include <boost/foreach.hpp> |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 26 | #include "ccnx-name-components.h" |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 28 | namespace ns3 |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 29 | { |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 30 | |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 31 | /** |
| 32 | * \ingroup ccnx-helpers |
| 33 | * \brief Helper providing hash value for the name prefix |
| 34 | * |
| 35 | * The whole prefix is considered as a long string with '/' delimiters |
| 36 | * |
| 37 | * \todo Testing is required to determine if this hash function |
| 38 | * actually provides good hash results |
| 39 | */ |
Ilya Moiseenko | 2bd1bc3 | 2011-08-23 16:01:35 -0700 | [diff] [blame] | 40 | struct CcnxPrefixHash : public std::unary_function<CcnxNameComponents, std::size_t> |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 41 | { |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 42 | std::size_t |
Ilya Moiseenko | 2bd1bc3 | 2011-08-23 16:01:35 -0700 | [diff] [blame] | 43 | operator() (const CcnxNameComponents &prefix) const |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 44 | { |
| 45 | std::size_t hash = 23; |
| 46 | BOOST_FOREACH (const std::string &str, prefix.GetComponents ()) |
| 47 | { |
| 48 | hash += str.size (); |
| 49 | hash = ((hash << 6) ^ (hash >> 27)) + '/'; |
| 50 | BOOST_FOREACH (char c, str) |
| 51 | { |
| 52 | hash = ((hash << 6) ^ (hash >> 27)) + c; |
| 53 | } |
| 54 | } |
| 55 | return hash; |
| 56 | } |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 57 | }; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 58 | } // namespace ns3 |
| 59 | |
| 60 | #endif // CCNX_HASH_HELPER_H |