2014-02-02 10:42:56 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#define DBMS_HASH_MAP_DEBUG_RESIZES
|
|
|
|
#define DBMS_HASH_MAP_COUNT_COLLISIONS
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
#include <cstdlib>
|
2014-02-02 10:42:56 +00:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Types.h>
|
|
|
|
#include <Common/Exception.h>
|
2014-02-02 10:42:56 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
2014-02-03 03:09:50 +00:00
|
|
|
|
2017-06-23 20:22:35 +00:00
|
|
|
#include <common/StringRef.h>
|
2014-02-02 10:42:56 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/HashTable/HashMap.h>
|
2014-02-02 10:42:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
<
|
2017-04-01 07:20:54 +00:00
|
|
|
typename Key,
|
|
|
|
typename Mapped,
|
|
|
|
typename Hash = DefaultHash<Key>,
|
|
|
|
typename Grower = HashTableGrower<>,
|
|
|
|
typename Allocator = HashTableAllocator
|
2014-02-02 10:42:56 +00:00
|
|
|
>
|
2014-04-28 03:25:05 +00:00
|
|
|
class HashMapWithDump : public HashMap<Key, Mapped, Hash, Grower, Allocator>
|
2014-02-02 10:42:56 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
void dump() const
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < this->grower.bufSize(); ++i)
|
|
|
|
{
|
|
|
|
if (this->buf[i].isZero(*this))
|
|
|
|
std::cerr << "[ ]";
|
|
|
|
else
|
2019-08-01 15:57:02 +00:00
|
|
|
std::cerr << '[' << this->buf[i].getValue().first.data << ", " << this->buf[i].getValue().second << ']';
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
2014-02-02 10:42:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-02-13 04:28:31 +00:00
|
|
|
struct SimpleHash
|
2014-02-02 10:42:56 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t operator() (UInt64 x) const { return x; }
|
|
|
|
size_t operator() (StringRef x) const { return DB::parse<UInt64>(x.data); }
|
2014-02-02 10:42:56 +00:00
|
|
|
};
|
|
|
|
|
2014-05-03 16:03:49 +00:00
|
|
|
struct Grower : public HashTableGrower<2>
|
2014-02-02 10:42:56 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
void increaseSize()
|
|
|
|
{
|
|
|
|
++size_degree;
|
|
|
|
}
|
2014-02-02 10:42:56 +00:00
|
|
|
};
|
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
int main(int, char **)
|
2014-02-02 10:42:56 +00:00
|
|
|
{
|
2017-06-10 09:04:31 +00:00
|
|
|
using Map = HashMapWithDump<
|
2017-04-01 07:20:54 +00:00
|
|
|
StringRef,
|
|
|
|
UInt64,
|
|
|
|
SimpleHash,
|
|
|
|
Grower,
|
2017-06-10 09:04:31 +00:00
|
|
|
HashTableAllocatorWithStackMemory<4 * 24>>;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
Map map;
|
|
|
|
|
|
|
|
map.dump();
|
|
|
|
std::cerr << "size: " << map.size() << std::endl;
|
|
|
|
map[StringRef("1", 1)] = 1;
|
|
|
|
map.dump();
|
|
|
|
std::cerr << "size: " << map.size() << std::endl;
|
|
|
|
map[StringRef("9", 1)] = 1;
|
|
|
|
map.dump();
|
|
|
|
std::cerr << "size: " << map.size() << std::endl;
|
|
|
|
std::cerr << "Collisions: " << map.getCollisions() << std::endl;
|
|
|
|
map[StringRef("3", 1)] = 2;
|
|
|
|
map.dump();
|
|
|
|
std::cerr << "size: " << map.size() << std::endl;
|
|
|
|
std::cerr << "Collisions: " << map.getCollisions() << std::endl;
|
|
|
|
|
|
|
|
for (auto x : map)
|
2019-02-28 09:35:38 +00:00
|
|
|
std::cerr << x.getFirst().toString() << " -> " << x.getSecond() << std::endl;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return 0;
|
2014-02-02 10:42:56 +00:00
|
|
|
}
|