-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DuplicateTracker transparently wraps std::pmr::unordered_map with a std::pmr::monotonic_memory_resource in a non-copyable, non-movable class with a narrowly-focussed API (basically, hasSeen()). If C++17 <memory_resource> isn't available, it falls back to the default std::allocator. This is the third iteration of this class. The first was QDuplicateTracker in Qt, which suffers from too small a static buffer, and a very restricted API. The second iteration was developed in a customer project and got the size of the static buffer (almost) right, as well as adding contains() and set(). The latter was made possible because, unlike QDuplicateTracker, this version always used std::unordered_set, just with different allocators, so the interface and semantics of decltype(set()) no longer varied wildly between C++17 and C++14 builds. In this third iteration, I factored the preallocated storage into a separate base class, to allow more implementation sharing between DuplicateTracker<T, N>s with same T, but different N. It also adds template arguments for the Hash and Equal template arguments (which is important when you don't, yet, use Qt 5.15, because then you might want to pass KDToolBox::QtHasher). Lastly, it exposes the unordered_set(N) ctor, so we can control the bucket_count() already at construction time, which means we no longer waste a default-sized bucket list allocated from the monotonic_buffer_resource when default-constructing a DuplicateTracker and then calling reserve(). Change-Id: Iaf7bd1d2291796e05beb36fef7172aed0c3e6c1f
- Loading branch information
Showing
9 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
TEMPLATE = subdirs | ||
SUBDIRS += \ | ||
duplicatetracker \ | ||
future-backports \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
TEMPLATE = subdirs | ||
|
||
SUBDIRS += \ | ||
include \ | ||
tests \ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/**************************************************************************** | ||
** MIT License | ||
** | ||
** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected], author Marc Mutz <[email protected]> | ||
** | ||
** This file is part of KDToolBox (https://github.com/KDAB/KDToolBox). | ||
** | ||
** Permission is hereby granted, free of charge, to any person obtaining a copy | ||
** of this software and associated documentation files (the "Software"), to deal | ||
** in the Software without restriction, including without limitation the rights | ||
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
** copies of the Software, ** and to permit persons to whom the Software is | ||
** furnished to do so, subject to the following conditions: | ||
** | ||
** The above copyright notice and this permission notice (including the next paragraph) | ||
** shall be included in all copies or substantial portions of the Software. | ||
** | ||
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
** LIABILITY, WHETHER IN AN ACTION OF ** CONTRACT, TORT OR OTHERWISE, | ||
** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
** DEALINGS IN THE SOFTWARE. | ||
****************************************************************************/ | ||
|
||
#pragma once | ||
|
||
#include <cstddef> // for std::max_align_t | ||
#include <unordered_set> | ||
#ifdef __has_include | ||
# if __has_include(<memory_resource>) && __cplusplus > 201402L | ||
# include <memory_resource> | ||
# endif | ||
#endif | ||
|
||
namespace KDToolBox { | ||
|
||
namespace detail { | ||
template <std::size_t N> struct alignas(std::max_align_t) Storage { | ||
char m_buffer[N]; | ||
}; | ||
|
||
template <typename T, typename Hash, typename Equal> | ||
class DuplicateTrackerBaseBase { | ||
protected: | ||
#ifdef __cpp_lib_memory_resource | ||
std::pmr::monotonic_buffer_resource m_res; | ||
std::pmr::unordered_set<T, Hash, Equal> m_set; | ||
#else | ||
std::unordered_set<T, Hash, Equal> m_set; | ||
#endif | ||
#ifdef __cpp_lib_memory_resource | ||
explicit DuplicateTrackerBaseBase(char *buffer, std::size_t bufferSize, std::size_t N, const Hash& h, const Equal &e) | ||
: m_res(buffer, bufferSize), | ||
m_set(N, h, e, &m_res) | ||
{} | ||
#else | ||
explicit DuplicateTrackerBaseBase(std::size_t N, const Hash& h, const Equal &e) | ||
: m_set(N, h, e) | ||
{} | ||
#endif | ||
DuplicateTrackerBaseBase(const DuplicateTrackerBaseBase&) = delete; | ||
DuplicateTrackerBaseBase(DuplicateTrackerBaseBase&&) = delete; | ||
DuplicateTrackerBaseBase& operator=(const DuplicateTrackerBaseBase&) = delete; | ||
DuplicateTrackerBaseBase& operator=(DuplicateTrackerBaseBase&&) = delete; | ||
~DuplicateTrackerBaseBase() = default; | ||
|
||
void reserve(std::size_t n) { m_set.reserve(n); } | ||
|
||
bool hasSeen(const T& t) { return !m_set.insert(t).second; } | ||
bool hasSeen(T&& t) { return !m_set.insert(std::move(t)).second; } | ||
|
||
bool contains(const T& t) const { | ||
return m_set.find(t) != m_set.end(); | ||
} | ||
|
||
const decltype(m_set)& set() const noexcept { return m_set; } | ||
decltype(m_set)& set() noexcept { return m_set; } | ||
}; | ||
template <typename T> | ||
struct node_guesstimate { void* next; std::size_t hash; T value; }; | ||
template <typename T> | ||
constexpr std::size_t calc_memory(std::size_t numNodes) noexcept | ||
{ | ||
return sizeof(node_guesstimate<T>) * numNodes // nodes | ||
+ sizeof(void*) * numNodes; // bucket list | ||
} | ||
|
||
template <typename T, std::size_t Prealloc, typename Hash, typename Equal> | ||
class DuplicateTrackerBase : | ||
#ifdef __cpp_lib_memory_resource | ||
Storage<calc_memory<T>(Prealloc)>, | ||
#endif | ||
DuplicateTrackerBaseBase<T, Hash, Equal> | ||
{ | ||
protected: | ||
using Base = detail::DuplicateTrackerBaseBase<T, Hash, Equal>; | ||
|
||
explicit DuplicateTrackerBase(std::size_t numBuckets, const Hash& h, const Equal& e) | ||
#ifdef __cpp_lib_memory_resource | ||
: Base(this->m_buffer, sizeof(this->m_buffer), numBuckets, h, e) {} | ||
#else | ||
: Base(numBuckets, h, e) {} | ||
#endif | ||
~DuplicateTrackerBase() = default; | ||
|
||
using Base::reserve; | ||
using Base::hasSeen; | ||
using Base::contains; | ||
using Base::set; | ||
}; | ||
} // namespace detail | ||
|
||
template <typename T, std::size_t Prealloc = 64, typename Hash = std::hash<T>, typename Equal = std::equal_to<T>> | ||
class DuplicateTracker : detail::DuplicateTrackerBase<T, Prealloc, Hash, Equal> | ||
{ | ||
using Base = detail::DuplicateTrackerBase<T, Prealloc, Hash, Equal>; | ||
public: | ||
DuplicateTracker() : DuplicateTracker(Prealloc, Hash(), Equal()) {} | ||
explicit DuplicateTracker(std::size_t numBuckets, const Hash& h = Hash(), const Equal& e = Equal()) | ||
: Base(numBuckets, h, e) {} | ||
|
||
using Base::reserve; | ||
using Base::hasSeen; | ||
using Base::contains; | ||
using Base::set; | ||
}; | ||
|
||
} // namespace KDToolBox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
TEMPLATE = subdirs | ||
|
||
CONFIG += c++17 | ||
|
||
HEADERS += \ | ||
duplicatetracker.h \ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CONFIG += testcase moc | ||
TARGET = tst_duplicatetracker_11 | ||
QT = core testlib | ||
CONFIG += c++11 | ||
SOURCES += $$PWD/../tst_duplicatetracker.cpp | ||
INCLUDEPATH += ../../../include |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CONFIG += testcase moc | ||
TARGET = tst_duplicatetracker_17 | ||
QT = core testlib | ||
CONFIG += c++1z | ||
SOURCES += $$PWD/../tst_duplicatetracker.cpp | ||
INCLUDEPATH += ../../../include |
5 changes: 5 additions & 0 deletions
5
cpp/duplicatetracker/tests/duplicatetracker/duplicatetracker.pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
TEMPLATE = subdirs | ||
|
||
SUBDIRS += \ | ||
cxx11 \ | ||
cxx17 \ |
96 changes: 96 additions & 0 deletions
96
cpp/duplicatetracker/tests/duplicatetracker/tst_duplicatetracker.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/**************************************************************************** | ||
** MIT License | ||
** | ||
** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected], author Marc Mutz <[email protected]> | ||
** | ||
** This file is part of KDToolBox (https://github.com/KDAB/KDToolBox). | ||
** | ||
** Permission is hereby granted, free of charge, to any person obtaining a copy | ||
** of this software and associated documentation files (the "Software"), to deal | ||
** in the Software without restriction, including without limitation the rights | ||
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
** copies of the Software, ** and to permit persons to whom the Software is | ||
** furnished to do so, subject to the following conditions: | ||
** | ||
** The above copyright notice and this permission notice (including the next paragraph) | ||
** shall be included in all copies or substantial portions of the Software. | ||
** | ||
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
** LIABILITY, WHETHER IN AN ACTION OF ** CONTRACT, TORT OR OTHERWISE, | ||
** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
** DEALINGS IN THE SOFTWARE. | ||
****************************************************************************/ | ||
|
||
#include "duplicatetracker.h" | ||
|
||
#include <QTest> | ||
|
||
#include <type_traits> | ||
|
||
using namespace KDToolBox; | ||
|
||
namespace { | ||
template <typename DuplicateTracker> struct prealloc {}; | ||
template <typename T, size_t Prealloc, typename H, typename Eq> | ||
struct prealloc<DuplicateTracker<T, Prealloc, H, Eq>> | ||
: std::integral_constant<std::size_t, Prealloc> {}; | ||
} // unnamed namespace | ||
|
||
class tst_DuplicateTracker : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
private Q_SLOTS: | ||
void defaultCtor(); | ||
void reserve(); | ||
void hasSeen(); | ||
}; | ||
|
||
void tst_DuplicateTracker::defaultCtor() | ||
{ | ||
DuplicateTracker<int> tracker; | ||
QVERIFY(tracker.set().empty()); | ||
QVERIFY(tracker.set().bucket_count() >= prealloc<decltype(tracker)>::value); | ||
} | ||
|
||
void tst_DuplicateTracker::reserve() | ||
{ | ||
for (size_t i : {2, 13, 63, 64, 65, 1024}) { | ||
{ | ||
DuplicateTracker<std::string> tracker(i); | ||
QVERIFY(tracker.set().bucket_count() >= i); | ||
} | ||
{ | ||
DuplicateTracker<std::string> tracker; | ||
tracker.reserve(i); | ||
QVERIFY(tracker.set().bucket_count() >= i); | ||
} | ||
} | ||
} | ||
|
||
void tst_DuplicateTracker::hasSeen() | ||
{ | ||
DuplicateTracker<std::string> tracker; | ||
QVERIFY(!tracker.contains("hello")); | ||
QVERIFY(!tracker.hasSeen("hello")); | ||
QVERIFY( tracker.contains("hello")); | ||
QVERIFY( tracker.hasSeen("hello")); | ||
|
||
QVERIFY(!tracker.contains("world")); | ||
QVERIFY(!tracker.hasSeen("world")); | ||
QVERIFY( tracker.contains("world")); | ||
QVERIFY( tracker.hasSeen("world")); | ||
|
||
const auto exclamation = std::string("!"); | ||
QVERIFY(!tracker.contains(exclamation)); | ||
QVERIFY(!tracker.hasSeen(exclamation)); | ||
QVERIFY( tracker.contains(exclamation)); | ||
QVERIFY( tracker.hasSeen(exclamation)); | ||
} | ||
|
||
QTEST_APPLESS_MAIN(tst_DuplicateTracker) | ||
|
||
#include "tst_duplicatetracker.moc" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
TEMPLATE = subdirs | ||
|
||
SUBDIRS += \ | ||
duplicatetracker |