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 | b4fee8b | 2012-06-06 12:54:26 -0700 | [diff] [blame] | 42 | /** |
| 43 | * @brief Compute hash of the name prefix |
| 44 | */ |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 45 | std::size_t |
Ilya Moiseenko | 2bd1bc3 | 2011-08-23 16:01:35 -0700 | [diff] [blame] | 46 | operator() (const CcnxNameComponents &prefix) const |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 47 | { |
| 48 | std::size_t hash = 23; |
| 49 | BOOST_FOREACH (const std::string &str, prefix.GetComponents ()) |
| 50 | { |
| 51 | hash += str.size (); |
| 52 | hash = ((hash << 6) ^ (hash >> 27)) + '/'; |
| 53 | BOOST_FOREACH (char c, str) |
| 54 | { |
| 55 | hash = ((hash << 6) ^ (hash >> 27)) + c; |
| 56 | } |
| 57 | } |
| 58 | return hash; |
| 59 | } |
Ilya Moiseenko | 1c570bc | 2011-08-17 19:18:02 -0700 | [diff] [blame] | 60 | }; |
Alexander Afanasyev | 070aa48 | 2011-08-20 00:38:25 -0700 | [diff] [blame] | 61 | } // namespace ns3 |
| 62 | |
| 63 | #endif // CCNX_HASH_HELPER_H |