Skip to content

Commit bf6ee4c

Browse files
committed
fdsdump: add StdHashTable component
1 parent c05e837 commit bf6ee4c

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

src/tools/fdsdump/src/aggregator/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ set(AGGREGATOR_SRC
1010
printer.cpp
1111
jsonPrinter.cpp
1212
tablePrinter.cpp
13+
stdHashTable.cpp
1314
stdAllocator.cpp
1415
)
1516

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @file
3+
* @author Michal Sedlak <[email protected]>
4+
* @brief Efficient hash table implementation
5+
*/
6+
7+
#define XXH_INLINE_ALL
8+
9+
#include "stdHashTable.hpp"
10+
#include <cstring>
11+
#include "3rd_party/xxhash/xxhash.h"
12+
13+
namespace fdsdump {
14+
namespace aggregator {
15+
16+
StdHashTable::StdHashTable(std::size_t key_size, std::size_t value_size) :
17+
m_key_size(key_size),
18+
m_value_size(value_size)
19+
{
20+
auto hash = [=](const uint8_t *key) {
21+
return XXH3_64bits(key, m_key_size);
22+
};
23+
auto equals = [=](const uint8_t *a, const uint8_t *b) {
24+
return std::memcmp(a, b, m_key_size) == 0;
25+
};
26+
m_map = Map(1, hash, equals);
27+
}
28+
29+
bool
30+
StdHashTable::find(uint8_t *key, uint8_t *&item)
31+
{
32+
auto it = m_map.find(key);
33+
if (it != m_map.end()) {
34+
item = it->second;
35+
return true;
36+
} else {
37+
return false;
38+
}
39+
}
40+
41+
bool
42+
StdHashTable::find_or_create(uint8_t *key, uint8_t *&item)
43+
{
44+
auto it = m_map.find(key);
45+
if (it != m_map.end()) {
46+
item = it->second;
47+
return true;
48+
} else {
49+
uint8_t *data = m_allocator.allocate(m_key_size + m_value_size);
50+
std::memcpy(data, key, m_key_size);
51+
m_map.insert({data, data});
52+
m_items.push_back(data);
53+
item = data;
54+
return false;
55+
}
56+
}
57+
58+
} // aggregator
59+
} // fdsdump
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @file
3+
* @author Michal Sedlak <[email protected]>
4+
* @brief Standard hash table implementation
5+
*/
6+
#pragma once
7+
8+
#include "stdAllocator.hpp"
9+
10+
#include <cstdint>
11+
#include <functional>
12+
#include <vector>
13+
#include <memory>
14+
#include <unordered_map>
15+
16+
namespace fdsdump {
17+
namespace aggregator {
18+
19+
/**
20+
* @brief A standard hash table implementation based on std::unordered_map
21+
*
22+
*/
23+
class StdHashTable {
24+
public:
25+
/**
26+
* @brief Constructs a new instance.
27+
* @param[in] key_size Number of bytes of the key portion of the record
28+
* @param[in] value_size Number of bytes of the value portion of the record
29+
*/
30+
StdHashTable(std::size_t key_size, std::size_t value_size);
31+
32+
/**
33+
* @brief Find a record corresponding to the provided key
34+
* @param key The key
35+
* @param item The stored record including the key
36+
* @return true if the record was found, false otherwise
37+
*/
38+
bool
39+
find(uint8_t *key, uint8_t *&item);
40+
41+
/**
42+
* @brief Find a record corresponding to the provided key or create a new
43+
* one if not found.
44+
* @param key The key
45+
* @param item The stored record including the key
46+
* @return true if the record was found, false if it wasn't and a new record
47+
* was created
48+
*/
49+
bool
50+
find_or_create(uint8_t *key, uint8_t *&item);
51+
52+
/**
53+
* @brief Access the stored records.
54+
* @warning
55+
* If the vector is modified bu the caller in some way, the behavior of
56+
* following calls to the hash table methods are undefined
57+
* @return Vector of the stored records
58+
*/
59+
std::vector<uint8_t *> &items() { return m_items; }
60+
61+
private:
62+
using Map = std::unordered_map<uint8_t *, uint8_t *,
63+
std::function<std::size_t(const uint8_t *)>,
64+
std::function<bool(const uint8_t *, const uint8_t *)>>;
65+
66+
std::size_t m_key_size;
67+
std::size_t m_value_size;
68+
std::vector<uint8_t *> m_items;
69+
StdAllocator m_allocator;
70+
Map m_map;
71+
};
72+
73+
} // aggregator
74+
} // fdsdump

0 commit comments

Comments
 (0)