Refactor to make include order explicit, without guards.

This commit is contained in:
Peter D. Barnes, Jr.
2012-11-13 16:34:34 -08:00
parent 62fd21d15a
commit 94d417c60f
10 changed files with 235 additions and 177 deletions

View File

@@ -30,18 +30,17 @@
#include <sys/types.h>
#include <stdlib.h>
#include "ns3/hash.h"
#include "ns3/hash-fnv.h"
#include "ns3/log.h"
#include "log.h"
#include "hash-fnv.h"
namespace ns3 {
NS_LOG_COMPONENT_DEFINE ("Hash-Fnv");
namespace HashImplNS {
namespace Fnv1aNS {
namespace HashFunction {
namespace Fnv1aImplementation {
/*************************************************
** class FnvHashImplementation
@@ -152,7 +151,7 @@ typedef u_int32_t Fnv32_t;
*
* Use fully qualified type so this define works outside this scope //PDB
*/
#define FNV0_32_INIT ((HashImplNS::Fnv1aNS::Fnv32_t)0)
#define FNV0_32_INIT ((HashFunction::Fnv1aImplementation::Fnv32_t)0)
/*
@@ -169,7 +168,7 @@ typedef u_int32_t Fnv32_t;
*
* Use fully qualified type so this define works outside this scope //PDB
*/
#define FNV1_32_INIT ((HashImplNS::Fnv1aNS::Fnv32_t)0x811c9dc5)
#define FNV1_32_INIT ((HashFunction::Fnv1aImplementation::Fnv32_t)0x811c9dc5)
#define FNV1_32A_INIT FNV1_32_INIT
@@ -202,10 +201,10 @@ typedef struct {
* Use fully qualified type so this define works outside this scope //PDB
*/
#if defined(HAVE_64BIT_LONG_LONG)
#define FNV0_64_INIT ((HashImplNS::Fnv1aNS::Fnv64_t)0)
#define FNV0_64_INIT ((HashFunction::Fnv1aImplementation::Fnv64_t)0)
#else /* HAVE_64BIT_LONG_LONG */
extern const Fnv64_t fnv0_64_init;
#define FNV0_64_INIT (HashImplNS::Fnv1aNS::fnv0_64_init)
#define FNV0_64_INIT (HashFunction::Fnv1aImplementation::fnv0_64_init)
#endif /* HAVE_64BIT_LONG_LONG */
@@ -222,7 +221,7 @@ extern const Fnv64_t fnv0_64_init;
* NOTE: The FNV-1a initial basis is the same value as FNV-1 by definition.
*/
#if defined(HAVE_64BIT_LONG_LONG)
#define FNV1_64_INIT ((HashImplNS::Fnv1aNS::Fnv64_t)0xcbf29ce484222325ULL)
#define FNV1_64_INIT ((HashFunction::Fnv1aImplementation::Fnv64_t)0xcbf29ce484222325ULL)
#define FNV1A_64_INIT FNV1_64_INIT
#else /* HAVE_64BIT_LONG_LONG */
extern const fnv1_64_init;
@@ -338,7 +337,7 @@ enum fnv_type {
/*
* 32 bit magic FNV-1a prime
*/
#define FNV_32_PRIME ((HashImplNS::Fnv1aNS::Fnv32_t)0x01000193)
#define FNV_32_PRIME ((HashFunction::Fnv1aImplementation::Fnv32_t)0x01000193)
/*
@@ -497,7 +496,7 @@ const Fnv64_t fnv1a_64_init = { 0x84222325, 0xcbf29ce4 };
* 64 bit magic FNV-1a prime
*/
#if defined(HAVE_64BIT_LONG_LONG)
#define FNV_64_PRIME ((HashImplNS::Fnv1aNS::Fnv64_t)0x100000001b3ULL)
#define FNV_64_PRIME ((HashFunction::Fnv1aImplementation::Fnv64_t)0x100000001b3ULL)
#else /* HAVE_64BIT_LONG_LONG */
#define FNV_64_PRIME_LOW ((unsigned long)0x1b3) /* lower bits of FNV prime */
#define FNV_64_PRIME_SHIFT (8) /* top FNV prime shift above 2^32 */
@@ -723,24 +722,26 @@ fnv_64a_str(char *str, Fnv64_t hval)
//-----------------------------------------------------------------------------
} // namespace Fnv1aNS
} // namespace Fnv1aImplementation
Hash::Hash32_t
Hash32_t
Fnv1a::GetHash32 (const char * buffer, const size_t size)
{
uint32_t result = Fnv1aNS::fnv_32a_buf ((void *)buffer, size, FNV1_32A_INIT);
uint32_t result =
Fnv1aImplementation::fnv_32a_buf ((void *)buffer, size, FNV1_32A_INIT);
return result;
}
Hash::Hash64_t
Hash64_t
Fnv1a::GetHash64 (const char * buffer, const size_t size)
{
uint64_t result = Fnv1aNS::fnv_64a_buf ((void *)buffer, size, FNV1A_64_INIT);
uint64_t result =
Fnv1aImplementation::fnv_64a_buf ((void *)buffer, size, FNV1A_64_INIT);
return result;
}
} // namespace HashImpl
} // namespace HashFunction
} // namespace ns3

View File

@@ -21,11 +21,11 @@
#ifndef HASH_FNV_H
#define HASH_FNV_H
#include "ns3/hash.h"
#include "hash-implementation.h"
namespace ns3 {
namespace HashImplNS {
namespace HashFunction {
/**
* \ingroup hash
@@ -43,7 +43,7 @@ public:
* \param [in] size length of the buffer, in bytes
* \return 32-bit hash of the buffer
*/
Hash::Hash32_t GetHash32 (const char * buffer, const size_t size);
Hash32_t GetHash32 (const char * buffer, const size_t size);
/**
* Compute 64-bit hash of a byte buffer.
*
@@ -51,14 +51,11 @@ public:
* \param [in] size length of the buffer, in bytes
* \return 64-bit hash of the buffer
*/
Hash::Hash64_t GetHash64 (const char * buffer, const size_t size);
Hash64_t GetHash64 (const char * buffer, const size_t size);
private:
/**
* Seed value
*
* This has to be a constant for all MPI ranks to generate
* the same hash from the same string.
*/
enum seed
{
@@ -67,7 +64,7 @@ private:
}; // class Fnv1a
} // namespace HashImplNS
} // namespace HashFunction
} // namespace ns3

View File

@@ -0,0 +1,41 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2012 Lawrence Livermore National Laboratory
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
*/
#include "log.h"
#include "hash-implementation.h"
namespace ns3 {
NS_LOG_COMPONENT_DEFINE ("HashImplementation");
/*************************************************
** class HashImplementation
************************************************/
Hash64_t
HashImplementation::GetHash64 (const char * buffer, const size_t size)
{
NS_LOG_WARN("64-bit hash requested, only 32-bit implementation available");
return GetHash32 (buffer, size);
}
} // namespace ns3

View File

@@ -0,0 +1,123 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2012 Lawrence Livermore National Laboratory
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
*/
#ifndef HASHIMPLEMENTATION_H
#define HASHIMPLEMENTATION_H
#include "simple-ref-count.h"
namespace ns3 {
/**
* \ingroup hash
*
* Hash value opaque types
*/
typedef uint32_t Hash32_t;
typedef uint64_t Hash64_t;
/**
* \ingroup hash
*
* \brief Hash function implementation base class
*/
class HashImplementation : public SimpleRefCount<HashImplementation>
{
public:
/**
* Compute 32-bit hash of a byte buffer
*
* \param [in] buffer pointer to the beginning of the buffer
* \param [in] size length of the buffer, in bytes
* \return 32-bit hash of the buffer
*/
virtual Hash32_t GetHash32 (const char * buffer, const size_t size) = 0;
/**
* Compute 64-bit hash of a byte buffer.
*
* Default implementation returns 32-bit hash, with a warning.
*
* \param [in] buffer pointer to the beginning of the buffer
* \param [in] size length of the buffer, in bytes
* \return 64-bit hash of the buffer
*/
virtual Hash64_t GetHash64 (const char * buffer, const size_t size);
/*
* Destructor
*/
virtual ~HashImplementation () {} ;
}; // HashImplementation
/*--------------------------------------
* Hash function implementation
* by function pointers and templates
*/
/**
*
* \ingroup hash
*
* \brief Basic hash function typedefs.
*
* See Hash32Implementation<> or Hash64Implementation<>
*/
typedef Hash32_t (*Hash32Function_ptr) (const char *, const size_t);
typedef Hash64_t (*Hash64Function_ptr) (const char *, const size_t);
/**
* \ingroup hash
*
* \brief Template for HashImplementations from 32-bit hash functions
*/
template <Hash32Function_ptr hp>
class Hash32Implementation : public HashImplementation
{
Hash32_t GetHash32 (const char * buffer, const size_t size)
{
return (*hp) (buffer, size);
}
}; // Hash32Implementation<HashFunction>
/**
* \ingroup hash
*
* \brief Template for HashImplementations from 64-bit hash functions
*/
template <Hash64Function_ptr hp>
class Hash64Implementation : public HashImplementation
{
Hash64_t GetHash64 (const char * buffer, const size_t size)
{
return (*hp) (buffer, size);
}
Hash32_t GetHash32 (const char * buffer, const size_t size)
{
Hash64_t hash = GetHash64(buffer, size);
return (Hash32_t *)(&hash);
}
}; // Hash32Implementation<HashFunction>
} // namespace ns3
#endif /* HASHIMPLEMENTATION_H */

View File

@@ -18,18 +18,16 @@
* Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
*/
#include "ns3/hash.h"
#include "ns3/hash-murmur3.h"
#include "ns3/log.h"
#include "log.h"
#include "hash-murmur3.h"
namespace ns3 {
NS_LOG_COMPONENT_DEFINE ("Hash-Murmur3");
namespace HashImplNS {
namespace HashFunction {
namespace Murmur3NS {
namespace Murmur3Implementation {
/*************************************************
** class Murmur3HashImplementation
@@ -353,29 +351,29 @@ void MurmurHash3_x64_128 ( const void * key, const int len,
//-----------------------------------------------------------------------------
} // namespace Murmur3NS
} // namespace Murmur3Implementation
Hash::Hash32_t
Hash32_t
Murmur3::GetHash32 (const char * buffer, const size_t size)
{
uint32_t result;
Murmur3NS::MurmurHash3_x86_32 (buffer, size,
Murmur3Implementation::MurmurHash3_x86_32 (buffer, size,
(uint32_t)SEED, (void *)(&result));
return result;
}
Hash::Hash64_t
Hash64_t
Murmur3::GetHash64 (const char * buffer, const size_t size)
{
uint64_t result[2];
Murmur3NS::MurmurHash3_x86_128 (buffer, size,
Murmur3Implementation::MurmurHash3_x86_128 (buffer, size,
(uint32_t)SEED, (void *)(result));
return result[0];
}
} // namespace HashImpl
} // namespace HashFunction
} // namespace ns3

View File

@@ -21,11 +21,11 @@
#ifndef HASH_MURMUR3_H
#define HASH_MURMUR3_H
#include "ns3/hash.h"
#include "hash-implementation.h"
namespace ns3 {
namespace HashImplNS {
namespace HashFunction {
/**
* \ingroup hash
@@ -52,7 +52,7 @@ public:
* \param [in] size length of the buffer, in bytes
* \return 32-bit hash of the buffer
*/
Hash::Hash32_t GetHash32 (const char * buffer, const size_t size);
Hash32_t GetHash32 (const char * buffer, const size_t size);
/**
* Compute 64-bit hash of a byte buffer.
*
@@ -60,7 +60,7 @@ public:
* \param [in] size length of the buffer, in bytes
* \return 64-bit hash of the buffer
*/
Hash::Hash64_t GetHash64 (const char * buffer, const size_t size);
Hash64_t GetHash64 (const char * buffer, const size_t size);
private:
/**
@@ -76,7 +76,7 @@ private:
}; // class Murmur3
} // namespace HashImplNS
} // namespace HashFunction
} // namespace ns3

View File

@@ -18,8 +18,8 @@
* Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
*/
#include "ns3/hash.h"
#include "ns3/log.h"
#include "log.h"
#include "hash.h"
namespace ns3 {
@@ -32,7 +32,7 @@ NS_LOG_COMPONENT_DEFINE ("Hash");
Hash::Hash ()
{
m_impl = Create <HashImplNS::Murmur3> ();
m_impl = Create <HashFunction::Murmur3> ();
NS_ASSERT (m_impl != 0);
}
@@ -42,16 +42,4 @@ Hash::Hash (Ptr<HashImplementation> hp)
NS_ASSERT (m_impl != 0);
}
/*************************************************
** class HashImplementation
************************************************/
Hash::Hash64_t
HashImplementation::GetHash64 (const char * buffer, const size_t size)
{
NS_LOG_WARN("64-bit hash requested, only 32-bit implementation available");
return GetHash32 (buffer, size);
}
} // namespace ns3

View File

@@ -23,14 +23,15 @@
#include <string>
#include "ns3/assert.h"
#include "ns3/ptr.h"
#include "ns3/simple-ref-count.h"
#include "assert.h"
#include "ptr.h"
#include "hash-implementation.h" // typedef ns3::Hash32_t, ns3::Hash64_t
#include "hash-murmur3.h"
#include "hash-fnv.h"
namespace ns3 {
class HashImplementation;
/**
* \ingroup hash
*
@@ -41,7 +42,7 @@ class HashImplementation;
* The choice of hash function can be made at construction by
* passing a Ptr<> to the desired HashImplementation.
* The available implementations are documented in group hash.
* The default implementation is Murmur3. FNV is also available.
* The default implementation is Murmur3. FNV1a is also available.
*
* In addition to this class interface, global functions are
* defined which use the default hash implementation.
@@ -109,8 +110,9 @@ private:
}; // Hash
/*************************************************
** Global functions
** Global functions declarations
************************************************/
/**
@@ -122,7 +124,7 @@ private:
* \param [in] size length of the buffer, in bytes
* \return 32-bit hash of the buffer
*/
Hash::Hash32_t Hash32 (const char * buffer, const size_t size);
Hash32_t Hash32 (const char * buffer, const size_t size);
/**
* \ingroup hash
*
@@ -132,7 +134,7 @@ Hash::Hash32_t Hash32 (const char * buffer, const size_t size);
* \param [in] size length of the buffer, in bytes
* \return 64-bit hash of the buffer
*/
Hash::Hash64_t Hash64 (const char * buffer, const size_t size);
Hash64_t Hash64 (const char * buffer, const size_t size);
/**
* \ingroup hash
@@ -142,7 +144,7 @@ Hash::Hash64_t Hash64 (const char * buffer, const size_t size);
* \param [in] s string to hash
* \return 32-bit hash of the string
*/
Hash::Hash32_t Hash32 (const std::string s);
Hash32_t Hash32 (const std::string s);
/**
* \ingroup hash
*
@@ -151,105 +153,11 @@ Hash::Hash32_t Hash32 (const std::string s);
* \param [in] s string to hash
* \return 64-bit hash of the string
*/
Hash::Hash64_t Hash64 (const std::string s);
/*************************************************
** Hash function implementation class
************************************************/
/**
* \ingroup hash
*
* \brief Hash function implementation base class
*/
#ifndef HASHIMPLEMENTATION_C
#define HASHIMPLEMENTATION_C
class HashImplementation : public SimpleRefCount<HashImplementation>
{
public:
/**
* Compute 32-bit hash of a byte buffer
*
* \param [in] buffer pointer to the beginning of the buffer
* \param [in] size length of the buffer, in bytes
* \return 32-bit hash of the buffer
*/
virtual Hash::Hash32_t GetHash32 (const char * buffer, const size_t size) = 0;
/**
* Compute 64-bit hash of a byte buffer.
*
* Default implementation returns 32-bit hash, with a warning.
*
* \param [in] buffer pointer to the beginning of the buffer
* \param [in] size length of the buffer, in bytes
* \return 64-bit hash of the buffer
*/
virtual Hash::Hash64_t GetHash64 (const char * buffer, const size_t size);
/*
* Destructor
*/
virtual ~HashImplementation () {} ;
}; // HashImplementation
#endif /* HASHIMPLEMENTATION_C */
/**
*
* \ingroup hash
*
* \brief Basic hash function typedefs.
*
* See Hash32Implementation<> or Hash64Implementation<>
*/
typedef Hash::Hash32_t (*Hash32Function_ptr) (const char *, const size_t);
typedef Hash::Hash64_t (*Hash64Function_ptr) (const char *, const size_t);
/**
* \ingroup hash
*
* \brief Template for HashImplementations from 32-bit hash functions
*/
template <Hash32Function_ptr hp>
class Hash32Implementation : public HashImplementation
{
Hash::Hash32_t GetHash32 (const char * buffer, const size_t size)
{
return (*hp) (buffer, size);
}
}; // Hash32Implementation<HashFunction>
/**
* \ingroup hash
*
* \brief Template for HashImplementations from 64-bit hash functions
*/
template <Hash64Function_ptr hp>
class Hash64Implementation : public HashImplementation
{
Hash::Hash64_t GetHash64 (const char * buffer, const size_t size)
{
return (*hp) (buffer, size);
}
Hash::Hash32_t GetHash32 (const char * buffer, const size_t size)
{
Hash::Hash64_t hash = GetHash64(buffer, size);
return (Hash::Hash32_t *)(&hash);
}
}; // Hash32Implementation<HashFunction>
Hash64_t Hash64 (const std::string s);
} // namespace ns3
/*************************************************
** Real Hash function implementations
************************************************/
#include "ns3/hash-murmur3.h"
#include "ns3/hash-fnv.h"
/*************************************************
** Inline implementations for rvo
************************************************/
@@ -261,7 +169,7 @@ namespace ns3 {
*/
inline
Hash::Hash32_t
Hash32_t
Hash::GetHash32 (const char * buffer, const size_t size)
{
NS_ASSERT (m_impl != 0);
@@ -269,7 +177,7 @@ Hash::GetHash32 (const char * buffer, const size_t size)
}
inline
Hash::Hash64_t
Hash64_t
Hash::GetHash64 (const char * buffer, const size_t size)
{
NS_ASSERT (m_impl != 0);
@@ -277,7 +185,7 @@ Hash::GetHash64 (const char * buffer, const size_t size)
}
inline
Hash::Hash32_t
Hash32_t
Hash::GetHash32 (const std::string s)
{
NS_ASSERT (m_impl != 0);
@@ -285,7 +193,7 @@ Hash::GetHash32 (const std::string s)
}
inline
Hash::Hash64_t
Hash64_t
Hash::GetHash64 (const std::string s)
{
NS_ASSERT (m_impl != 0);
@@ -298,28 +206,28 @@ Hash::GetHash64 (const std::string s)
*/
inline
Hash::Hash32_t
Hash32_t
Hash32 (const char * buffer, const size_t size)
{
return Hash().GetHash32 (buffer, size);
}
inline
Hash::Hash64_t
Hash64_t
Hash64 (const char * buffer, const size_t size)
{
return Hash().GetHash64 (buffer, size);
}
inline
Hash::Hash32_t
Hash32_t
Hash32 (const std::string s)
{
return Hash().GetHash32 (s);
}
inline
Hash::Hash64_t
Hash64_t
Hash64 (const std::string s)
{
return Hash().GetHash64 (s);

View File

@@ -21,8 +21,8 @@
#include <iomanip>
#include <string>
#include "ns3/hash.h"
#include "ns3/test.h"
#include "ns3/hash.h"
namespace ns3 {
@@ -95,7 +95,7 @@ void
HashFnv1aTestCase::DoRun (void)
{
std::string key("The quick brown fnv1a.");
Hash hasher = Hash ( Create<HashImplNS::Fnv1a> () );
Hash hasher = Hash ( Create<HashFunction::Fnv1a> () );
uint32_t h32r = 0x5735855b; // FNV1A(key)
uint32_t h32 = hasher.GetHash32 (key);
@@ -139,7 +139,7 @@ void
HashMurmur3TestCase::DoRun (void)
{
std::string key("The quick brown murmur3.");
Hash hasher = Hash ( Create<HashImplNS::Murmur3> () );
Hash hasher = Hash ( Create<HashFunction::Murmur3> () );
uint32_t h32r = 0xe8a2d100; // Murmur3(key)
uint32_t h32 = hasher.GetHash32 (key);

View File

@@ -150,6 +150,7 @@ def build(bld):
'helper/random-variable-stream-helper.cc',
'helper/event-garbage-collector.cc',
'model/hash.cc',
'model/hash-implementation.cc',
'model/hash-murmur3.cc',
'model/hash-fnv.cc',
]
@@ -256,6 +257,7 @@ def build(bld):
'helper/event-garbage-collector.h',
'helper/random-variable-stream-helper.h',
'model/hash.h',
'model/hash-implementation.h',
'model/hash-murmur3.h',
'model/hash-fnv.h',
]