blob: 779f7f14e5ec8f5e1f5825f44ea615b38a9933ab [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 Afanasyev070aa482011-08-20 00:38:25 -070042 std::size_t
Ilya Moiseenko2bd1bc32011-08-23 16:01:35 -070043 operator() (const CcnxNameComponents &prefix) const
Alexander Afanasyev070aa482011-08-20 00:38:25 -070044 {
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 Moiseenko1c570bc2011-08-17 19:18:02 -070057};
Alexander Afanasyev070aa482011-08-20 00:38:25 -070058} // namespace ns3
59
60#endif // CCNX_HASH_HELPER_H