Skip to content

Commit 11c54e6

Browse files
committed
util/TransparentHash: new class
1 parent 264edd7 commit 11c54e6

File tree

5 files changed

+54
-14
lines changed

5 files changed

+54
-14
lines changed

src/spawn/TmpfsManager.cxx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
#include "TmpfsCreate.hxx"
77
#include "system/Error.hxx"
88
#include "system/Mount.hxx"
9-
#include "util/djb_hash.hxx"
109
#include "util/SharedLease.hxx"
11-
#include "util/SpanCast.hxx"
1210

1311
#include <cassert>
1412
#include <chrono>
@@ -88,12 +86,6 @@ struct TmpfsManager::Item final
8886
// TDOO void OnBroken() noexcept override;
8987
};
9088

91-
inline std::size_t
92-
TmpfsManager::ItemHash::operator()(std::string_view name) const noexcept
93-
{
94-
return djb_hash(AsBytes(name));
95-
}
96-
9789
inline std::string_view
9890
TmpfsManager::ItemGetKey::operator()(const Item &item) const noexcept
9991
{

src/spawn/TmpfsManager.hxx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "io/UniqueFileDescriptor.hxx"
88
#include "util/IntrusiveHashSet.hxx"
99
#include "util/IntrusiveList.hxx"
10+
#include "util/TransparentHash.hxx"
1011

1112
#include <string_view>
1213
#include <utility> // for std::pair
@@ -21,18 +22,13 @@ class SharedLease;
2122
class TmpfsManager {
2223
struct Item;
2324

24-
struct ItemHash {
25-
[[gnu::pure]]
26-
std::size_t operator()(std::string_view name) const noexcept;
27-
};
28-
2925
struct ItemGetKey {
3026
[[gnu::pure]]
3127
std::string_view operator()(const Item &item) const noexcept;
3228
};
3329

3430
IntrusiveHashSet<Item, 1024,
35-
IntrusiveHashSetOperators<Item, ItemGetKey, ItemHash,
31+
IntrusiveHashSetOperators<Item, ItemGetKey, TransparentHash,
3632
std::equal_to<std::string_view>>> items;
3733
IntrusiveList<Item> abandoned;
3834

src/util/TransparentHash.cxx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: BSD-2-Clause
2+
// Copyright CM4all GmbH
3+
// author: Max Kellermann <max.kellermann@ionos.com>
4+
5+
#include "TransparentHash.hxx"
6+
#include "djb_hash.hxx"
7+
#include "SpanCast.hxx"
8+
9+
std::size_t
10+
TransparentHash::operator()(const char *s) const noexcept
11+
{
12+
return djb_hash_string(s);
13+
}
14+
15+
std::size_t
16+
TransparentHash::operator()(std::span<const std::byte> s) const noexcept
17+
{
18+
return djb_hash(s);
19+
}
20+
21+
std::size_t
22+
TransparentHash::operator()(std::string_view s) const noexcept
23+
{
24+
return djb_hash(AsBytes(s));
25+
}

src/util/TransparentHash.hxx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-License-Identifier: BSD-2-Clause
2+
// Copyright CM4all GmbH
3+
// author: Max Kellermann <max.kellermann@ionos.com>
4+
5+
#pragma once
6+
7+
#include <cstddef>
8+
#include <span>
9+
#include <string_view>
10+
11+
/**
12+
* A hasher based on the DJB's cdb hash function. Being
13+
* "transparent", it allows efficient standard container lookup.
14+
*/
15+
struct TransparentHash {
16+
using is_transparent = void;
17+
18+
[[gnu::pure]]
19+
std::size_t operator()(const char *s) const noexcept;
20+
21+
[[gnu::pure]]
22+
std::size_t operator()(std::span<const std::byte> s) const noexcept;
23+
24+
[[gnu::pure]]
25+
std::size_t operator()(std::string_view s) const noexcept;
26+
};

src/util/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ util_sources = [
1111
'StringParser.cxx',
1212
'StringStrip.cxx',
1313
'StringWithHash.cxx',
14+
'TransparentHash.cxx',
1415
'UTF8.cxx',
1516
]
1617

0 commit comments

Comments
 (0)