blob: 45e9f0e5774e8642be974b380bf1f887f30f9ede [file] [log] [blame]
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -07001/* -*- 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 Afanasyev070aa482011-08-20 00:38:25 -070021#ifndef CCNX_HASH_HELPER_H
22#define CCNX_HASH_HELPER_H
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070023
24#include <string>
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070025#include <boost/foreach.hpp>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070026#include "ccnx-name-components.h"
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070027
Alexander Afanasyev070aa482011-08-20 00:38:25 -070028namespace ns3
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070029{
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070030
Alexander Afanasyev070aa482011-08-20 00:38:25 -070031/**
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 Moiseenko2bd1bc32011-08-23 16:01:35 -070040struct CcnxPrefixHash : public std::unary_function<CcnxNameComponents, std::size_t>
Ilya Moiseenko1c570bc2011-08-17 19:18:02 -070041{
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070042 /**
43 * @brief Compute hash of the name prefix
44 */
Alexander Afanasyev070aa482011-08-20 00:38:25 -070045 std::size_t
Ilya Moiseenko2bd1bc32011-08-23 16:01:35 -070046 operator() (const CcnxNameComponents &prefix) const
Alexander Afanasyev070aa482011-08-20 00:38:25 -070047 {
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 Moiseenko1c570bc2011-08-17 19:18:02 -070060};
Alexander Afanasyev070aa482011-08-20 00:38:25 -070061} // namespace ns3
62
63#endif // CCNX_HASH_HELPER_H