Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bbd4177dd0 | ||
|
|
5d5f53e6e3 | ||
|
|
ec44c4a157 | ||
|
|
28069e43dc | ||
|
|
3c46788e06 | ||
|
|
51d749f451 | ||
|
|
343be60eb3 |
@@ -735,7 +735,6 @@ add_subdirectory(${FILAMENT}/filament)
|
||||
add_subdirectory(${FILAMENT}/shaders)
|
||||
add_subdirectory(${EXTERNAL}/basisu/tnt)
|
||||
add_subdirectory(${EXTERNAL}/civetweb/tnt)
|
||||
add_subdirectory(${EXTERNAL}/hat-trie/tnt)
|
||||
add_subdirectory(${EXTERNAL}/imgui/tnt)
|
||||
add_subdirectory(${EXTERNAL}/robin-map/tnt)
|
||||
add_subdirectory(${EXTERNAL}/smol-v/tnt)
|
||||
|
||||
@@ -31,7 +31,7 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'com.google.android.filament:filament-android:1.52.2'
|
||||
implementation 'com.google.android.filament:filament-android:1.52.3'
|
||||
}
|
||||
```
|
||||
|
||||
@@ -51,7 +51,7 @@ Here are all the libraries available in the group `com.google.android.filament`:
|
||||
iOS projects can use CocoaPods to install the latest release:
|
||||
|
||||
```shell
|
||||
pod 'Filament', '~> 1.52.2'
|
||||
pod 'Filament', '~> 1.52.3'
|
||||
```
|
||||
|
||||
### Snapshots
|
||||
|
||||
@@ -7,6 +7,9 @@ A new header is inserted each time a *tag* is created.
|
||||
Instead, if you are authoring a PR for the main branch, add your release note to
|
||||
[NEW_RELEASE_NOTES.md](./NEW_RELEASE_NOTES.md).
|
||||
|
||||
## v1.52.3
|
||||
|
||||
|
||||
## v1.52.2
|
||||
|
||||
|
||||
|
||||
@@ -120,7 +120,6 @@ set(GLTFIO_INCLUDE_DIRS
|
||||
../../third_party/cgltf
|
||||
../../third_party/meshoptimizer/src
|
||||
../../third_party/robin-map
|
||||
../../third_party/hat-trie
|
||||
../../third_party/stb
|
||||
../../libs/utils/include
|
||||
../../libs/ktxreader/include
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
GROUP=com.google.android.filament
|
||||
VERSION_NAME=1.52.2
|
||||
VERSION_NAME=1.52.3
|
||||
|
||||
POM_DESCRIPTION=Real-time physically based rendering engine for Android.
|
||||
|
||||
|
||||
@@ -109,6 +109,7 @@ private:
|
||||
NSUInteger headlessWidth = 0;
|
||||
NSUInteger headlessHeight = 0;
|
||||
CAMetalLayer* layer = nullptr;
|
||||
std::mutex layerDrawableMutex;
|
||||
MetalExternalImage externalImage;
|
||||
SwapChainType type;
|
||||
|
||||
|
||||
@@ -174,13 +174,21 @@ id<MTLTexture> MetalSwapChain::acquireDrawable() {
|
||||
}
|
||||
|
||||
assert_invariant(isCaMetalLayer());
|
||||
drawable = [layer nextDrawable];
|
||||
|
||||
// CAMetalLayer's drawable pool is not thread safe. Use a mutex when
|
||||
// calling -nextDrawable, or when releasing the last known reference
|
||||
// to any CAMetalDrawable returned from a previous -nextDrawable.
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(layerDrawableMutex);
|
||||
drawable = [layer nextDrawable];
|
||||
}
|
||||
|
||||
FILAMENT_CHECK_POSTCONDITION(drawable != nil) << "Could not obtain drawable.";
|
||||
return drawable.texture;
|
||||
}
|
||||
|
||||
void MetalSwapChain::releaseDrawable() {
|
||||
std::lock_guard<std::mutex> lock(layerDrawableMutex);
|
||||
drawable = nil;
|
||||
}
|
||||
|
||||
@@ -256,9 +264,11 @@ public:
|
||||
PresentDrawableData(const PresentDrawableData&) = delete;
|
||||
PresentDrawableData& operator=(const PresentDrawableData&) = delete;
|
||||
|
||||
static PresentDrawableData* create(id<CAMetalDrawable> drawable, MetalDriver* driver) {
|
||||
static PresentDrawableData* create(id<CAMetalDrawable> drawable,
|
||||
std::mutex* drawableMutex, MetalDriver* driver) {
|
||||
assert_invariant(drawableMutex);
|
||||
assert_invariant(driver);
|
||||
return new PresentDrawableData(drawable, driver);
|
||||
return new PresentDrawableData(drawable, drawableMutex, driver);
|
||||
}
|
||||
|
||||
static void maybePresentAndDestroyAsync(PresentDrawableData* that, bool shouldPresent) {
|
||||
@@ -277,16 +287,22 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
PresentDrawableData(id<CAMetalDrawable> drawable, MetalDriver* driver)
|
||||
: mDrawable(drawable), mDriver(driver) {}
|
||||
PresentDrawableData(id<CAMetalDrawable> drawable, std::mutex* drawableMutex,
|
||||
MetalDriver* driver)
|
||||
: mDrawable(drawable), mDrawableMutex(drawableMutex), mDriver(driver) {}
|
||||
|
||||
static void cleanupAndDestroy(PresentDrawableData *that) {
|
||||
that->mDrawable = nil;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(*(that->mDrawableMutex));
|
||||
that->mDrawable = nil;
|
||||
}
|
||||
that->mDrawableMutex = nullptr;
|
||||
that->mDriver = nullptr;
|
||||
delete that;
|
||||
}
|
||||
|
||||
id<CAMetalDrawable> mDrawable;
|
||||
std::mutex* mDrawableMutex = nullptr;
|
||||
MetalDriver* mDriver = nullptr;
|
||||
};
|
||||
|
||||
@@ -304,8 +320,8 @@ void MetalSwapChain::scheduleFrameScheduledCallback() {
|
||||
|
||||
struct Callback {
|
||||
Callback(std::shared_ptr<FrameScheduledCallback> callback, id<CAMetalDrawable> drawable,
|
||||
MetalDriver* driver)
|
||||
: f(callback), data(PresentDrawableData::create(drawable, driver)) {}
|
||||
std::mutex* drawableMutex, MetalDriver* driver)
|
||||
: f(callback), data(PresentDrawableData::create(drawable, drawableMutex, driver)) {}
|
||||
std::shared_ptr<FrameScheduledCallback> f;
|
||||
// PresentDrawableData* is destroyed by maybePresentAndDestroyAsync() later.
|
||||
std::unique_ptr<PresentDrawableData> data;
|
||||
@@ -320,8 +336,8 @@ void MetalSwapChain::scheduleFrameScheduledCallback() {
|
||||
|
||||
// This callback pointer will be captured by the block. Even if the scheduled handler is never
|
||||
// called, the unique_ptr will still ensure we don't leak memory.
|
||||
__block auto callback =
|
||||
std::make_unique<Callback>(frameScheduled.callback, drawable, context.driver);
|
||||
__block auto callback = std::make_unique<Callback>(
|
||||
frameScheduled.callback, drawable, &layerDrawableMutex, context.driver);
|
||||
|
||||
backend::CallbackHandler* handler = frameScheduled.handler;
|
||||
MetalDriver* driver = context.driver;
|
||||
|
||||
@@ -325,6 +325,10 @@ ShaderModel VulkanDriver::getShaderModel() const noexcept {
|
||||
}
|
||||
|
||||
void VulkanDriver::terminate() {
|
||||
// Flush and wait here to make sure all queued commands are executed and resources that are tied
|
||||
// to those commands are no longer referenced.
|
||||
finish(0);
|
||||
|
||||
delete mEmptyBufferObject;
|
||||
delete mEmptyTexture;
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
Pod::Spec.new do |spec|
|
||||
spec.name = "Filament"
|
||||
spec.version = "1.52.2"
|
||||
spec.version = "1.52.3"
|
||||
spec.license = { :type => "Apache 2.0", :file => "LICENSE" }
|
||||
spec.homepage = "https://google.github.io/filament"
|
||||
spec.authors = "Google LLC."
|
||||
spec.summary = "Filament is a real-time physically based rendering engine for Android, iOS, Windows, Linux, macOS, and WASM/WebGL."
|
||||
spec.platform = :ios, "11.0"
|
||||
spec.source = { :http => "https://github.com/google/filament/releases/download/v1.52.2/filament-v1.52.2-ios.tgz" }
|
||||
spec.source = { :http => "https://github.com/google/filament/releases/download/v1.52.3/filament-v1.52.3-ios.tgz" }
|
||||
|
||||
# Fix linking error with Xcode 12; we do not yet support the simulator on Apple silicon.
|
||||
spec.pod_target_xcconfig = {
|
||||
|
||||
@@ -179,7 +179,7 @@ set_target_properties(uberarchive PROPERTIES FOLDER Libs)
|
||||
# ==================================================================================================
|
||||
|
||||
include_directories(${PUBLIC_HDR_DIR} ${RESOURCE_DIR})
|
||||
link_libraries(math utils filament cgltf stb ktxreader geometry tsl trie uberzlib)
|
||||
link_libraries(math utils filament cgltf stb ktxreader geometry tsl uberzlib)
|
||||
|
||||
add_library(gltfio_core STATIC ${PUBLIC_HDRS} ${SRCS})
|
||||
|
||||
|
||||
@@ -47,8 +47,8 @@
|
||||
#include "FFilamentInstance.h"
|
||||
#include "Utility.h"
|
||||
|
||||
#include <tsl/htrie_map.h>
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
@@ -273,7 +273,7 @@ struct FFilamentAsset : public FilamentAsset {
|
||||
bool mResourcesLoaded = false;
|
||||
|
||||
DependencyGraph mDependencyGraph;
|
||||
tsl::htrie_map<char, std::vector<utils::Entity>> mNameToEntity;
|
||||
std::unordered_map<std::string, std::vector<utils::Entity>> mNameToEntity;
|
||||
utils::CString mAssetExtras;
|
||||
bool mDetachedFilamentComponents = false;
|
||||
|
||||
|
||||
@@ -211,7 +211,7 @@ Entity FFilamentAsset::getFirstEntityByName(const char* name) noexcept {
|
||||
if (iter == mNameToEntity.end()) {
|
||||
return {};
|
||||
}
|
||||
return iter->front();
|
||||
return iter->second.front();
|
||||
}
|
||||
|
||||
size_t FFilamentAsset::getEntitiesByName(const char* name, Entity* entities,
|
||||
@@ -220,7 +220,7 @@ size_t FFilamentAsset::getEntitiesByName(const char* name, Entity* entities,
|
||||
if (iter == mNameToEntity.end()) {
|
||||
return 0;
|
||||
}
|
||||
const auto& source = *iter;
|
||||
const auto& source = iter->second;
|
||||
if (entities == nullptr) {
|
||||
return source.size();
|
||||
}
|
||||
@@ -240,25 +240,20 @@ size_t FFilamentAsset::getEntitiesByName(const char* name, Entity* entities,
|
||||
|
||||
size_t FFilamentAsset::getEntitiesByPrefix(const char* prefix, Entity* entities,
|
||||
size_t maxCount) const noexcept {
|
||||
const auto range = mNameToEntity.equal_prefix_range(prefix);
|
||||
size_t count = 0;
|
||||
for (auto iter = range.first; iter != range.second; ++iter) {
|
||||
count += iter->size();
|
||||
}
|
||||
if (entities == nullptr) {
|
||||
return count;
|
||||
}
|
||||
maxCount = std::min(maxCount, count);
|
||||
if (maxCount == 0) {
|
||||
return 0;
|
||||
}
|
||||
count = 0;
|
||||
for (auto iter = range.first; iter != range.second; ++iter) {
|
||||
const auto& source = *iter;
|
||||
for (Entity entity : source) {
|
||||
entities[count] = entity;
|
||||
if (++count >= maxCount) {
|
||||
return count;
|
||||
std::string_view prefixString(prefix);
|
||||
size_t count = 0;
|
||||
for (auto& [k, v] : mNameToEntity) {
|
||||
if (k.compare(0, prefixString.size(), prefixString) == 0) {
|
||||
for (Entity entity : v) {
|
||||
if (entities) {
|
||||
entities[count] = entity;
|
||||
}
|
||||
if (++count >= maxCount) {
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -486,7 +486,7 @@ struct Voidify final {
|
||||
void operator&&(const T&) const&& {}
|
||||
};
|
||||
|
||||
class PanicStream {
|
||||
class UTILS_PUBLIC PanicStream {
|
||||
public:
|
||||
PanicStream(
|
||||
char const* function,
|
||||
|
||||
@@ -17,11 +17,14 @@
|
||||
#ifndef TNT_UTILS_SSTREAM_H
|
||||
#define TNT_UTILS_SSTREAM_H
|
||||
|
||||
#include <utils/compiler.h>
|
||||
#include <utils/ostream.h>
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
namespace utils::io {
|
||||
|
||||
class sstream : public ostream {
|
||||
class UTILS_PUBLIC sstream : public ostream {
|
||||
public:
|
||||
ostream& flush() noexcept override;
|
||||
const char* c_str() const noexcept;
|
||||
|
||||
22
third_party/hat-trie/CMakeLists.txt
vendored
22
third_party/hat-trie/CMakeLists.txt
vendored
@@ -1,22 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.1)
|
||||
|
||||
project(tsl_hat_trie)
|
||||
|
||||
add_library(tsl_hat_trie INTERFACE)
|
||||
# Use tsl::hat_trie as target, more consistent with other libraries conventions (Boost, Qt, ...)
|
||||
add_library(tsl::hat_trie ALIAS tsl_hat_trie)
|
||||
|
||||
target_include_directories(tsl_hat_trie INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include")
|
||||
target_sources(tsl_hat_trie INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/include/tsl/array-hash/array_growth_policy.h"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include/tsl/array-hash/array_hash.h"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include/tsl/array-hash/array_map.h"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include/tsl/array-hash/array_set.h"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include/tsl/htrie_hash.h"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include/tsl/htrie_map.h"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/include/tsl/htrie_set.h")
|
||||
|
||||
|
||||
if(${CMAKE_VERSION} VERSION_GREATER "3.7")
|
||||
# Only available since version 3.8
|
||||
target_compile_features(tsl_hat_trie INTERFACE cxx_std_11)
|
||||
endif()
|
||||
21
third_party/hat-trie/LICENSE
vendored
21
third_party/hat-trie/LICENSE
vendored
@@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Thibaut Goetghebuer-Planchon <tessil@gmx.com>
|
||||
|
||||
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 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.
|
||||
531
third_party/hat-trie/README.md
vendored
531
third_party/hat-trie/README.md
vendored
@@ -1,531 +0,0 @@
|
||||
[](https://travis-ci.org/Tessil/hat-trie) [](https://ci.appveyor.com/project/Tessil/hat-trie/branch/master)
|
||||
## A C++ implementation of a fast and memory efficient HAT-trie
|
||||
|
||||
Trie implementation based on the "HAT-trie: A Cache-conscious Trie-based Data Structure for Strings." (Askitis Nikolas and Sinha Ranjan, 2007) paper. For now, only the pure HAT-trie has been implemented, the hybrid version may arrive later. Details regarding the HAT-trie data structure can be found [here](https://tessil.github.io/2017/06/22/hat-trie.html).
|
||||
|
||||
The library provides an efficient and compact way to store a set or a map of strings by compressing the common prefixes. It also allows to search for keys that match a prefix. Note though that the default parameters of the structure are geared toward optimizing exact searches, if you do a lot of prefix searches you may want to reduce the burst threshold through the `burst_threshold` method.
|
||||
|
||||
It's a well adapted structure to store a large number of strings.
|
||||
|
||||
<p align="center">
|
||||
<img src="https://tessil.github.io/images/hat-trie.png" width="600px" />
|
||||
</p>
|
||||
|
||||
For the array hash part, the [array-hash](https://github.com/Tessil/array-hash) project is used and included in the repository.
|
||||
|
||||
The library provides two classes: `tsl::htrie_map` and `tsl::htrie_set`.
|
||||
|
||||
### Overview
|
||||
|
||||
- Header-only library, just add the [include](include/) directory to your include path and you are ready to go. If you use CMake, you can also use the `tsl::hat_trie` exported target from the [CMakeLists.txt](CMakeLists.txt).
|
||||
- Low memory usage while keeping reasonable performances (see [benchmark](#benchmark)).
|
||||
- Support prefix searches through `equal_prefix_range` (useful for autocompletion for example) and prefix erasures through `erase_prefix`.
|
||||
- Support longest matching prefix searches through `longest_prefix`.
|
||||
- Support for efficient serialization and deserialization (see [example](#serialization) and the `serialize/deserialize` methods in the [API](https://tessil.github.io/hat-trie/doc/html/classtsl_1_1htrie__map.html) for details).
|
||||
- Keys are not ordered as they are partially stored in a hash map.
|
||||
- All operations modifying the data structure (insert, emplace, erase, ...) invalidate the iterators.
|
||||
- Support null characters in the key (you can thus store binary data in the trie).
|
||||
- Support for any type of value as long at it's either copy-constructible or both nothrow move constructible and nothrow move assignable.
|
||||
- The balance between speed and memory usage can be modified through the `max_load_factor` method. A lower max load factor will increase the speed, a higher one will reduce the memory usage. Its default value is set to 8.0.
|
||||
- The default burst threshold, which is the maximum size of an array hash node before a burst occurs, is set to 16 384 which provides good performances for exact searches. If you mainly use prefix searches, you may want to reduce it to something like 1024 or lower for faster iteration on the results through the `burst_threshold` method.
|
||||
- By default the maximum allowed size for a key is set to 65 535. This can be raised through the `KeySizeT` template parameter.
|
||||
|
||||
Thread-safety and exception guarantees are similar to the STL containers.
|
||||
|
||||
### Hash function
|
||||
|
||||
The default hash function used by the structure depends on the presence of `std::string_view`. If it is available, `std::hash<std::string_view>` is used, otherwise a simple [FNV-1a](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash) hash function is used to avoid any dependency.
|
||||
|
||||
If you can't use C++17 or later, we recommend to replace the hash function with something like [CityHash](https://github.com/google/cityhash), MurmurHash, [FarmHash](https://github.com/google/farmhash), ... for better performances. On the tests we did, CityHash64 offers a ~20% improvement on reads compared to FNV-1a.
|
||||
|
||||
|
||||
```c++
|
||||
#include <city.h>
|
||||
|
||||
struct str_hash {
|
||||
std::size_t operator()(const char* key, std::size_t key_size) const {
|
||||
return CityHash64(key, key_size);
|
||||
}
|
||||
};
|
||||
|
||||
tsl::htrie_map<char, int, str_hash> map;
|
||||
```
|
||||
|
||||
The `std::hash<std::string>` can't be used efficiently as the structure doesn't store any `std::string` object. Any time a hash would be needed, a temporary `std::string` would have to be created.
|
||||
|
||||
|
||||
### Benchmark
|
||||
|
||||
#### Wikipedia dataset
|
||||
The benchmark consists in inserting all the titles from the main namespace of the Wikipedia archive into the data structure, check the used memory space after the insert (including potential memory fragmentation) and search for all the titles again in the data structure. The peak memory usage during the insert process is also measured with [time(1)](https://linux.die.net/man/1/time).
|
||||
|
||||
* Dataset: [enwiki-20170320-all-titles-in-ns0.gz](https://dumps.wikimedia.org/enwiki/20170320/)
|
||||
* Size: 262.7 MiB
|
||||
* Number of keys: 13 099 148
|
||||
* Average key length: 19.90
|
||||
* Median key length: 17
|
||||
* Max key length: 251
|
||||
|
||||
Each title is associated with an int (32 bits). All the hash based structures use [CityHash64](https://github.com/google/cityhash) as hash function. For the tests marked *with reserve*, the `reserve` function is called beforehand to avoid any rehash.
|
||||
|
||||
Note that `tsl::hopscotch_map`, `std::unordered_map`, `google::dense_hash_map` and `spp::sparse_hash_map` use `std::string` as key which imposes a minimum size of 32 bytes (on x64) even if the key is only one character long. Other structures may be able to store one-character keys with 1 byte + 8 bytes for a pointer (on x64).
|
||||
|
||||
The benchmark was compiled with GCC 6.3 and ran on Debian Stretch x64 with an Intel i5-5200u and 8Go of RAM. Best of 20 runs was taken.
|
||||
|
||||
The code of the benchmark can be found on [Gist](https://gist.github.com/Tessil/72e11891fc155f5b2eb53de22cbc4053).
|
||||
|
||||
##### Unsorted
|
||||
|
||||
The *enwiki-20170320-all-titles-in-ns0.gz* dataset is alphabetically sorted. For this benchmark, we first shuffle the dataset through [shuf(1)](https://linux.die.net/man/1/shuf) to avoid a biased sorted dataset.
|
||||
|
||||
| Library | Data structure | Peak memory (MiB) | Memory (MiB) | Insert (ns/key) | Read (ns/key) |
|
||||
|---------|----------------|------------------:|-------------:|----------------:|--------------:|
|
||||
| [tsl::htrie_map](https://github.com/Tessil/hat-trie) | HAT-trie | **405.22** | **402.25** | 643.10 | 250.87 |
|
||||
| [tsl::htrie_map](https://github.com/Tessil/hat-trie) <br/> max_load_factor=4 | HAT-trie | 471.85 | 468.50 | 638.66 | 212.90 |
|
||||
| [tsl::htrie_map](https://github.com/Tessil/hat-trie) <br/> max_load_factor=2 | HAT-trie | 569.76 | 566.52 | 630.61 | 201.10 |
|
||||
| [tsl::htrie_map](https://github.com/Tessil/hat-trie) <br/> max_load_factor=1 | HAT-trie | 713.44 | 709.81 | 645.76 | 190.87 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) | Double-array trie | 1269.68 | 1254.41 | 1102.93 | 557.20 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) ORDERED=false | Double-array trie | 1269.80 | 1254.41 | 1089.78 | 570.13 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) | Double-array reduced trie | 1183.07 | 1167.79 | 1076.68 | 645.79 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) ORDERED=false | Double-array reduced trie | 1183.14 | 1167.85 | 1065.43 | 641.98 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) | Double-array prefix trie | 498.69 | 496.54 | 1096.90 | 628.01 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) ORDERED=false | Double-array prefix trie | 498.65 | 496.60 | 1048.40 | 628.94 |
|
||||
| [hat-trie](https://github.com/dcjones/hat-trie)<sup>1</sup> (C) | HAT-trie | 504.07 | 501.50 | 917.49 | 261.00 |
|
||||
| [qp trie](https://github.com/fanf2/qp) (C) | QP trie | 941.23 | 938.17 | 1349.25 | 1281.46 |
|
||||
| [crit-bit trie](https://github.com/fanf2/qp) (C) | Crit-bit trie | 1074.96 | 1071.98 | 2930.42 | 2869.74 |
|
||||
| [JudySL](http://judy.sourceforge.net/) (C) | Judy array | 631.09 | 628.37 | 884.29 | 803.58 |
|
||||
| [JudyHS](http://judy.sourceforge.net/) (C) | Judy array | 723.44 | 719.47 | 476.79 | 417.15 |
|
||||
| [tsl::array_map](https://github.com/Tessil/array-hash) | Array hash table | 823.54 | 678.73 | 603.94 | 138.24 |
|
||||
| [tsl::array_map](https://github.com/Tessil/array-hash) <br>with reserve | Array hash table | 564.26 | 555.91 | 249.52 | 128.28 |
|
||||
| [tsl::hopscotch_map](https://github.com/Tessil/hopscotch-map) | Hash table | 1325.83 | 1077.99 | 368.26 | **119.49** |
|
||||
| [tsl::hopscotch_map](https://github.com/Tessil/hopscotch-map) <br>with reserve | Hash table | 1080.51 | 1077.98 | **240.58** | 119.91 |
|
||||
| [google::dense_hash_map](https://github.com/sparsehash/sparsehash) | Hash table | 2319.40 | 1677.11 | 466.60 | 138.87 |
|
||||
| [google::dense_hash_map](https://github.com/sparsehash/sparsehash) <br>with reserve | Hash table | 1592.51 | 1589.99 | 259.56 | 120.40 |
|
||||
| [spp::sparse_hash_map](https://github.com/greg7mdp/sparsepp) | Sparse hash table | 918.67 | 917.10 | 769.00 | 175.59 |
|
||||
| [spp::sparse_hash_map](https://github.com/greg7mdp/sparsepp) <br>with reserve | Sparse hash table | 913.35 | 910.65 | 427.22 | 159.08 |
|
||||
| [std::unordered_map](http://en.cppreference.com/w/cpp/container/unordered_map) | Hash table | 1249.05 | 1246.60 | 590.88 | 173.58 |
|
||||
| [std::unordered_map](http://en.cppreference.com/w/cpp/container/unordered_map) <br>with reserve | Hash table | 1212.23 | 1209.71 | 350.33 | 178.70 |
|
||||
|
||||
1. As the hash function can't be passed in parameter, the code of the library itself is modified to use CityHash64.
|
||||
|
||||
##### Sorted
|
||||
|
||||
The key are inserted and read in alphabetical order.
|
||||
|
||||
| Library | Data structure | Peak memory (MiB) | Memory (MiB) | Insert (ns/key) | Read (ns/key) |
|
||||
|---------|----------------|------------------:|-------------:|----------------:|--------------:|
|
||||
| [tsl::htrie_map](https://github.com/Tessil/hat-trie) | HAT-trie | **396.10** | **393.22** | 255.76 | 68.08 |
|
||||
| [tsl::htrie_map](https://github.com/Tessil/hat-trie) <br/> max_load_factor=4 | HAT-trie | 465.02 | 461.80 | 248.88 | 59.23 |
|
||||
| [tsl::htrie_map](https://github.com/Tessil/hat-trie) <br/> max_load_factor=2 | HAT-trie | 543.99 | 541.21 | 230.13 | 53.50 |
|
||||
| [tsl::htrie_map](https://github.com/Tessil/hat-trie) <br/> max_load_factor=1 | HAT-trie | 692.29 | 689.70 | 243.84 | **49.22** |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) | Double-array trie | 1269.58 | 1254.41 | 278.51 | 54.72 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) ORDERED=false | Double-array trie | 1269.66 | 1254.41 | 264.43 | 56.02 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) | Double-array reduced trie | 1183.01 | 1167.78 | 254.60 | 69.18 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) ORDERED=false | Double-array reduced trie | 1183.03 | 1167.78 | 241.45 | 69.67 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) | Double-array prefix trie | 621.59 | 619.38 | 246.88 | 57.83 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) ORDERED=false | Double-array prefix trie | 621.59 | 619.38 | **187.98** | 58.56 |
|
||||
| [hat-trie](https://github.com/dcjones/hat-trie)<sup>2</sup> (C) | HAT-trie | 521.25 | 518.52 | 503.01 | 86.40 |
|
||||
| [qp trie](https://github.com/fanf2/qp) (C) | QP trie | 940.65 | 937.66 | 392.86 | 190.19 |
|
||||
| [crit-bit trie](https://github.com/fanf2/qp) (C) | Crit-bit trie | 1074.87 | 1071.98 | 430.04 | 347.60 |
|
||||
| [JudySL](http://judy.sourceforge.net/) (C) | Judy array | 616.95 | 614.27 | 279.07 | 114.47 |
|
||||
| [JudyHS](http://judy.sourceforge.net/) (C) | Judy array | 722.29 | 719.47 | 439.66 | 372.25 |
|
||||
| [tsl::array_map](https://github.com/Tessil/array-hash) | Array hash table | 826.98 | 682.99 | 612.31 | 139.16 |
|
||||
| [tsl::array_map](https://github.com/Tessil/array-hash) <br>with reserve | Array hash table | 565.37 | 555.35 | 246.55 | 126.32 |
|
||||
| [tsl::hopscotch_map](https://github.com/Tessil/hopscotch-map) | Hash table | 1331.87 | 1078.02 | 375.19 | 118.08 |
|
||||
| [tsl::hopscotch_map](https://github.com/Tessil/hopscotch-map) <br>with reserve | Hash table | 1080.51 | 1077.97 | 238.93 | 117.20 |
|
||||
| [google::dense_hash_map](https://github.com/sparsehash/sparsehash) | Hash table | 2325.27 | 1683.07 | 483.95 | 137.09 |
|
||||
| [google::dense_hash_map](https://github.com/sparsehash/sparsehash) <br>with reserve | Hash table | 1592.54 | 1589.99 | 257.22 | 113.71 |
|
||||
| [spp::sparse_hash_map](https://github.com/greg7mdp/sparsepp) | Sparse hash table | 920.96 | 918.70 | 772.03 | 176.64 |
|
||||
| [spp::sparse_hash_map](https://github.com/greg7mdp/sparsepp) <br>with reserve | Sparse hash table | 914.84 | 912.47 | 422.85 | 158.73 |
|
||||
| [std::unordered_map](http://en.cppreference.com/w/cpp/container/unordered_map) | Hash table | 1249.09 | 1246.65 | 594.85 | 173.54 |
|
||||
| [std::unordered_map](http://en.cppreference.com/w/cpp/container/unordered_map) <br>with reserve | Hash table | 1212.21 | 1209.71 | 347.40 | 176.49 |
|
||||
|
||||
2. As the hash function can't be passed in parameter, the code of the library itself is modified to use CityHash64.
|
||||
|
||||
|
||||
#### Dr. Askitis dataset
|
||||
|
||||
The benchmark consists in inserting all the words from the "Distinct Strings" dataset of Dr. Askitis into the data structure, check the used memory space and search for all the words from the "Skew String Set 1" dataset (where a string can be present multiple times) in the data structure. Note that the strings in this dataset have a quite short average and median key length (which may not be a realistic use case compared to the Wikipedia dataset used above). It's similar to the one on the [cedar](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) homepage.
|
||||
|
||||
* Dataset: [distinct_1](http://web.archive.org/web/20120206015921/http://www.naskitis.com/) (write) / [skew1_1](http://web.archive.org/web/20120206015921/http://www.naskitis.com/) (read)
|
||||
* Size: 290.45 MiB / 1 029.46 MiB
|
||||
* Number of keys: 28 772 169 / 177 999 203
|
||||
* Average key length: 9.59 / 5.06
|
||||
* Median key length: 8 / 4
|
||||
* Max key length: 126 / 62
|
||||
|
||||
The benchmark protocol is the same as for the [Wikipedia dataset](https://github.com/Tessil/hat-trie#wikipedia-dataset).
|
||||
|
||||
|
||||
| Library | Data structure | Peak memory (MiB) | Memory (MiB) | Insert (ns/key) | Read (ns/key) |
|
||||
|---------|----------------|------------------:|-------------:|----------------:|--------------:|
|
||||
| [tsl::htrie_map](https://github.com/Tessil/hat-trie) | HAT-trie | **604.76** | **601.79** | 485.45 | 77.80 |
|
||||
| [tsl::htrie_map](https://github.com/Tessil/hat-trie) <br/> max_load_factor=4 | HAT-trie | 768.10 | 764.98 | 491.78 | 75.48 |
|
||||
| [tsl::htrie_map](https://github.com/Tessil/hat-trie) <br/> max_load_factor=2 | HAT-trie | 1002.42 | 999.34 | 496.78 | 72.53 |
|
||||
| [tsl::htrie_map](https://github.com/Tessil/hat-trie) <br/> max_load_factor=1 | HAT-trie | 1344.98 | 1341.97 | 520.66 | 72.45 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) | Double-array trie | 1105.45 | 1100.05 | 682.25 | 71.98 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) ORDERED=false | Double-array trie | 1105.47 | 1100.05 | 668.75 | 71.95 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) | Double-array reduced trie | 941.16 | 926.04 | 684.38 | 79.11 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) ORDERED=false | Double-array reduced trie | 941.16 | 925.98 | 672.14 | 79.02 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) | Double-array prefix trie | 714.58 | 712.59 | 831.71 | 75.83 |
|
||||
| [cedar::da](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar/) ORDERED=false | Double-array prefix trie | 714.66 | 712.31 | 786.93 | 75.89 |
|
||||
| [hat-trie](https://github.com/dcjones/hat-trie)<sup>3</sup> (C) | HAT-trie | 786.93 | 784.32 | 743.34 | 93.58 |
|
||||
| [qp trie](https://github.com/fanf2/qp) (C) | QP trie | 1800.02 | 1797.21 | 987.95 | 428.51 |
|
||||
| [crit-bit trie](https://github.com/fanf2/qp) (C) | Crit-bit trie | 2210.52 | 2207.64 | 1986.19 | 1109.88 |
|
||||
| [JudySL](http://judy.sourceforge.net/) (C) | Judy array | 1025.59 | 1023.11 | 535.02 | 202.36 |
|
||||
| [JudyHS](http://judy.sourceforge.net/) (C) | Judy array | 1002.50 | 999.97 | 456.09 | 148.36 |
|
||||
| [tsl::array_map](https://github.com/Tessil/array-hash) | Array hash table | 1308.08 | 1031.67 | 545.82 | 46.41 |
|
||||
| [tsl::array_map](https://github.com/Tessil/array-hash) <br>with reserve | Array hash table | 979.44 | 921.363 | 244.19 | 45.74 |
|
||||
| [tsl::hopscotch_map](https://github.com/Tessil/hopscotch-map) | Hash table | 2336.39 | 1611.54 | 288.70 | 47.05 |
|
||||
| [tsl::hopscotch_map](https://github.com/Tessil/hopscotch-map) <br>with reserve | Hash table | 1614.22 | 1611.64 | **220.67** | 46.39 |
|
||||
| [google::dense_hash_map](https://github.com/sparsehash/sparsehash) | Hash table | 3913.64 | 2636.31 | 317.66 | 43.62 |
|
||||
| [google::dense_hash_map](https://github.com/sparsehash/sparsehash) <br>with reserve | Hash table | 2638.19 | 2635.68 | 227.58 | **43.09** |
|
||||
| [spp::sparse_hash_map](https://github.com/greg7mdp/sparsepp) | Sparse hash table | 1419.69 | 1417.61 | 586.26 | 56.00 |
|
||||
| [spp::sparse_hash_map](https://github.com/greg7mdp/sparsepp) <br>with reserve | Sparse hash table | 1424.21 | 1421.69 | 392.76 | 55.73 |
|
||||
| [std::unordered_map](http://en.cppreference.com/w/cpp/container/unordered_map) | Hash table | 2112.66 | 2110.19 | 554.02 | 105.05 |
|
||||
| [std::unordered_map](http://en.cppreference.com/w/cpp/container/unordered_map) <br>with reserve | Hash table | 2053.95 | 2051.67 | 309.06 | 109.89 |
|
||||
|
||||
3. As the hash function can't be passed in parameter, the code of the library itself is modified to use CityHash64.
|
||||
|
||||
### Installation
|
||||
To use the library, just add the [include](include/) directory to your include path. It is a **header-only** library.
|
||||
|
||||
If you use CMake, you can also use the `tsl::hat_trie` exported target from the [CMakeLists.txt](CMakeLists.txt) with `target_link_libraries`.
|
||||
```cmake
|
||||
# Example where the hat-trie project is stored in a third-party directory
|
||||
add_subdirectory(third-party/hat-trie)
|
||||
target_link_libraries(your_target PRIVATE tsl::hat_trie)
|
||||
```
|
||||
|
||||
The code should work with any C++11 standard-compliant compiler and has been tested with GCC 4.8.4, Clang 3.5.0 and Visual Studio 2015.
|
||||
|
||||
To run the tests you will need the Boost Test library and CMake.
|
||||
|
||||
```bash
|
||||
git clone https://github.com/Tessil/hat-trie.git
|
||||
cd hat-trie/tests
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
cmake --build .
|
||||
./tsl_hat_trie_tests
|
||||
```
|
||||
|
||||
### Usage
|
||||
|
||||
The API can be found [here](https://tessil.github.io/hat-trie/doc_without_string_view/html). If `std::string_view` is available, the API changes slightly and can be found [here](https://tessil.github.io/hat-trie/doc/html/).
|
||||
|
||||
### Example
|
||||
|
||||
```c++
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <tsl/htrie_map.h>
|
||||
#include <tsl/htrie_set.h>
|
||||
|
||||
|
||||
int main() {
|
||||
/*
|
||||
* Map of strings to int having char as character type.
|
||||
* There is no support for wchar_t, char16_t or char32_t yet,
|
||||
* but UTF-8 strings will work fine.
|
||||
*/
|
||||
tsl::htrie_map<char, int> map = {{"one", 1}, {"two", 2}};
|
||||
map["three"] = 3;
|
||||
map["four"] = 4;
|
||||
|
||||
map.insert("five", 5);
|
||||
map.insert_ks("six_with_extra_chars_we_ignore", 3, 6);
|
||||
|
||||
map.erase("two");
|
||||
|
||||
/*
|
||||
* Due to the compression on the common prefixes, the letters of the string
|
||||
* are not always stored contiguously. When we retrieve the key, we have to
|
||||
* construct it.
|
||||
*
|
||||
* To avoid a heap-allocation at each iteration (when SSO doesn't occur),
|
||||
* we reuse the key_buffer to construct the key.
|
||||
*/
|
||||
std::string key_buffer;
|
||||
for(auto it = map.begin(); it != map.end(); ++it) {
|
||||
it.key(key_buffer);
|
||||
std::cout << "{" << key_buffer << ", " << it.value() << "}" << std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
* If you don't care about the allocation.
|
||||
*/
|
||||
for(auto it = map.begin(); it != map.end(); ++it) {
|
||||
std::cout << "{" << it.key() << ", " << *it << "}" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
tsl::htrie_map<char, int> map2 = {{"apple", 1}, {"mango", 2}, {"apricot", 3},
|
||||
{"mandarin", 4}, {"melon", 5}, {"macadamia", 6}};
|
||||
|
||||
// Prefix search
|
||||
auto prefix_range = map2.equal_prefix_range("ma");
|
||||
|
||||
// {mandarin, 4} {mango, 2} {macadamia, 6}
|
||||
for(auto it = prefix_range.first; it != prefix_range.second; ++it) {
|
||||
std::cout << "{" << it.key() << ", " << *it << "}" << std::endl;
|
||||
}
|
||||
|
||||
// Find longest match prefix.
|
||||
auto longest_prefix = map2.longest_prefix("apple juice");
|
||||
if(longest_prefix != map2.end()) {
|
||||
// {apple, 1}
|
||||
std::cout << "{" << longest_prefix.key() << ", "
|
||||
<< *longest_prefix << "}" << std::endl;
|
||||
}
|
||||
|
||||
// Prefix erase
|
||||
map2.erase_prefix("ma");
|
||||
|
||||
// {apricot, 3} {melon, 5} {apple, 1}
|
||||
for(auto it = map2.begin(); it != map2.end(); ++it) {
|
||||
std::cout << "{" << it.key() << ", " << *it << "}" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
tsl::htrie_set<char> set = {"one", "two", "three"};
|
||||
set.insert({"four", "five"});
|
||||
|
||||
// {one} {two} {five} {four} {three}
|
||||
for(auto it = set.begin(); it != set.end(); ++it) {
|
||||
it.key(key_buffer);
|
||||
std::cout << "{" << key_buffer << "}" << std::endl;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Serialization
|
||||
|
||||
The library provides an efficient way to serialize and deserialize a map or a set so that it can be saved to a file or send through the network.
|
||||
To do so, it requires the user to provide a function object for both serialization and deserialization.
|
||||
|
||||
```c++
|
||||
struct serializer {
|
||||
// Must support the following types for U: std::uint64_t, float and T if a map is used.
|
||||
template<typename U>
|
||||
void operator()(const U& value);
|
||||
void operator()(const CharT* value, std::size_t value_size);
|
||||
};
|
||||
```
|
||||
|
||||
```c++
|
||||
struct deserializer {
|
||||
// Must support the following types for U: std::uint64_t, float and T if a map is used.
|
||||
template<typename U>
|
||||
U operator()();
|
||||
void operator()(CharT* value_out, std::size_t value_size);
|
||||
};
|
||||
```
|
||||
|
||||
Note that the implementation leaves binary compatibility (endianness, float binary representation, size of int, ...) of the types it serializes/deserializes in the hands of the provided function objects if compatibility is required.
|
||||
|
||||
More details regarding the `serialize` and `deserialize` methods can be found in the [API](https://tessil.github.io/hat-trie/doc/html/classtsl_1_1htrie__map.html).
|
||||
|
||||
```c++
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <type_traits>
|
||||
#include <tsl/htrie_map.h>
|
||||
|
||||
|
||||
class serializer {
|
||||
public:
|
||||
serializer(const char* file_name) {
|
||||
m_ostream.exceptions(m_ostream.badbit | m_ostream.failbit);
|
||||
m_ostream.open(file_name);
|
||||
}
|
||||
|
||||
template<class T,
|
||||
typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
|
||||
void operator()(const T& value) {
|
||||
m_ostream.write(reinterpret_cast<const char*>(&value), sizeof(T));
|
||||
}
|
||||
|
||||
void operator()(const char* value, std::size_t value_size) {
|
||||
m_ostream.write(value, value_size);
|
||||
}
|
||||
|
||||
private:
|
||||
std::ofstream m_ostream;
|
||||
};
|
||||
|
||||
class deserializer {
|
||||
public:
|
||||
deserializer(const char* file_name) {
|
||||
m_istream.exceptions(m_istream.badbit | m_istream.failbit | m_istream.eofbit);
|
||||
m_istream.open(file_name);
|
||||
}
|
||||
|
||||
template<class T,
|
||||
typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
|
||||
T operator()() {
|
||||
T value;
|
||||
m_istream.read(reinterpret_cast<char*>(&value), sizeof(T));
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
void operator()(char* value_out, std::size_t value_size) {
|
||||
m_istream.read(value_out, value_size);
|
||||
}
|
||||
|
||||
private:
|
||||
std::ifstream m_istream;
|
||||
};
|
||||
|
||||
|
||||
int main() {
|
||||
const tsl::htrie_map<char, std::int64_t> map = {{"one", 1}, {"two", 2},
|
||||
{"three", 3}, {"four", 4}};
|
||||
|
||||
|
||||
const char* file_name = "htrie_map.data";
|
||||
{
|
||||
serializer serial(file_name);
|
||||
map.serialize(serial);
|
||||
}
|
||||
|
||||
{
|
||||
deserializer dserial(file_name);
|
||||
auto map_deserialized = tsl::htrie_map<char, std::int64_t>::deserialize(dserial);
|
||||
|
||||
assert(map == map_deserialized);
|
||||
}
|
||||
|
||||
{
|
||||
deserializer dserial(file_name);
|
||||
|
||||
/**
|
||||
* If the serialized and deserialized map are hash compatibles (see conditions in API),
|
||||
* setting the argument to true speed-up the deserialization process as we don't have
|
||||
* to recalculate the hash of each key. We also know how much space each bucket needs.
|
||||
*/
|
||||
const bool hash_compatible = true;
|
||||
auto map_deserialized =
|
||||
tsl::htrie_map<char, std::int64_t>::deserialize(dserial, hash_compatible);
|
||||
|
||||
assert(map == map_deserialized);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
##### Serialization with Boost Serialization and compression with zlib
|
||||
|
||||
It's possible to use a serialization library to avoid some of the boilerplate if the types to serialize are more complex.
|
||||
|
||||
The following example uses Boost Serialization with the Boost zlib compression stream to reduce the size of the resulting serialized file.
|
||||
|
||||
|
||||
```c++
|
||||
#include <boost/archive/binary_iarchive.hpp>
|
||||
#include <boost/archive/binary_oarchive.hpp>
|
||||
#include <boost/iostreams/filter/zlib.hpp>
|
||||
#include <boost/iostreams/filtering_stream.hpp>
|
||||
#include <boost/serialization/split_free.hpp>
|
||||
#include <boost/serialization/utility.hpp>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <tsl/htrie_map.h>
|
||||
|
||||
|
||||
template<typename Archive>
|
||||
struct serializer {
|
||||
Archive& ar;
|
||||
|
||||
template<typename T>
|
||||
void operator()(const T& val) { ar & val; }
|
||||
|
||||
template<typename CharT>
|
||||
void operator()(const CharT* val, std::size_t val_size) {
|
||||
ar.save_binary(reinterpret_cast<const void*>(val), val_size*sizeof(CharT));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Archive>
|
||||
struct deserializer {
|
||||
Archive& ar;
|
||||
|
||||
template<typename T>
|
||||
T operator()() { T val; ar & val; return val; }
|
||||
|
||||
template<typename CharT>
|
||||
void operator()(CharT* val_out, std::size_t val_size) {
|
||||
ar.load_binary(reinterpret_cast<void*>(val_out), val_size*sizeof(CharT));
|
||||
}
|
||||
};
|
||||
|
||||
namespace boost { namespace serialization {
|
||||
template<class Archive, class CharT, class T>
|
||||
void serialize(Archive & ar, tsl::htrie_map<CharT, T>& map, const unsigned int version) {
|
||||
split_free(ar, map, version);
|
||||
}
|
||||
|
||||
template<class Archive, class CharT, class T>
|
||||
void save(Archive & ar, const tsl::htrie_map<CharT, T>& map, const unsigned int version) {
|
||||
serializer<Archive> serial{ar};
|
||||
map.serialize(serial);
|
||||
}
|
||||
|
||||
|
||||
template<class Archive, class CharT, class T>
|
||||
void load(Archive & ar, tsl::htrie_map<CharT, T>& map, const unsigned int version) {
|
||||
deserializer<Archive> deserial{ar};
|
||||
map = tsl::htrie_map<CharT, T>::deserialize(deserial);
|
||||
}
|
||||
}}
|
||||
|
||||
|
||||
int main() {
|
||||
const tsl::htrie_map<char, std::int64_t> map = {{"one", 1}, {"two", 2},
|
||||
{"three", 3}, {"four", 4}};
|
||||
|
||||
|
||||
const char* file_name = "htrie_map.data";
|
||||
{
|
||||
std::ofstream ofs;
|
||||
ofs.exceptions(ofs.badbit | ofs.failbit);
|
||||
ofs.open(file_name, std::ios::binary);
|
||||
|
||||
boost::iostreams::filtering_ostream fo;
|
||||
fo.push(boost::iostreams::zlib_compressor());
|
||||
fo.push(ofs);
|
||||
|
||||
boost::archive::binary_oarchive oa(fo);
|
||||
|
||||
oa << map;
|
||||
}
|
||||
|
||||
{
|
||||
std::ifstream ifs;
|
||||
ifs.exceptions(ifs.badbit | ifs.failbit | ifs.eofbit);
|
||||
ifs.open(file_name, std::ios::binary);
|
||||
|
||||
boost::iostreams::filtering_istream fi;
|
||||
fi.push(boost::iostreams::zlib_decompressor());
|
||||
fi.push(ifs);
|
||||
|
||||
boost::archive::binary_iarchive ia(fi);
|
||||
|
||||
tsl::htrie_map<char, std::int64_t> map_deserialized;
|
||||
ia >> map_deserialized;
|
||||
|
||||
assert(map == map_deserialized);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### License
|
||||
|
||||
The code is licensed under the MIT license, see the [LICENSE file](LICENSE) for details.
|
||||
39
third_party/hat-trie/appveyor.yml
vendored
39
third_party/hat-trie/appveyor.yml
vendored
@@ -1,39 +0,0 @@
|
||||
environment:
|
||||
BOOST_ROOT: C:\Libraries\boost_1_67_0
|
||||
matrix:
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
||||
ARCH: Win32
|
||||
BOOST_LIBRARYDIR: C:\Libraries\boost_1_67_0\lib32-msvc-14.0
|
||||
CMAKE_GENERATOR: Visual Studio 14 2015
|
||||
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
||||
ARCH: x64
|
||||
BOOST_LIBRARYDIR: C:\Libraries\boost_1_67_0\lib64-msvc-14.0
|
||||
CMAKE_GENERATOR: Visual Studio 14 2015 Win64
|
||||
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
|
||||
ARCH: Win32
|
||||
BOOST_LIBRARYDIR: C:\Libraries\boost_1_67_0\lib32-msvc-14.1
|
||||
CMAKE_GENERATOR: Visual Studio 15 2017
|
||||
CXXFLAGS: /permissive-
|
||||
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
|
||||
ARCH: x64
|
||||
BOOST_LIBRARYDIR: C:\Libraries\boost_1_67_0\lib64-msvc-14.1
|
||||
CMAKE_GENERATOR: Visual Studio 15 2017 Win64
|
||||
CXXFLAGS: /permissive-
|
||||
|
||||
configuration:
|
||||
- Debug
|
||||
- Release
|
||||
|
||||
build_script:
|
||||
- cd tests
|
||||
- mkdir build
|
||||
- cd build
|
||||
- cmake -DCMAKE_BUILD_TYPE=%CONFIGURATION% -G"%CMAKE_GENERATOR%" ..
|
||||
- cmake --build . --config %CONFIGURATION%
|
||||
|
||||
test_script:
|
||||
- set PATH=%PATH%;%BOOST_LIBRARYDIR%
|
||||
- .\%CONFIGURATION%\tsl_hat_trie_tests.exe
|
||||
2481
third_party/hat-trie/doxygen.conf
vendored
2481
third_party/hat-trie/doxygen.conf
vendored
File diff suppressed because it is too large
Load Diff
23
third_party/hat-trie/tnt/CMakeLists.txt
vendored
23
third_party/hat-trie/tnt/CMakeLists.txt
vendored
@@ -1,23 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.19)
|
||||
project(trie)
|
||||
|
||||
set(TARGET trie)
|
||||
set(OUR_DIR ${CMAKE_CURRENT_SOURCE_DIR}/..)
|
||||
|
||||
# ==================================================================================================
|
||||
# Sources and headers
|
||||
# ==================================================================================================
|
||||
set(PUBLIC_HDRS
|
||||
${OUR_DIR}/tsl/htrie_hash.h
|
||||
${OUR_DIR}/tsl/htrie_map.h
|
||||
${OUR_DIR}/tsl/htrie_set.h
|
||||
)
|
||||
|
||||
# ==================================================================================================
|
||||
# Include and target definitions
|
||||
# ==================================================================================================
|
||||
include_directories(${OUR_DIR}/tsl)
|
||||
|
||||
add_library(${TARGET} INTERFACE)
|
||||
target_sources(${TARGET} INTERFACE ${PUBLIC_HDRS})
|
||||
target_include_directories(${TARGET} INTERFACE ${OUR_DIR})
|
||||
2
third_party/hat-trie/tnt/README.md
vendored
2
third_party/hat-trie/tnt/README.md
vendored
@@ -1,2 +0,0 @@
|
||||
This is a patched copy of Tessil/hat-trie at commit 1739fa.
|
||||
It has been patched to allow exceptions to be disabled.
|
||||
@@ -1,307 +0,0 @@
|
||||
/**
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2017 Thibaut Goetghebuer-Planchon <tessil@gmx.com>
|
||||
*
|
||||
* 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 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.
|
||||
*/
|
||||
#ifndef TSL_ARRAY_GROWTH_POLICY_H
|
||||
#define TSL_ARRAY_GROWTH_POLICY_H
|
||||
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <climits>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <ratio>
|
||||
#include <stdexcept>
|
||||
|
||||
|
||||
#ifdef __EXCEPTIONS
|
||||
# define THROW(_e, _m) throw _e(_m)
|
||||
#else
|
||||
# include <stdio.h>
|
||||
# ifndef NDEBUG
|
||||
# define THROW(_e, _m) do { fprintf(stderr, _m); std::terminate(); } while(0)
|
||||
# else
|
||||
# define THROW(_e, _m) std::terminate()
|
||||
# endif
|
||||
#endif
|
||||
|
||||
|
||||
namespace tsl {
|
||||
namespace ah {
|
||||
|
||||
/**
|
||||
* Grow the hash table by a factor of GrowthFactor keeping the bucket count to a power of two. It allows
|
||||
* the table to use a mask operation instead of a modulo operation to map a hash to a bucket.
|
||||
*
|
||||
* GrowthFactor must be a power of two >= 2.
|
||||
*/
|
||||
template<std::size_t GrowthFactor>
|
||||
class power_of_two_growth_policy {
|
||||
public:
|
||||
/**
|
||||
* Called on the hash table creation and on rehash. The number of buckets for the table is passed in parameter.
|
||||
* This number is a minimum, the policy may update this value with a higher value if needed (but not lower).
|
||||
*
|
||||
* If 0 is given, min_bucket_count_in_out must still be 0 after the policy creation and
|
||||
* bucket_for_hash must always return 0 in this case.
|
||||
*/
|
||||
explicit power_of_two_growth_policy(std::size_t& min_bucket_count_in_out) {
|
||||
if(min_bucket_count_in_out > max_bucket_count()) {
|
||||
THROW(std::length_error, "The hash table exceeds its maximum size.");
|
||||
}
|
||||
|
||||
if(min_bucket_count_in_out > 0) {
|
||||
min_bucket_count_in_out = round_up_to_power_of_two(min_bucket_count_in_out);
|
||||
m_mask = min_bucket_count_in_out - 1;
|
||||
}
|
||||
else {
|
||||
m_mask = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the bucket [0, bucket_count()) to which the hash belongs.
|
||||
* If bucket_count() is 0, it must always return 0.
|
||||
*/
|
||||
std::size_t bucket_for_hash(std::size_t hash) const noexcept {
|
||||
return hash & m_mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of buckets that should be used on next growth.
|
||||
*/
|
||||
std::size_t next_bucket_count() const {
|
||||
if((m_mask + 1) > max_bucket_count() / GrowthFactor) {
|
||||
THROW(std::length_error, "The hash table exceeds its maximum size.");
|
||||
}
|
||||
|
||||
return (m_mask + 1) * GrowthFactor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the maximum number of buckets supported by the policy.
|
||||
*/
|
||||
std::size_t max_bucket_count() const {
|
||||
// Largest power of two.
|
||||
return (std::numeric_limits<std::size_t>::max() / 2) + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the growth policy as if it was created with a bucket count of 0.
|
||||
* After a clear, the policy must always return 0 when bucket_for_hash is called.
|
||||
*/
|
||||
void clear() noexcept {
|
||||
m_mask = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
static std::size_t round_up_to_power_of_two(std::size_t value) {
|
||||
if(is_power_of_two(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if(value == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
--value;
|
||||
for(std::size_t i = 1; i < sizeof(std::size_t) * CHAR_BIT; i *= 2) {
|
||||
value |= value >> i;
|
||||
}
|
||||
|
||||
return value + 1;
|
||||
}
|
||||
|
||||
static constexpr bool is_power_of_two(std::size_t value) {
|
||||
return value != 0 && (value & (value - 1)) == 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
static_assert(is_power_of_two(GrowthFactor) && GrowthFactor >= 2, "GrowthFactor must be a power of two >= 2.");
|
||||
|
||||
std::size_t m_mask;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Grow the hash table by GrowthFactor::num / GrowthFactor::den and use a modulo to map a hash
|
||||
* to a bucket. Slower but it can be useful if you want a slower growth.
|
||||
*/
|
||||
template<class GrowthFactor = std::ratio<3, 2>>
|
||||
class mod_growth_policy {
|
||||
public:
|
||||
explicit mod_growth_policy(std::size_t& min_bucket_count_in_out) {
|
||||
if(min_bucket_count_in_out > max_bucket_count()) {
|
||||
THROW(std::length_error, "The hash table exceeds its maximum size.");
|
||||
}
|
||||
|
||||
if(min_bucket_count_in_out > 0) {
|
||||
m_mod = min_bucket_count_in_out;
|
||||
}
|
||||
else {
|
||||
m_mod = 1;
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t bucket_for_hash(std::size_t hash) const noexcept {
|
||||
return hash % m_mod;
|
||||
}
|
||||
|
||||
std::size_t next_bucket_count() const {
|
||||
if(m_mod == max_bucket_count()) {
|
||||
THROW(std::length_error, "The hash table exceeds its maximum size.");
|
||||
}
|
||||
|
||||
const double next_bucket_count = std::ceil(double(m_mod) * REHASH_SIZE_MULTIPLICATION_FACTOR);
|
||||
if(!std::isnormal(next_bucket_count)) {
|
||||
THROW(std::length_error, "The hash table exceeds its maximum size.");
|
||||
}
|
||||
|
||||
if(next_bucket_count > double(max_bucket_count())) {
|
||||
return max_bucket_count();
|
||||
}
|
||||
else {
|
||||
return std::size_t(next_bucket_count);
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t max_bucket_count() const {
|
||||
return MAX_BUCKET_COUNT;
|
||||
}
|
||||
|
||||
void clear() noexcept {
|
||||
m_mod = 1;
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr double REHASH_SIZE_MULTIPLICATION_FACTOR = 1.0 * GrowthFactor::num / GrowthFactor::den;
|
||||
static const std::size_t MAX_BUCKET_COUNT =
|
||||
std::size_t(double(
|
||||
std::numeric_limits<std::size_t>::max() / REHASH_SIZE_MULTIPLICATION_FACTOR
|
||||
));
|
||||
|
||||
static_assert(REHASH_SIZE_MULTIPLICATION_FACTOR >= 1.1, "Growth factor should be >= 1.1.");
|
||||
|
||||
std::size_t m_mod;
|
||||
};
|
||||
|
||||
|
||||
|
||||
namespace detail {
|
||||
|
||||
static constexpr const std::array<std::size_t, 40> PRIMES = {{
|
||||
1ul, 5ul, 17ul, 29ul, 37ul, 53ul, 67ul, 79ul, 97ul, 131ul, 193ul, 257ul, 389ul, 521ul, 769ul, 1031ul,
|
||||
1543ul, 2053ul, 3079ul, 6151ul, 12289ul, 24593ul, 49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
|
||||
1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul, 50331653ul, 100663319ul, 201326611ul,
|
||||
402653189ul, 805306457ul, 1610612741ul, 3221225473ul, 4294967291ul
|
||||
}};
|
||||
|
||||
template<unsigned int IPrime>
|
||||
static constexpr std::size_t mod(std::size_t hash) { return hash % PRIMES[IPrime]; }
|
||||
|
||||
// MOD_PRIME[iprime](hash) returns hash % PRIMES[iprime]. This table allows for faster modulo as the
|
||||
// compiler can optimize the modulo code better with a constant known at the compilation.
|
||||
static constexpr const std::array<std::size_t(*)(std::size_t), 40> MOD_PRIME = {{
|
||||
&mod<0>, &mod<1>, &mod<2>, &mod<3>, &mod<4>, &mod<5>, &mod<6>, &mod<7>, &mod<8>, &mod<9>, &mod<10>,
|
||||
&mod<11>, &mod<12>, &mod<13>, &mod<14>, &mod<15>, &mod<16>, &mod<17>, &mod<18>, &mod<19>, &mod<20>,
|
||||
&mod<21>, &mod<22>, &mod<23>, &mod<24>, &mod<25>, &mod<26>, &mod<27>, &mod<28>, &mod<29>, &mod<30>,
|
||||
&mod<31>, &mod<32>, &mod<33>, &mod<34>, &mod<35>, &mod<36>, &mod<37> , &mod<38>, &mod<39>
|
||||
}};
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Grow the hash table by using prime numbers as bucket count. Slower than tsl::ah::power_of_two_growth_policy in
|
||||
* general but will probably distribute the values around better in the buckets with a poor hash function.
|
||||
*
|
||||
* To allow the compiler to optimize the modulo operation, a lookup table is used with constant primes numbers.
|
||||
*
|
||||
* With a switch the code would look like:
|
||||
* \code
|
||||
* switch(iprime) { // iprime is the current prime of the hash table
|
||||
* case 0: hash % 5ul;
|
||||
* break;
|
||||
* case 1: hash % 17ul;
|
||||
* break;
|
||||
* case 2: hash % 29ul;
|
||||
* break;
|
||||
* ...
|
||||
* }
|
||||
* \endcode
|
||||
*
|
||||
* Due to the constant variable in the modulo the compiler is able to optimize the operation
|
||||
* by a series of multiplications, substractions and shifts.
|
||||
*
|
||||
* The 'hash % 5' could become something like 'hash - (hash * 0xCCCCCCCD) >> 34) * 5' in a 64 bits environment.
|
||||
*/
|
||||
class prime_growth_policy {
|
||||
public:
|
||||
explicit prime_growth_policy(std::size_t& min_bucket_count_in_out) {
|
||||
auto it_prime = std::lower_bound(detail::PRIMES.begin(),
|
||||
detail::PRIMES.end(), min_bucket_count_in_out);
|
||||
if(it_prime == detail::PRIMES.end()) {
|
||||
THROW(std::length_error, "The hash table exceeds its maximum size.");
|
||||
}
|
||||
|
||||
m_iprime = static_cast<unsigned int>(std::distance(detail::PRIMES.begin(), it_prime));
|
||||
if(min_bucket_count_in_out > 0) {
|
||||
min_bucket_count_in_out = *it_prime;
|
||||
}
|
||||
else {
|
||||
min_bucket_count_in_out = 0;
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t bucket_for_hash(std::size_t hash) const noexcept {
|
||||
return detail::MOD_PRIME[m_iprime](hash);
|
||||
}
|
||||
|
||||
std::size_t next_bucket_count() const {
|
||||
if(m_iprime + 1 >= detail::PRIMES.size()) {
|
||||
THROW(std::length_error, "The hash table exceeds its maximum size.");
|
||||
}
|
||||
|
||||
return detail::PRIMES[m_iprime + 1];
|
||||
}
|
||||
|
||||
std::size_t max_bucket_count() const {
|
||||
return detail::PRIMES.back();
|
||||
}
|
||||
|
||||
void clear() noexcept {
|
||||
m_iprime = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned int m_iprime;
|
||||
|
||||
static_assert(std::numeric_limits<decltype(m_iprime)>::max() >= detail::PRIMES.size(),
|
||||
"The type of m_iprime is not big enough.");
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
1766
third_party/hat-trie/tsl/array-hash/array_hash.h
vendored
1766
third_party/hat-trie/tsl/array-hash/array_hash.h
vendored
File diff suppressed because it is too large
Load Diff
863
third_party/hat-trie/tsl/array-hash/array_map.h
vendored
863
third_party/hat-trie/tsl/array-hash/array_map.h
vendored
@@ -1,863 +0,0 @@
|
||||
/**
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2017 Thibaut Goetghebuer-Planchon <tessil@gmx.com>
|
||||
*
|
||||
* 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 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.
|
||||
*/
|
||||
#ifndef TSL_ARRAY_MAP_H
|
||||
#define TSL_ARRAY_MAP_H
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <initializer_list>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include "array_hash.h"
|
||||
|
||||
namespace tsl {
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of a cache-conscious string hash map.
|
||||
*
|
||||
* The map stores the strings as `const CharT*`. If `StoreNullTerminator` is true,
|
||||
* the strings are stored with the a null-terminator (the `key()` method of the iterators
|
||||
* will return a pointer to this null-terminated string). Otherwise the null character
|
||||
* is not stored (which allow an economy of 1 byte per string).
|
||||
*
|
||||
* The value `T` must be either nothrow move-constructible, copy-constructible or both.
|
||||
*
|
||||
* The size of a key string is limited to `std::numeric_limits<KeySizeT>::max() - 1`.
|
||||
* That is 65 535 characters by default, but can be raised with the `KeySizeT` template parameter.
|
||||
* See `max_key_size()` for an easy access to this limit.
|
||||
*
|
||||
* The number of elements in the map is limited to `std::numeric_limits<IndexSizeT>::max()`.
|
||||
* That is 4 294 967 296 elements, but can be raised with the `IndexSizeT` template parameter.
|
||||
* See `max_size()` for an easy access to this limit.
|
||||
*
|
||||
* Iterators invalidation:
|
||||
* - clear, operator=: always invalidate the iterators.
|
||||
* - insert, emplace, operator[]: always invalidate the iterators.
|
||||
* - erase: always invalidate the iterators.
|
||||
* - shrink_to_fit: always invalidate the iterators.
|
||||
*/
|
||||
template<class CharT,
|
||||
class T,
|
||||
class Hash = tsl::ah::str_hash<CharT>,
|
||||
class KeyEqual = tsl::ah::str_equal<CharT>,
|
||||
bool StoreNullTerminator = true,
|
||||
class KeySizeT = std::uint16_t,
|
||||
class IndexSizeT = std::uint32_t,
|
||||
class GrowthPolicy = tsl::ah::power_of_two_growth_policy<2>>
|
||||
class array_map {
|
||||
private:
|
||||
template<typename U>
|
||||
using is_iterator = tsl::detail_array_hash::is_iterator<U>;
|
||||
|
||||
using ht = tsl::detail_array_hash::array_hash<CharT, T, Hash, KeyEqual, StoreNullTerminator,
|
||||
KeySizeT, IndexSizeT, GrowthPolicy>;
|
||||
|
||||
public:
|
||||
using char_type = typename ht::char_type;
|
||||
using mapped_type = T;
|
||||
using key_size_type = typename ht::key_size_type;
|
||||
using index_size_type = typename ht::index_size_type;
|
||||
using size_type = typename ht::size_type;
|
||||
using hasher = typename ht::hasher;
|
||||
using key_equal = typename ht::key_equal;
|
||||
using iterator = typename ht::iterator;
|
||||
using const_iterator = typename ht::const_iterator;
|
||||
|
||||
public:
|
||||
array_map(): array_map(ht::DEFAULT_INIT_BUCKET_COUNT) {
|
||||
}
|
||||
|
||||
explicit array_map(size_type bucket_count,
|
||||
const Hash& hash = Hash()): m_ht(bucket_count, hash, ht::DEFAULT_MAX_LOAD_FACTOR)
|
||||
{
|
||||
}
|
||||
|
||||
template<class InputIt, typename std::enable_if<is_iterator<InputIt>::value>::type* = nullptr>
|
||||
array_map(InputIt first, InputIt last,
|
||||
size_type bucket_count = ht::DEFAULT_INIT_BUCKET_COUNT,
|
||||
const Hash& hash = Hash()): array_map(bucket_count, hash)
|
||||
{
|
||||
insert(first, last);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
array_map(std::initializer_list<std::pair<std::basic_string_view<CharT>, T>> init,
|
||||
size_type bucket_count = ht::DEFAULT_INIT_BUCKET_COUNT,
|
||||
const Hash& hash = Hash()): array_map(bucket_count, hash)
|
||||
{
|
||||
insert(init);
|
||||
}
|
||||
#else
|
||||
array_map(std::initializer_list<std::pair<const CharT*, T>> init,
|
||||
size_type bucket_count = ht::DEFAULT_INIT_BUCKET_COUNT,
|
||||
const Hash& hash = Hash()): array_map(bucket_count, hash)
|
||||
{
|
||||
insert(init);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
array_map& operator=(std::initializer_list<std::pair<std::basic_string_view<CharT>, T>> ilist) {
|
||||
clear();
|
||||
|
||||
reserve(ilist.size());
|
||||
insert(ilist);
|
||||
|
||||
return *this;
|
||||
}
|
||||
#else
|
||||
array_map& operator=(std::initializer_list<std::pair<const CharT*, T>> ilist) {
|
||||
clear();
|
||||
|
||||
reserve(ilist.size());
|
||||
insert(ilist);
|
||||
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Iterators
|
||||
*/
|
||||
iterator begin() noexcept { return m_ht.begin(); }
|
||||
const_iterator begin() const noexcept { return m_ht.begin(); }
|
||||
const_iterator cbegin() const noexcept { return m_ht.cbegin(); }
|
||||
|
||||
iterator end() noexcept { return m_ht.end(); }
|
||||
const_iterator end() const noexcept { return m_ht.end(); }
|
||||
const_iterator cend() const noexcept { return m_ht.cend(); }
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Capacity
|
||||
*/
|
||||
bool empty() const noexcept { return m_ht.empty(); }
|
||||
size_type size() const noexcept { return m_ht.size(); }
|
||||
size_type max_size() const noexcept { return m_ht.max_size(); }
|
||||
size_type max_key_size() const noexcept { return m_ht.max_key_size(); }
|
||||
void shrink_to_fit() { m_ht.shrink_to_fit(); }
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Modifiers
|
||||
*/
|
||||
void clear() noexcept { m_ht.clear(); }
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
std::pair<iterator, bool> insert(const std::basic_string_view<CharT>& key, const T& value) {
|
||||
return m_ht.emplace(key.data(), key.size(), value);
|
||||
}
|
||||
#else
|
||||
std::pair<iterator, bool> insert(const CharT* key, const T& value) {
|
||||
return m_ht.emplace(key, std::char_traits<CharT>::length(key), value);
|
||||
}
|
||||
|
||||
std::pair<iterator, bool> insert(const std::basic_string<CharT>& key, const T& value) {
|
||||
return m_ht.emplace(key.data(), key.size(), value);
|
||||
}
|
||||
#endif
|
||||
std::pair<iterator, bool> insert_ks(const CharT* key, size_type key_size, const T& value) {
|
||||
return m_ht.emplace(key, key_size, value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
std::pair<iterator, bool> insert(const std::basic_string_view<CharT>& key, T&& value) {
|
||||
return m_ht.emplace(key.data(), key.size(), std::move(value));
|
||||
}
|
||||
#else
|
||||
std::pair<iterator, bool> insert(const CharT* key, T&& value) {
|
||||
return m_ht.emplace(key, std::char_traits<CharT>::length(key), std::move(value));
|
||||
}
|
||||
|
||||
std::pair<iterator, bool> insert(const std::basic_string<CharT>& key, T&& value) {
|
||||
return m_ht.emplace(key.data(), key.size(), std::move(value));
|
||||
}
|
||||
#endif
|
||||
std::pair<iterator, bool> insert_ks(const CharT* key, size_type key_size, T&& value) {
|
||||
return m_ht.emplace(key, key_size, std::move(value));
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class InputIt, typename std::enable_if<is_iterator<InputIt>::value>::type* = nullptr>
|
||||
void insert(InputIt first, InputIt last) {
|
||||
if(std::is_base_of<std::forward_iterator_tag,
|
||||
typename std::iterator_traits<InputIt>::iterator_category>::value)
|
||||
{
|
||||
const auto nb_elements_insert = std::distance(first, last);
|
||||
const std::size_t nb_free_buckets = std::size_t(float(bucket_count())*max_load_factor()) - size();
|
||||
|
||||
if(nb_elements_insert > 0 && nb_free_buckets < std::size_t(nb_elements_insert)) {
|
||||
reserve(size() + std::size_t(nb_elements_insert));
|
||||
}
|
||||
}
|
||||
|
||||
for(auto it = first; it != last; ++it) {
|
||||
insert_pair(*it);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
void insert(std::initializer_list<std::pair<std::basic_string_view<CharT>, T>> ilist) {
|
||||
insert(ilist.begin(), ilist.end());
|
||||
}
|
||||
#else
|
||||
void insert(std::initializer_list<std::pair<const CharT*, T>> ilist) {
|
||||
insert(ilist.begin(), ilist.end());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
template<class M>
|
||||
std::pair<iterator, bool> insert_or_assign(const std::basic_string_view<CharT>& key, M&& obj) {
|
||||
return m_ht.insert_or_assign(key.data(), key.size(), std::forward<M>(obj));
|
||||
}
|
||||
#else
|
||||
template<class M>
|
||||
std::pair<iterator, bool> insert_or_assign(const CharT* key, M&& obj) {
|
||||
return m_ht.insert_or_assign(key, std::char_traits<CharT>::length(key), std::forward<M>(obj));
|
||||
}
|
||||
|
||||
template<class M>
|
||||
std::pair<iterator, bool> insert_or_assign(const std::basic_string<CharT>& key, M&& obj) {
|
||||
return m_ht.insert_or_assign(key.data(), key.size(), std::forward<M>(obj));
|
||||
}
|
||||
#endif
|
||||
template<class M>
|
||||
std::pair<iterator, bool> insert_or_assign_ks(const CharT* key, size_type key_size, M&& obj) {
|
||||
return m_ht.insert_or_assign(key, key_size, std::forward<M>(obj));
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
template<class... Args>
|
||||
std::pair<iterator, bool> emplace(const std::basic_string_view<CharT>& key, Args&&... args) {
|
||||
return m_ht.emplace(key.data(), key.size(), std::forward<Args>(args)...);
|
||||
}
|
||||
#else
|
||||
template<class... Args>
|
||||
std::pair<iterator, bool> emplace(const CharT* key, Args&&... args) {
|
||||
return m_ht.emplace(key, std::char_traits<CharT>::length(key), std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<class... Args>
|
||||
std::pair<iterator, bool> emplace(const std::basic_string<CharT>& key, Args&&... args) {
|
||||
return m_ht.emplace(key.data(), key.size(), std::forward<Args>(args)...);
|
||||
}
|
||||
#endif
|
||||
template<class... Args>
|
||||
std::pair<iterator, bool> emplace_ks(const CharT* key, size_type key_size, Args&&... args) {
|
||||
return m_ht.emplace(key, key_size, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Erase has an amortized O(1) runtime complexity, but even if it removes the key immediately,
|
||||
* it doesn't do the same for the associated value T.
|
||||
*
|
||||
* T will only be removed when the ratio between the size of the map and
|
||||
* the size of the map + the number of deleted values still stored is low enough.
|
||||
*
|
||||
* To force the deletion you can call shrink_to_fit.
|
||||
*/
|
||||
iterator erase(const_iterator pos) { return m_ht.erase(pos); }
|
||||
|
||||
/**
|
||||
* @copydoc erase(const_iterator pos)
|
||||
*/
|
||||
iterator erase(const_iterator first, const_iterator last) { return m_ht.erase(first, last); }
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc erase(const_iterator pos)
|
||||
*/
|
||||
size_type erase(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.erase(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc erase(const_iterator pos)
|
||||
*/
|
||||
size_type erase(const CharT* key) {
|
||||
return m_ht.erase(key, std::char_traits<CharT>::length(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc erase(const_iterator pos)
|
||||
*/
|
||||
size_type erase(const std::basic_string<CharT>& key) {
|
||||
return m_ht.erase(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* @copydoc erase(const_iterator pos)
|
||||
*/
|
||||
size_type erase_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.erase(key, key_size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc erase_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
size_type erase(const std::basic_string_view<CharT>& key, std::size_t precalculated_hash) {
|
||||
return m_ht.erase(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc erase_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
size_type erase(const CharT* key, std::size_t precalculated_hash) {
|
||||
return m_ht.erase(key, std::char_traits<CharT>::length(key), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc erase_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
size_type erase(const std::basic_string<CharT>& key, std::size_t precalculated_hash) {
|
||||
return m_ht.erase(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* @copydoc erase(const_iterator pos)
|
||||
*
|
||||
* Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
|
||||
* as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.
|
||||
*/
|
||||
size_type erase_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) {
|
||||
return m_ht.erase(key, key_size, precalculated_hash);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void swap(array_map& other) { other.m_ht.swap(m_ht); }
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Lookup
|
||||
*/
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
T& at(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.at(key.data(), key.size());
|
||||
}
|
||||
|
||||
const T& at(const std::basic_string_view<CharT>& key) const {
|
||||
return m_ht.at(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
T& at(const CharT* key) {
|
||||
return m_ht.at(key, std::char_traits<CharT>::length(key));
|
||||
}
|
||||
|
||||
const T& at(const CharT* key) const {
|
||||
return m_ht.at(key, std::char_traits<CharT>::length(key));
|
||||
}
|
||||
|
||||
T& at(const std::basic_string<CharT>& key) {
|
||||
return m_ht.at(key.data(), key.size());
|
||||
}
|
||||
|
||||
const T& at(const std::basic_string<CharT>& key) const {
|
||||
return m_ht.at(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
T& at_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.at(key, key_size);
|
||||
}
|
||||
|
||||
const T& at_ks(const CharT* key, size_type key_size) const {
|
||||
return m_ht.at(key, key_size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc at_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
T& at(const std::basic_string_view<CharT>& key, std::size_t precalculated_hash) {
|
||||
return m_ht.at(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc at_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
const T& at(const std::basic_string_view<CharT>& key, std::size_t precalculated_hash) const {
|
||||
return m_ht.at(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc at_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
T& at(const CharT* key, std::size_t precalculated_hash) {
|
||||
return m_ht.at(key, std::char_traits<CharT>::length(key), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc at_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
const T& at(const CharT* key, std::size_t precalculated_hash) const {
|
||||
return m_ht.at(key, std::char_traits<CharT>::length(key), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc at_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
T& at(const std::basic_string<CharT>& key, std::size_t precalculated_hash) {
|
||||
return m_ht.at(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc at_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
const T& at(const std::basic_string<CharT>& key, std::size_t precalculated_hash) const {
|
||||
return m_ht.at(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
|
||||
* as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.
|
||||
*/
|
||||
T& at_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) {
|
||||
return m_ht.at(key, key_size, precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc at_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
const T& at_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) const {
|
||||
return m_ht.at(key, key_size, precalculated_hash);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
T& operator[](const std::basic_string_view<CharT>& key) { return m_ht.access_operator(key.data(), key.size()); }
|
||||
#else
|
||||
T& operator[](const CharT* key) { return m_ht.access_operator(key, std::char_traits<CharT>::length(key)); }
|
||||
T& operator[](const std::basic_string<CharT>& key) { return m_ht.access_operator(key.data(), key.size()); }
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
size_type count(const std::basic_string_view<CharT>& key) const {
|
||||
return m_ht.count(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
size_type count(const CharT* key) const {
|
||||
return m_ht.count(key, std::char_traits<CharT>::length(key));
|
||||
}
|
||||
|
||||
size_type count(const std::basic_string<CharT>& key) const {
|
||||
return m_ht.count(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
size_type count_ks(const CharT* key, size_type key_size) const {
|
||||
return m_ht.count(key, key_size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc count_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) const
|
||||
*/
|
||||
size_type count(const std::basic_string_view<CharT>& key, std::size_t precalculated_hash) const {
|
||||
return m_ht.count(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc count_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) const
|
||||
*/
|
||||
size_type count(const CharT* key, std::size_t precalculated_hash) const {
|
||||
return m_ht.count(key, std::char_traits<CharT>::length(key), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc count_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) const
|
||||
*/
|
||||
size_type count(const std::basic_string<CharT>& key, std::size_t precalculated_hash) const {
|
||||
return m_ht.count(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
|
||||
* as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.
|
||||
*/
|
||||
size_type count_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) const {
|
||||
return m_ht.count(key, key_size, precalculated_hash);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
iterator find(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
|
||||
const_iterator find(const std::basic_string_view<CharT>& key) const {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
iterator find(const CharT* key) {
|
||||
return m_ht.find(key, std::char_traits<CharT>::length(key));
|
||||
}
|
||||
|
||||
const_iterator find(const CharT* key) const {
|
||||
return m_ht.find(key, std::char_traits<CharT>::length(key));
|
||||
}
|
||||
|
||||
iterator find(const std::basic_string<CharT>& key) {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
|
||||
const_iterator find(const std::basic_string<CharT>& key) const {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
iterator find_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.find(key, key_size);
|
||||
}
|
||||
|
||||
const_iterator find_ks(const CharT* key, size_type key_size) const {
|
||||
return m_ht.find(key, key_size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
iterator find(const std::basic_string_view<CharT>& key, std::size_t precalculated_hash) {
|
||||
return m_ht.find(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
const_iterator find(const std::basic_string_view<CharT>& key, std::size_t precalculated_hash) const {
|
||||
return m_ht.find(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
iterator find(const CharT* key, std::size_t precalculated_hash) {
|
||||
return m_ht.find(key, std::char_traits<CharT>::length(key), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
const_iterator find(const CharT* key, std::size_t precalculated_hash) const {
|
||||
return m_ht.find(key, std::char_traits<CharT>::length(key), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
iterator find(const std::basic_string<CharT>& key, std::size_t precalculated_hash) {
|
||||
return m_ht.find(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
const_iterator find(const std::basic_string<CharT>& key, std::size_t precalculated_hash) const {
|
||||
return m_ht.find(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
|
||||
* as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.
|
||||
*/
|
||||
iterator find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) {
|
||||
return m_ht.find(key, key_size, precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
const_iterator find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) const {
|
||||
return m_ht.find(key, key_size, precalculated_hash);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
std::pair<iterator, iterator> equal_range(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range(const std::basic_string_view<CharT>& key) const {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
std::pair<iterator, iterator> equal_range(const CharT* key) {
|
||||
return m_ht.equal_range(key, std::char_traits<CharT>::length(key));
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range(const CharT* key) const {
|
||||
return m_ht.equal_range(key, std::char_traits<CharT>::length(key));
|
||||
}
|
||||
|
||||
std::pair<iterator, iterator> equal_range(const std::basic_string<CharT>& key) {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range(const std::basic_string<CharT>& key) const {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
std::pair<iterator, iterator> equal_range_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.equal_range(key, key_size);
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range_ks(const CharT* key, size_type key_size) const {
|
||||
return m_ht.equal_range(key, key_size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
std::pair<iterator, iterator> equal_range(const std::basic_string_view<CharT>& key, std::size_t precalculated_hash) {
|
||||
return m_ht.equal_range(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
std::pair<const_iterator, const_iterator> equal_range(const std::basic_string_view<CharT>& key, std::size_t precalculated_hash) const {
|
||||
return m_ht.equal_range(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
std::pair<iterator, iterator> equal_range(const CharT* key, std::size_t precalculated_hash) {
|
||||
return m_ht.equal_range(key, std::char_traits<CharT>::length(key), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
std::pair<const_iterator, const_iterator> equal_range(const CharT* key, std::size_t precalculated_hash) const {
|
||||
return m_ht.equal_range(key, std::char_traits<CharT>::length(key), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
std::pair<iterator, iterator> equal_range(const std::basic_string<CharT>& key, std::size_t precalculated_hash) {
|
||||
return m_ht.equal_range(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
std::pair<const_iterator, const_iterator> equal_range(const std::basic_string<CharT>& key, std::size_t precalculated_hash) const {
|
||||
return m_ht.equal_range(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
|
||||
* as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.
|
||||
*/
|
||||
std::pair<iterator, iterator> equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) {
|
||||
return m_ht.equal_range(key, key_size, precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
std::pair<const_iterator, const_iterator> equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) const {
|
||||
return m_ht.equal_range(key, key_size, precalculated_hash);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Bucket interface
|
||||
*/
|
||||
size_type bucket_count() const { return m_ht.bucket_count(); }
|
||||
size_type max_bucket_count() const { return m_ht.max_bucket_count(); }
|
||||
|
||||
|
||||
/*
|
||||
* Hash policy
|
||||
*/
|
||||
float load_factor() const { return m_ht.load_factor(); }
|
||||
float max_load_factor() const { return m_ht.max_load_factor(); }
|
||||
void max_load_factor(float ml) { m_ht.max_load_factor(ml); }
|
||||
|
||||
void rehash(size_type count) { m_ht.rehash(count); }
|
||||
void reserve(size_type count) { m_ht.reserve(count); }
|
||||
|
||||
|
||||
/*
|
||||
* Observers
|
||||
*/
|
||||
hasher hash_function() const { return m_ht.hash_function(); }
|
||||
key_equal key_eq() const { return m_ht.key_eq(); }
|
||||
|
||||
|
||||
/*
|
||||
* Other
|
||||
*/
|
||||
/**
|
||||
* Return the `const_iterator it` as an `iterator`.
|
||||
*/
|
||||
iterator mutable_iterator(const_iterator it) noexcept { return m_ht.mutable_iterator(it); }
|
||||
|
||||
/**
|
||||
* Serialize the map through the `serializer` parameter.
|
||||
*
|
||||
* The `serializer` parameter must be a function object that supports the following calls:
|
||||
* - `template<typename U> void operator()(const U& value);` where the types `std::uint64_t`, `float` and `T` must be supported for U.
|
||||
* - `void operator()(const CharT* value, std::size_t value_size);`
|
||||
*
|
||||
* The implementation leaves binary compatibility (endianness, IEEE 754 for floats, ...) of the types it serializes
|
||||
* in the hands of the `Serializer` function object if compatibility is required.
|
||||
*/
|
||||
template<class Serializer>
|
||||
void serialize(Serializer& serializer) const {
|
||||
m_ht.serialize(serializer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize a previously serialized map through the `deserializer` parameter.
|
||||
*
|
||||
* The `deserializer` parameter must be a function object that supports the following calls:
|
||||
* - `template<typename U> U operator()();` where the types `std::uint64_t`, `float` and `T` must be supported for U.
|
||||
* - `void operator()(CharT* value_out, std::size_t value_size);`
|
||||
*
|
||||
* If the deserialized hash map type is hash compatible with the serialized map, the deserialization process can be
|
||||
* sped up by setting `hash_compatible` to true. To be hash compatible, the Hash (take care of the 32-bits vs 64 bits),
|
||||
* KeyEqual, GrowthPolicy, StoreNullTerminator, KeySizeT and IndexSizeT must behave the same than the ones used on the
|
||||
* serialized map. Otherwise the behaviour is undefined with `hash_compatible` sets to true.
|
||||
*
|
||||
* The behaviour is undefined if the type `CharT` and `T` of the `array_map` are not the same as the
|
||||
* types used during serialization.
|
||||
*
|
||||
* The implementation leaves binary compatibility (endianness, IEEE 754 for floats, size of int, ...) of the types it
|
||||
* deserializes in the hands of the `Deserializer` function object if compatibility is required.
|
||||
*/
|
||||
template<class Deserializer>
|
||||
static array_map deserialize(Deserializer& deserializer, bool hash_compatible = false) {
|
||||
array_map map(0);
|
||||
map.m_ht.deserialize(deserializer, hash_compatible);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
friend bool operator==(const array_map& lhs, const array_map& rhs) {
|
||||
if(lhs.size() != rhs.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for(auto it = lhs.cbegin(); it != lhs.cend(); ++it) {
|
||||
const auto it_element_rhs = rhs.find_ks(it.key(), it.key_size());
|
||||
if(it_element_rhs == rhs.cend() || it.value() != it_element_rhs.value()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
friend bool operator!=(const array_map& lhs, const array_map& rhs) {
|
||||
return !operator==(lhs, rhs);
|
||||
}
|
||||
|
||||
friend void swap(array_map& lhs, array_map& rhs) {
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
template<class U, class V>
|
||||
void insert_pair(const std::pair<U, V>& value) {
|
||||
insert(value.first, value.second);
|
||||
}
|
||||
|
||||
template<class U, class V>
|
||||
void insert_pair(std::pair<U, V>&& value) {
|
||||
insert(value.first, std::move(value.second));
|
||||
}
|
||||
|
||||
public:
|
||||
static const size_type MAX_KEY_SIZE = ht::MAX_KEY_SIZE;
|
||||
|
||||
private:
|
||||
ht m_ht;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Same as
|
||||
* `tsl::array_map<CharT, T, Hash, KeyEqual, StoreNullTerminator, KeySizeT, IndexSizeT, tsl::ah::prime_growth_policy>`.
|
||||
*/
|
||||
template<class CharT,
|
||||
class T,
|
||||
class Hash = tsl::ah::str_hash<CharT>,
|
||||
class KeyEqual = tsl::ah::str_equal<CharT>,
|
||||
bool StoreNullTerminator = true,
|
||||
class KeySizeT = std::uint16_t,
|
||||
class IndexSizeT = std::uint32_t>
|
||||
using array_pg_map = array_map<CharT, T, Hash, KeyEqual, StoreNullTerminator,
|
||||
KeySizeT, IndexSizeT, tsl::ah::prime_growth_policy>;
|
||||
|
||||
} //end namespace tsl
|
||||
|
||||
#endif
|
||||
664
third_party/hat-trie/tsl/array-hash/array_set.h
vendored
664
third_party/hat-trie/tsl/array-hash/array_set.h
vendored
@@ -1,664 +0,0 @@
|
||||
/**
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2017 Thibaut Goetghebuer-Planchon <tessil@gmx.com>
|
||||
*
|
||||
* 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 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.
|
||||
*/
|
||||
#ifndef TSL_ARRAY_SET_H
|
||||
#define TSL_ARRAY_SET_H
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <initializer_list>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include "array_hash.h"
|
||||
|
||||
namespace tsl {
|
||||
|
||||
/**
|
||||
* Implementation of a cache-conscious string hash set.
|
||||
*
|
||||
* The set stores the strings as `const CharT*`. If `StoreNullTerminator` is true,
|
||||
* the strings are stored with the a null-terminator (the `key()` method of the iterators
|
||||
* will return a pointer to this null-terminated string). Otherwise the null character
|
||||
* is not stored (which allow an economy of 1 byte per string).
|
||||
*
|
||||
* The size of a key string is limited to `std::numeric_limits<KeySizeT>::max() - 1`.
|
||||
* That is 65 535 characters by default, but can be raised with the `KeySizeT` template parameter.
|
||||
* See `max_key_size()` for an easy access to this limit.
|
||||
*
|
||||
* The number of elements in the set is limited to `std::numeric_limits<IndexSizeT>::max()`.
|
||||
* That is 4 294 967 296 elements, but can be raised with the `IndexSizeT` template parameter.
|
||||
* See `max_size()` for an easy access to this limit.
|
||||
*
|
||||
* Iterators invalidation:
|
||||
* - clear, operator=: always invalidate the iterators.
|
||||
* - insert, emplace, operator[]: always invalidate the iterators.
|
||||
* - erase: always invalidate the iterators.
|
||||
* - shrink_to_fit: always invalidate the iterators.
|
||||
*/
|
||||
template<class CharT,
|
||||
class Hash = tsl::ah::str_hash<CharT>,
|
||||
class KeyEqual = tsl::ah::str_equal<CharT>,
|
||||
bool StoreNullTerminator = true,
|
||||
class KeySizeT = std::uint16_t,
|
||||
class IndexSizeT = std::uint32_t,
|
||||
class GrowthPolicy = tsl::ah::power_of_two_growth_policy<2>>
|
||||
class array_set {
|
||||
private:
|
||||
template<typename U>
|
||||
using is_iterator = tsl::detail_array_hash::is_iterator<U>;
|
||||
|
||||
using ht = tsl::detail_array_hash::array_hash<CharT, void, Hash, KeyEqual, StoreNullTerminator,
|
||||
KeySizeT, IndexSizeT, GrowthPolicy>;
|
||||
|
||||
public:
|
||||
using char_type = typename ht::char_type;
|
||||
using key_size_type = typename ht::key_size_type;
|
||||
using index_size_type = typename ht::index_size_type;
|
||||
using size_type = typename ht::size_type;
|
||||
using hasher = typename ht::hasher;
|
||||
using key_equal = typename ht::key_equal;
|
||||
using iterator = typename ht::iterator;
|
||||
using const_iterator = typename ht::const_iterator;
|
||||
|
||||
array_set(): array_set(ht::DEFAULT_INIT_BUCKET_COUNT) {
|
||||
}
|
||||
|
||||
explicit array_set(size_type bucket_count,
|
||||
const Hash& hash = Hash()): m_ht(bucket_count, hash, ht::DEFAULT_MAX_LOAD_FACTOR)
|
||||
{
|
||||
}
|
||||
|
||||
template<class InputIt, typename std::enable_if<is_iterator<InputIt>::value>::type* = nullptr>
|
||||
array_set(InputIt first, InputIt last,
|
||||
size_type bucket_count = ht::DEFAULT_INIT_BUCKET_COUNT,
|
||||
const Hash& hash = Hash()): array_set(bucket_count, hash)
|
||||
{
|
||||
insert(first, last);
|
||||
}
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
array_set(std::initializer_list<std::basic_string_view<CharT>> init,
|
||||
size_type bucket_count = ht::DEFAULT_INIT_BUCKET_COUNT,
|
||||
const Hash& hash = Hash()): array_set(bucket_count, hash)
|
||||
{
|
||||
insert(init);
|
||||
}
|
||||
#else
|
||||
array_set(std::initializer_list<const CharT*> init,
|
||||
size_type bucket_count = ht::DEFAULT_INIT_BUCKET_COUNT,
|
||||
const Hash& hash = Hash()): array_set(bucket_count, hash)
|
||||
{
|
||||
insert(init);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
array_set& operator=(std::initializer_list<std::basic_string_view<CharT>> ilist) {
|
||||
clear();
|
||||
|
||||
reserve(ilist.size());
|
||||
insert(ilist);
|
||||
|
||||
return *this;
|
||||
}
|
||||
#else
|
||||
array_set& operator=(std::initializer_list<const CharT*> ilist) {
|
||||
clear();
|
||||
|
||||
reserve(ilist.size());
|
||||
insert(ilist);
|
||||
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Iterators
|
||||
*/
|
||||
iterator begin() noexcept { return m_ht.begin(); }
|
||||
const_iterator begin() const noexcept { return m_ht.begin(); }
|
||||
const_iterator cbegin() const noexcept { return m_ht.cbegin(); }
|
||||
|
||||
iterator end() noexcept { return m_ht.end(); }
|
||||
const_iterator end() const noexcept { return m_ht.end(); }
|
||||
const_iterator cend() const noexcept { return m_ht.cend(); }
|
||||
|
||||
|
||||
/*
|
||||
* Capacity
|
||||
*/
|
||||
bool empty() const noexcept { return m_ht.empty(); }
|
||||
size_type size() const noexcept { return m_ht.size(); }
|
||||
size_type max_size() const noexcept { return m_ht.max_size(); }
|
||||
size_type max_key_size() const noexcept { return m_ht.max_key_size(); }
|
||||
void shrink_to_fit() { m_ht.shrink_to_fit(); }
|
||||
|
||||
|
||||
/*
|
||||
* Modifiers
|
||||
*/
|
||||
void clear() noexcept { m_ht.clear(); }
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
std::pair<iterator, bool> insert(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.emplace(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
std::pair<iterator, bool> insert(const CharT* key) {
|
||||
return m_ht.emplace(key, std::char_traits<CharT>::length(key));
|
||||
}
|
||||
|
||||
std::pair<iterator, bool> insert(const std::basic_string<CharT>& key) {
|
||||
return m_ht.emplace(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
std::pair<iterator, bool> insert_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.emplace(key, key_size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class InputIt, typename std::enable_if<is_iterator<InputIt>::value>::type* = nullptr>
|
||||
void insert(InputIt first, InputIt last) {
|
||||
if(std::is_base_of<std::forward_iterator_tag,
|
||||
typename std::iterator_traits<InputIt>::iterator_category>::value)
|
||||
{
|
||||
const auto nb_elements_insert = std::distance(first, last);
|
||||
const std::size_t nb_free_buckets = std::size_t(float(bucket_count())*max_load_factor()) - size();
|
||||
|
||||
if(nb_elements_insert > 0 && nb_free_buckets < std::size_t(nb_elements_insert)) {
|
||||
reserve(size() + std::size_t(nb_elements_insert));
|
||||
}
|
||||
}
|
||||
|
||||
for(auto it = first; it != last; ++it) {
|
||||
insert(*it);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
void insert(std::initializer_list<std::basic_string_view<CharT>> ilist) {
|
||||
insert(ilist.begin(), ilist.end());
|
||||
}
|
||||
#else
|
||||
void insert(std::initializer_list<const CharT*> ilist) {
|
||||
insert(ilist.begin(), ilist.end());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc emplace_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
std::pair<iterator, bool> emplace(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.emplace(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc emplace_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
std::pair<iterator, bool> emplace(const CharT* key) {
|
||||
return m_ht.emplace(key, std::char_traits<CharT>::length(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc emplace_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
std::pair<iterator, bool> emplace(const std::basic_string<CharT>& key) {
|
||||
return m_ht.emplace(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* No difference compared to the insert method. Mainly here for coherence with array_map.
|
||||
*/
|
||||
std::pair<iterator, bool> emplace_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.emplace(key, key_size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
iterator erase(const_iterator pos) { return m_ht.erase(pos); }
|
||||
iterator erase(const_iterator first, const_iterator last) { return m_ht.erase(first, last); }
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
size_type erase(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.erase(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
size_type erase(const CharT* key) {
|
||||
return m_ht.erase(key, std::char_traits<CharT>::length(key));
|
||||
}
|
||||
|
||||
size_type erase(const std::basic_string<CharT>& key) {
|
||||
return m_ht.erase(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
size_type erase_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.erase(key, key_size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc erase_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
size_type erase(const std::basic_string_view<CharT>& key, std::size_t precalculated_hash) {
|
||||
return m_ht.erase(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc erase_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
size_type erase(const CharT* key, std::size_t precalculated_hash) {
|
||||
return m_ht.erase(key, std::char_traits<CharT>::length(key), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc erase_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
size_type erase(const std::basic_string<CharT>& key, std::size_t precalculated_hash) {
|
||||
return m_ht.erase(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
|
||||
* as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.
|
||||
*/
|
||||
size_type erase_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) {
|
||||
return m_ht.erase(key, key_size, precalculated_hash);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void swap(array_set& other) { other.m_ht.swap(m_ht); }
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Lookup
|
||||
*/
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
size_type count(const std::basic_string_view<CharT>& key) const { return m_ht.count(key.data(), key.size()); }
|
||||
#else
|
||||
size_type count(const CharT* key) const { return m_ht.count(key, std::char_traits<CharT>::length(key)); }
|
||||
size_type count(const std::basic_string<CharT>& key) const { return m_ht.count(key.data(), key.size()); }
|
||||
#endif
|
||||
size_type count_ks(const CharT* key, size_type key_size) const { return m_ht.count(key, key_size); }
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc count_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) const
|
||||
*/
|
||||
size_type count(const std::basic_string_view<CharT>& key, std::size_t precalculated_hash) const {
|
||||
return m_ht.count(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc count_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) const
|
||||
*/
|
||||
size_type count(const CharT* key, std::size_t precalculated_hash) const {
|
||||
return m_ht.count(key, std::char_traits<CharT>::length(key), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc count_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) const
|
||||
*/
|
||||
size_type count(const std::basic_string<CharT>& key, std::size_t precalculated_hash) const {
|
||||
return m_ht.count(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
|
||||
* as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.
|
||||
*/
|
||||
size_type count_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) const {
|
||||
return m_ht.count(key, key_size, precalculated_hash);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
iterator find(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
|
||||
const_iterator find(const std::basic_string_view<CharT>& key) const {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
iterator find(const CharT* key) {
|
||||
return m_ht.find(key, std::char_traits<CharT>::length(key));
|
||||
}
|
||||
|
||||
const_iterator find(const CharT* key) const {
|
||||
return m_ht.find(key, std::char_traits<CharT>::length(key));
|
||||
}
|
||||
|
||||
iterator find(const std::basic_string<CharT>& key) {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
|
||||
const_iterator find(const std::basic_string<CharT>& key) const {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
iterator find_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.find(key, key_size);
|
||||
}
|
||||
|
||||
const_iterator find_ks(const CharT* key, size_type key_size) const {
|
||||
return m_ht.find(key, key_size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
iterator find(const std::basic_string_view<CharT>& key, std::size_t precalculated_hash) {
|
||||
return m_ht.find(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
const_iterator find(const std::basic_string_view<CharT>& key, std::size_t precalculated_hash) const {
|
||||
return m_ht.find(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
iterator find(const CharT* key, std::size_t precalculated_hash) {
|
||||
return m_ht.find(key, std::char_traits<CharT>::length(key), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
const_iterator find(const CharT* key, std::size_t precalculated_hash) const {
|
||||
return m_ht.find(key, std::char_traits<CharT>::length(key), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
iterator find(const std::basic_string<CharT>& key, std::size_t precalculated_hash) {
|
||||
return m_ht.find(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
const_iterator find(const std::basic_string<CharT>& key, std::size_t precalculated_hash) const {
|
||||
return m_ht.find(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
|
||||
* as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.
|
||||
*/
|
||||
iterator find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) {
|
||||
return m_ht.find(key, key_size, precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
const_iterator find_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) const {
|
||||
return m_ht.find(key, key_size, precalculated_hash);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
std::pair<iterator, iterator> equal_range(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range(const std::basic_string_view<CharT>& key) const {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
std::pair<iterator, iterator> equal_range(const CharT* key) {
|
||||
return m_ht.equal_range(key, std::char_traits<CharT>::length(key));
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range(const CharT* key) const {
|
||||
return m_ht.equal_range(key, std::char_traits<CharT>::length(key));
|
||||
}
|
||||
|
||||
std::pair<iterator, iterator> equal_range(const std::basic_string<CharT>& key) {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range(const std::basic_string<CharT>& key) const {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
std::pair<iterator, iterator> equal_range_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.equal_range(key, key_size);
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range_ks(const CharT* key, size_type key_size) const {
|
||||
return m_ht.equal_range(key, key_size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_AH_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
std::pair<iterator, iterator> equal_range(const std::basic_string_view<CharT>& key, std::size_t precalculated_hash) {
|
||||
return m_ht.equal_range(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
std::pair<const_iterator, const_iterator> equal_range(const std::basic_string_view<CharT>& key, std::size_t precalculated_hash) const {
|
||||
return m_ht.equal_range(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
std::pair<iterator, iterator> equal_range(const CharT* key, std::size_t precalculated_hash) {
|
||||
return m_ht.equal_range(key, std::char_traits<CharT>::length(key), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
std::pair<const_iterator, const_iterator> equal_range(const CharT* key, std::size_t precalculated_hash) const {
|
||||
return m_ht.equal_range(key, std::char_traits<CharT>::length(key), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
std::pair<iterator, iterator> equal_range(const std::basic_string<CharT>& key, std::size_t precalculated_hash) {
|
||||
return m_ht.equal_range(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
std::pair<const_iterator, const_iterator> equal_range(const std::basic_string<CharT>& key, std::size_t precalculated_hash) const {
|
||||
return m_ht.equal_range(key.data(), key.size(), precalculated_hash);
|
||||
}
|
||||
#endif
|
||||
/**
|
||||
* Use the hash value 'precalculated_hash' instead of hashing the key. The hash value should be the same
|
||||
* as hash_function()(key). Useful to speed-up the lookup to the value if you already have the hash.
|
||||
*/
|
||||
std::pair<iterator, iterator> equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) {
|
||||
return m_ht.equal_range(key, key_size, precalculated_hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash)
|
||||
*/
|
||||
std::pair<const_iterator, const_iterator> equal_range_ks(const CharT* key, size_type key_size, std::size_t precalculated_hash) const {
|
||||
return m_ht.equal_range(key, key_size, precalculated_hash);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Bucket interface
|
||||
*/
|
||||
size_type bucket_count() const { return m_ht.bucket_count(); }
|
||||
size_type max_bucket_count() const { return m_ht.max_bucket_count(); }
|
||||
|
||||
|
||||
/*
|
||||
* Hash policy
|
||||
*/
|
||||
float load_factor() const { return m_ht.load_factor(); }
|
||||
float max_load_factor() const { return m_ht.max_load_factor(); }
|
||||
void max_load_factor(float ml) { m_ht.max_load_factor(ml); }
|
||||
|
||||
void rehash(size_type count) { m_ht.rehash(count); }
|
||||
void reserve(size_type count) { m_ht.reserve(count); }
|
||||
|
||||
|
||||
/*
|
||||
* Observers
|
||||
*/
|
||||
hasher hash_function() const { return m_ht.hash_function(); }
|
||||
key_equal key_eq() const { return m_ht.key_eq(); }
|
||||
|
||||
|
||||
/*
|
||||
* Other
|
||||
*/
|
||||
/**
|
||||
* Return the `const_iterator it` as an `iterator`.
|
||||
*/
|
||||
iterator mutable_iterator(const_iterator it) noexcept { return m_ht.mutable_iterator(it); }
|
||||
|
||||
/**
|
||||
* Serialize the set through the `serializer` parameter.
|
||||
*
|
||||
* The `serializer` parameter must be a function object that supports the following calls:
|
||||
* - `template<typename U> void operator()(const U& value);` where the types `std::uint64_t` and `float` must be supported for U.
|
||||
* - `void operator()(const CharT* value, std::size_t value_size);`
|
||||
*
|
||||
* The implementation leaves binary compatibility (endianness, IEEE 754 for floats, ...) of the types it serializes
|
||||
* in the hands of the `Serializer` function object if compatibility is required.
|
||||
*/
|
||||
template<class Serializer>
|
||||
void serialize(Serializer& serializer) const {
|
||||
m_ht.serialize(serializer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize a previously serialized set through the `deserializer` parameter.
|
||||
*
|
||||
* The `deserializer` parameter must be a function object that supports the following calls:
|
||||
* - `template<typename U> U operator()();` where the types `std::uint64_t` and `float` must be supported for U.
|
||||
* - `void operator()(CharT* value_out, std::size_t value_size);`
|
||||
*
|
||||
* If the deserialized hash set type is hash compatible with the serialized set, the deserialization process can be
|
||||
* sped up by setting `hash_compatible` to true. To be hash compatible, the Hash (take care of the 32-bits vs 64 bits),
|
||||
* KeyEqual, GrowthPolicy, StoreNullTerminator, KeySizeT and IndexSizeT must behave the same than the ones used on the
|
||||
* serialized set. Otherwise the behaviour is undefined with `hash_compatible` sets to true.
|
||||
*
|
||||
* The behaviour is undefined if the type `CharT` of the `array_set` is not the same as the
|
||||
* type used during serialization.
|
||||
*
|
||||
* The implementation leaves binary compatibility (endianness, IEEE 754 for floats, size of int, ...) of the types it
|
||||
* deserializes in the hands of the `Deserializer` function object if compatibility is required.
|
||||
*/
|
||||
template<class Deserializer>
|
||||
static array_set deserialize(Deserializer& deserializer, bool hash_compatible = false) {
|
||||
array_set set(0);
|
||||
set.m_ht.deserialize(deserializer, hash_compatible);
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
friend bool operator==(const array_set& lhs, const array_set& rhs) {
|
||||
if(lhs.size() != rhs.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for(auto it = lhs.cbegin(); it != lhs.cend(); ++it) {
|
||||
const auto it_element_rhs = rhs.find_ks(it.key(), it.key_size());
|
||||
if(it_element_rhs == rhs.cend()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
friend bool operator!=(const array_set& lhs, const array_set& rhs) {
|
||||
return !operator==(lhs, rhs);
|
||||
}
|
||||
|
||||
friend void swap(array_set& lhs, array_set& rhs) {
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
public:
|
||||
static const size_type MAX_KEY_SIZE = ht::MAX_KEY_SIZE;
|
||||
|
||||
private:
|
||||
ht m_ht;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Same as
|
||||
* `tsl::array_set<CharT, Hash, KeyEqual, StoreNullTerminator, KeySizeT, IndexSizeT, tsl::ah::prime_growth_policy>`.
|
||||
*/
|
||||
template<class CharT,
|
||||
class Hash = tsl::ah::str_hash<CharT>,
|
||||
class KeyEqual = tsl::ah::str_equal<CharT>,
|
||||
bool StoreNullTerminator = true,
|
||||
class KeySizeT = std::uint16_t,
|
||||
class IndexSizeT = std::uint32_t>
|
||||
using array_pg_set = array_set<CharT, Hash, KeyEqual, StoreNullTerminator,
|
||||
KeySizeT, IndexSizeT, tsl::ah::prime_growth_policy>;
|
||||
|
||||
} //end namespace tsl
|
||||
|
||||
#endif
|
||||
2090
third_party/hat-trie/tsl/htrie_hash.h
vendored
2090
third_party/hat-trie/tsl/htrie_hash.h
vendored
File diff suppressed because it is too large
Load Diff
647
third_party/hat-trie/tsl/htrie_map.h
vendored
647
third_party/hat-trie/tsl/htrie_map.h
vendored
@@ -1,647 +0,0 @@
|
||||
/**
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2017 Thibaut Goetghebuer-Planchon <tessil@gmx.com>
|
||||
*
|
||||
* 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 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.
|
||||
*/
|
||||
#ifndef TSL_HTRIE_MAP_H
|
||||
#define TSL_HTRIE_MAP_H
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <initializer_list>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include "htrie_hash.h"
|
||||
|
||||
namespace tsl {
|
||||
|
||||
/**
|
||||
* Implementation of a hat-trie map.
|
||||
*
|
||||
* The value T must be either nothrow move-constructible/assignable, copy-constructible or both.
|
||||
*
|
||||
* The size of a key string is limited to std::numeric_limits<KeySizeT>::max() - 1.
|
||||
* That is 65 535 characters by default, but can be raised with the KeySizeT template parameter.
|
||||
* See max_key_size() for an easy access to this limit.
|
||||
*
|
||||
* Iterators invalidation:
|
||||
* - clear, operator=: always invalidate the iterators.
|
||||
* - insert, emplace, operator[]: always invalidate the iterators.
|
||||
* - erase: always invalidate the iterators.
|
||||
*/
|
||||
template<class CharT,
|
||||
class T,
|
||||
class Hash = tsl::ah::str_hash<CharT>,
|
||||
class KeySizeT = std::uint16_t>
|
||||
class htrie_map {
|
||||
private:
|
||||
template<typename U>
|
||||
using is_iterator = tsl::detail_array_hash::is_iterator<U>;
|
||||
|
||||
using ht = tsl::detail_htrie_hash::htrie_hash<CharT, T, Hash, KeySizeT>;
|
||||
|
||||
public:
|
||||
using char_type = typename ht::char_type;
|
||||
using mapped_type = T;
|
||||
using key_size_type = typename ht::key_size_type;
|
||||
using size_type = typename ht::size_type;
|
||||
using hasher = typename ht::hasher;
|
||||
using iterator = typename ht::iterator;
|
||||
using const_iterator = typename ht::const_iterator;
|
||||
using prefix_iterator = typename ht::prefix_iterator;
|
||||
using const_prefix_iterator = typename ht::const_prefix_iterator;
|
||||
|
||||
public:
|
||||
explicit htrie_map(const Hash& hash = Hash()): m_ht(hash, ht::HASH_NODE_DEFAULT_MAX_LOAD_FACTOR,
|
||||
ht::DEFAULT_BURST_THRESHOLD)
|
||||
{
|
||||
}
|
||||
|
||||
explicit htrie_map(size_type burst_threshold,
|
||||
const Hash& hash = Hash()): m_ht(hash, ht::HASH_NODE_DEFAULT_MAX_LOAD_FACTOR,
|
||||
burst_threshold)
|
||||
{
|
||||
}
|
||||
|
||||
template<class InputIt, typename std::enable_if<is_iterator<InputIt>::value>::type* = nullptr>
|
||||
htrie_map(InputIt first, InputIt last,
|
||||
const Hash& hash = Hash()): htrie_map(hash)
|
||||
{
|
||||
insert(first, last);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
htrie_map(std::initializer_list<std::pair<std::basic_string_view<CharT>, T>> init,
|
||||
const Hash& hash = Hash()): htrie_map(hash)
|
||||
{
|
||||
insert(init);
|
||||
}
|
||||
#else
|
||||
htrie_map(std::initializer_list<std::pair<const CharT*, T>> init,
|
||||
const Hash& hash = Hash()): htrie_map(hash)
|
||||
{
|
||||
insert(init);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
htrie_map& operator=(std::initializer_list<std::pair<std::basic_string_view<CharT>, T>> ilist) {
|
||||
clear();
|
||||
insert(ilist);
|
||||
|
||||
return *this;
|
||||
}
|
||||
#else
|
||||
htrie_map& operator=(std::initializer_list<std::pair<const CharT*, T>> ilist) {
|
||||
clear();
|
||||
insert(ilist);
|
||||
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Iterators
|
||||
*/
|
||||
iterator begin() noexcept { return m_ht.begin(); }
|
||||
const_iterator begin() const noexcept { return m_ht.begin(); }
|
||||
const_iterator cbegin() const noexcept { return m_ht.cbegin(); }
|
||||
|
||||
iterator end() noexcept { return m_ht.end(); }
|
||||
const_iterator end() const noexcept { return m_ht.end(); }
|
||||
const_iterator cend() const noexcept { return m_ht.cend(); }
|
||||
|
||||
|
||||
/*
|
||||
* Capacity
|
||||
*/
|
||||
bool empty() const noexcept { return m_ht.empty(); }
|
||||
size_type size() const noexcept { return m_ht.size(); }
|
||||
size_type max_size() const noexcept { return m_ht.max_size(); }
|
||||
size_type max_key_size() const noexcept { return m_ht.max_key_size(); }
|
||||
|
||||
/**
|
||||
* Call shrink_to_fit() on each hash node of the hat-trie to reduce its size.
|
||||
*/
|
||||
void shrink_to_fit() { m_ht.shrink_to_fit(); }
|
||||
|
||||
|
||||
/*
|
||||
* Modifiers
|
||||
*/
|
||||
void clear() noexcept { m_ht.clear(); }
|
||||
|
||||
|
||||
|
||||
std::pair<iterator, bool> insert_ks(const CharT* key, size_type key_size, const T& value) {
|
||||
return m_ht.insert(key, key_size, value);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
std::pair<iterator, bool> insert(const std::basic_string_view<CharT>& key, const T& value) {
|
||||
return m_ht.insert(key.data(), key.size(), value);
|
||||
}
|
||||
#else
|
||||
std::pair<iterator, bool> insert(const CharT* key, const T& value) {
|
||||
return m_ht.insert(key, std::strlen(key), value);
|
||||
}
|
||||
|
||||
std::pair<iterator, bool> insert(const std::basic_string<CharT>& key, const T& value) {
|
||||
return m_ht.insert(key.data(), key.size(), value);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
std::pair<iterator, bool> insert_ks(const CharT* key, size_type key_size, T&& value) {
|
||||
return m_ht.insert(key, key_size, std::move(value));
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
std::pair<iterator, bool> insert(const std::basic_string_view<CharT>& key, T&& value) {
|
||||
return m_ht.insert(key.data(), key.size(), std::move(value));
|
||||
}
|
||||
#else
|
||||
std::pair<iterator, bool> insert(const CharT* key, T&& value) {
|
||||
return m_ht.insert(key, std::strlen(key), std::move(value));
|
||||
}
|
||||
|
||||
std::pair<iterator, bool> insert(const std::basic_string<CharT>& key, T&& value) {
|
||||
return m_ht.insert(key.data(), key.size(), std::move(value));
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
template<class InputIt, typename std::enable_if<is_iterator<InputIt>::value>::type* = nullptr>
|
||||
void insert(InputIt first, InputIt last) {
|
||||
for(auto it = first; it != last; ++it) {
|
||||
insert_pair(*it);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
void insert(std::initializer_list<std::pair<std::basic_string_view<CharT>, T>> ilist) {
|
||||
insert(ilist.begin(), ilist.end());
|
||||
}
|
||||
#else
|
||||
void insert(std::initializer_list<std::pair<const CharT*, T>> ilist) {
|
||||
insert(ilist.begin(), ilist.end());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
template<class... Args>
|
||||
std::pair<iterator, bool> emplace_ks(const CharT* key, size_type key_size, Args&&... args) {
|
||||
return m_ht.insert(key, key_size, std::forward<Args>(args)...);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
template<class... Args>
|
||||
std::pair<iterator, bool> emplace(const std::basic_string_view<CharT>& key, Args&&... args) {
|
||||
return m_ht.insert(key.data(), key.size(), std::forward<Args>(args)...);
|
||||
}
|
||||
#else
|
||||
template<class... Args>
|
||||
std::pair<iterator, bool> emplace(const CharT* key, Args&&... args) {
|
||||
return m_ht.insert(key, std::strlen(key), std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<class... Args>
|
||||
std::pair<iterator, bool> emplace(const std::basic_string<CharT>& key, Args&&... args) {
|
||||
return m_ht.insert(key.data(), key.size(), std::forward<Args>(args)...);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
iterator erase(const_iterator pos) { return m_ht.erase(pos); }
|
||||
iterator erase(const_iterator first, const_iterator last) { return m_ht.erase(first, last); }
|
||||
|
||||
|
||||
|
||||
size_type erase_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.erase(key, key_size);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
size_type erase(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.erase(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
size_type erase(const CharT* key) {
|
||||
return m_ht.erase(key, std::strlen(key));
|
||||
}
|
||||
|
||||
size_type erase(const std::basic_string<CharT>& key) {
|
||||
return m_ht.erase(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Erase all the elements which have 'prefix' as prefix. Return the number of erase elements.
|
||||
*/
|
||||
size_type erase_prefix_ks(const CharT* prefix, size_type prefix_size) {
|
||||
return m_ht.erase_prefix(prefix, prefix_size);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc erase_prefix_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
size_type erase_prefix(const std::basic_string_view<CharT>& prefix) {
|
||||
return m_ht.erase_prefix(prefix.data(), prefix.size());
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc erase_prefix_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
size_type erase_prefix(const CharT* prefix) {
|
||||
return m_ht.erase_prefix(prefix, std::strlen(prefix));
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc erase_prefix_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
size_type erase_prefix(const std::basic_string<CharT>& prefix) {
|
||||
return m_ht.erase_prefix(prefix.data(), prefix.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
void swap(htrie_map& other) { other.m_ht.swap(m_ht); }
|
||||
|
||||
/*
|
||||
* Lookup
|
||||
*/
|
||||
T& at_ks(const CharT* key, size_type key_size) { return m_ht.at(key, key_size); }
|
||||
const T& at_ks(const CharT* key, size_type key_size) const { return m_ht.at(key, key_size); }
|
||||
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
T& at(const std::basic_string_view<CharT>& key) { return m_ht.at(key.data(), key.size()); }
|
||||
const T& at(const std::basic_string_view<CharT>& key) const { return m_ht.at(key.data(), key.size()); }
|
||||
#else
|
||||
T& at(const CharT* key) { return m_ht.at(key, std::strlen(key)); }
|
||||
const T& at(const CharT* key) const { return m_ht.at(key, std::strlen(key)); }
|
||||
|
||||
T& at(const std::basic_string<CharT>& key) { return m_ht.at(key.data(), key.size()); }
|
||||
const T& at(const std::basic_string<CharT>& key) const { return m_ht.at(key.data(), key.size()); }
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
T& operator[](const std::basic_string_view<CharT>& key) { return m_ht.access_operator(key.data(), key.size()); }
|
||||
#else
|
||||
T& operator[](const CharT* key) { return m_ht.access_operator(key, std::strlen(key)); }
|
||||
T& operator[](const std::basic_string<CharT>& key) { return m_ht.access_operator(key.data(), key.size()); }
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
size_type count_ks(const CharT* key, size_type key_size) const { return m_ht.count(key, key_size); }
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
size_type count(const std::basic_string_view<CharT>& key) const { return m_ht.count(key.data(), key.size()); }
|
||||
#else
|
||||
size_type count(const CharT* key) const { return m_ht.count(key, std::strlen(key)); }
|
||||
size_type count(const std::basic_string<CharT>& key) const { return m_ht.count(key.data(), key.size()); }
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
iterator find_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.find(key, key_size);
|
||||
}
|
||||
|
||||
const_iterator find_ks(const CharT* key, size_type key_size) const {
|
||||
return m_ht.find(key, key_size);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
iterator find(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
|
||||
const_iterator find(const std::basic_string_view<CharT>& key) const {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
iterator find(const CharT* key) {
|
||||
return m_ht.find(key, std::strlen(key));
|
||||
}
|
||||
|
||||
const_iterator find(const CharT* key) const {
|
||||
return m_ht.find(key, std::strlen(key));
|
||||
}
|
||||
|
||||
iterator find(const std::basic_string<CharT>& key) {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
|
||||
const_iterator find(const std::basic_string<CharT>& key) const {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
std::pair<iterator, iterator> equal_range_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.equal_range(key, key_size);
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range_ks(const CharT* key, size_type key_size) const {
|
||||
return m_ht.equal_range(key, key_size);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
std::pair<iterator, iterator> equal_range(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range(const std::basic_string_view<CharT>& key) const {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
std::pair<iterator, iterator> equal_range(const CharT* key) {
|
||||
return m_ht.equal_range(key, std::strlen(key));
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range(const CharT* key) const {
|
||||
return m_ht.equal_range(key, std::strlen(key));
|
||||
}
|
||||
|
||||
std::pair<iterator, iterator> equal_range(const std::basic_string<CharT>& key) {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range(const std::basic_string<CharT>& key) const {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/**
|
||||
* Return a range containing all the elements which have 'prefix' as prefix. The range is defined by a pair
|
||||
* of iterator, the first being the begin iterator and the second being the end iterator.
|
||||
*/
|
||||
std::pair<prefix_iterator, prefix_iterator> equal_prefix_range_ks(const CharT* prefix, size_type prefix_size) {
|
||||
return m_ht.equal_prefix_range(prefix, prefix_size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_prefix_range_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
std::pair<const_prefix_iterator, const_prefix_iterator> equal_prefix_range_ks(const CharT* prefix, size_type prefix_size) const {
|
||||
return m_ht.equal_prefix_range(prefix, prefix_size);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc equal_prefix_range_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
std::pair<prefix_iterator, prefix_iterator> equal_prefix_range(const std::basic_string_view<CharT>& prefix) {
|
||||
return m_ht.equal_prefix_range(prefix.data(), prefix.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_prefix_range_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
std::pair<const_prefix_iterator, const_prefix_iterator> equal_prefix_range(const std::basic_string_view<CharT>& prefix) const {
|
||||
return m_ht.equal_prefix_range(prefix.data(), prefix.size());
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc equal_prefix_range_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
std::pair<prefix_iterator, prefix_iterator> equal_prefix_range(const CharT* prefix) {
|
||||
return m_ht.equal_prefix_range(prefix, std::strlen(prefix));
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_prefix_range_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
std::pair<const_prefix_iterator, const_prefix_iterator> equal_prefix_range(const CharT* prefix) const {
|
||||
return m_ht.equal_prefix_range(prefix, std::strlen(prefix));
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_prefix_range_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
std::pair<prefix_iterator, prefix_iterator> equal_prefix_range(const std::basic_string<CharT>& prefix) {
|
||||
return m_ht.equal_prefix_range(prefix.data(), prefix.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_prefix_range_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
std::pair<const_prefix_iterator, const_prefix_iterator> equal_prefix_range(const std::basic_string<CharT>& prefix) const {
|
||||
return m_ht.equal_prefix_range(prefix.data(), prefix.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Return the element in the trie which is the longest prefix of `key`. If no
|
||||
* element in the trie is a prefix of `key`, the end iterator is returned.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* tsl::htrie_map<char, int> map = {{"/foo", 1}, {"/foo/bar", 1}};
|
||||
*
|
||||
* map.longest_prefix("/foo"); // returns {"/foo", 1}
|
||||
* map.longest_prefix("/foo/baz"); // returns {"/foo", 1}
|
||||
* map.longest_prefix("/foo/bar/baz"); // returns {"/foo/bar", 1}
|
||||
* map.longest_prefix("/foo/bar/"); // returns {"/foo/bar", 1}
|
||||
* map.longest_prefix("/bar"); // returns end()
|
||||
* map.longest_prefix(""); // returns end()
|
||||
*/
|
||||
iterator longest_prefix_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.longest_prefix(key, key_size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc longest_prefix_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
const_iterator longest_prefix_ks(const CharT* key, size_type key_size) const {
|
||||
return m_ht.longest_prefix(key, key_size);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc longest_prefix_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
iterator longest_prefix(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.longest_prefix(key.data(), key.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc longest_prefix_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
const_iterator longest_prefix(const std::basic_string_view<CharT>& key) const {
|
||||
return m_ht.longest_prefix(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc longest_prefix_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
iterator longest_prefix(const CharT* key) {
|
||||
return m_ht.longest_prefix(key, std::strlen(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc longest_prefix_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
const_iterator longest_prefix(const CharT* key) const {
|
||||
return m_ht.longest_prefix(key, std::strlen(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc longest_prefix_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
iterator longest_prefix(const std::basic_string<CharT>& key) {
|
||||
return m_ht.longest_prefix(key.data(), key.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc longest_prefix_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
const_iterator longest_prefix(const std::basic_string<CharT>& key) const {
|
||||
return m_ht.longest_prefix(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Hash policy
|
||||
*/
|
||||
float max_load_factor() const { return m_ht.max_load_factor(); }
|
||||
void max_load_factor(float ml) { m_ht.max_load_factor(ml); }
|
||||
|
||||
|
||||
/*
|
||||
* Burst policy
|
||||
*/
|
||||
size_type burst_threshold() const { return m_ht.burst_threshold(); }
|
||||
void burst_threshold(size_type threshold) { m_ht.burst_threshold(threshold); }
|
||||
|
||||
|
||||
/*
|
||||
* Observers
|
||||
*/
|
||||
hasher hash_function() const { return m_ht.hash_function(); }
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Other
|
||||
*/
|
||||
|
||||
/**
|
||||
* Serialize the map through the `serializer` parameter.
|
||||
*
|
||||
* The `serializer` parameter must be a function object that supports the following calls:
|
||||
* - `void operator()(const U& value);` where the types `std::uint64_t`, `float` and `T` must be supported for U.
|
||||
* - `void operator()(const CharT* value, std::size_t value_size);`
|
||||
*
|
||||
* The implementation leaves binary compatibility (endianness, IEEE 754 for floats, ...) of the types it serializes
|
||||
* in the hands of the `Serializer` function object if compatibility is required.
|
||||
*/
|
||||
template<class Serializer>
|
||||
void serialize(Serializer& serializer) const {
|
||||
m_ht.serialize(serializer);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deserialize a previously serialized map through the `deserializer` parameter.
|
||||
*
|
||||
* The `deserializer` parameter must be a function object that supports the following calls:
|
||||
* - `template<typename U> U operator()();` where the types `std::uint64_t`, `float` and `T` must be supported for U.
|
||||
* - `void operator()(CharT* value_out, std::size_t value_size);`
|
||||
*
|
||||
* If the deserialized hash map part of the hat-trie is hash compatible with the serialized map, the deserialization process
|
||||
* can be sped up by setting `hash_compatible` to true. To be hash compatible, the Hash (take care of the 32-bits vs 64 bits),
|
||||
* and KeySizeT must behave the same than the ones used in the serialized map. Otherwise the behaviour is undefined
|
||||
* with `hash_compatible` sets to true.
|
||||
*
|
||||
* The behaviour is undefined if the type `CharT` and `T` of the `htrie_map` are not the same as the
|
||||
* types used during serialization.
|
||||
*
|
||||
* The implementation leaves binary compatibility (endianness, IEEE 754 for floats, size of int, ...) of the types it
|
||||
* deserializes in the hands of the `Deserializer` function object if compatibility is required.
|
||||
*/
|
||||
template<class Deserializer>
|
||||
static htrie_map deserialize(Deserializer& deserializer, bool hash_compatible = false) {
|
||||
htrie_map map;
|
||||
map.m_ht.deserialize(deserializer, hash_compatible);
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
friend bool operator==(const htrie_map& lhs, const htrie_map& rhs) {
|
||||
if(lhs.size() != rhs.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string key_buffer;
|
||||
for(auto it = lhs.cbegin(); it != lhs.cend(); ++it) {
|
||||
it.key(key_buffer);
|
||||
|
||||
const auto it_element_rhs = rhs.find(key_buffer);
|
||||
if(it_element_rhs == rhs.cend() || it.value() != it_element_rhs.value()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
friend bool operator!=(const htrie_map& lhs, const htrie_map& rhs) {
|
||||
return !operator==(lhs, rhs);
|
||||
}
|
||||
|
||||
friend void swap(htrie_map& lhs, htrie_map& rhs) {
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
template<class U, class V>
|
||||
void insert_pair(const std::pair<U, V>& value) {
|
||||
insert(value.first, value.second);
|
||||
}
|
||||
|
||||
template<class U, class V>
|
||||
void insert_pair(std::pair<U, V>&& value) {
|
||||
insert(value.first, std::move(value.second));
|
||||
}
|
||||
|
||||
private:
|
||||
ht m_ht;
|
||||
};
|
||||
|
||||
} // end namespace tsl
|
||||
|
||||
#endif
|
||||
586
third_party/hat-trie/tsl/htrie_set.h
vendored
586
third_party/hat-trie/tsl/htrie_set.h
vendored
@@ -1,586 +0,0 @@
|
||||
/**
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2017 Thibaut Goetghebuer-Planchon <tessil@gmx.com>
|
||||
*
|
||||
* 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 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.
|
||||
*/
|
||||
#ifndef TSL_HTRIE_SET_H
|
||||
#define TSL_HTRIE_SET_H
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <initializer_list>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include "htrie_hash.h"
|
||||
|
||||
namespace tsl {
|
||||
|
||||
/**
|
||||
* Implementation of a hat-trie set.
|
||||
*
|
||||
* The size of a key string is limited to std::numeric_limits<KeySizeT>::max() - 1.
|
||||
* That is 65 535 characters by default, but can be raised with the KeySizeT template parameter.
|
||||
* See max_key_size() for an easy access to this limit.
|
||||
*
|
||||
* Iterators invalidation:
|
||||
* - clear, operator=: always invalidate the iterators.
|
||||
* - insert: always invalidate the iterators.
|
||||
* - erase: always invalidate the iterators.
|
||||
*/
|
||||
template<class CharT,
|
||||
class Hash = tsl::ah::str_hash<CharT>,
|
||||
class KeySizeT = std::uint16_t>
|
||||
class htrie_set {
|
||||
private:
|
||||
template<typename U>
|
||||
using is_iterator = tsl::detail_array_hash::is_iterator<U>;
|
||||
|
||||
using ht = tsl::detail_htrie_hash::htrie_hash<CharT, void, Hash, KeySizeT>;
|
||||
|
||||
public:
|
||||
using char_type = typename ht::char_type;
|
||||
using key_size_type = typename ht::key_size_type;
|
||||
using size_type = typename ht::size_type;
|
||||
using hasher = typename ht::hasher;
|
||||
using iterator = typename ht::iterator;
|
||||
using const_iterator = typename ht::const_iterator;
|
||||
using prefix_iterator = typename ht::prefix_iterator;
|
||||
using const_prefix_iterator = typename ht::const_prefix_iterator;
|
||||
|
||||
public:
|
||||
explicit htrie_set(const Hash& hash = Hash()): m_ht(hash, ht::HASH_NODE_DEFAULT_MAX_LOAD_FACTOR,
|
||||
ht::DEFAULT_BURST_THRESHOLD)
|
||||
{
|
||||
}
|
||||
|
||||
explicit htrie_set(size_type burst_threshold,
|
||||
const Hash& hash = Hash()): m_ht(hash, ht::HASH_NODE_DEFAULT_MAX_LOAD_FACTOR,
|
||||
burst_threshold)
|
||||
{
|
||||
}
|
||||
|
||||
template<class InputIt, typename std::enable_if<is_iterator<InputIt>::value>::type* = nullptr>
|
||||
htrie_set(InputIt first, InputIt last,
|
||||
const Hash& hash = Hash()): htrie_set(hash)
|
||||
{
|
||||
insert(first, last);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
htrie_set(std::initializer_list<std::basic_string_view<CharT>> init,
|
||||
const Hash& hash = Hash()): htrie_set(hash)
|
||||
{
|
||||
insert(init);
|
||||
}
|
||||
#else
|
||||
htrie_set(std::initializer_list<const CharT*> init,
|
||||
const Hash& hash = Hash()): htrie_set(hash)
|
||||
{
|
||||
insert(init);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
htrie_set& operator=(std::initializer_list<std::basic_string_view<CharT>> ilist) {
|
||||
clear();
|
||||
insert(ilist);
|
||||
|
||||
return *this;
|
||||
}
|
||||
#else
|
||||
htrie_set& operator=(std::initializer_list<const CharT*> ilist) {
|
||||
clear();
|
||||
insert(ilist);
|
||||
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Iterators
|
||||
*/
|
||||
iterator begin() noexcept { return m_ht.begin(); }
|
||||
const_iterator begin() const noexcept { return m_ht.begin(); }
|
||||
const_iterator cbegin() const noexcept { return m_ht.cbegin(); }
|
||||
|
||||
iterator end() noexcept { return m_ht.end(); }
|
||||
const_iterator end() const noexcept { return m_ht.end(); }
|
||||
const_iterator cend() const noexcept { return m_ht.cend(); }
|
||||
|
||||
|
||||
/*
|
||||
* Capacity
|
||||
*/
|
||||
bool empty() const noexcept { return m_ht.empty(); }
|
||||
size_type size() const noexcept { return m_ht.size(); }
|
||||
size_type max_size() const noexcept { return m_ht.max_size(); }
|
||||
size_type max_key_size() const noexcept { return m_ht.max_key_size(); }
|
||||
|
||||
/**
|
||||
* Call shrink_to_fit() on each hash node of the hat-trie to reduce its size.
|
||||
*/
|
||||
void shrink_to_fit() { m_ht.shrink_to_fit(); }
|
||||
|
||||
|
||||
/*
|
||||
* Modifiers
|
||||
*/
|
||||
void clear() noexcept { m_ht.clear(); }
|
||||
|
||||
|
||||
|
||||
std::pair<iterator, bool> insert_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.insert(key, key_size);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
std::pair<iterator, bool> insert(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.insert(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
std::pair<iterator, bool> insert(const CharT* key) {
|
||||
return m_ht.insert(key, std::strlen(key));
|
||||
}
|
||||
|
||||
std::pair<iterator, bool> insert(const std::basic_string<CharT>& key) {
|
||||
return m_ht.insert(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
template<class InputIt, typename std::enable_if<is_iterator<InputIt>::value>::type* = nullptr>
|
||||
void insert(InputIt first, InputIt last) {
|
||||
for(auto it = first; it != last; ++it) {
|
||||
insert(*it);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
void insert(std::initializer_list<std::basic_string_view<CharT>> ilist) {
|
||||
insert(ilist.begin(), ilist.end());
|
||||
}
|
||||
#else
|
||||
void insert(std::initializer_list<const CharT*> ilist) {
|
||||
insert(ilist.begin(), ilist.end());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
std::pair<iterator, bool> emplace_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.insert(key, key_size);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
std::pair<iterator, bool> emplace(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.insert(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
std::pair<iterator, bool> emplace(const CharT* key) {
|
||||
return m_ht.insert(key, std::strlen(key));
|
||||
}
|
||||
|
||||
std::pair<iterator, bool> emplace(const std::basic_string<CharT>& key) {
|
||||
return m_ht.insert(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
iterator erase(const_iterator pos) { return m_ht.erase(pos); }
|
||||
iterator erase(const_iterator first, const_iterator last) { return m_ht.erase(first, last); }
|
||||
|
||||
|
||||
|
||||
size_type erase_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.erase(key, key_size);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
size_type erase(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.erase(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
size_type erase(const CharT* key) {
|
||||
return m_ht.erase(key, std::strlen(key));
|
||||
}
|
||||
|
||||
size_type erase(const std::basic_string<CharT>& key) {
|
||||
return m_ht.erase(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Erase all the elements which have 'prefix' as prefix. Return the number of erase elements.
|
||||
*/
|
||||
size_type erase_prefix_ks(const CharT* prefix, size_type prefix_size) {
|
||||
return m_ht.erase_prefix(prefix, prefix_size);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc erase_prefix_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
size_type erase_prefix(const std::basic_string_view<CharT>& prefix) {
|
||||
return m_ht.erase_prefix(prefix.data(), prefix.size());
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc erase_prefix_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
size_type erase_prefix(const CharT* prefix) {
|
||||
return m_ht.erase_prefix(prefix, std::strlen(prefix));
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc erase_prefix_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
size_type erase_prefix(const std::basic_string<CharT>& prefix) {
|
||||
return m_ht.erase_prefix(prefix.data(), prefix.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
void swap(htrie_set& other) { other.m_ht.swap(m_ht); }
|
||||
|
||||
|
||||
/*
|
||||
* Lookup
|
||||
*/
|
||||
size_type count_ks(const CharT* key, size_type key_size) const { return m_ht.count(key, key_size); }
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
size_type count(const std::basic_string_view<CharT>& key) const { return m_ht.count(key.data(), key.size()); }
|
||||
#else
|
||||
size_type count(const CharT* key) const { return m_ht.count(key, std::strlen(key)); }
|
||||
size_type count(const std::basic_string<CharT>& key) const { return m_ht.count(key.data(), key.size()); }
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
iterator find_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.find(key, key_size);
|
||||
}
|
||||
|
||||
const_iterator find_ks(const CharT* key, size_type key_size) const {
|
||||
return m_ht.find(key, key_size);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
iterator find(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
|
||||
const_iterator find(const std::basic_string_view<CharT>& key) const {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
iterator find(const CharT* key) {
|
||||
return m_ht.find(key, std::strlen(key));
|
||||
}
|
||||
|
||||
const_iterator find(const CharT* key) const {
|
||||
return m_ht.find(key, std::strlen(key));
|
||||
}
|
||||
|
||||
iterator find(const std::basic_string<CharT>& key) {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
|
||||
const_iterator find(const std::basic_string<CharT>& key) const {
|
||||
return m_ht.find(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
std::pair<iterator, iterator> equal_range_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.equal_range(key, key_size);
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range_ks(const CharT* key, size_type key_size) const {
|
||||
return m_ht.equal_range(key, key_size);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
std::pair<iterator, iterator> equal_range(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range(const std::basic_string_view<CharT>& key) const {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
std::pair<iterator, iterator> equal_range(const CharT* key) {
|
||||
return m_ht.equal_range(key, std::strlen(key));
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range(const CharT* key) const {
|
||||
return m_ht.equal_range(key, std::strlen(key));
|
||||
}
|
||||
|
||||
std::pair<iterator, iterator> equal_range(const std::basic_string<CharT>& key) {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
|
||||
std::pair<const_iterator, const_iterator> equal_range(const std::basic_string<CharT>& key) const {
|
||||
return m_ht.equal_range(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Return a range containing all the elements which have 'prefix' as prefix. The range is defined by a pair
|
||||
* of iterator, the first being the begin iterator and the second being the end iterator.
|
||||
*/
|
||||
std::pair<prefix_iterator, prefix_iterator> equal_prefix_range_ks(const CharT* prefix, size_type prefix_size) {
|
||||
return m_ht.equal_prefix_range(prefix, prefix_size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_prefix_range_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
std::pair<const_prefix_iterator, const_prefix_iterator> equal_prefix_range_ks(const CharT* prefix, size_type prefix_size) const {
|
||||
return m_ht.equal_prefix_range(prefix, prefix_size);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc equal_prefix_range_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
std::pair<prefix_iterator, prefix_iterator> equal_prefix_range(const std::basic_string_view<CharT>& prefix) {
|
||||
return m_ht.equal_prefix_range(prefix.data(), prefix.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_prefix_range_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
std::pair<const_prefix_iterator, const_prefix_iterator> equal_prefix_range(const std::basic_string_view<CharT>& prefix) const {
|
||||
return m_ht.equal_prefix_range(prefix.data(), prefix.size());
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc equal_prefix_range_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
std::pair<prefix_iterator, prefix_iterator> equal_prefix_range(const CharT* prefix) {
|
||||
return m_ht.equal_prefix_range(prefix, std::strlen(prefix));
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_prefix_range_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
std::pair<const_prefix_iterator, const_prefix_iterator> equal_prefix_range(const CharT* prefix) const {
|
||||
return m_ht.equal_prefix_range(prefix, std::strlen(prefix));
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_prefix_range_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
std::pair<prefix_iterator, prefix_iterator> equal_prefix_range(const std::basic_string<CharT>& prefix) {
|
||||
return m_ht.equal_prefix_range(prefix.data(), prefix.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc equal_prefix_range_ks(const CharT* prefix, size_type prefix_size)
|
||||
*/
|
||||
std::pair<const_prefix_iterator, const_prefix_iterator> equal_prefix_range(const std::basic_string<CharT>& prefix) const {
|
||||
return m_ht.equal_prefix_range(prefix.data(), prefix.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Return the element in the trie which is the longest prefix of `key`. If no
|
||||
* element in the trie is a prefix of `key`, the end iterator is returned.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* tsl::htrie_set<char> set = {"/foo", "/foo/bar"};
|
||||
*
|
||||
* set.longest_prefix("/foo"); // returns "/foo"
|
||||
* set.longest_prefix("/foo/baz"); // returns "/foo"
|
||||
* set.longest_prefix("/foo/bar/baz"); // returns "/foo/bar"
|
||||
* set.longest_prefix("/foo/bar/"); // returns "/foo/bar"
|
||||
* set.longest_prefix("/bar"); // returns end()
|
||||
* set.longest_prefix(""); // returns end()
|
||||
*/
|
||||
iterator longest_prefix_ks(const CharT* key, size_type key_size) {
|
||||
return m_ht.longest_prefix(key, key_size);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc longest_prefix_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
const_iterator longest_prefix_ks(const CharT* key, size_type key_size) const {
|
||||
return m_ht.longest_prefix(key, key_size);
|
||||
}
|
||||
#ifdef TSL_HT_HAS_STRING_VIEW
|
||||
/**
|
||||
* @copydoc longest_prefix_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
iterator longest_prefix(const std::basic_string_view<CharT>& key) {
|
||||
return m_ht.longest_prefix(key.data(), key.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc longest_prefix_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
const_iterator longest_prefix(const std::basic_string_view<CharT>& key) const {
|
||||
return m_ht.longest_prefix(key.data(), key.size());
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* @copydoc longest_prefix_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
iterator longest_prefix(const CharT* key) {
|
||||
return m_ht.longest_prefix(key, std::strlen(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc longest_prefix_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
const_iterator longest_prefix(const CharT* key) const {
|
||||
return m_ht.longest_prefix(key, std::strlen(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc longest_prefix_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
iterator longest_prefix(const std::basic_string<CharT>& key) {
|
||||
return m_ht.longest_prefix(key.data(), key.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc longest_prefix_ks(const CharT* key, size_type key_size)
|
||||
*/
|
||||
const_iterator longest_prefix(const std::basic_string<CharT>& key) const {
|
||||
return m_ht.longest_prefix(key.data(), key.size());
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Hash policy
|
||||
*/
|
||||
float max_load_factor() const { return m_ht.max_load_factor(); }
|
||||
void max_load_factor(float ml) { m_ht.max_load_factor(ml); }
|
||||
|
||||
|
||||
/*
|
||||
* Burst policy
|
||||
*/
|
||||
size_type burst_threshold() const { return m_ht.burst_threshold(); }
|
||||
void burst_threshold(size_type threshold) { m_ht.burst_threshold(threshold); }
|
||||
|
||||
|
||||
/*
|
||||
* Observers
|
||||
*/
|
||||
hasher hash_function() const { return m_ht.hash_function(); }
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Other
|
||||
*/
|
||||
|
||||
/**
|
||||
* Serialize the set through the `serializer` parameter.
|
||||
*
|
||||
* The `serializer` parameter must be a function object that supports the following calls:
|
||||
* - `void operator()(const U& value);` where the types `std::uint64_t` and `float` must be supported for U.
|
||||
* - `void operator()(const CharT* value, std::size_t value_size);`
|
||||
*
|
||||
* The implementation leaves binary compatibility (endianness, IEEE 754 for floats, ...) of the types it serializes
|
||||
* in the hands of the `Serializer` function object if compatibility is required.
|
||||
*/
|
||||
template<class Serializer>
|
||||
void serialize(Serializer& serializer) const {
|
||||
m_ht.serialize(serializer);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deserialize a previously serialized set through the `deserializer` parameter.
|
||||
*
|
||||
* The `deserializer` parameter must be a function object that supports the following calls:
|
||||
* - `template<typename U> U operator()();` where the types `std::uint64_t` and `float` must be supported for U.
|
||||
* - `void operator()(CharT* value_out, std::size_t value_size);`
|
||||
*
|
||||
* If the deserialized hash set part of the hat-trie is hash compatible with the serialized set, the deserialization process
|
||||
* can be sped up by setting `hash_compatible` to true. To be hash compatible, the Hash (take care of the 32-bits vs 64 bits),
|
||||
* and KeySizeT must behave the same than the ones used in the serialized set. Otherwise the behaviour is undefined
|
||||
* with `hash_compatible` sets to true.
|
||||
*
|
||||
* The behaviour is undefined if the type `CharT` of the `htrie_set` is not the same as the
|
||||
* type used during serialization.
|
||||
*
|
||||
* The implementation leaves binary compatibility (endianness, IEEE 754 for floats, size of int, ...) of the types it
|
||||
* deserializes in the hands of the `Deserializer` function object if compatibility is required.
|
||||
*/
|
||||
template<class Deserializer>
|
||||
static htrie_set deserialize(Deserializer& deserializer, bool hash_compatible = false) {
|
||||
htrie_set set;
|
||||
set.m_ht.deserialize(deserializer, hash_compatible);
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
friend bool operator==(const htrie_set& lhs, const htrie_set& rhs) {
|
||||
if(lhs.size() != rhs.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string key_buffer;
|
||||
for(auto it = lhs.cbegin(); it != lhs.cend(); ++it) {
|
||||
it.key(key_buffer);
|
||||
|
||||
const auto it_element_rhs = rhs.find(key_buffer);
|
||||
if(it_element_rhs == rhs.cend()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
friend bool operator!=(const htrie_set& lhs, const htrie_set& rhs) {
|
||||
return !operator==(lhs, rhs);
|
||||
}
|
||||
|
||||
friend void swap(htrie_set& lhs, htrie_set& rhs) {
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
ht m_ht;
|
||||
};
|
||||
|
||||
} // end namespace tsl
|
||||
|
||||
#endif
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "filament",
|
||||
"version": "1.52.2",
|
||||
"version": "1.52.3",
|
||||
"description": "Real-time physically based rendering engine",
|
||||
"main": "filament.js",
|
||||
"module": "filament.js",
|
||||
|
||||
Reference in New Issue
Block a user