mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-20 11:05:54 +00:00
Add HashMap unit test
This commit is contained in:
@@ -478,6 +478,12 @@
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\beast_core\containers\HashMap.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\beast_core\diagnostic\beast_Debug.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||
|
||||
@@ -1465,6 +1465,9 @@
|
||||
<ClCompile Include="..\..\modules\beast_core\containers\DynamicList.cpp">
|
||||
<Filter>beast_core\containers</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\beast_core\containers\HashMap.cpp">
|
||||
<Filter>beast_core\containers</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Text Include="..\..\TODO.txt" />
|
||||
|
||||
@@ -145,6 +145,7 @@ namespace beast
|
||||
#include "containers/beast_Variant.cpp"
|
||||
#include "containers/DynamicArray.cpp"
|
||||
#include "containers/DynamicList.cpp"
|
||||
#include "containers/HashMap.cpp"
|
||||
|
||||
#include "diagnostic/beast_Debug.cpp"
|
||||
#include "diagnostic/beast_Error.cpp"
|
||||
|
||||
149
Subtrees/beast/modules/beast_core/containers/HashMap.cpp
Normal file
149
Subtrees/beast/modules/beast_core/containers/HashMap.cpp
Normal file
@@ -0,0 +1,149 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of Beast: https://github.com/vinniefalco/Beast
|
||||
Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
class HashMapTests : public UnitTest
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
numberOfItems = 100 * 1000
|
||||
};
|
||||
|
||||
template <int keyBytes>
|
||||
class TestTraits
|
||||
{
|
||||
public:
|
||||
struct Value
|
||||
{
|
||||
int unused;
|
||||
};
|
||||
|
||||
struct Key
|
||||
{
|
||||
class Equal
|
||||
{
|
||||
public:
|
||||
bool operator() (Key const& lhs, Key const& rhs) const noexcept
|
||||
{
|
||||
return memcmp (lhs.data, rhs.data, keyBytes) == 0;
|
||||
}
|
||||
};
|
||||
|
||||
// stateful hardened hash
|
||||
class Hash
|
||||
{
|
||||
public:
|
||||
explicit Hash (HashValue seedToUse = Random::getSystemRandom ().nextInt ())
|
||||
: m_seed (seedToUse)
|
||||
{
|
||||
}
|
||||
|
||||
HashValue generateHash (Key const& key) const noexcept
|
||||
{
|
||||
HashValue hash;
|
||||
Murmur::Hash (key.data, keyBytes, m_seed, &hash);
|
||||
return hash;
|
||||
}
|
||||
|
||||
private:
|
||||
HashValue m_seed;
|
||||
};
|
||||
|
||||
/*
|
||||
Key ()
|
||||
: std::memset (data, 0, keyBytes)
|
||||
{
|
||||
}
|
||||
*/
|
||||
|
||||
uint8 data [keyBytes];
|
||||
};
|
||||
|
||||
typedef Key key_type;
|
||||
typedef Value value_type;
|
||||
typedef typename Key::Hash hasher;
|
||||
typedef typename Key::Equal key_equal;
|
||||
typedef std::size_t size_type;
|
||||
|
||||
TestTraits (size_type const numberOfKeys, Random& random)
|
||||
{
|
||||
// need to static_bassert keyBytes can represent numberOfKeys. Log base 256 or something?
|
||||
|
||||
m_keys.reserve (numberOfKeys);
|
||||
m_shuffled_keys.reserve (numberOfKeys);
|
||||
for (size_type i = 0; i < numberOfKeys; ++i)
|
||||
{
|
||||
// VFALCO NOTE std::vector is garbage..want to emplace_back() here
|
||||
Key key;
|
||||
memset (key.data, 0, sizeof (key.data));
|
||||
memcpy (& key.data [0], &i, std::min (sizeof (key.data), sizeof (i)));
|
||||
m_keys.push_back (key);
|
||||
m_shuffled_keys.push_back (&m_keys [i]);
|
||||
}
|
||||
|
||||
UnitTestUtilities::repeatableShuffle (numberOfKeys, m_shuffled_keys, random);
|
||||
}
|
||||
|
||||
Key const& getKey (size_type index) const noexcept
|
||||
{
|
||||
return *m_shuffled_keys [index];
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector <Key> m_keys;
|
||||
std::vector <Key*> m_shuffled_keys;
|
||||
};
|
||||
|
||||
template <int keyBytes>
|
||||
void testInsert (std::size_t numberOfKeys, Random& random)
|
||||
{
|
||||
beginTestCase (String
|
||||
("insertion, numberOfKeys = ") + String::fromNumber (numberOfKeys) +
|
||||
", keyBytes = " + String::fromNumber (keyBytes));
|
||||
|
||||
typedef TestTraits <keyBytes> Traits;
|
||||
Traits traits (numberOfKeys, random);
|
||||
|
||||
typedef HashMap <
|
||||
typename Traits::key_type,
|
||||
typename Traits::value_type,
|
||||
typename Traits::hasher,
|
||||
typename Traits::key_equal> Map;
|
||||
Map map;
|
||||
|
||||
for (std::size_t i = 0; i < numberOfKeys; ++i)
|
||||
map.insert (traits.getKey (i));
|
||||
|
||||
this->logMessage ("load_factor = " + String::fromNumber (map.load_factor ()));
|
||||
}
|
||||
|
||||
void runTest ()
|
||||
{
|
||||
int64 const seedValue = 072472;
|
||||
Random random (seedValue);
|
||||
testInsert <4> (numberOfItems, random);
|
||||
testInsert <20> (numberOfItems, random);
|
||||
}
|
||||
|
||||
HashMapTests () : UnitTest ("HashMap", "beast")
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
static HashMapTests hashMapTests;
|
||||
@@ -100,6 +100,8 @@ private:
|
||||
namespace detail
|
||||
{
|
||||
|
||||
struct BucketTag { };
|
||||
|
||||
template <typename M, typename I>
|
||||
class HashMapLocalIterator
|
||||
: public std::iterator <std::forward_iterator_tag, typename M::size_type>
|
||||
@@ -194,7 +196,7 @@ private:
|
||||
typedef detail::ListIterator <typename detail::copyconst <M,
|
||||
typename List <Bucket>::Node>::type> bucket_iterator;
|
||||
typedef detail::ListIterator <typename detail::copyconst <M,
|
||||
typename List <Item, Bucket>::Node>::type> item_iterator;
|
||||
typename List <Item, detail::BucketTag>::Node>::type> item_iterator;
|
||||
|
||||
public:
|
||||
typedef typename M::Pair value_type;
|
||||
@@ -202,15 +204,36 @@ public:
|
||||
typedef value_type& reference;
|
||||
typedef typename M::size_type size_type;
|
||||
|
||||
HashMapIterator (M* map = nullptr,
|
||||
bucket_iterator bucket = bucket_iterator (),
|
||||
item_iterator local = item_iterator ())
|
||||
HashMapIterator ()
|
||||
: m_map (nullptr)
|
||||
, m_bucket (bucket_iterator ())
|
||||
, m_local (item_iterator ())
|
||||
{
|
||||
}
|
||||
|
||||
// represents end()
|
||||
explicit HashMapIterator (M* map)
|
||||
: m_map (map)
|
||||
, m_bucket (bucket_iterator ())
|
||||
, m_local (item_iterator ())
|
||||
{
|
||||
}
|
||||
|
||||
HashMapIterator (M* map, bucket_iterator const& bucket, item_iterator const& local)
|
||||
: m_map (map)
|
||||
, m_bucket (bucket)
|
||||
, m_local (local)
|
||||
{
|
||||
}
|
||||
|
||||
#if 0
|
||||
HashMapIterator (HashMapIterator const& other) noexcept
|
||||
: m_map (other.m_map)
|
||||
, m_bucket (other.m_bucket)
|
||||
, m_local (other.m_local)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename N>
|
||||
HashMapIterator (HashMapIterator <N> const& other) noexcept
|
||||
: m_map (other.m_map)
|
||||
@@ -218,7 +241,7 @@ public:
|
||||
, m_local (other.m_local)
|
||||
{
|
||||
}
|
||||
|
||||
#endif
|
||||
template <typename N>
|
||||
HashMapIterator& operator= (HashMapIterator <N> const& other) noexcept
|
||||
{
|
||||
@@ -359,7 +382,7 @@ private:
|
||||
return items.empty ();
|
||||
}
|
||||
|
||||
List <Item, Bucket> items;
|
||||
List <Item, detail::BucketTag> items;
|
||||
|
||||
private:
|
||||
Bucket& operator= (Bucket const&);
|
||||
@@ -369,7 +392,7 @@ private:
|
||||
// Every item in the map is in one linked list
|
||||
struct Item
|
||||
: List <Item>::Node
|
||||
, List <Item, Bucket>::Node
|
||||
, List <Item, detail::BucketTag>::Node
|
||||
{
|
||||
Item (Pair const& pair_)
|
||||
: m_pair (pair_)
|
||||
@@ -404,16 +427,16 @@ public:
|
||||
typedef value_type const* const_pointer;
|
||||
typedef value_type const& const_reference;
|
||||
|
||||
typedef detail::HashMapIterator <HashMap <Key, T, Hash> > iterator;
|
||||
typedef detail::HashMapIterator <HashMap <Key, T, Hash> const> const_iterator;
|
||||
typedef HashMap <Key, T, Hash, KeyEqual, Allocator> container_type;
|
||||
|
||||
typedef detail::HashMapLocalIterator <
|
||||
HashMap <Key, T, Hash>,
|
||||
typename List <Item, Bucket>::iterator> local_iterator;
|
||||
typedef detail::HashMapIterator <container_type> iterator;
|
||||
typedef detail::HashMapIterator <container_type const> const_iterator;
|
||||
|
||||
typedef detail::HashMapLocalIterator <
|
||||
HashMap <Key, T, Hash> const,
|
||||
typename List <Item, Bucket>::const_iterator> const_local_iterator;
|
||||
typedef detail::HashMapLocalIterator <container_type,
|
||||
typename List <Item, detail::BucketTag>::iterator> local_iterator;
|
||||
|
||||
typedef detail::HashMapLocalIterator <container_type const,
|
||||
typename List <Item, detail::BucketTag>::const_iterator> const_local_iterator;
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
@@ -514,17 +537,17 @@ public:
|
||||
|
||||
iterator end () noexcept
|
||||
{
|
||||
return iterator (this, m_bucketlist.end ());
|
||||
return iterator (static_cast <container_type*> (this));
|
||||
}
|
||||
|
||||
const_iterator end () const noexcept
|
||||
{
|
||||
return const_iterator (this, m_bucketlist.end ());
|
||||
return const_iterator (this);
|
||||
}
|
||||
|
||||
const_iterator cend () const noexcept
|
||||
{
|
||||
return const_iterator (this, m_bucketlist.cend ());
|
||||
return const_iterator (this);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
@@ -601,10 +624,10 @@ public:
|
||||
{
|
||||
size_type found (0);
|
||||
Bucket& b (m_buckets [bucket (key)]);
|
||||
for (typename List <Item, Bucket>::iterator iter (b.items.begin ());
|
||||
for (typename List <Item, detail::BucketTag>::iterator iter (b.items.begin ());
|
||||
iter != b.items.end ();)
|
||||
{
|
||||
typename List <Item, Bucket>::iterator cur (iter++);
|
||||
typename List <Item, detail::BucketTag>::iterator cur (iter++);
|
||||
if (m_equal (cur->pair ().key (), key))
|
||||
{
|
||||
erase (b, cur);
|
||||
@@ -641,7 +664,7 @@ public:
|
||||
{
|
||||
size_type n = 0;
|
||||
Bucket const& b (m_buckets [bucket (key)]);
|
||||
for (typename List <Item, Bucket>::iterator iter = b.items.begin ();
|
||||
for (typename List <Item, detail::BucketTag>::iterator iter = b.items.begin ();
|
||||
iter != b.items.end (); ++iter)
|
||||
if (m_equal (iter->key (), key))
|
||||
++n;
|
||||
@@ -765,16 +788,16 @@ private:
|
||||
|
||||
void grow_buckets ()
|
||||
{
|
||||
float const scale = 1.f + (float (percentageIncrease) / 100.f);
|
||||
size_type const count (std::ceil (
|
||||
(size () / max_load_factor ()) * scale));
|
||||
double const scale = 1. + (double (percentageIncrease) / 100.);
|
||||
size_type const count (size_type (std::ceil (
|
||||
(double (size ()) / double (max_load_factor ())) * scale)));
|
||||
rehash (count);
|
||||
}
|
||||
|
||||
iterator find (KeyParam key, size_type n) noexcept
|
||||
{
|
||||
Bucket& b (m_buckets [n]);
|
||||
for (typename List <Item, Bucket>::iterator iter =
|
||||
for (typename List <Item, detail::BucketTag>::iterator iter =
|
||||
b.items.begin (); iter != b.items.end (); ++iter)
|
||||
if (m_equal (iter->pair ().key (), key))
|
||||
return iterator (this, m_bucketlist.iterator_to (b), iter);
|
||||
@@ -784,7 +807,7 @@ private:
|
||||
const_iterator find (KeyParam key, size_type n) const noexcept
|
||||
{
|
||||
Bucket const& b (m_buckets [n]);
|
||||
for (typename List <Item, Bucket>::const_iterator iter =
|
||||
for (typename List <Item, detail::BucketTag>::const_iterator iter =
|
||||
b.items.begin (); iter != b.items.end (); ++iter)
|
||||
if (m_equal (iter->pair ().key (), key))
|
||||
return const_iterator (this,
|
||||
@@ -805,7 +828,7 @@ private:
|
||||
b.items.begin ());
|
||||
}
|
||||
|
||||
void erase (Bucket& b, typename List <Item, Bucket>::iterator pos)
|
||||
void erase (Bucket& b, typename List <Item, detail::BucketTag>::iterator pos)
|
||||
{
|
||||
Item& item (*pos);
|
||||
b.items.erase (b.items.iterator_to (item));
|
||||
|
||||
@@ -236,13 +236,14 @@ public:
|
||||
: m_node (node)
|
||||
{
|
||||
}
|
||||
#if 0
|
||||
|
||||
template <typename M>
|
||||
ListIterator (ListIterator <M> const& other) noexcept
|
||||
: m_node (other.m_node)
|
||||
{
|
||||
}
|
||||
|
||||
#if 0
|
||||
template <typename M>
|
||||
ListIterator& operator= (ListIterator <M> const& other) noexcept
|
||||
{
|
||||
|
||||
@@ -33,13 +33,7 @@ struct copyconst
|
||||
template <typename T, typename U>
|
||||
struct copyconst <T const, U>
|
||||
{
|
||||
typedef U const type;
|
||||
};
|
||||
|
||||
template <typename T, typename U>
|
||||
struct copyconst <T const, U const>
|
||||
{
|
||||
typedef U type;
|
||||
typedef typename removecv <U>::type const type;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -26,18 +26,22 @@ namespace UnitTestUtilities
|
||||
/** Fairly shuffle an array pseudo-randomly.
|
||||
*/
|
||||
template <class T>
|
||||
void repeatableShuffle (int const numberOfItems, T& arrayOfItems, int64 seedValue)
|
||||
void repeatableShuffle (int const numberOfItems, T& arrayOfItems, Random& r)
|
||||
{
|
||||
Random r (seedValue);
|
||||
|
||||
for (int i = numberOfItems - 1; i > 0; --i)
|
||||
{
|
||||
int const choice = r.nextInt (i + 1);
|
||||
|
||||
std::swap (arrayOfItems [i], arrayOfItems [choice]);
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void repeatableShuffle (int const numberOfItems, T& arrayOfItems, int64 seedValue)
|
||||
{
|
||||
Random r (seedValue);
|
||||
repeatableShuffle (numberOfItems, arrayOfItems, r);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/** A block of memory used for test data.
|
||||
|
||||
Reference in New Issue
Block a user