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>
|
|
|
|
|
|
|
|
#include <DB/Core/Types.h>
|
2015-10-05 01:35:28 +00:00
|
|
|
#include <DB/Common/Exception.h>
|
2014-02-02 10:42:56 +00:00
|
|
|
|
2014-02-03 03:09:50 +00:00
|
|
|
#include <DB/IO/ReadHelpers.h>
|
|
|
|
|
|
|
|
#include <DB/Core/StringRef.h>
|
2014-02-02 10:42:56 +00:00
|
|
|
|
2014-04-28 03:25:05 +00:00
|
|
|
#include <DB/Common/HashTable/HashMap.h>
|
2014-02-02 10:42:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
template
|
|
|
|
<
|
|
|
|
typename Key,
|
|
|
|
typename Mapped,
|
2014-04-28 03:25:05 +00:00
|
|
|
typename Hash = DefaultHash<Key>,
|
2014-05-03 16:03:49 +00:00
|
|
|
typename Grower = HashTableGrower<>,
|
2014-04-28 03:25:05 +00:00
|
|
|
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:
|
|
|
|
void dump() const
|
|
|
|
{
|
2014-04-28 03:25:05 +00:00
|
|
|
for (size_t i = 0; i < this->grower.bufSize(); ++i)
|
2014-02-03 03:09:50 +00:00
|
|
|
{
|
2014-04-28 03:25:05 +00:00
|
|
|
if (this->buf[i].isZero(*this))
|
2014-02-03 03:09:50 +00:00
|
|
|
std::cerr << "[ ]";
|
|
|
|
else
|
2014-04-28 03:25:05 +00:00
|
|
|
std::cerr << '[' << this->buf[i].getValue().first.data << ", " << this->buf[i].getValue().second << ']';
|
2014-02-03 03:09:50 +00:00
|
|
|
}
|
2014-02-02 10:42:56 +00:00
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-02-13 04:28:31 +00:00
|
|
|
struct SimpleHash
|
2014-02-02 10:42:56 +00:00
|
|
|
{
|
|
|
|
size_t operator() (UInt64 x) const { return x; }
|
2014-04-28 01:48:24 +00:00
|
|
|
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
|
|
|
{
|
2014-05-03 16:03:49 +00:00
|
|
|
void increaseSize()
|
|
|
|
{
|
|
|
|
++size_degree;
|
|
|
|
}
|
2014-02-02 10:42:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
2014-04-28 03:25:05 +00:00
|
|
|
typedef HashMapWithDump<
|
2014-04-28 01:48:24 +00:00
|
|
|
StringRef,
|
2014-02-03 03:09:50 +00:00
|
|
|
UInt64,
|
2015-02-13 04:28:31 +00:00
|
|
|
SimpleHash,
|
2014-04-28 03:25:05 +00:00
|
|
|
Grower,
|
|
|
|
HashTableAllocatorWithStackMemory<4 * 24> > Map;
|
2014-02-02 10:42:56 +00:00
|
|
|
|
|
|
|
Map map;
|
|
|
|
|
|
|
|
map.dump();
|
2014-02-03 03:09:50 +00:00
|
|
|
std::cerr << "size: " << map.size() << std::endl;
|
2014-04-28 01:48:24 +00:00
|
|
|
map[StringRef("1", 1)] = 1;
|
2014-02-02 10:42:56 +00:00
|
|
|
map.dump();
|
2014-02-03 03:09:50 +00:00
|
|
|
std::cerr << "size: " << map.size() << std::endl;
|
2014-04-28 01:48:24 +00:00
|
|
|
map[StringRef("9", 1)] = 1;
|
2014-02-02 10:42:56 +00:00
|
|
|
map.dump();
|
2014-02-03 03:09:50 +00:00
|
|
|
std::cerr << "size: " << map.size() << std::endl;
|
2014-02-02 10:42:56 +00:00
|
|
|
std::cerr << "Collisions: " << map.getCollisions() << std::endl;
|
2014-04-28 01:48:24 +00:00
|
|
|
map[StringRef("3", 1)] = 2;
|
2014-02-02 10:42:56 +00:00
|
|
|
map.dump();
|
2014-02-03 03:09:50 +00:00
|
|
|
std::cerr << "size: " << map.size() << std::endl;
|
2014-02-02 10:42:56 +00:00
|
|
|
std::cerr << "Collisions: " << map.getCollisions() << std::endl;
|
|
|
|
|
|
|
|
for (auto x : map)
|
2014-02-03 03:09:50 +00:00
|
|
|
std::cerr << x.first.toString() << " -> " << x.second << std::endl;
|
2014-02-02 10:42:56 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|