Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
c++ source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
6502-c++ 11.1.0
ARM GCC 10.2.0
ARM GCC 10.3.0
ARM GCC 10.4.0
ARM GCC 10.5.0
ARM GCC 11.1.0
ARM GCC 11.2.0
ARM GCC 11.3.0
ARM GCC 11.4.0
ARM GCC 12.1.0
ARM GCC 12.2.0
ARM GCC 12.3.0
ARM GCC 12.4.0
ARM GCC 12.5.0
ARM GCC 13.1.0
ARM GCC 13.2.0
ARM GCC 13.2.0 (unknown-eabi)
ARM GCC 13.3.0
ARM GCC 13.3.0 (unknown-eabi)
ARM GCC 13.4.0
ARM GCC 13.4.0 (unknown-eabi)
ARM GCC 14.1.0
ARM GCC 14.1.0 (unknown-eabi)
ARM GCC 14.2.0
ARM GCC 14.2.0 (unknown-eabi)
ARM GCC 14.3.0
ARM GCC 14.3.0 (unknown-eabi)
ARM GCC 15.1.0
ARM GCC 15.1.0 (unknown-eabi)
ARM GCC 15.2.0
ARM GCC 15.2.0 (unknown-eabi)
ARM GCC 4.5.4
ARM GCC 4.6.4
ARM GCC 5.4
ARM GCC 6.3.0
ARM GCC 6.4.0
ARM GCC 7.3.0
ARM GCC 7.5.0
ARM GCC 8.2.0
ARM GCC 8.5.0
ARM GCC 9.3.0
ARM GCC 9.4.0
ARM GCC 9.5.0
ARM GCC trunk
ARM gcc 10.2.1 (none)
ARM gcc 10.3.1 (2021.07 none)
ARM gcc 10.3.1 (2021.10 none)
ARM gcc 11.2.1 (none)
ARM gcc 5.4.1 (none)
ARM gcc 7.2.1 (none)
ARM gcc 8.2 (WinCE)
ARM gcc 8.3.1 (none)
ARM gcc 9.2.1 (none)
ARM msvc v19.0 (ex-WINE)
ARM msvc v19.10 (ex-WINE)
ARM msvc v19.14 (ex-WINE)
ARM64 Morello gcc 10.1 Alpha 2
ARM64 gcc 10.2
ARM64 gcc 10.3
ARM64 gcc 10.4
ARM64 gcc 10.5.0
ARM64 gcc 11.1
ARM64 gcc 11.2
ARM64 gcc 11.3
ARM64 gcc 11.4.0
ARM64 gcc 12.1
ARM64 gcc 12.2.0
ARM64 gcc 12.3.0
ARM64 gcc 12.4.0
ARM64 gcc 12.5.0
ARM64 gcc 13.1.0
ARM64 gcc 13.2.0
ARM64 gcc 13.3.0
ARM64 gcc 13.4.0
ARM64 gcc 14.1.0
ARM64 gcc 14.2.0
ARM64 gcc 14.3.0
ARM64 gcc 15.1.0
ARM64 gcc 15.2.0
ARM64 gcc 4.9.4
ARM64 gcc 5.4
ARM64 gcc 5.5.0
ARM64 gcc 6.3
ARM64 gcc 6.4
ARM64 gcc 7.3
ARM64 gcc 7.5
ARM64 gcc 8.2
ARM64 gcc 8.5
ARM64 gcc 9.3
ARM64 gcc 9.4
ARM64 gcc 9.5
ARM64 gcc trunk
ARM64 msvc v19.14 (ex-WINE)
AVR gcc 10.3.0
AVR gcc 11.1.0
AVR gcc 12.1.0
AVR gcc 12.2.0
AVR gcc 12.3.0
AVR gcc 12.4.0
AVR gcc 12.5.0
AVR gcc 13.1.0
AVR gcc 13.2.0
AVR gcc 13.3.0
AVR gcc 13.4.0
AVR gcc 14.1.0
AVR gcc 14.2.0
AVR gcc 14.3.0
AVR gcc 15.1.0
AVR gcc 15.2.0
AVR gcc 4.5.4
AVR gcc 4.6.4
AVR gcc 5.4.0
AVR gcc 9.2.0
AVR gcc 9.3.0
Arduino Mega (1.8.9)
Arduino Uno (1.8.9)
BPF clang (trunk)
BPF clang 13.0.0
BPF clang 14.0.0
BPF clang 15.0.0
BPF clang 16.0.0
BPF clang 17.0.1
BPF clang 18.1.0
BPF clang 19.1.0
BPF clang 20.1.0
BPF clang 21.1.0
EDG (experimental reflection)
EDG 6.5
EDG 6.5 (GNU mode gcc 13)
EDG 6.6
EDG 6.6 (GNU mode gcc 13)
EDG 6.7
EDG 6.7 (GNU mode gcc 14)
FRC 2019
FRC 2020
FRC 2023
HPPA gcc 14.2.0
HPPA gcc 14.3.0
HPPA gcc 15.1.0
HPPA gcc 15.2.0
KVX ACB 4.1.0 (GCC 7.5.0)
KVX ACB 4.1.0-cd1 (GCC 7.5.0)
KVX ACB 4.10.0 (GCC 10.3.1)
KVX ACB 4.11.1 (GCC 10.3.1)
KVX ACB 4.12.0 (GCC 11.3.0)
KVX ACB 4.2.0 (GCC 7.5.0)
KVX ACB 4.3.0 (GCC 7.5.0)
KVX ACB 4.4.0 (GCC 7.5.0)
KVX ACB 4.6.0 (GCC 9.4.1)
KVX ACB 4.8.0 (GCC 9.4.1)
KVX ACB 4.9.0 (GCC 9.4.1)
KVX ACB 5.0.0 (GCC 12.2.1)
KVX ACB 5.2.0 (GCC 13.2.1)
LoongArch64 clang (trunk)
LoongArch64 clang 17.0.1
LoongArch64 clang 18.1.0
LoongArch64 clang 19.1.0
LoongArch64 clang 20.1.0
LoongArch64 clang 21.1.0
M68K gcc 13.1.0
M68K gcc 13.2.0
M68K gcc 13.3.0
M68K gcc 13.4.0
M68K gcc 14.1.0
M68K gcc 14.2.0
M68K gcc 14.3.0
M68K gcc 15.1.0
M68K gcc 15.2.0
M68k clang (trunk)
MRISC32 gcc (trunk)
MSP430 gcc 4.5.3
MSP430 gcc 5.3.0
MSP430 gcc 6.2.1
MinGW clang 14.0.3
MinGW clang 14.0.6
MinGW clang 15.0.7
MinGW clang 16.0.0
MinGW clang 16.0.2
MinGW gcc 11.3.0
MinGW gcc 12.1.0
MinGW gcc 12.2.0
MinGW gcc 13.1.0
RISC-V (32-bits) gcc (trunk)
RISC-V (32-bits) gcc 10.2.0
RISC-V (32-bits) gcc 10.3.0
RISC-V (32-bits) gcc 11.2.0
RISC-V (32-bits) gcc 11.3.0
RISC-V (32-bits) gcc 11.4.0
RISC-V (32-bits) gcc 12.1.0
RISC-V (32-bits) gcc 12.2.0
RISC-V (32-bits) gcc 12.3.0
RISC-V (32-bits) gcc 12.4.0
RISC-V (32-bits) gcc 12.5.0
RISC-V (32-bits) gcc 13.1.0
RISC-V (32-bits) gcc 13.2.0
RISC-V (32-bits) gcc 13.3.0
RISC-V (32-bits) gcc 13.4.0
RISC-V (32-bits) gcc 14.1.0
RISC-V (32-bits) gcc 14.2.0
RISC-V (32-bits) gcc 14.3.0
RISC-V (32-bits) gcc 15.1.0
RISC-V (32-bits) gcc 15.2.0
RISC-V (32-bits) gcc 8.2.0
RISC-V (32-bits) gcc 8.5.0
RISC-V (32-bits) gcc 9.4.0
RISC-V (64-bits) gcc (trunk)
RISC-V (64-bits) gcc 10.2.0
RISC-V (64-bits) gcc 10.3.0
RISC-V (64-bits) gcc 11.2.0
RISC-V (64-bits) gcc 11.3.0
RISC-V (64-bits) gcc 11.4.0
RISC-V (64-bits) gcc 12.1.0
RISC-V (64-bits) gcc 12.2.0
RISC-V (64-bits) gcc 12.3.0
RISC-V (64-bits) gcc 12.4.0
RISC-V (64-bits) gcc 12.5.0
RISC-V (64-bits) gcc 13.1.0
RISC-V (64-bits) gcc 13.2.0
RISC-V (64-bits) gcc 13.3.0
RISC-V (64-bits) gcc 13.4.0
RISC-V (64-bits) gcc 14.1.0
RISC-V (64-bits) gcc 14.2.0
RISC-V (64-bits) gcc 14.3.0
RISC-V (64-bits) gcc 15.1.0
RISC-V (64-bits) gcc 15.2.0
RISC-V (64-bits) gcc 8.2.0
RISC-V (64-bits) gcc 8.5.0
RISC-V (64-bits) gcc 9.4.0
RISC-V rv32gc clang (trunk)
RISC-V rv32gc clang 10.0.0
RISC-V rv32gc clang 10.0.1
RISC-V rv32gc clang 11.0.0
RISC-V rv32gc clang 11.0.1
RISC-V rv32gc clang 12.0.0
RISC-V rv32gc clang 12.0.1
RISC-V rv32gc clang 13.0.0
RISC-V rv32gc clang 13.0.1
RISC-V rv32gc clang 14.0.0
RISC-V rv32gc clang 15.0.0
RISC-V rv32gc clang 16.0.0
RISC-V rv32gc clang 17.0.1
RISC-V rv32gc clang 18.1.0
RISC-V rv32gc clang 19.1.0
RISC-V rv32gc clang 20.1.0
RISC-V rv32gc clang 21.1.0
RISC-V rv32gc clang 9.0.0
RISC-V rv32gc clang 9.0.1
RISC-V rv64gc clang (trunk)
RISC-V rv64gc clang 10.0.0
RISC-V rv64gc clang 10.0.1
RISC-V rv64gc clang 11.0.0
RISC-V rv64gc clang 11.0.1
RISC-V rv64gc clang 12.0.0
RISC-V rv64gc clang 12.0.1
RISC-V rv64gc clang 13.0.0
RISC-V rv64gc clang 13.0.1
RISC-V rv64gc clang 14.0.0
RISC-V rv64gc clang 15.0.0
RISC-V rv64gc clang 16.0.0
RISC-V rv64gc clang 17.0.1
RISC-V rv64gc clang 18.1.0
RISC-V rv64gc clang 19.1.0
RISC-V rv64gc clang 20.1.0
RISC-V rv64gc clang 21.1.0
RISC-V rv64gc clang 9.0.0
RISC-V rv64gc clang 9.0.1
Raspbian Buster
Raspbian Stretch
SPARC LEON gcc 12.2.0
SPARC LEON gcc 12.3.0
SPARC LEON gcc 12.4.0
SPARC LEON gcc 12.5.0
SPARC LEON gcc 13.1.0
SPARC LEON gcc 13.2.0
SPARC LEON gcc 13.3.0
SPARC LEON gcc 13.4.0
SPARC LEON gcc 14.1.0
SPARC LEON gcc 14.2.0
SPARC LEON gcc 14.3.0
SPARC LEON gcc 15.1.0
SPARC LEON gcc 15.2.0
SPARC gcc 12.2.0
SPARC gcc 12.3.0
SPARC gcc 12.4.0
SPARC gcc 12.5.0
SPARC gcc 13.1.0
SPARC gcc 13.2.0
SPARC gcc 13.3.0
SPARC gcc 13.4.0
SPARC gcc 14.1.0
SPARC gcc 14.2.0
SPARC gcc 14.3.0
SPARC gcc 15.1.0
SPARC gcc 15.2.0
SPARC64 gcc 12.2.0
SPARC64 gcc 12.3.0
SPARC64 gcc 12.4.0
SPARC64 gcc 12.5.0
SPARC64 gcc 13.1.0
SPARC64 gcc 13.2.0
SPARC64 gcc 13.3.0
SPARC64 gcc 13.4.0
SPARC64 gcc 14.1.0
SPARC64 gcc 14.2.0
SPARC64 gcc 14.3.0
SPARC64 gcc 15.1.0
SPARC64 gcc 15.2.0
TI C6x gcc 12.2.0
TI C6x gcc 12.3.0
TI C6x gcc 12.4.0
TI C6x gcc 12.5.0
TI C6x gcc 13.1.0
TI C6x gcc 13.2.0
TI C6x gcc 13.3.0
TI C6x gcc 13.4.0
TI C6x gcc 14.1.0
TI C6x gcc 14.2.0
TI C6x gcc 14.3.0
TI C6x gcc 15.1.0
TI C6x gcc 15.2.0
TI CL430 21.6.1
Tricore gcc 11.3.0 (EEESlab)
VAX gcc NetBSDELF 10.4.0
VAX gcc NetBSDELF 10.5.0 (Nov 15 03:50:22 2023)
VAX gcc NetBSDELF 12.4.0 (Apr 16 05:27 2025)
WebAssembly clang (trunk)
Xtensa ESP32 gcc 11.2.0 (2022r1)
Xtensa ESP32 gcc 12.2.0 (20230208)
Xtensa ESP32 gcc 14.2.0 (20241119)
Xtensa ESP32 gcc 8.2.0 (2019r2)
Xtensa ESP32 gcc 8.2.0 (2020r1)
Xtensa ESP32 gcc 8.2.0 (2020r2)
Xtensa ESP32 gcc 8.4.0 (2020r3)
Xtensa ESP32 gcc 8.4.0 (2021r1)
Xtensa ESP32 gcc 8.4.0 (2021r2)
Xtensa ESP32-S2 gcc 11.2.0 (2022r1)
Xtensa ESP32-S2 gcc 12.2.0 (20230208)
Xtensa ESP32-S2 gcc 14.2.0 (20241119)
Xtensa ESP32-S2 gcc 8.2.0 (2019r2)
Xtensa ESP32-S2 gcc 8.2.0 (2020r1)
Xtensa ESP32-S2 gcc 8.2.0 (2020r2)
Xtensa ESP32-S2 gcc 8.4.0 (2020r3)
Xtensa ESP32-S2 gcc 8.4.0 (2021r1)
Xtensa ESP32-S2 gcc 8.4.0 (2021r2)
Xtensa ESP32-S3 gcc 11.2.0 (2022r1)
Xtensa ESP32-S3 gcc 12.2.0 (20230208)
Xtensa ESP32-S3 gcc 14.2.0 (20241119)
Xtensa ESP32-S3 gcc 8.4.0 (2020r3)
Xtensa ESP32-S3 gcc 8.4.0 (2021r1)
Xtensa ESP32-S3 gcc 8.4.0 (2021r2)
arm64 msvc v19.20 VS16.0
arm64 msvc v19.21 VS16.1
arm64 msvc v19.22 VS16.2
arm64 msvc v19.23 VS16.3
arm64 msvc v19.24 VS16.4
arm64 msvc v19.25 VS16.5
arm64 msvc v19.27 VS16.7
arm64 msvc v19.28 VS16.8
arm64 msvc v19.28 VS16.9
arm64 msvc v19.29 VS16.10
arm64 msvc v19.29 VS16.11
arm64 msvc v19.30 VS17.0
arm64 msvc v19.31 VS17.1
arm64 msvc v19.32 VS17.2
arm64 msvc v19.33 VS17.3
arm64 msvc v19.34 VS17.4
arm64 msvc v19.35 VS17.5
arm64 msvc v19.36 VS17.6
arm64 msvc v19.37 VS17.7
arm64 msvc v19.38 VS17.8
arm64 msvc v19.39 VS17.9
arm64 msvc v19.40 VS17.10
arm64 msvc v19.41 VS17.11
arm64 msvc v19.42 VS17.12
arm64 msvc v19.43 VS17.13
arm64 msvc v19.latest
armv7-a clang (trunk)
armv7-a clang 10.0.0
armv7-a clang 10.0.1
armv7-a clang 11.0.0
armv7-a clang 11.0.1
armv7-a clang 12.0.0
armv7-a clang 12.0.1
armv7-a clang 13.0.0
armv7-a clang 13.0.1
armv7-a clang 14.0.0
armv7-a clang 15.0.0
armv7-a clang 16.0.0
armv7-a clang 17.0.1
armv7-a clang 18.1.0
armv7-a clang 19.1.0
armv7-a clang 20.1.0
armv7-a clang 21.1.0
armv7-a clang 9.0.0
armv7-a clang 9.0.1
armv8-a clang (all architectural features, trunk)
armv8-a clang (trunk)
armv8-a clang 10.0.0
armv8-a clang 10.0.1
armv8-a clang 11.0.0
armv8-a clang 11.0.1
armv8-a clang 12.0.0
armv8-a clang 13.0.0
armv8-a clang 14.0.0
armv8-a clang 15.0.0
armv8-a clang 16.0.0
armv8-a clang 17.0.1
armv8-a clang 18.1.0
armv8-a clang 19.1.0
armv8-a clang 20.1.0
armv8-a clang 21.1.0
armv8-a clang 9.0.0
armv8-a clang 9.0.1
clad trunk (clang 21.1.0)
clad v1.10 (clang 20.1.0)
clad v1.8 (clang 18.1.0)
clad v1.9 (clang 19.1.0)
clad v2.00 (clang 20.1.0)
clang-cl 18.1.0
ellcc 0.1.33
ellcc 0.1.34
ellcc 2017-07-16
ez80-clang 15.0.0
ez80-clang 15.0.7
hexagon-clang 16.0.5
llvm-mos atari2600-3e
llvm-mos atari2600-4k
llvm-mos atari2600-common
llvm-mos atari5200-supercart
llvm-mos atari8-cart-megacart
llvm-mos atari8-cart-std
llvm-mos atari8-cart-xegs
llvm-mos atari8-common
llvm-mos atari8-dos
llvm-mos c128
llvm-mos c64
llvm-mos commodore
llvm-mos cpm65
llvm-mos cx16
llvm-mos dodo
llvm-mos eater
llvm-mos mega65
llvm-mos nes
llvm-mos nes-action53
llvm-mos nes-cnrom
llvm-mos nes-gtrom
llvm-mos nes-mmc1
llvm-mos nes-mmc3
llvm-mos nes-nrom
llvm-mos nes-unrom
llvm-mos nes-unrom-512
llvm-mos osi-c1p
llvm-mos pce
llvm-mos pce-cd
llvm-mos pce-common
llvm-mos pet
llvm-mos rp6502
llvm-mos rpc8e
llvm-mos supervision
llvm-mos vic20
loongarch64 gcc 12.2.0
loongarch64 gcc 12.3.0
loongarch64 gcc 12.4.0
loongarch64 gcc 12.5.0
loongarch64 gcc 13.1.0
loongarch64 gcc 13.2.0
loongarch64 gcc 13.3.0
loongarch64 gcc 13.4.0
loongarch64 gcc 14.1.0
loongarch64 gcc 14.2.0
loongarch64 gcc 14.3.0
loongarch64 gcc 15.1.0
loongarch64 gcc 15.2.0
mips clang 13.0.0
mips clang 14.0.0
mips clang 15.0.0
mips clang 16.0.0
mips clang 17.0.1
mips clang 18.1.0
mips clang 19.1.0
mips clang 20.1.0
mips clang 21.1.0
mips gcc 11.2.0
mips gcc 12.1.0
mips gcc 12.2.0
mips gcc 12.3.0
mips gcc 12.4.0
mips gcc 12.5.0
mips gcc 13.1.0
mips gcc 13.2.0
mips gcc 13.3.0
mips gcc 13.4.0
mips gcc 14.1.0
mips gcc 14.2.0
mips gcc 14.3.0
mips gcc 15.1.0
mips gcc 15.2.0
mips gcc 4.9.4
mips gcc 5.4
mips gcc 5.5.0
mips gcc 9.3.0 (codescape)
mips gcc 9.5.0
mips64 (el) gcc 12.1.0
mips64 (el) gcc 12.2.0
mips64 (el) gcc 12.3.0
mips64 (el) gcc 12.4.0
mips64 (el) gcc 12.5.0
mips64 (el) gcc 13.1.0
mips64 (el) gcc 13.2.0
mips64 (el) gcc 13.3.0
mips64 (el) gcc 13.4.0
mips64 (el) gcc 14.1.0
mips64 (el) gcc 14.2.0
mips64 (el) gcc 14.3.0
mips64 (el) gcc 15.1.0
mips64 (el) gcc 15.2.0
mips64 (el) gcc 4.9.4
mips64 (el) gcc 5.4.0
mips64 (el) gcc 5.5.0
mips64 (el) gcc 9.5.0
mips64 clang 13.0.0
mips64 clang 14.0.0
mips64 clang 15.0.0
mips64 clang 16.0.0
mips64 clang 17.0.1
mips64 clang 18.1.0
mips64 clang 19.1.0
mips64 clang 20.1.0
mips64 clang 21.1.0
mips64 gcc 11.2.0
mips64 gcc 12.1.0
mips64 gcc 12.2.0
mips64 gcc 12.3.0
mips64 gcc 12.4.0
mips64 gcc 12.5.0
mips64 gcc 13.1.0
mips64 gcc 13.2.0
mips64 gcc 13.3.0
mips64 gcc 13.4.0
mips64 gcc 14.1.0
mips64 gcc 14.2.0
mips64 gcc 14.3.0
mips64 gcc 15.1.0
mips64 gcc 15.2.0
mips64 gcc 4.9.4
mips64 gcc 5.4.0
mips64 gcc 5.5.0
mips64 gcc 9.5.0
mips64el clang 13.0.0
mips64el clang 14.0.0
mips64el clang 15.0.0
mips64el clang 16.0.0
mips64el clang 17.0.1
mips64el clang 18.1.0
mips64el clang 19.1.0
mips64el clang 20.1.0
mips64el clang 21.1.0
mipsel clang 13.0.0
mipsel clang 14.0.0
mipsel clang 15.0.0
mipsel clang 16.0.0
mipsel clang 17.0.1
mipsel clang 18.1.0
mipsel clang 19.1.0
mipsel clang 20.1.0
mipsel clang 21.1.0
mipsel gcc 12.1.0
mipsel gcc 12.2.0
mipsel gcc 12.3.0
mipsel gcc 12.4.0
mipsel gcc 12.5.0
mipsel gcc 13.1.0
mipsel gcc 13.2.0
mipsel gcc 13.3.0
mipsel gcc 13.4.0
mipsel gcc 14.1.0
mipsel gcc 14.2.0
mipsel gcc 14.3.0
mipsel gcc 15.1.0
mipsel gcc 15.2.0
mipsel gcc 4.9.4
mipsel gcc 5.4.0
mipsel gcc 5.5.0
mipsel gcc 9.5.0
nanoMIPS gcc 6.3.0 (mtk)
power gcc 11.2.0
power gcc 12.1.0
power gcc 12.2.0
power gcc 12.3.0
power gcc 12.4.0
power gcc 12.5.0
power gcc 13.1.0
power gcc 13.2.0
power gcc 13.3.0
power gcc 13.4.0
power gcc 14.1.0
power gcc 14.2.0
power gcc 14.3.0
power gcc 15.1.0
power gcc 15.2.0
power gcc 4.8.5
power64 AT12.0 (gcc8)
power64 AT13.0 (gcc9)
power64 gcc 11.2.0
power64 gcc 12.1.0
power64 gcc 12.2.0
power64 gcc 12.3.0
power64 gcc 12.4.0
power64 gcc 12.5.0
power64 gcc 13.1.0
power64 gcc 13.2.0
power64 gcc 13.3.0
power64 gcc 13.4.0
power64 gcc 14.1.0
power64 gcc 14.2.0
power64 gcc 14.3.0
power64 gcc 15.1.0
power64 gcc 15.2.0
power64 gcc trunk
power64le AT12.0 (gcc8)
power64le AT13.0 (gcc9)
power64le clang (trunk)
power64le gcc 11.2.0
power64le gcc 12.1.0
power64le gcc 12.2.0
power64le gcc 12.3.0
power64le gcc 12.4.0
power64le gcc 12.5.0
power64le gcc 13.1.0
power64le gcc 13.2.0
power64le gcc 13.3.0
power64le gcc 13.4.0
power64le gcc 14.1.0
power64le gcc 14.2.0
power64le gcc 14.3.0
power64le gcc 15.1.0
power64le gcc 15.2.0
power64le gcc 6.3.0
power64le gcc trunk
powerpc64 clang (trunk)
qnx 8.0.0
s390x gcc 11.2.0
s390x gcc 12.1.0
s390x gcc 12.2.0
s390x gcc 12.3.0
s390x gcc 12.4.0
s390x gcc 12.5.0
s390x gcc 13.1.0
s390x gcc 13.2.0
s390x gcc 13.3.0
s390x gcc 13.4.0
s390x gcc 14.1.0
s390x gcc 14.2.0
s390x gcc 14.3.0
s390x gcc 15.1.0
s390x gcc 15.2.0
sh gcc 12.2.0
sh gcc 12.3.0
sh gcc 12.4.0
sh gcc 12.5.0
sh gcc 13.1.0
sh gcc 13.2.0
sh gcc 13.3.0
sh gcc 13.4.0
sh gcc 14.1.0
sh gcc 14.2.0
sh gcc 14.3.0
sh gcc 15.1.0
sh gcc 15.2.0
sh gcc 4.9.4
sh gcc 9.5.0
vast (trunk)
x64 msvc v19.0 (ex-WINE)
x64 msvc v19.10 (ex-WINE)
x64 msvc v19.14 (ex-WINE)
x64 msvc v19.20 VS16.0
x64 msvc v19.21 VS16.1
x64 msvc v19.22 VS16.2
x64 msvc v19.23 VS16.3
x64 msvc v19.24 VS16.4
x64 msvc v19.25 VS16.5
x64 msvc v19.27 VS16.7
x64 msvc v19.28 VS16.8
x64 msvc v19.28 VS16.9
x64 msvc v19.29 VS16.10
x64 msvc v19.29 VS16.11
x64 msvc v19.30 VS17.0
x64 msvc v19.31 VS17.1
x64 msvc v19.32 VS17.2
x64 msvc v19.33 VS17.3
x64 msvc v19.34 VS17.4
x64 msvc v19.35 VS17.5
x64 msvc v19.36 VS17.6
x64 msvc v19.37 VS17.7
x64 msvc v19.38 VS17.8
x64 msvc v19.39 VS17.9
x64 msvc v19.40 VS17.10
x64 msvc v19.41 VS17.11
x64 msvc v19.42 VS17.12
x64 msvc v19.43 VS17.13
x64 msvc v19.latest
x86 djgpp 4.9.4
x86 djgpp 5.5.0
x86 djgpp 6.4.0
x86 djgpp 7.2.0
x86 msvc v19.0 (ex-WINE)
x86 msvc v19.10 (ex-WINE)
x86 msvc v19.14 (ex-WINE)
x86 msvc v19.20 VS16.0
x86 msvc v19.21 VS16.1
x86 msvc v19.22 VS16.2
x86 msvc v19.23 VS16.3
x86 msvc v19.24 VS16.4
x86 msvc v19.25 VS16.5
x86 msvc v19.27 VS16.7
x86 msvc v19.28 VS16.8
x86 msvc v19.28 VS16.9
x86 msvc v19.29 VS16.10
x86 msvc v19.29 VS16.11
x86 msvc v19.30 VS17.0
x86 msvc v19.31 VS17.1
x86 msvc v19.32 VS17.2
x86 msvc v19.33 VS17.3
x86 msvc v19.34 VS17.4
x86 msvc v19.35 VS17.5
x86 msvc v19.36 VS17.6
x86 msvc v19.37 VS17.7
x86 msvc v19.38 VS17.8
x86 msvc v19.39 VS17.9
x86 msvc v19.40 VS17.10
x86 msvc v19.41 VS17.11
x86 msvc v19.42 VS17.12
x86 msvc v19.43 VS17.13
x86 msvc v19.latest
x86 nvc++ 22.11
x86 nvc++ 22.7
x86 nvc++ 22.9
x86 nvc++ 23.1
x86 nvc++ 23.11
x86 nvc++ 23.3
x86 nvc++ 23.5
x86 nvc++ 23.7
x86 nvc++ 23.9
x86 nvc++ 24.1
x86 nvc++ 24.11
x86 nvc++ 24.3
x86 nvc++ 24.5
x86 nvc++ 24.7
x86 nvc++ 24.9
x86 nvc++ 25.1
x86 nvc++ 25.3
x86 nvc++ 25.5
x86 nvc++ 25.7
x86-64 Zapcc 190308
x86-64 clang (-fimplicit-constexpr)
x86-64 clang (Chris Bazley N3089)
x86-64 clang (EricWF contracts)
x86-64 clang (amd-staging)
x86-64 clang (assertions trunk)
x86-64 clang (clangir)
x86-64 clang (experimental -Wlifetime)
x86-64 clang (experimental P1061)
x86-64 clang (experimental P1144)
x86-64 clang (experimental P1221)
x86-64 clang (experimental P2998)
x86-64 clang (experimental P3068)
x86-64 clang (experimental P3309)
x86-64 clang (experimental P3367)
x86-64 clang (experimental P3372)
x86-64 clang (experimental P3385)
x86-64 clang (experimental P3776)
x86-64 clang (experimental metaprogramming - P2632)
x86-64 clang (old concepts branch)
x86-64 clang (p1974)
x86-64 clang (pattern matching - P2688)
x86-64 clang (reflection - C++26)
x86-64 clang (reflection - TS)
x86-64 clang (resugar)
x86-64 clang (string interpolation - P3412)
x86-64 clang (thephd.dev)
x86-64 clang (trunk)
x86-64 clang (variadic friends - P2893)
x86-64 clang (widberg)
x86-64 clang 10.0.0
x86-64 clang 10.0.0 (assertions)
x86-64 clang 10.0.1
x86-64 clang 11.0.0
x86-64 clang 11.0.0 (assertions)
x86-64 clang 11.0.1
x86-64 clang 12.0.0
x86-64 clang 12.0.0 (assertions)
x86-64 clang 12.0.1
x86-64 clang 13.0.0
x86-64 clang 13.0.0 (assertions)
x86-64 clang 13.0.1
x86-64 clang 14.0.0
x86-64 clang 14.0.0 (assertions)
x86-64 clang 15.0.0
x86-64 clang 15.0.0 (assertions)
x86-64 clang 16.0.0
x86-64 clang 16.0.0 (assertions)
x86-64 clang 17.0.1
x86-64 clang 17.0.1 (assertions)
x86-64 clang 18.1.0
x86-64 clang 18.1.0 (assertions)
x86-64 clang 19.1.0
x86-64 clang 19.1.0 (assertions)
x86-64 clang 2.6.0 (assertions)
x86-64 clang 2.7.0 (assertions)
x86-64 clang 2.8.0 (assertions)
x86-64 clang 2.9.0 (assertions)
x86-64 clang 20.1.0
x86-64 clang 20.1.0 (assertions)
x86-64 clang 21.1.0
x86-64 clang 21.1.0 (assertions)
x86-64 clang 3.0.0
x86-64 clang 3.0.0 (assertions)
x86-64 clang 3.1
x86-64 clang 3.1 (assertions)
x86-64 clang 3.2
x86-64 clang 3.2 (assertions)
x86-64 clang 3.3
x86-64 clang 3.3 (assertions)
x86-64 clang 3.4 (assertions)
x86-64 clang 3.4.1
x86-64 clang 3.5
x86-64 clang 3.5 (assertions)
x86-64 clang 3.5.1
x86-64 clang 3.5.2
x86-64 clang 3.6
x86-64 clang 3.6 (assertions)
x86-64 clang 3.7
x86-64 clang 3.7 (assertions)
x86-64 clang 3.7.1
x86-64 clang 3.8
x86-64 clang 3.8 (assertions)
x86-64 clang 3.8.1
x86-64 clang 3.9.0
x86-64 clang 3.9.0 (assertions)
x86-64 clang 3.9.1
x86-64 clang 4.0.0
x86-64 clang 4.0.0 (assertions)
x86-64 clang 4.0.1
x86-64 clang 5.0.0
x86-64 clang 5.0.0 (assertions)
x86-64 clang 5.0.1
x86-64 clang 5.0.2
x86-64 clang 6.0.0
x86-64 clang 6.0.0 (assertions)
x86-64 clang 6.0.1
x86-64 clang 7.0.0
x86-64 clang 7.0.0 (assertions)
x86-64 clang 7.0.1
x86-64 clang 7.1.0
x86-64 clang 8.0.0
x86-64 clang 8.0.0 (assertions)
x86-64 clang 8.0.1
x86-64 clang 9.0.0
x86-64 clang 9.0.0 (assertions)
x86-64 clang 9.0.1
x86-64 clang rocm-4.5.2
x86-64 clang rocm-5.0.2
x86-64 clang rocm-5.1.3
x86-64 clang rocm-5.2.3
x86-64 clang rocm-5.3.3
x86-64 clang rocm-5.7.0
x86-64 clang rocm-6.0.2
x86-64 clang rocm-6.1.2
x86-64 clang rocm-6.2.4
x86-64 clang rocm-6.3.3
x86-64 clang rocm-6.4.0
x86-64 gcc (P2034 lambdas)
x86-64 gcc (contract labels)
x86-64 gcc (contracts natural syntax)
x86-64 gcc (contracts)
x86-64 gcc (coroutines)
x86-64 gcc (modules)
x86-64 gcc (trunk)
x86-64 gcc 10.1
x86-64 gcc 10.2
x86-64 gcc 10.3
x86-64 gcc 10.3 (assertions)
x86-64 gcc 10.4
x86-64 gcc 10.4 (assertions)
x86-64 gcc 10.5
x86-64 gcc 10.5 (assertions)
x86-64 gcc 11.1
x86-64 gcc 11.1 (assertions)
x86-64 gcc 11.2
x86-64 gcc 11.2 (assertions)
x86-64 gcc 11.3
x86-64 gcc 11.3 (assertions)
x86-64 gcc 11.4
x86-64 gcc 11.4 (assertions)
x86-64 gcc 12.1
x86-64 gcc 12.1 (assertions)
x86-64 gcc 12.2
x86-64 gcc 12.2 (assertions)
x86-64 gcc 12.3
x86-64 gcc 12.3 (assertions)
x86-64 gcc 12.4
x86-64 gcc 12.4 (assertions)
x86-64 gcc 12.5
x86-64 gcc 12.5 (assertions)
x86-64 gcc 13.1
x86-64 gcc 13.1 (assertions)
x86-64 gcc 13.2
x86-64 gcc 13.2 (assertions)
x86-64 gcc 13.3
x86-64 gcc 13.3 (assertions)
x86-64 gcc 13.4
x86-64 gcc 13.4 (assertions)
x86-64 gcc 14.1
x86-64 gcc 14.1 (assertions)
x86-64 gcc 14.2
x86-64 gcc 14.2 (assertions)
x86-64 gcc 14.3
x86-64 gcc 14.3 (assertions)
x86-64 gcc 15.1
x86-64 gcc 15.1 (assertions)
x86-64 gcc 15.2
x86-64 gcc 15.2 (assertions)
x86-64 gcc 3.4.6
x86-64 gcc 4.0.4
x86-64 gcc 4.1.2
x86-64 gcc 4.4.7
x86-64 gcc 4.5.3
x86-64 gcc 4.6.4
x86-64 gcc 4.7.1
x86-64 gcc 4.7.2
x86-64 gcc 4.7.3
x86-64 gcc 4.7.4
x86-64 gcc 4.8.1
x86-64 gcc 4.8.2
x86-64 gcc 4.8.3
x86-64 gcc 4.8.4
x86-64 gcc 4.8.5
x86-64 gcc 4.9.0
x86-64 gcc 4.9.1
x86-64 gcc 4.9.2
x86-64 gcc 4.9.3
x86-64 gcc 4.9.4
x86-64 gcc 5.1
x86-64 gcc 5.2
x86-64 gcc 5.3
x86-64 gcc 5.4
x86-64 gcc 5.5
x86-64 gcc 6.1
x86-64 gcc 6.2
x86-64 gcc 6.3
x86-64 gcc 6.4
x86-64 gcc 6.5
x86-64 gcc 7.1
x86-64 gcc 7.2
x86-64 gcc 7.3
x86-64 gcc 7.4
x86-64 gcc 7.5
x86-64 gcc 8.1
x86-64 gcc 8.2
x86-64 gcc 8.3
x86-64 gcc 8.4
x86-64 gcc 8.5
x86-64 gcc 9.1
x86-64 gcc 9.2
x86-64 gcc 9.3
x86-64 gcc 9.4
x86-64 gcc 9.5
x86-64 icc 13.0.1
x86-64 icc 16.0.3
x86-64 icc 17.0.0
x86-64 icc 18.0.0
x86-64 icc 19.0.0
x86-64 icc 19.0.1
x86-64 icc 2021.1.2
x86-64 icc 2021.10.0
x86-64 icc 2021.2.0
x86-64 icc 2021.3.0
x86-64 icc 2021.4.0
x86-64 icc 2021.5.0
x86-64 icc 2021.6.0
x86-64 icc 2021.7.0
x86-64 icc 2021.7.1
x86-64 icc 2021.8.0
x86-64 icc 2021.9.0
x86-64 icx 2021.1.2
x86-64 icx 2021.2.0
x86-64 icx 2021.3.0
x86-64 icx 2021.4.0
x86-64 icx 2022.0.0
x86-64 icx 2022.1.0
x86-64 icx 2022.2.0
x86-64 icx 2022.2.1
x86-64 icx 2023.0.0
x86-64 icx 2023.1.0
x86-64 icx 2023.2.1
x86-64 icx 2024.0.0
x86-64 icx 2024.1.0
x86-64 icx 2024.2.0
x86-64 icx 2024.2.1
x86-64 icx 2025.0.0
x86-64 icx 2025.0.1
x86-64 icx 2025.0.3
x86-64 icx 2025.0.4
x86-64 icx 2025.1.0
x86-64 icx 2025.1.1
x86-64 icx 2025.2.0
x86-64 icx 2025.2.1
x86-64 icx 2025.2.1
z180-clang 15.0.0
z180-clang 15.0.7
z80-clang 15.0.0
z80-clang 15.0.7
zig c++ 0.10.0
zig c++ 0.11.0
zig c++ 0.12.0
zig c++ 0.12.1
zig c++ 0.13.0
zig c++ 0.14.0
zig c++ 0.14.1
zig c++ 0.15.1
zig c++ 0.6.0
zig c++ 0.7.0
zig c++ 0.7.1
zig c++ 0.8.0
zig c++ 0.9.0
zig c++ trunk
Options
Source code
#include <iostream> #include <benchmark/benchmark.h> #include <unordered_map> #include <cstdint> #include <cstddef> #include <functional> #include <cmath> #include <algorithm> #include <iterator> #include <utility> #include <type_traits> #ifdef _MSC_VER #define SKA_NOINLINE(...) __declspec(noinline) __VA_ARGS__ #else #define SKA_NOINLINE(...) __VA_ARGS__ __attribute__((noinline)) #endif namespace ska { struct prime_number_hash_policy; struct power_of_two_hash_policy; struct fibonacci_hash_policy; namespace detailv3 { template<typename Result, typename Functor> struct functor_storage : Functor { functor_storage() = default; functor_storage(const Functor & functor) : Functor(functor) { } template<typename... Args> Result operator()(Args &&... args) { return static_cast<Functor &>(*this)(std::forward<Args>(args)...); } template<typename... Args> Result operator()(Args &&... args) const { return static_cast<const Functor &>(*this)(std::forward<Args>(args)...); } }; template<typename Result, typename... Args> struct functor_storage<Result, Result (*)(Args...)> { typedef Result (*function_ptr)(Args...); function_ptr function; functor_storage(function_ptr function) : function(function) { } Result operator()(Args... args) const { return function(std::forward<Args>(args)...); } operator function_ptr &() { return function; } operator const function_ptr &() { return function; } }; template<typename key_type, typename value_type, typename hasher> struct KeyOrValueHasher : functor_storage<size_t, hasher> { typedef functor_storage<size_t, hasher> hasher_storage; KeyOrValueHasher() = default; KeyOrValueHasher(const hasher & hash) : hasher_storage(hash) { } size_t operator()(const key_type & key) { return static_cast<hasher_storage &>(*this)(key); } size_t operator()(const key_type & key) const { return static_cast<const hasher_storage &>(*this)(key); } size_t operator()(const value_type & value) { return static_cast<hasher_storage &>(*this)(value.first); } size_t operator()(const value_type & value) const { return static_cast<const hasher_storage &>(*this)(value.first); } template<typename F, typename S> size_t operator()(const std::pair<F, S> & value) { return static_cast<hasher_storage &>(*this)(value.first); } template<typename F, typename S> size_t operator()(const std::pair<F, S> & value) const { return static_cast<const hasher_storage &>(*this)(value.first); } }; template<typename key_type, typename value_type, typename key_equal> struct KeyOrValueEquality : functor_storage<bool, key_equal> { typedef functor_storage<bool, key_equal> equality_storage; KeyOrValueEquality() = default; KeyOrValueEquality(const key_equal & equality) : equality_storage(equality) { } bool operator()(const key_type & lhs, const key_type & rhs) { return static_cast<equality_storage &>(*this)(lhs, rhs); } bool operator()(const key_type & lhs, const value_type & rhs) { return static_cast<equality_storage &>(*this)(lhs, rhs.first); } bool operator()(const value_type & lhs, const key_type & rhs) { return static_cast<equality_storage &>(*this)(lhs.first, rhs); } bool operator()(const value_type & lhs, const value_type & rhs) { return static_cast<equality_storage &>(*this)(lhs.first, rhs.first); } template<typename F, typename S> bool operator()(const key_type & lhs, const std::pair<F, S> & rhs) { return static_cast<equality_storage &>(*this)(lhs, rhs.first); } template<typename F, typename S> bool operator()(const std::pair<F, S> & lhs, const key_type & rhs) { return static_cast<equality_storage &>(*this)(lhs.first, rhs); } template<typename F, typename S> bool operator()(const value_type & lhs, const std::pair<F, S> & rhs) { return static_cast<equality_storage &>(*this)(lhs.first, rhs.first); } template<typename F, typename S> bool operator()(const std::pair<F, S> & lhs, const value_type & rhs) { return static_cast<equality_storage &>(*this)(lhs.first, rhs.first); } template<typename FL, typename SL, typename FR, typename SR> bool operator()(const std::pair<FL, SL> & lhs, const std::pair<FR, SR> & rhs) { return static_cast<equality_storage &>(*this)(lhs.first, rhs.first); } }; static constexpr int8_t min_lookups = 4; template<typename T> struct sherwood_v3_entry { sherwood_v3_entry() { } sherwood_v3_entry(int8_t distance_from_desired) : distance_from_desired(distance_from_desired) { } ~sherwood_v3_entry() { } static sherwood_v3_entry * empty_default_table() { static sherwood_v3_entry result[min_lookups] = { {}, {}, {}, {special_end_value} }; return result; } bool has_value() const { return distance_from_desired >= 0; } bool is_empty() const { return distance_from_desired < 0; } bool is_at_desired_position() const { return distance_from_desired <= 0; } template<typename... Args> void emplace(int8_t distance, Args &&... args) { new (std::addressof(value)) T(std::forward<Args>(args)...); distance_from_desired = distance; } void destroy_value() { value.~T(); distance_from_desired = -1; } int8_t distance_from_desired = -1; static constexpr int8_t special_end_value = 0; union { T value; }; }; inline int8_t log2(size_t value) { static constexpr int8_t table[64] = { 63, 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3, 61, 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4, 62, 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21, 56, 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5 }; value |= value >> 1; value |= value >> 2; value |= value >> 4; value |= value >> 8; value |= value >> 16; value |= value >> 32; return table[((value - (value >> 1)) * 0x07EDD5E59A4E28C2) >> 58]; } template<typename T, bool> struct AssignIfTrue { void operator()(T & lhs, const T & rhs) { lhs = rhs; } void operator()(T & lhs, T && rhs) { lhs = std::move(rhs); } }; template<typename T> struct AssignIfTrue<T, false> { void operator()(T &, const T &) { } void operator()(T &, T &&) { } }; inline size_t next_power_of_two(size_t i) { --i; i |= i >> 1; i |= i >> 2; i |= i >> 4; i |= i >> 8; i |= i >> 16; i |= i >> 32; ++i; return i; } template<typename...> using void_t = void; template<typename T, typename = void> struct HashPolicySelector { typedef fibonacci_hash_policy type; }; template<typename T> struct HashPolicySelector<T, void_t<typename T::hash_policy>> { typedef typename T::hash_policy type; }; template<typename T, typename FindKey, typename ArgumentHash, typename Hasher, typename ArgumentEqual, typename Equal, typename ArgumentAlloc, typename EntryAlloc> class sherwood_v3_table : private EntryAlloc, private Hasher, private Equal { using Entry = detailv3::sherwood_v3_entry<T>; using AllocatorTraits = std::allocator_traits<EntryAlloc>; using EntryPointer = typename AllocatorTraits::pointer; struct convertible_to_iterator; public: using value_type = T; using size_type = size_t; using difference_type = std::ptrdiff_t; using hasher = ArgumentHash; using key_equal = ArgumentEqual; using allocator_type = EntryAlloc; using reference = value_type &; using const_reference = const value_type &; using pointer = value_type *; using const_pointer = const value_type *; sherwood_v3_table() { } explicit sherwood_v3_table(size_type bucket_count, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc()) : EntryAlloc(alloc), Hasher(hash), Equal(equal) { rehash(bucket_count); } sherwood_v3_table(size_type bucket_count, const ArgumentAlloc & alloc) : sherwood_v3_table(bucket_count, ArgumentHash(), ArgumentEqual(), alloc) { } sherwood_v3_table(size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc) : sherwood_v3_table(bucket_count, hash, ArgumentEqual(), alloc) { } explicit sherwood_v3_table(const ArgumentAlloc & alloc) : EntryAlloc(alloc) { } template<typename It> sherwood_v3_table(It first, It last, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc()) : sherwood_v3_table(bucket_count, hash, equal, alloc) { insert(first, last); } template<typename It> sherwood_v3_table(It first, It last, size_type bucket_count, const ArgumentAlloc & alloc) : sherwood_v3_table(first, last, bucket_count, ArgumentHash(), ArgumentEqual(), alloc) { } template<typename It> sherwood_v3_table(It first, It last, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc) : sherwood_v3_table(first, last, bucket_count, hash, ArgumentEqual(), alloc) { } sherwood_v3_table(std::initializer_list<T> il, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc()) : sherwood_v3_table(bucket_count, hash, equal, alloc) { if (bucket_count == 0) rehash(il.size()); insert(il.begin(), il.end()); } sherwood_v3_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentAlloc & alloc) : sherwood_v3_table(il, bucket_count, ArgumentHash(), ArgumentEqual(), alloc) { } sherwood_v3_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc) : sherwood_v3_table(il, bucket_count, hash, ArgumentEqual(), alloc) { } sherwood_v3_table(const sherwood_v3_table & other) : sherwood_v3_table(other, AllocatorTraits::select_on_container_copy_construction(other.get_allocator())) { } sherwood_v3_table(const sherwood_v3_table & other, const ArgumentAlloc & alloc) : EntryAlloc(alloc), Hasher(other), Equal(other), _max_load_factor(other._max_load_factor) { rehash_for_other_container(other); try { insert(other.begin(), other.end()); } catch(...) { clear(); deallocate_data(entries, num_slots_minus_one, max_lookups); throw; } } sherwood_v3_table(sherwood_v3_table && other) noexcept : EntryAlloc(std::move(other)), Hasher(std::move(other)), Equal(std::move(other)) { swap_pointers(other); } sherwood_v3_table(sherwood_v3_table && other, const ArgumentAlloc & alloc) noexcept : EntryAlloc(alloc), Hasher(std::move(other)), Equal(std::move(other)) { swap_pointers(other); } sherwood_v3_table & operator=(const sherwood_v3_table & other) { if (this == std::addressof(other)) return *this; clear(); if (AllocatorTraits::propagate_on_container_copy_assignment::value) { if (static_cast<EntryAlloc &>(*this) != static_cast<const EntryAlloc &>(other)) { reset_to_empty_state(); } AssignIfTrue<EntryAlloc, AllocatorTraits::propagate_on_container_copy_assignment::value>()(*this, other); } _max_load_factor = other._max_load_factor; static_cast<Hasher &>(*this) = other; static_cast<Equal &>(*this) = other; rehash_for_other_container(other); insert(other.begin(), other.end()); return *this; } sherwood_v3_table & operator=(sherwood_v3_table && other) noexcept { if (this == std::addressof(other)) return *this; else if (AllocatorTraits::propagate_on_container_move_assignment::value) { clear(); reset_to_empty_state(); AssignIfTrue<EntryAlloc, AllocatorTraits::propagate_on_container_move_assignment::value>()(*this, std::move(other)); swap_pointers(other); } else if (static_cast<EntryAlloc &>(*this) == static_cast<EntryAlloc &>(other)) { swap_pointers(other); } else { clear(); _max_load_factor = other._max_load_factor; rehash_for_other_container(other); for (T & elem : other) emplace(std::move(elem)); other.clear(); } static_cast<Hasher &>(*this) = std::move(other); static_cast<Equal &>(*this) = std::move(other); return *this; } ~sherwood_v3_table() { clear(); deallocate_data(entries, num_slots_minus_one, max_lookups); } const allocator_type & get_allocator() const { return static_cast<const allocator_type &>(*this); } const ArgumentEqual & key_eq() const { return static_cast<const ArgumentEqual &>(*this); } const ArgumentHash & hash_function() const { return static_cast<const ArgumentHash &>(*this); } template<typename ValueType> struct templated_iterator { templated_iterator() = default; templated_iterator(EntryPointer current) : current(current) { } EntryPointer current = EntryPointer(); using iterator_category = std::forward_iterator_tag; using value_type = ValueType; using difference_type = ptrdiff_t; using pointer = ValueType *; using reference = ValueType &; friend bool operator==(const templated_iterator & lhs, const templated_iterator & rhs) { return lhs.current == rhs.current; } friend bool operator!=(const templated_iterator & lhs, const templated_iterator & rhs) { return !(lhs == rhs); } templated_iterator & operator++() { do { ++current; } while(current->is_empty()); return *this; } templated_iterator operator++(int) { templated_iterator copy(*this); ++*this; return copy; } ValueType & operator*() const { return current->value; } ValueType * operator->() const { return std::addressof(current->value); } operator templated_iterator<const value_type>() const { return { current }; } }; using iterator = templated_iterator<value_type>; using const_iterator = templated_iterator<const value_type>; iterator begin() { for (EntryPointer it = entries;; ++it) { if (it->has_value()) return { it }; } } const_iterator begin() const { for (EntryPointer it = entries;; ++it) { if (it->has_value()) return { it }; } } const_iterator cbegin() const { return begin(); } iterator end() { return { entries + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups) }; } const_iterator end() const { return { entries + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups) }; } const_iterator cend() const { return end(); } iterator find(const FindKey & key) { size_t index = hash_policy.index_for_hash(hash_object(key), num_slots_minus_one); EntryPointer it = entries + ptrdiff_t(index); for (int8_t distance = 0; it->distance_from_desired >= distance; ++distance, ++it) { if (compares_equal(key, it->value)) return { it }; } return end(); } const_iterator find(const FindKey & key) const { return const_cast<sherwood_v3_table *>(this)->find(key); } size_t count(const FindKey & key) const { return find(key) == end() ? 0 : 1; } std::pair<iterator, iterator> equal_range(const FindKey & key) { iterator found = find(key); if (found == end()) return { found, found }; else return { found, std::next(found) }; } std::pair<const_iterator, const_iterator> equal_range(const FindKey & key) const { const_iterator found = find(key); if (found == end()) return { found, found }; else return { found, std::next(found) }; } template<typename Key, typename... Args> std::pair<iterator, bool> emplace(Key && key, Args &&... args) { size_t index = hash_policy.index_for_hash(hash_object(key), num_slots_minus_one); EntryPointer current_entry = entries + ptrdiff_t(index); int8_t distance_from_desired = 0; for (; current_entry->distance_from_desired >= distance_from_desired; ++current_entry, ++distance_from_desired) { if (compares_equal(key, current_entry->value)) return { { current_entry }, false }; } return emplace_new_key(distance_from_desired, current_entry, std::forward<Key>(key), std::forward<Args>(args)...); } std::pair<iterator, bool> insert(const value_type & value) { return emplace(value); } std::pair<iterator, bool> insert(value_type && value) { return emplace(std::move(value)); } template<typename... Args> iterator emplace_hint(const_iterator, Args &&... args) { return emplace(std::forward<Args>(args)...).first; } iterator insert(const_iterator, const value_type & value) { return emplace(value).first; } iterator insert(const_iterator, value_type && value) { return emplace(std::move(value)).first; } template<typename It> void insert(It begin, It end) { for (; begin != end; ++begin) { emplace(*begin); } } void insert(std::initializer_list<value_type> il) { insert(il.begin(), il.end()); } void rehash(size_t num_buckets) { num_buckets = std::max(num_buckets, static_cast<size_t>(std::ceil(num_elements / static_cast<double>(_max_load_factor)))); if (num_buckets == 0) { reset_to_empty_state(); return; } auto new_prime_index = hash_policy.next_size_over(num_buckets); if (num_buckets == bucket_count()) return; int8_t new_max_lookups = compute_max_lookups(num_buckets); EntryPointer new_buckets(AllocatorTraits::allocate(*this, num_buckets + new_max_lookups)); EntryPointer special_end_item = new_buckets + static_cast<ptrdiff_t>(num_buckets + new_max_lookups - 1); for (EntryPointer it = new_buckets; it != special_end_item; ++it) it->distance_from_desired = -1; special_end_item->distance_from_desired = Entry::special_end_value; std::swap(entries, new_buckets); std::swap(num_slots_minus_one, num_buckets); --num_slots_minus_one; hash_policy.commit(new_prime_index); int8_t old_max_lookups = max_lookups; max_lookups = new_max_lookups; num_elements = 0; for (EntryPointer it = new_buckets, end = it + static_cast<ptrdiff_t>(num_buckets + old_max_lookups); it != end; ++it) { if (it->has_value()) { emplace(std::move(it->value)); it->destroy_value(); } } deallocate_data(new_buckets, num_buckets, old_max_lookups); } void reserve(size_t num_elements) { size_t required_buckets = num_buckets_for_reserve(num_elements); if (required_buckets > bucket_count()) rehash(required_buckets); } // the return value is a type that can be converted to an iterator // the reason for doing this is that it's not free to find the // iterator pointing at the next element. if you care about the // next iterator, turn the return value into an iterator convertible_to_iterator erase(const_iterator to_erase) { EntryPointer current = to_erase.current; current->destroy_value(); --num_elements; for (EntryPointer next = current + ptrdiff_t(1); !next->is_at_desired_position(); ++current, ++next) { current->emplace(next->distance_from_desired - 1, std::move(next->value)); next->destroy_value(); } return { to_erase.current }; } iterator erase(const_iterator begin_it, const_iterator end_it) { if (begin_it == end_it) return { begin_it.current }; for (EntryPointer it = begin_it.current, end = end_it.current; it != end; ++it) { if (it->has_value()) { it->destroy_value(); --num_elements; } } if (end_it == this->end()) return this->end(); ptrdiff_t num_to_move = std::min(static_cast<ptrdiff_t>(end_it.current->distance_from_desired), end_it.current - begin_it.current); EntryPointer to_return = end_it.current - num_to_move; for (EntryPointer it = end_it.current; !it->is_at_desired_position();) { EntryPointer target = it - num_to_move; target->emplace(it->distance_from_desired - num_to_move, std::move(it->value)); it->destroy_value(); ++it; num_to_move = std::min(static_cast<ptrdiff_t>(it->distance_from_desired), num_to_move); } return { to_return }; } size_t erase(const FindKey & key) { auto found = find(key); if (found == end()) return 0; else { erase(found); return 1; } } void clear() { for (EntryPointer it = entries, end = it + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups); it != end; ++it) { if (it->has_value()) it->destroy_value(); } num_elements = 0; } void shrink_to_fit() { rehash_for_other_container(*this); } void swap(sherwood_v3_table & other) { using std::swap; swap_pointers(other); swap(static_cast<ArgumentHash &>(*this), static_cast<ArgumentHash &>(other)); swap(static_cast<ArgumentEqual &>(*this), static_cast<ArgumentEqual &>(other)); if (AllocatorTraits::propagate_on_container_swap::value) swap(static_cast<EntryAlloc &>(*this), static_cast<EntryAlloc &>(other)); } size_t size() const { return num_elements; } size_t max_size() const { return (AllocatorTraits::max_size(*this)) / sizeof(Entry); } size_t bucket_count() const { return num_slots_minus_one ? num_slots_minus_one + 1 : 0; } size_type max_bucket_count() const { return (AllocatorTraits::max_size(*this) - min_lookups) / sizeof(Entry); } size_t bucket(const FindKey & key) const { return hash_policy.index_for_hash(hash_object(key), num_slots_minus_one); } float load_factor() const { size_t buckets = bucket_count(); if (buckets) return static_cast<float>(num_elements) / bucket_count(); else return 0; } void max_load_factor(float value) { _max_load_factor = value; } float max_load_factor() const { return _max_load_factor; } bool empty() const { return num_elements == 0; } private: EntryPointer entries = Entry::empty_default_table(); size_t num_slots_minus_one = 0; typename HashPolicySelector<ArgumentHash>::type hash_policy; int8_t max_lookups = detailv3::min_lookups - 1; float _max_load_factor = 0.5f; size_t num_elements = 0; static int8_t compute_max_lookups(size_t num_buckets) { int8_t desired = detailv3::log2(num_buckets); return std::max(detailv3::min_lookups, desired); } size_t num_buckets_for_reserve(size_t num_elements) const { return static_cast<size_t>(std::ceil(num_elements / std::min(0.5, static_cast<double>(_max_load_factor)))); } void rehash_for_other_container(const sherwood_v3_table & other) { rehash(std::min(num_buckets_for_reserve(other.size()), other.bucket_count())); } void swap_pointers(sherwood_v3_table & other) { using std::swap; swap(hash_policy, other.hash_policy); swap(entries, other.entries); swap(num_slots_minus_one, other.num_slots_minus_one); swap(num_elements, other.num_elements); swap(max_lookups, other.max_lookups); swap(_max_load_factor, other._max_load_factor); } template<typename Key, typename... Args> SKA_NOINLINE(std::pair<iterator, bool>) emplace_new_key(int8_t distance_from_desired, EntryPointer current_entry, Key && key, Args &&... args) { using std::swap; if (num_slots_minus_one == 0 || distance_from_desired == max_lookups || num_elements + 1 > (num_slots_minus_one + 1) * static_cast<double>(_max_load_factor)) { grow(); return emplace(std::forward<Key>(key), std::forward<Args>(args)...); } else if (current_entry->is_empty()) { current_entry->emplace(distance_from_desired, std::forward<Key>(key), std::forward<Args>(args)...); ++num_elements; return { { current_entry }, true }; } value_type to_insert(std::forward<Key>(key), std::forward<Args>(args)...); swap(distance_from_desired, current_entry->distance_from_desired); swap(to_insert, current_entry->value); iterator result = { current_entry }; for (++distance_from_desired, ++current_entry;; ++current_entry) { if (current_entry->is_empty()) { current_entry->emplace(distance_from_desired, std::move(to_insert)); ++num_elements; return { result, true }; } else if (current_entry->distance_from_desired < distance_from_desired) { swap(distance_from_desired, current_entry->distance_from_desired); swap(to_insert, current_entry->value); ++distance_from_desired; } else { ++distance_from_desired; if (distance_from_desired == max_lookups) { swap(to_insert, result.current->value); grow(); return emplace(std::move(to_insert)); } } } } void grow() { rehash(std::max(size_t(4), 2 * bucket_count())); } void deallocate_data(EntryPointer begin, size_t num_slots_minus_one, int8_t max_lookups) { if (begin != Entry::empty_default_table()) { AllocatorTraits::deallocate(*this, begin, num_slots_minus_one + max_lookups + 1); } } void reset_to_empty_state() { deallocate_data(entries, num_slots_minus_one, max_lookups); entries = Entry::empty_default_table(); num_slots_minus_one = 0; hash_policy.reset(); max_lookups = detailv3::min_lookups - 1; } template<typename U> size_t hash_object(const U & key) { return static_cast<Hasher &>(*this)(key); } template<typename U> size_t hash_object(const U & key) const { return static_cast<const Hasher &>(*this)(key); } template<typename L, typename R> bool compares_equal(const L & lhs, const R & rhs) { return static_cast<Equal &>(*this)(lhs, rhs); } struct convertible_to_iterator { EntryPointer it; operator iterator() { if (it->has_value()) return { it }; else return ++iterator{it}; } operator const_iterator() { if (it->has_value()) return { it }; else return ++const_iterator{it}; } }; }; } struct prime_number_hash_policy { static size_t mod0(size_t) { return 0llu; } static size_t mod2(size_t hash) { return hash % 2llu; } static size_t mod3(size_t hash) { return hash % 3llu; } static size_t mod5(size_t hash) { return hash % 5llu; } static size_t mod7(size_t hash) { return hash % 7llu; } static size_t mod11(size_t hash) { return hash % 11llu; } static size_t mod13(size_t hash) { return hash % 13llu; } static size_t mod17(size_t hash) { return hash % 17llu; } static size_t mod23(size_t hash) { return hash % 23llu; } static size_t mod29(size_t hash) { return hash % 29llu; } static size_t mod37(size_t hash) { return hash % 37llu; } static size_t mod47(size_t hash) { return hash % 47llu; } static size_t mod59(size_t hash) { return hash % 59llu; } static size_t mod73(size_t hash) { return hash % 73llu; } static size_t mod97(size_t hash) { return hash % 97llu; } static size_t mod127(size_t hash) { return hash % 127llu; } static size_t mod151(size_t hash) { return hash % 151llu; } static size_t mod197(size_t hash) { return hash % 197llu; } static size_t mod251(size_t hash) { return hash % 251llu; } static size_t mod313(size_t hash) { return hash % 313llu; } static size_t mod397(size_t hash) { return hash % 397llu; } static size_t mod499(size_t hash) { return hash % 499llu; } static size_t mod631(size_t hash) { return hash % 631llu; } static size_t mod797(size_t hash) { return hash % 797llu; } static size_t mod1009(size_t hash) { return hash % 1009llu; } static size_t mod1259(size_t hash) { return hash % 1259llu; } static size_t mod1597(size_t hash) { return hash % 1597llu; } static size_t mod2011(size_t hash) { return hash % 2011llu; } static size_t mod2539(size_t hash) { return hash % 2539llu; } static size_t mod3203(size_t hash) { return hash % 3203llu; } static size_t mod4027(size_t hash) { return hash % 4027llu; } static size_t mod5087(size_t hash) { return hash % 5087llu; } static size_t mod6421(size_t hash) { return hash % 6421llu; } static size_t mod8089(size_t hash) { return hash % 8089llu; } static size_t mod10193(size_t hash) { return hash % 10193llu; } static size_t mod12853(size_t hash) { return hash % 12853llu; } static size_t mod16193(size_t hash) { return hash % 16193llu; } static size_t mod20399(size_t hash) { return hash % 20399llu; } static size_t mod25717(size_t hash) { return hash % 25717llu; } static size_t mod32401(size_t hash) { return hash % 32401llu; } static size_t mod40823(size_t hash) { return hash % 40823llu; } static size_t mod51437(size_t hash) { return hash % 51437llu; } static size_t mod64811(size_t hash) { return hash % 64811llu; } static size_t mod81649(size_t hash) { return hash % 81649llu; } static size_t mod102877(size_t hash) { return hash % 102877llu; } static size_t mod129607(size_t hash) { return hash % 129607llu; } static size_t mod163307(size_t hash) { return hash % 163307llu; } static size_t mod205759(size_t hash) { return hash % 205759llu; } static size_t mod259229(size_t hash) { return hash % 259229llu; } static size_t mod326617(size_t hash) { return hash % 326617llu; } static size_t mod411527(size_t hash) { return hash % 411527llu; } static size_t mod518509(size_t hash) { return hash % 518509llu; } static size_t mod653267(size_t hash) { return hash % 653267llu; } static size_t mod823117(size_t hash) { return hash % 823117llu; } static size_t mod1037059(size_t hash) { return hash % 1037059llu; } static size_t mod1306601(size_t hash) { return hash % 1306601llu; } static size_t mod1646237(size_t hash) { return hash % 1646237llu; } static size_t mod2074129(size_t hash) { return hash % 2074129llu; } static size_t mod2613229(size_t hash) { return hash % 2613229llu; } static size_t mod3292489(size_t hash) { return hash % 3292489llu; } static size_t mod4148279(size_t hash) { return hash % 4148279llu; } static size_t mod5226491(size_t hash) { return hash % 5226491llu; } static size_t mod6584983(size_t hash) { return hash % 6584983llu; } static size_t mod8296553(size_t hash) { return hash % 8296553llu; } static size_t mod10453007(size_t hash) { return hash % 10453007llu; } static size_t mod13169977(size_t hash) { return hash % 13169977llu; } static size_t mod16593127(size_t hash) { return hash % 16593127llu; } static size_t mod20906033(size_t hash) { return hash % 20906033llu; } static size_t mod26339969(size_t hash) { return hash % 26339969llu; } static size_t mod33186281(size_t hash) { return hash % 33186281llu; } static size_t mod41812097(size_t hash) { return hash % 41812097llu; } static size_t mod52679969(size_t hash) { return hash % 52679969llu; } static size_t mod66372617(size_t hash) { return hash % 66372617llu; } static size_t mod83624237(size_t hash) { return hash % 83624237llu; } static size_t mod105359939(size_t hash) { return hash % 105359939llu; } static size_t mod132745199(size_t hash) { return hash % 132745199llu; } static size_t mod167248483(size_t hash) { return hash % 167248483llu; } static size_t mod210719881(size_t hash) { return hash % 210719881llu; } static size_t mod265490441(size_t hash) { return hash % 265490441llu; } static size_t mod334496971(size_t hash) { return hash % 334496971llu; } static size_t mod421439783(size_t hash) { return hash % 421439783llu; } static size_t mod530980861(size_t hash) { return hash % 530980861llu; } static size_t mod668993977(size_t hash) { return hash % 668993977llu; } static size_t mod842879579(size_t hash) { return hash % 842879579llu; } static size_t mod1061961721(size_t hash) { return hash % 1061961721llu; } static size_t mod1337987929(size_t hash) { return hash % 1337987929llu; } static size_t mod1685759167(size_t hash) { return hash % 1685759167llu; } static size_t mod2123923447(size_t hash) { return hash % 2123923447llu; } static size_t mod2675975881(size_t hash) { return hash % 2675975881llu; } static size_t mod3371518343(size_t hash) { return hash % 3371518343llu; } static size_t mod4247846927(size_t hash) { return hash % 4247846927llu; } static size_t mod5351951779(size_t hash) { return hash % 5351951779llu; } static size_t mod6743036717(size_t hash) { return hash % 6743036717llu; } static size_t mod8495693897(size_t hash) { return hash % 8495693897llu; } static size_t mod10703903591(size_t hash) { return hash % 10703903591llu; } static size_t mod13486073473(size_t hash) { return hash % 13486073473llu; } static size_t mod16991387857(size_t hash) { return hash % 16991387857llu; } static size_t mod21407807219(size_t hash) { return hash % 21407807219llu; } static size_t mod26972146961(size_t hash) { return hash % 26972146961llu; } static size_t mod33982775741(size_t hash) { return hash % 33982775741llu; } static size_t mod42815614441(size_t hash) { return hash % 42815614441llu; } static size_t mod53944293929(size_t hash) { return hash % 53944293929llu; } static size_t mod67965551447(size_t hash) { return hash % 67965551447llu; } static size_t mod85631228929(size_t hash) { return hash % 85631228929llu; } static size_t mod107888587883(size_t hash) { return hash % 107888587883llu; } static size_t mod135931102921(size_t hash) { return hash % 135931102921llu; } static size_t mod171262457903(size_t hash) { return hash % 171262457903llu; } static size_t mod215777175787(size_t hash) { return hash % 215777175787llu; } static size_t mod271862205833(size_t hash) { return hash % 271862205833llu; } static size_t mod342524915839(size_t hash) { return hash % 342524915839llu; } static size_t mod431554351609(size_t hash) { return hash % 431554351609llu; } static size_t mod543724411781(size_t hash) { return hash % 543724411781llu; } static size_t mod685049831731(size_t hash) { return hash % 685049831731llu; } static size_t mod863108703229(size_t hash) { return hash % 863108703229llu; } static size_t mod1087448823553(size_t hash) { return hash % 1087448823553llu; } static size_t mod1370099663459(size_t hash) { return hash % 1370099663459llu; } static size_t mod1726217406467(size_t hash) { return hash % 1726217406467llu; } static size_t mod2174897647073(size_t hash) { return hash % 2174897647073llu; } static size_t mod2740199326961(size_t hash) { return hash % 2740199326961llu; } static size_t mod3452434812973(size_t hash) { return hash % 3452434812973llu; } static size_t mod4349795294267(size_t hash) { return hash % 4349795294267llu; } static size_t mod5480398654009(size_t hash) { return hash % 5480398654009llu; } static size_t mod6904869625999(size_t hash) { return hash % 6904869625999llu; } static size_t mod8699590588571(size_t hash) { return hash % 8699590588571llu; } static size_t mod10960797308051(size_t hash) { return hash % 10960797308051llu; } static size_t mod13809739252051(size_t hash) { return hash % 13809739252051llu; } static size_t mod17399181177241(size_t hash) { return hash % 17399181177241llu; } static size_t mod21921594616111(size_t hash) { return hash % 21921594616111llu; } static size_t mod27619478504183(size_t hash) { return hash % 27619478504183llu; } static size_t mod34798362354533(size_t hash) { return hash % 34798362354533llu; } static size_t mod43843189232363(size_t hash) { return hash % 43843189232363llu; } static size_t mod55238957008387(size_t hash) { return hash % 55238957008387llu; } static size_t mod69596724709081(size_t hash) { return hash % 69596724709081llu; } static size_t mod87686378464759(size_t hash) { return hash % 87686378464759llu; } static size_t mod110477914016779(size_t hash) { return hash % 110477914016779llu; } static size_t mod139193449418173(size_t hash) { return hash % 139193449418173llu; } static size_t mod175372756929481(size_t hash) { return hash % 175372756929481llu; } static size_t mod220955828033581(size_t hash) { return hash % 220955828033581llu; } static size_t mod278386898836457(size_t hash) { return hash % 278386898836457llu; } static size_t mod350745513859007(size_t hash) { return hash % 350745513859007llu; } static size_t mod441911656067171(size_t hash) { return hash % 441911656067171llu; } static size_t mod556773797672909(size_t hash) { return hash % 556773797672909llu; } static size_t mod701491027718027(size_t hash) { return hash % 701491027718027llu; } static size_t mod883823312134381(size_t hash) { return hash % 883823312134381llu; } static size_t mod1113547595345903(size_t hash) { return hash % 1113547595345903llu; } static size_t mod1402982055436147(size_t hash) { return hash % 1402982055436147llu; } static size_t mod1767646624268779(size_t hash) { return hash % 1767646624268779llu; } static size_t mod2227095190691797(size_t hash) { return hash % 2227095190691797llu; } static size_t mod2805964110872297(size_t hash) { return hash % 2805964110872297llu; } static size_t mod3535293248537579(size_t hash) { return hash % 3535293248537579llu; } static size_t mod4454190381383713(size_t hash) { return hash % 4454190381383713llu; } static size_t mod5611928221744609(size_t hash) { return hash % 5611928221744609llu; } static size_t mod7070586497075177(size_t hash) { return hash % 7070586497075177llu; } static size_t mod8908380762767489(size_t hash) { return hash % 8908380762767489llu; } static size_t mod11223856443489329(size_t hash) { return hash % 11223856443489329llu; } static size_t mod14141172994150357(size_t hash) { return hash % 14141172994150357llu; } static size_t mod17816761525534927(size_t hash) { return hash % 17816761525534927llu; } static size_t mod22447712886978529(size_t hash) { return hash % 22447712886978529llu; } static size_t mod28282345988300791(size_t hash) { return hash % 28282345988300791llu; } static size_t mod35633523051069991(size_t hash) { return hash % 35633523051069991llu; } static size_t mod44895425773957261(size_t hash) { return hash % 44895425773957261llu; } static size_t mod56564691976601587(size_t hash) { return hash % 56564691976601587llu; } static size_t mod71267046102139967(size_t hash) { return hash % 71267046102139967llu; } static size_t mod89790851547914507(size_t hash) { return hash % 89790851547914507llu; } static size_t mod113129383953203213(size_t hash) { return hash % 113129383953203213llu; } static size_t mod142534092204280003(size_t hash) { return hash % 142534092204280003llu; } static size_t mod179581703095829107(size_t hash) { return hash % 179581703095829107llu; } static size_t mod226258767906406483(size_t hash) { return hash % 226258767906406483llu; } static size_t mod285068184408560057(size_t hash) { return hash % 285068184408560057llu; } static size_t mod359163406191658253(size_t hash) { return hash % 359163406191658253llu; } static size_t mod452517535812813007(size_t hash) { return hash % 452517535812813007llu; } static size_t mod570136368817120201(size_t hash) { return hash % 570136368817120201llu; } static size_t mod718326812383316683(size_t hash) { return hash % 718326812383316683llu; } static size_t mod905035071625626043(size_t hash) { return hash % 905035071625626043llu; } static size_t mod1140272737634240411(size_t hash) { return hash % 1140272737634240411llu; } static size_t mod1436653624766633509(size_t hash) { return hash % 1436653624766633509llu; } static size_t mod1810070143251252131(size_t hash) { return hash % 1810070143251252131llu; } static size_t mod2280545475268481167(size_t hash) { return hash % 2280545475268481167llu; } static size_t mod2873307249533267101(size_t hash) { return hash % 2873307249533267101llu; } static size_t mod3620140286502504283(size_t hash) { return hash % 3620140286502504283llu; } static size_t mod4561090950536962147(size_t hash) { return hash % 4561090950536962147llu; } static size_t mod5746614499066534157(size_t hash) { return hash % 5746614499066534157llu; } static size_t mod7240280573005008577(size_t hash) { return hash % 7240280573005008577llu; } static size_t mod9122181901073924329(size_t hash) { return hash % 9122181901073924329llu; } static size_t mod11493228998133068689(size_t hash) { return hash % 11493228998133068689llu; } static size_t mod14480561146010017169(size_t hash) { return hash % 14480561146010017169llu; } static size_t mod18446744073709551557(size_t hash) { return hash % 18446744073709551557llu; } using mod_function = size_t (*)(size_t); mod_function next_size_over(size_t & size) const { // prime numbers generated by the following method: // 1. start with a prime p = 2 // 2. go to wolfram alpha and get p = NextPrime(2 * p) // 3. repeat 2. until you overflow 64 bits // you now have large gaps which you would hit if somebody called reserve() with an unlucky number. // 4. to fill the gaps for every prime p go to wolfram alpha and get ClosestPrime(p * 2^(1/3)) and ClosestPrime(p * 2^(2/3)) and put those in the gaps // 5. get PrevPrime(2^64) and put it at the end static constexpr const size_t prime_list[] = { 2llu, 3llu, 5llu, 7llu, 11llu, 13llu, 17llu, 23llu, 29llu, 37llu, 47llu, 59llu, 73llu, 97llu, 127llu, 151llu, 197llu, 251llu, 313llu, 397llu, 499llu, 631llu, 797llu, 1009llu, 1259llu, 1597llu, 2011llu, 2539llu, 3203llu, 4027llu, 5087llu, 6421llu, 8089llu, 10193llu, 12853llu, 16193llu, 20399llu, 25717llu, 32401llu, 40823llu, 51437llu, 64811llu, 81649llu, 102877llu, 129607llu, 163307llu, 205759llu, 259229llu, 326617llu, 411527llu, 518509llu, 653267llu, 823117llu, 1037059llu, 1306601llu, 1646237llu, 2074129llu, 2613229llu, 3292489llu, 4148279llu, 5226491llu, 6584983llu, 8296553llu, 10453007llu, 13169977llu, 16593127llu, 20906033llu, 26339969llu, 33186281llu, 41812097llu, 52679969llu, 66372617llu, 83624237llu, 105359939llu, 132745199llu, 167248483llu, 210719881llu, 265490441llu, 334496971llu, 421439783llu, 530980861llu, 668993977llu, 842879579llu, 1061961721llu, 1337987929llu, 1685759167llu, 2123923447llu, 2675975881llu, 3371518343llu, 4247846927llu, 5351951779llu, 6743036717llu, 8495693897llu, 10703903591llu, 13486073473llu, 16991387857llu, 21407807219llu, 26972146961llu, 33982775741llu, 42815614441llu, 53944293929llu, 67965551447llu, 85631228929llu, 107888587883llu, 135931102921llu, 171262457903llu, 215777175787llu, 271862205833llu, 342524915839llu, 431554351609llu, 543724411781llu, 685049831731llu, 863108703229llu, 1087448823553llu, 1370099663459llu, 1726217406467llu, 2174897647073llu, 2740199326961llu, 3452434812973llu, 4349795294267llu, 5480398654009llu, 6904869625999llu, 8699590588571llu, 10960797308051llu, 13809739252051llu, 17399181177241llu, 21921594616111llu, 27619478504183llu, 34798362354533llu, 43843189232363llu, 55238957008387llu, 69596724709081llu, 87686378464759llu, 110477914016779llu, 139193449418173llu, 175372756929481llu, 220955828033581llu, 278386898836457llu, 350745513859007llu, 441911656067171llu, 556773797672909llu, 701491027718027llu, 883823312134381llu, 1113547595345903llu, 1402982055436147llu, 1767646624268779llu, 2227095190691797llu, 2805964110872297llu, 3535293248537579llu, 4454190381383713llu, 5611928221744609llu, 7070586497075177llu, 8908380762767489llu, 11223856443489329llu, 14141172994150357llu, 17816761525534927llu, 22447712886978529llu, 28282345988300791llu, 35633523051069991llu, 44895425773957261llu, 56564691976601587llu, 71267046102139967llu, 89790851547914507llu, 113129383953203213llu, 142534092204280003llu, 179581703095829107llu, 226258767906406483llu, 285068184408560057llu, 359163406191658253llu, 452517535812813007llu, 570136368817120201llu, 718326812383316683llu, 905035071625626043llu, 1140272737634240411llu, 1436653624766633509llu, 1810070143251252131llu, 2280545475268481167llu, 2873307249533267101llu, 3620140286502504283llu, 4561090950536962147llu, 5746614499066534157llu, 7240280573005008577llu, 9122181901073924329llu, 11493228998133068689llu, 14480561146010017169llu, 18446744073709551557llu }; static constexpr size_t (* const mod_functions[])(size_t) = { &mod0, &mod2, &mod3, &mod5, &mod7, &mod11, &mod13, &mod17, &mod23, &mod29, &mod37, &mod47, &mod59, &mod73, &mod97, &mod127, &mod151, &mod197, &mod251, &mod313, &mod397, &mod499, &mod631, &mod797, &mod1009, &mod1259, &mod1597, &mod2011, &mod2539, &mod3203, &mod4027, &mod5087, &mod6421, &mod8089, &mod10193, &mod12853, &mod16193, &mod20399, &mod25717, &mod32401, &mod40823, &mod51437, &mod64811, &mod81649, &mod102877, &mod129607, &mod163307, &mod205759, &mod259229, &mod326617, &mod411527, &mod518509, &mod653267, &mod823117, &mod1037059, &mod1306601, &mod1646237, &mod2074129, &mod2613229, &mod3292489, &mod4148279, &mod5226491, &mod6584983, &mod8296553, &mod10453007, &mod13169977, &mod16593127, &mod20906033, &mod26339969, &mod33186281, &mod41812097, &mod52679969, &mod66372617, &mod83624237, &mod105359939, &mod132745199, &mod167248483, &mod210719881, &mod265490441, &mod334496971, &mod421439783, &mod530980861, &mod668993977, &mod842879579, &mod1061961721, &mod1337987929, &mod1685759167, &mod2123923447, &mod2675975881, &mod3371518343, &mod4247846927, &mod5351951779, &mod6743036717, &mod8495693897, &mod10703903591, &mod13486073473, &mod16991387857, &mod21407807219, &mod26972146961, &mod33982775741, &mod42815614441, &mod53944293929, &mod67965551447, &mod85631228929, &mod107888587883, &mod135931102921, &mod171262457903, &mod215777175787, &mod271862205833, &mod342524915839, &mod431554351609, &mod543724411781, &mod685049831731, &mod863108703229, &mod1087448823553, &mod1370099663459, &mod1726217406467, &mod2174897647073, &mod2740199326961, &mod3452434812973, &mod4349795294267, &mod5480398654009, &mod6904869625999, &mod8699590588571, &mod10960797308051, &mod13809739252051, &mod17399181177241, &mod21921594616111, &mod27619478504183, &mod34798362354533, &mod43843189232363, &mod55238957008387, &mod69596724709081, &mod87686378464759, &mod110477914016779, &mod139193449418173, &mod175372756929481, &mod220955828033581, &mod278386898836457, &mod350745513859007, &mod441911656067171, &mod556773797672909, &mod701491027718027, &mod883823312134381, &mod1113547595345903, &mod1402982055436147, &mod1767646624268779, &mod2227095190691797, &mod2805964110872297, &mod3535293248537579, &mod4454190381383713, &mod5611928221744609, &mod7070586497075177, &mod8908380762767489, &mod11223856443489329, &mod14141172994150357, &mod17816761525534927, &mod22447712886978529, &mod28282345988300791, &mod35633523051069991, &mod44895425773957261, &mod56564691976601587, &mod71267046102139967, &mod89790851547914507, &mod113129383953203213, &mod142534092204280003, &mod179581703095829107, &mod226258767906406483, &mod285068184408560057, &mod359163406191658253, &mod452517535812813007, &mod570136368817120201, &mod718326812383316683, &mod905035071625626043, &mod1140272737634240411, &mod1436653624766633509, &mod1810070143251252131, &mod2280545475268481167, &mod2873307249533267101, &mod3620140286502504283, &mod4561090950536962147, &mod5746614499066534157, &mod7240280573005008577, &mod9122181901073924329, &mod11493228998133068689, &mod14480561146010017169, &mod18446744073709551557 }; const size_t * found = std::lower_bound(std::begin(prime_list), std::end(prime_list) - 1, size); size = *found; return mod_functions[1 + found - prime_list]; } void commit(mod_function new_mod_function) { current_mod_function = new_mod_function; } void reset() { current_mod_function = &mod0; } size_t index_for_hash(size_t hash, size_t /*num_slots_minus_one*/) const { return current_mod_function(hash); } size_t keep_in_range(size_t index, size_t num_slots_minus_one) const { return index > num_slots_minus_one ? current_mod_function(index) : index; } private: mod_function current_mod_function = &mod0; }; struct power_of_two_hash_policy { size_t index_for_hash(size_t hash, size_t num_slots_minus_one) const { return hash & num_slots_minus_one; } size_t keep_in_range(size_t index, size_t num_slots_minus_one) const { return index_for_hash(index, num_slots_minus_one); } int8_t next_size_over(size_t & size) const { size = detailv3::next_power_of_two(size); return 0; } void commit(int8_t) { } void reset() { } }; struct fibonacci_hash_policy { size_t index_for_hash(size_t hash, size_t /*num_slots_minus_one*/) const { return (11400714819323198485ull * hash) >> shift; } size_t keep_in_range(size_t index, size_t num_slots_minus_one) const { return index & num_slots_minus_one; } int8_t next_size_over(size_t & size) const { size = std::max(size_t(2), detailv3::next_power_of_two(size)); return 64 - detailv3::log2(size); } void commit(int8_t shift) { this->shift = shift; } void reset() { shift = 63; } private: int8_t shift = 63; }; template<typename K, typename V, typename H = std::hash<K>, typename E = std::equal_to<K>, typename A = std::allocator<std::pair<K, V> > > class flat_hash_map : public detailv3::sherwood_v3_table < std::pair<K, V>, K, H, detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>, E, detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>, A, typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<std::pair<K, V>>> > { using Table = detailv3::sherwood_v3_table < std::pair<K, V>, K, H, detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>, E, detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>, A, typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<std::pair<K, V>>> >; public: using key_type = K; using mapped_type = V; using Table::Table; flat_hash_map() { } inline V & operator[](const K & key) { return emplace(key, convertible_to_value()).first->second; } inline V & operator[](K && key) { return emplace(std::move(key), convertible_to_value()).first->second; } V & at(const K & key) { auto found = this->find(key); if (found == this->end()) throw std::out_of_range("Argument passed to at() was not in the map."); return found->second; } const V & at(const K & key) const { auto found = this->find(key); if (found == this->end()) throw std::out_of_range("Argument passed to at() was not in the map."); return found->second; } using Table::emplace; std::pair<typename Table::iterator, bool> emplace() { return emplace(key_type(), convertible_to_value()); } template<typename M> std::pair<typename Table::iterator, bool> insert_or_assign(const key_type & key, M && m) { auto emplace_result = emplace(key, std::forward<M>(m)); if (!emplace_result.second) emplace_result.first->second = std::forward<M>(m); return emplace_result; } template<typename M> std::pair<typename Table::iterator, bool> insert_or_assign(key_type && key, M && m) { auto emplace_result = emplace(std::move(key), std::forward<M>(m)); if (!emplace_result.second) emplace_result.first->second = std::forward<M>(m); return emplace_result; } template<typename M> typename Table::iterator insert_or_assign(typename Table::const_iterator, const key_type & key, M && m) { return insert_or_assign(key, std::forward<M>(m)).first; } template<typename M> typename Table::iterator insert_or_assign(typename Table::const_iterator, key_type && key, M && m) { return insert_or_assign(std::move(key), std::forward<M>(m)).first; } friend bool operator==(const flat_hash_map & lhs, const flat_hash_map & rhs) { if (lhs.size() != rhs.size()) return false; for (const typename Table::value_type & value : lhs) { auto found = rhs.find(value.first); if (found == rhs.end()) return false; else if (value.second != found->second) return false; } return true; } friend bool operator!=(const flat_hash_map & lhs, const flat_hash_map & rhs) { return !(lhs == rhs); } private: struct convertible_to_value { operator V() const { return V(); } }; }; template<typename T, typename H = std::hash<T>, typename E = std::equal_to<T>, typename A = std::allocator<T> > class flat_hash_set : public detailv3::sherwood_v3_table < T, T, H, detailv3::functor_storage<size_t, H>, E, detailv3::functor_storage<bool, E>, A, typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<T>> > { using Table = detailv3::sherwood_v3_table < T, T, H, detailv3::functor_storage<size_t, H>, E, detailv3::functor_storage<bool, E>, A, typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<T>> >; public: using key_type = T; using Table::Table; flat_hash_set() { } template<typename... Args> std::pair<typename Table::iterator, bool> emplace(Args &&... args) { return Table::emplace(T(std::forward<Args>(args)...)); } std::pair<typename Table::iterator, bool> emplace(const key_type & arg) { return Table::emplace(arg); } std::pair<typename Table::iterator, bool> emplace(key_type & arg) { return Table::emplace(arg); } std::pair<typename Table::iterator, bool> emplace(const key_type && arg) { return Table::emplace(std::move(arg)); } std::pair<typename Table::iterator, bool> emplace(key_type && arg) { return Table::emplace(std::move(arg)); } friend bool operator==(const flat_hash_set & lhs, const flat_hash_set & rhs) { if (lhs.size() != rhs.size()) return false; for (const T & value : lhs) { if (rhs.find(value) == rhs.end()) return false; } return true; } friend bool operator!=(const flat_hash_set & lhs, const flat_hash_set & rhs) { return !(lhs == rhs); } }; template<typename T> struct power_of_two_std_hash : std::hash<T> { typedef ska::power_of_two_hash_policy hash_policy; }; } // end namespace ska // Copyright Malte Skarupke 2017. // Distributed under the Boost Software License, Version 1.0. // (See http://www.boost.org/LICENSE_1_0.txt) #include <cstdint> #include <cstddef> #include <cmath> #include <array> #include <algorithm> #include <iterator> #include <utility> #include <type_traits> namespace ska { namespace detailv10 { template<typename T, typename Allocator> struct sherwood_v10_entry { sherwood_v10_entry() { } ~sherwood_v10_entry() { } using EntryPointer = typename std::allocator_traits<typename std::allocator_traits<Allocator>::template rebind_alloc<sherwood_v10_entry>>::pointer; EntryPointer next = nullptr; union { T value; }; static EntryPointer * empty_pointer() { static EntryPointer result[3] = { EntryPointer(nullptr) + ptrdiff_t(1), nullptr, nullptr }; return result + 1; } }; using ska::detailv3::functor_storage; using ska::detailv3::KeyOrValueHasher; using ska::detailv3::KeyOrValueEquality; using ska::detailv3::AssignIfTrue; using ska::detailv3::HashPolicySelector; template<typename T, typename FindKey, typename ArgumentHash, typename Hasher, typename ArgumentEqual, typename Equal, typename ArgumentAlloc, typename EntryAlloc, typename BucketAllocator> class sherwood_v10_table : private EntryAlloc, private Hasher, private Equal, private BucketAllocator { using Entry = detailv10::sherwood_v10_entry<T, ArgumentAlloc>; using AllocatorTraits = std::allocator_traits<EntryAlloc>; using BucketAllocatorTraits = std::allocator_traits<BucketAllocator>; using EntryPointer = typename AllocatorTraits::pointer; struct convertible_to_iterator; public: using value_type = T; using size_type = size_t; using difference_type = std::ptrdiff_t; using hasher = ArgumentHash; using key_equal = ArgumentEqual; using allocator_type = EntryAlloc; using reference = value_type &; using const_reference = const value_type &; using pointer = value_type *; using const_pointer = const value_type *; sherwood_v10_table() { } explicit sherwood_v10_table(size_type bucket_count, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc()) : EntryAlloc(alloc), Hasher(hash), Equal(equal), BucketAllocator(alloc) { rehash(bucket_count); } sherwood_v10_table(size_type bucket_count, const ArgumentAlloc & alloc) : sherwood_v10_table(bucket_count, ArgumentHash(), ArgumentEqual(), alloc) { } sherwood_v10_table(size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc) : sherwood_v10_table(bucket_count, hash, ArgumentEqual(), alloc) { } explicit sherwood_v10_table(const ArgumentAlloc & alloc) : EntryAlloc(alloc), BucketAllocator(alloc) { } template<typename It> sherwood_v10_table(It first, It last, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc()) : sherwood_v10_table(bucket_count, hash, equal, alloc) { insert(first, last); } template<typename It> sherwood_v10_table(It first, It last, size_type bucket_count, const ArgumentAlloc & alloc) : sherwood_v10_table(first, last, bucket_count, ArgumentHash(), ArgumentEqual(), alloc) { } template<typename It> sherwood_v10_table(It first, It last, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc) : sherwood_v10_table(first, last, bucket_count, hash, ArgumentEqual(), alloc) { } sherwood_v10_table(std::initializer_list<T> il, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc()) : sherwood_v10_table(bucket_count, hash, equal, alloc) { if (bucket_count == 0) reserve(il.size()); insert(il.begin(), il.end()); } sherwood_v10_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentAlloc & alloc) : sherwood_v10_table(il, bucket_count, ArgumentHash(), ArgumentEqual(), alloc) { } sherwood_v10_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc) : sherwood_v10_table(il, bucket_count, hash, ArgumentEqual(), alloc) { } sherwood_v10_table(const sherwood_v10_table & other) : sherwood_v10_table(other, AllocatorTraits::select_on_container_copy_construction(other.get_allocator())) { } sherwood_v10_table(const sherwood_v10_table & other, const ArgumentAlloc & alloc) : EntryAlloc(alloc), Hasher(other), Equal(other), BucketAllocator(alloc), _max_load_factor(other._max_load_factor) { try { rehash_for_other_container(other); insert(other.begin(), other.end()); } catch(...) { clear(); deallocate_data(); throw; } } sherwood_v10_table(sherwood_v10_table && other) noexcept : EntryAlloc(std::move(other)), Hasher(std::move(other)), Equal(std::move(other)), BucketAllocator(std::move(other)), _max_load_factor(other._max_load_factor) { swap_pointers(other); } sherwood_v10_table(sherwood_v10_table && other, const ArgumentAlloc & alloc) noexcept : EntryAlloc(alloc), Hasher(std::move(other)), Equal(std::move(other)), BucketAllocator(alloc), _max_load_factor(other._max_load_factor) { swap_pointers(other); } sherwood_v10_table & operator=(const sherwood_v10_table & other) { if (this == std::addressof(other)) return *this; clear(); static_assert(AllocatorTraits::propagate_on_container_copy_assignment::value == BucketAllocatorTraits::propagate_on_container_copy_assignment::value, "The allocators have to behave the same way"); if (AllocatorTraits::propagate_on_container_copy_assignment::value) { if (static_cast<EntryAlloc &>(*this) != static_cast<const EntryAlloc &>(other) || static_cast<BucketAllocator &>(*this) != static_cast<const BucketAllocator &>(other)) { reset_to_empty_state(); } AssignIfTrue<EntryAlloc, AllocatorTraits::propagate_on_container_copy_assignment::value>()(*this, other); AssignIfTrue<BucketAllocator, BucketAllocatorTraits::propagate_on_container_copy_assignment::value>()(*this, other); } _max_load_factor = other._max_load_factor; static_cast<Hasher &>(*this) = other; static_cast<Equal &>(*this) = other; rehash_for_other_container(other); insert(other.begin(), other.end()); return *this; } sherwood_v10_table & operator=(sherwood_v10_table && other) noexcept { static_assert(AllocatorTraits::propagate_on_container_move_assignment::value == BucketAllocatorTraits::propagate_on_container_move_assignment::value, "The allocators have to behave the same way"); if (this == std::addressof(other)) return *this; else if (AllocatorTraits::propagate_on_container_move_assignment::value) { clear(); reset_to_empty_state(); AssignIfTrue<EntryAlloc, AllocatorTraits::propagate_on_container_move_assignment::value>()(*this, std::move(other)); AssignIfTrue<BucketAllocator, BucketAllocatorTraits::propagate_on_container_move_assignment::value>()(*this, std::move(other)); swap_pointers(other); } else if (static_cast<EntryAlloc &>(*this) == static_cast<EntryAlloc &>(other) && static_cast<BucketAllocator &>(*this) == static_cast<BucketAllocator &>(other)) { swap_pointers(other); } else { clear(); _max_load_factor = other._max_load_factor; rehash_for_other_container(other); for (T & elem : other) emplace(std::move(elem)); other.clear(); } static_cast<Hasher &>(*this) = std::move(other); static_cast<Equal &>(*this) = std::move(other); return *this; } ~sherwood_v10_table() { clear(); deallocate_data(); } const allocator_type & get_allocator() const { return static_cast<const allocator_type &>(*this); } const ArgumentEqual & key_eq() const { return static_cast<const ArgumentEqual &>(*this); } const ArgumentHash & hash_function() const { return static_cast<const ArgumentHash &>(*this); } template<typename ValueType> struct templated_iterator { templated_iterator() { } templated_iterator(EntryPointer element, EntryPointer * bucket) : current_element(element), current_bucket(bucket) { } EntryPointer current_element = nullptr; EntryPointer * current_bucket = nullptr; using iterator_category = std::forward_iterator_tag; using value_type = ValueType; using difference_type = ptrdiff_t; using pointer = ValueType *; using reference = ValueType &; friend bool operator==(const templated_iterator & lhs, const templated_iterator & rhs) { return lhs.current_element == rhs.current_element; } friend bool operator!=(const templated_iterator & lhs, const templated_iterator & rhs) { return !(lhs == rhs); } templated_iterator & operator++() { if (!current_element->next) { do { --current_bucket; } while (!*current_bucket); current_element = *current_bucket; } else current_element = current_element->next; return *this; } templated_iterator operator++(int) { templated_iterator copy(*this); ++*this; return copy; } ValueType & operator*() const { return current_element->value; } ValueType * operator->() const { return std::addressof(current_element->value); } operator templated_iterator<const value_type>() const { return { current_element, current_bucket }; } }; using iterator = templated_iterator<value_type>; using const_iterator = templated_iterator<const value_type>; iterator begin() { EntryPointer * end = entries - 1; for (EntryPointer * it = entries + num_slots_minus_one; it != end; --it) { if (*it) return { *it, it }; } return { *end, end }; } const_iterator begin() const { return const_cast<sherwood_v10_table *>(this)->begin(); } const_iterator cbegin() const { return begin(); } iterator end() { EntryPointer * end = entries - 1; return { *end, end }; } const_iterator end() const { EntryPointer * end = entries - 1; return { *end, end }; } const_iterator cend() const { return end(); } iterator find(const FindKey & key) { size_t index = hash_policy.index_for_hash(hash_object(key), num_slots_minus_one); EntryPointer * bucket = entries + ptrdiff_t(index); for (EntryPointer it = *bucket; it; it = it->next) { if (compares_equal(key, it->value)) return { it, bucket }; } return end(); } const_iterator find(const FindKey & key) const { return const_cast<sherwood_v10_table *>(this)->find(key); } size_t count(const FindKey & key) const { return find(key) == end() ? 0 : 1; } std::pair<iterator, iterator> equal_range(const FindKey & key) { iterator found = find(key); if (found == end()) return { found, found }; else return { found, std::next(found) }; } std::pair<const_iterator, const_iterator> equal_range(const FindKey & key) const { const_iterator found = find(key); if (found == end()) return { found, found }; else return { found, std::next(found) }; } template<typename Key, typename... Args> std::pair<iterator, bool> emplace(Key && key, Args &&... args) { size_t index = hash_policy.index_for_hash(hash_object(key), num_slots_minus_one); EntryPointer * bucket = entries + ptrdiff_t(index); for (EntryPointer it = *bucket; it; it = it->next) { if (compares_equal(key, it->value)) return { { it, bucket }, false }; } return emplace_new_key(bucket, std::forward<Key>(key), std::forward<Args>(args)...); } std::pair<iterator, bool> insert(const value_type & value) { return emplace(value); } std::pair<iterator, bool> insert(value_type && value) { return emplace(std::move(value)); } template<typename... Args> iterator emplace_hint(const_iterator, Args &&... args) { return emplace(std::forward<Args>(args)...).first; } iterator insert(const_iterator, const value_type & value) { return emplace(value).first; } iterator insert(const_iterator, value_type && value) { return emplace(std::move(value)).first; } template<typename It> void insert(It begin, It end) { for (; begin != end; ++begin) { emplace(*begin); } } void insert(std::initializer_list<value_type> il) { insert(il.begin(), il.end()); } void rehash(size_t num_buckets) { num_buckets = std::max(num_buckets, static_cast<size_t>(std::ceil(num_elements / static_cast<double>(_max_load_factor)))); if (num_buckets == 0) { reset_to_empty_state(); return; } auto new_prime_index = hash_policy.next_size_over(num_buckets); if (num_buckets == bucket_count()) return; EntryPointer * new_buckets(&*BucketAllocatorTraits::allocate(*this, num_buckets + 1)); EntryPointer * end_it = new_buckets + static_cast<ptrdiff_t>(num_buckets + 1); *new_buckets = EntryPointer(nullptr) + ptrdiff_t(1); ++new_buckets; std::fill(new_buckets, end_it, nullptr); std::swap(entries, new_buckets); std::swap(num_slots_minus_one, num_buckets); --num_slots_minus_one; hash_policy.commit(new_prime_index); if (!num_buckets) return; for (EntryPointer * it = new_buckets, * end = it + static_cast<ptrdiff_t>(num_buckets + 1); it != end; ++it) { for (EntryPointer e = *it; e;) { EntryPointer next = e->next; size_t index = hash_policy.index_for_hash(hash_object(e->value), num_slots_minus_one); EntryPointer & new_slot = entries[index]; e->next = new_slot; new_slot = e; e = next; } } BucketAllocatorTraits::deallocate(*this, new_buckets - 1, num_buckets + 2); } void reserve(size_t num_elements) { if (!num_elements) return; num_elements = static_cast<size_t>(std::ceil(num_elements / static_cast<double>(_max_load_factor))); if (num_elements > bucket_count()) rehash(num_elements); } // the return value is a type that can be converted to an iterator // the reason for doing this is that it's not free to find the // iterator pointing at the next element. if you care about the // next iterator, turn the return value into an iterator convertible_to_iterator erase(const_iterator to_erase) { --num_elements; AllocatorTraits::destroy(*this, std::addressof(to_erase.current_element->value)); EntryPointer * previous = to_erase.current_bucket; while (*previous != to_erase.current_element) { previous = &(*previous)->next; } *previous = to_erase.current_element->next; AllocatorTraits::deallocate(*this, to_erase.current_element, 1); return { *previous, to_erase.current_bucket }; } convertible_to_iterator erase(const_iterator begin_it, const_iterator end_it) { while (begin_it.current_bucket != end_it.current_bucket) { begin_it = erase(begin_it); } EntryPointer * bucket = begin_it.current_bucket; EntryPointer * previous = bucket; while (*previous != begin_it.current_element) previous = &(*previous)->next; while (*previous != end_it.current_element) { --num_elements; EntryPointer entry = *previous; AllocatorTraits::destroy(*this, std::addressof(entry->value)); *previous = entry->next; AllocatorTraits::deallocate(*this, entry, 1); } return { *previous, bucket }; } size_t erase(const FindKey & key) { auto found = find(key); if (found == end()) return 0; else { erase(found); return 1; } } void clear() { if (!num_slots_minus_one) return; for (EntryPointer * it = entries, * end = it + static_cast<ptrdiff_t>(num_slots_minus_one + 1); it != end; ++it) { for (EntryPointer e = *it; e;) { EntryPointer next = e->next; AllocatorTraits::destroy(*this, std::addressof(e->value)); AllocatorTraits::deallocate(*this, e, 1); e = next; } *it = nullptr; } num_elements = 0; } void swap(sherwood_v10_table & other) { using std::swap; swap_pointers(other); swap(static_cast<ArgumentHash &>(*this), static_cast<ArgumentHash &>(other)); swap(static_cast<ArgumentEqual &>(*this), static_cast<ArgumentEqual &>(other)); if (AllocatorTraits::propagate_on_container_swap::value) swap(static_cast<EntryAlloc &>(*this), static_cast<EntryAlloc &>(other)); if (BucketAllocatorTraits::propagate_on_container_swap::value) swap(static_cast<BucketAllocator &>(*this), static_cast<BucketAllocator &>(other)); } size_t size() const { return num_elements; } size_t max_size() const { return (AllocatorTraits::max_size(*this)) / sizeof(Entry); } size_t bucket_count() const { return num_slots_minus_one + 1; } size_type max_bucket_count() const { return (AllocatorTraits::max_size(*this) - 1) / sizeof(Entry); } size_t bucket(const FindKey & key) const { return hash_policy.template index_for_hash<0>(hash_object(key), num_slots_minus_one); } float load_factor() const { size_t buckets = bucket_count(); if (buckets) return static_cast<float>(num_elements) / bucket_count(); else return 0; } void max_load_factor(float value) { _max_load_factor = value; } float max_load_factor() const { return _max_load_factor; } bool empty() const { return num_elements == 0; } private: EntryPointer * entries = Entry::empty_pointer(); size_t num_slots_minus_one = 0; typename HashPolicySelector<ArgumentHash>::type hash_policy; float _max_load_factor = 1.0f; size_t num_elements = 0; void rehash_for_other_container(const sherwood_v10_table & other) { reserve(other.size()); } void swap_pointers(sherwood_v10_table & other) { using std::swap; swap(hash_policy, other.hash_policy); swap(entries, other.entries); swap(num_slots_minus_one, other.num_slots_minus_one); swap(num_elements, other.num_elements); swap(_max_load_factor, other._max_load_factor); } template<typename... Args> SKA_NOINLINE(std::pair<iterator, bool>) emplace_new_key(EntryPointer * bucket, Args &&... args) { using std::swap; if (is_full()) { grow(); return emplace(std::forward<Args>(args)...); } else { EntryPointer new_entry = AllocatorTraits::allocate(*this, 1); try { AllocatorTraits::construct(*this, std::addressof(new_entry->value), std::forward<Args>(args)...); } catch(...) { AllocatorTraits::deallocate(*this, new_entry, 1); throw; } ++num_elements; new_entry->next = *bucket; *bucket = new_entry; return { { new_entry, bucket }, true }; } } bool is_full() const { if (!num_slots_minus_one) return true; else return num_elements + 1 > (num_slots_minus_one + 1) * static_cast<double>(_max_load_factor); } void grow() { rehash(std::max(size_t(4), 2 * bucket_count())); } void deallocate_data() { if (entries != Entry::empty_pointer()) { BucketAllocatorTraits::deallocate(*this, entries - 1, num_slots_minus_one + 2); } } void reset_to_empty_state() { deallocate_data(); entries = Entry::empty_pointer(); num_slots_minus_one = 0; hash_policy.reset(); } template<typename U> size_t hash_object(const U & key) { return static_cast<Hasher &>(*this)(key); } template<typename U> size_t hash_object(const U & key) const { return static_cast<const Hasher &>(*this)(key); } template<typename L, typename R> bool compares_equal(const L & lhs, const R & rhs) { return static_cast<Equal &>(*this)(lhs, rhs); } struct convertible_to_iterator { EntryPointer element; EntryPointer * bucket; operator iterator() { if (element) return { element, bucket }; else { do { --bucket; } while (!*bucket); return { *bucket, bucket }; } } operator const_iterator() { if (element) return { element, bucket }; else { do { --bucket; } while (!*bucket); return { *bucket, bucket }; } } }; }; } template<typename K, typename V, typename H = std::hash<K>, typename E = std::equal_to<K>, typename A = std::allocator<std::pair<K, V> > > class unordered_map : public detailv10::sherwood_v10_table < std::pair<K, V>, K, H, detailv10::KeyOrValueHasher<K, std::pair<K, V>, H>, E, detailv10::KeyOrValueEquality<K, std::pair<K, V>, E>, A, typename std::allocator_traits<A>::template rebind_alloc<detailv10::sherwood_v10_entry<std::pair<K, V>, A>>, typename std::allocator_traits<A>::template rebind_alloc<typename std::allocator_traits<A>::template rebind_traits<detailv10::sherwood_v10_entry<std::pair<K, V>, A>>::pointer> > { using Table = detailv10::sherwood_v10_table < std::pair<K, V>, K, H, detailv10::KeyOrValueHasher<K, std::pair<K, V>, H>, E, detailv10::KeyOrValueEquality<K, std::pair<K, V>, E>, A, typename std::allocator_traits<A>::template rebind_alloc<detailv10::sherwood_v10_entry<std::pair<K, V>, A>>, typename std::allocator_traits<A>::template rebind_alloc<typename std::allocator_traits<A>::template rebind_traits<detailv10::sherwood_v10_entry<std::pair<K, V>, A>>::pointer> >; public: using key_type = K; using mapped_type = V; using Table::Table; unordered_map() { } V & operator[](const K & key) { return emplace(key, convertible_to_value()).first->second; } V & operator[](K && key) { return emplace(std::move(key), convertible_to_value()).first->second; } V & at(const K & key) { auto found = this->find(key); if (found == this->end()) throw std::out_of_range("Argument passed to at() was not in the map."); return found->second; } const V & at(const K & key) const { auto found = this->find(key); if (found == this->end()) throw std::out_of_range("Argument passed to at() was not in the map."); return found->second; } using Table::emplace; std::pair<typename Table::iterator, bool> emplace() { return emplace(key_type(), convertible_to_value()); } friend bool operator==(const unordered_map & lhs, const unordered_map & rhs) { if (lhs.size() != rhs.size()) return false; for (const typename Table::value_type & value : lhs) { auto found = rhs.find(value.first); if (found == rhs.end()) return false; else if (value.second != found->second) return false; } return true; } friend bool operator!=(const unordered_map & lhs, const unordered_map & rhs) { return !(lhs == rhs); } private: struct convertible_to_value { operator V() const { return V(); } }; }; template<typename T, typename H = std::hash<T>, typename E = std::equal_to<T>, typename A = std::allocator<T> > class unordered_set : public detailv10::sherwood_v10_table < T, T, H, detailv10::functor_storage<size_t, H>, E, detailv10::functor_storage<bool, E>, A, typename std::allocator_traits<A>::template rebind_alloc<detailv10::sherwood_v10_entry<T, A>>, typename std::allocator_traits<A>::template rebind_alloc<typename std::allocator_traits<A>::template rebind_traits<detailv10::sherwood_v10_entry<T, A>>::pointer> > { using Table = detailv10::sherwood_v10_table < T, T, H, detailv10::functor_storage<size_t, H>, E, detailv10::functor_storage<bool, E>, A, typename std::allocator_traits<A>::template rebind_alloc<detailv10::sherwood_v10_entry<T, A>>, typename std::allocator_traits<A>::template rebind_alloc<typename std::allocator_traits<A>::template rebind_traits<detailv10::sherwood_v10_entry<T, A>>::pointer> >; public: using key_type = T; using Table::Table; unordered_set() { } template<typename... Args> std::pair<typename Table::iterator, bool> emplace(Args &&... args) { return Table::emplace(T(std::forward<Args>(args)...)); } std::pair<typename Table::iterator, bool> emplace(const key_type & arg) { return Table::emplace(arg); } std::pair<typename Table::iterator, bool> emplace(key_type & arg) { return Table::emplace(arg); } std::pair<typename Table::iterator, bool> emplace(const key_type && arg) { return Table::emplace(std::move(arg)); } std::pair<typename Table::iterator, bool> emplace(key_type && arg) { return Table::emplace(std::move(arg)); } friend bool operator==(const unordered_set & lhs, const unordered_set & rhs) { if (lhs.size() != rhs.size()) return false; for (const T & value : lhs) { if (rhs.find(value) == rhs.end()) return false; } return true; } friend bool operator!=(const unordered_set & lhs, const unordered_set & rhs) { return !(lhs == rhs); } }; } // end namespace ska #include <cstdint> #include <cstddef> #include <cmath> #include <algorithm> #include <iterator> #include <utility> #include <type_traits> #include <vector> #include <array> namespace ska { namespace detailv8 { using ska::detailv3::functor_storage; using ska::detailv3::KeyOrValueHasher; using ska::detailv3::KeyOrValueEquality; using ska::detailv3::AssignIfTrue; using ska::detailv3::HashPolicySelector; template<typename = void> struct sherwood_v8_constants { static constexpr int8_t magic_for_empty = int8_t(0b11111111); static constexpr int8_t magic_for_reserved = int8_t(0b11111110); static constexpr int8_t bits_for_direct_hit = int8_t(0b10000000); static constexpr int8_t magic_for_direct_hit = int8_t(0b00000000); static constexpr int8_t magic_for_list_entry = int8_t(0b10000000); static constexpr int8_t bits_for_distance = int8_t(0b01111111); inline static int distance_from_metadata(int8_t metadata) { return metadata & bits_for_distance; } static constexpr int num_jump_distances = 126; // jump distances chosen like this: // 1. pick the first 16 integers to promote staying in the same block // 2. add the next 66 triangular numbers to get even jumps when // the hash table is a power of two // 3. add 44 more triangular numbers at a much steeper growth rate // to get a sequence that allows large jumps so that a table // with 10000 sequential numbers doesn't endlessly re-allocate static constexpr size_t jump_distances[num_jump_distances] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, 1326, 1378, 1431, 1485, 1540, 1596, 1653, 1711, 1770, 1830, 1891, 1953, 2016, 2080, 2145, 2211, 2278, 2346, 2415, 2485, 2556, 3741, 8385, 18915, 42486, 95703, 215496, 485605, 1091503, 2456436, 5529475, 12437578, 27986421, 62972253, 141700195, 318819126, 717314626, 1614000520, 3631437253, 8170829695, 18384318876, 41364501751, 93070021080, 209407709220, 471167588430, 1060127437995, 2385287281530, 5366895564381, 12075513791265, 27169907873235, 61132301007778, 137547673121001, 309482258302503, 696335090510256, 1566753939653640, 3525196427195653, 7931691866727775, 17846306747368716, 40154190394120111, 90346928493040500, 203280588949935750, 457381324898247375, 1029107980662394500, 2315492957028380766, 5209859150892887590, }; }; template<typename T> constexpr int8_t sherwood_v8_constants<T>::magic_for_empty; template<typename T> constexpr int8_t sherwood_v8_constants<T>::magic_for_reserved; template<typename T> constexpr int8_t sherwood_v8_constants<T>::bits_for_direct_hit; template<typename T> constexpr int8_t sherwood_v8_constants<T>::magic_for_direct_hit; template<typename T> constexpr int8_t sherwood_v8_constants<T>::magic_for_list_entry; template<typename T> constexpr int8_t sherwood_v8_constants<T>::bits_for_distance; template<typename T> constexpr int sherwood_v8_constants<T>::num_jump_distances; template<typename T> constexpr size_t sherwood_v8_constants<T>::jump_distances[num_jump_distances]; template<typename T, uint8_t BlockSize> struct sherwood_v8_block { sherwood_v8_block() { } ~sherwood_v8_block() { } int8_t control_bytes[BlockSize]; union { T data[BlockSize]; }; static sherwood_v8_block * empty_block() { static std::array<int8_t, BlockSize> empty_bytes = [] { std::array<int8_t, BlockSize> result; result.fill(sherwood_v8_constants<>::magic_for_empty); return result; }(); return reinterpret_cast<sherwood_v8_block *>(&empty_bytes); } int first_empty_index() const { for (int i = 0; i < BlockSize; ++i) { if (control_bytes[i] == sherwood_v8_constants<>::magic_for_empty) return i; } return -1; } void fill_control_bytes(int8_t value) { std::fill(std::begin(control_bytes), std::end(control_bytes), value); } }; template<typename T, typename FindKey, typename ArgumentHash, typename Hasher, typename ArgumentEqual, typename Equal, typename ArgumentAlloc, typename ByteAlloc, uint8_t BlockSize> class sherwood_v8_table : private ByteAlloc, private Hasher, private Equal { using AllocatorTraits = std::allocator_traits<ByteAlloc>; using BlockType = sherwood_v8_block<T, BlockSize>; using BlockPointer = BlockType *; using BytePointer = typename AllocatorTraits::pointer; struct convertible_to_iterator; using Constants = sherwood_v8_constants<>; public: using value_type = T; using size_type = size_t; using difference_type = std::ptrdiff_t; using hasher = ArgumentHash; using key_equal = ArgumentEqual; using allocator_type = ByteAlloc; using reference = value_type &; using const_reference = const value_type &; using pointer = value_type *; using const_pointer = const value_type *; sherwood_v8_table() { } explicit sherwood_v8_table(size_type bucket_count, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc()) : ByteAlloc(alloc), Hasher(hash), Equal(equal) { if (bucket_count) rehash(bucket_count); } sherwood_v8_table(size_type bucket_count, const ArgumentAlloc & alloc) : sherwood_v8_table(bucket_count, ArgumentHash(), ArgumentEqual(), alloc) { } sherwood_v8_table(size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc) : sherwood_v8_table(bucket_count, hash, ArgumentEqual(), alloc) { } explicit sherwood_v8_table(const ArgumentAlloc & alloc) : ByteAlloc(alloc) { } template<typename It> sherwood_v8_table(It first, It last, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc()) : sherwood_v8_table(bucket_count, hash, equal, alloc) { insert(first, last); } template<typename It> sherwood_v8_table(It first, It last, size_type bucket_count, const ArgumentAlloc & alloc) : sherwood_v8_table(first, last, bucket_count, ArgumentHash(), ArgumentEqual(), alloc) { } template<typename It> sherwood_v8_table(It first, It last, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc) : sherwood_v8_table(first, last, bucket_count, hash, ArgumentEqual(), alloc) { } sherwood_v8_table(std::initializer_list<T> il, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc()) : sherwood_v8_table(bucket_count, hash, equal, alloc) { if (bucket_count == 0) rehash(il.size()); insert(il.begin(), il.end()); } sherwood_v8_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentAlloc & alloc) : sherwood_v8_table(il, bucket_count, ArgumentHash(), ArgumentEqual(), alloc) { } sherwood_v8_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc) : sherwood_v8_table(il, bucket_count, hash, ArgumentEqual(), alloc) { } sherwood_v8_table(const sherwood_v8_table & other) : sherwood_v8_table(other, AllocatorTraits::select_on_container_copy_construction(other.get_allocator())) { } sherwood_v8_table(const sherwood_v8_table & other, const ArgumentAlloc & alloc) : ByteAlloc(alloc), Hasher(other), Equal(other), _max_load_factor(other._max_load_factor) { rehash_for_other_container(other); try { insert(other.begin(), other.end()); } catch(...) { clear(); deallocate_data(entries, num_slots_minus_one); throw; } } sherwood_v8_table(sherwood_v8_table && other) noexcept : ByteAlloc(std::move(other)), Hasher(std::move(other)), Equal(std::move(other)) , _max_load_factor(other._max_load_factor) { swap_pointers(other); } sherwood_v8_table(sherwood_v8_table && other, const ArgumentAlloc & alloc) noexcept : ByteAlloc(alloc), Hasher(std::move(other)), Equal(std::move(other)) , _max_load_factor(other._max_load_factor) { swap_pointers(other); } sherwood_v8_table & operator=(const sherwood_v8_table & other) { if (this == std::addressof(other)) return *this; clear(); if (AllocatorTraits::propagate_on_container_copy_assignment::value) { if (static_cast<ByteAlloc &>(*this) != static_cast<const ByteAlloc &>(other)) { reset_to_empty_state(); } AssignIfTrue<ByteAlloc, AllocatorTraits::propagate_on_container_copy_assignment::value>()(*this, other); } _max_load_factor = other._max_load_factor; static_cast<Hasher &>(*this) = other; static_cast<Equal &>(*this) = other; rehash_for_other_container(other); insert(other.begin(), other.end()); return *this; } sherwood_v8_table & operator=(sherwood_v8_table && other) noexcept { if (this == std::addressof(other)) return *this; else if (AllocatorTraits::propagate_on_container_move_assignment::value) { clear(); reset_to_empty_state(); AssignIfTrue<ByteAlloc, AllocatorTraits::propagate_on_container_move_assignment::value>()(*this, std::move(other)); swap_pointers(other); } else if (static_cast<ByteAlloc &>(*this) == static_cast<ByteAlloc &>(other)) { swap_pointers(other); } else { clear(); _max_load_factor = other._max_load_factor; rehash_for_other_container(other); for (T & elem : other) emplace(std::move(elem)); other.clear(); } static_cast<Hasher &>(*this) = std::move(other); static_cast<Equal &>(*this) = std::move(other); return *this; } ~sherwood_v8_table() { clear(); deallocate_data(entries, num_slots_minus_one); } const allocator_type & get_allocator() const { return static_cast<const allocator_type &>(*this); } const ArgumentEqual & key_eq() const { return static_cast<const ArgumentEqual &>(*this); } const ArgumentHash & hash_function() const { return static_cast<const ArgumentHash &>(*this); } template<typename ValueType> struct templated_iterator { private: friend class sherwood_v8_table; BlockPointer current = BlockPointer(); size_t index = 0; public: templated_iterator() { } templated_iterator(BlockPointer entries, size_t index) : current(entries) , index(index) { } using iterator_category = std::forward_iterator_tag; using value_type = ValueType; using difference_type = ptrdiff_t; using pointer = ValueType *; using reference = ValueType &; friend bool operator==(const templated_iterator & lhs, const templated_iterator & rhs) { return lhs.index == rhs.index; } friend bool operator!=(const templated_iterator & lhs, const templated_iterator & rhs) { return !(lhs == rhs); } templated_iterator & operator++() { do { if (index % BlockSize == 0) --current; if (index-- == 0) break; } while(current->control_bytes[index % BlockSize] == Constants::magic_for_empty); return *this; } templated_iterator operator++(int) { templated_iterator copy(*this); ++*this; return copy; } ValueType & operator*() const { return current->data[index % BlockSize]; } ValueType * operator->() const { return current->data + index % BlockSize; } operator templated_iterator<const value_type>() const { return { current, index }; } }; using iterator = templated_iterator<value_type>; using const_iterator = templated_iterator<const value_type>; iterator begin() { size_t num_slots = num_slots_minus_one ? num_slots_minus_one + 1 : 0; return ++iterator{ entries + num_slots / BlockSize, num_slots }; } const_iterator begin() const { size_t num_slots = num_slots_minus_one ? num_slots_minus_one + 1 : 0; return ++iterator{ entries + num_slots / BlockSize, num_slots }; } const_iterator cbegin() const { return begin(); } iterator end() { return { entries - 1, std::numeric_limits<size_t>::max() }; } const_iterator end() const { return { entries - 1, std::numeric_limits<size_t>::max() }; } const_iterator cend() const { return end(); } inline iterator find(const FindKey & key) { size_t index = hash_object(key); size_t num_slots_minus_one = this->num_slots_minus_one; BlockPointer entries = this->entries; index = hash_policy.index_for_hash(index, num_slots_minus_one); bool first = true; for (;;) { size_t block_index = index / BlockSize; int index_in_block = index % BlockSize; BlockPointer block = entries + block_index; int8_t metadata = block->control_bytes[index_in_block]; if (first) { if ((metadata & Constants::bits_for_direct_hit) != Constants::magic_for_direct_hit) return end(); first = false; } if (compares_equal(key, block->data[index_in_block])) return { block, index }; int8_t to_next_index = metadata & Constants::bits_for_distance; if (to_next_index == 0) return end(); index += Constants::jump_distances[to_next_index]; index = hash_policy.keep_in_range(index, num_slots_minus_one); } } inline const_iterator find(const FindKey & key) const { return const_cast<sherwood_v8_table *>(this)->find(key); } size_t count(const FindKey & key) const { return find(key) == end() ? 0 : 1; } std::pair<iterator, iterator> equal_range(const FindKey & key) { iterator found = find(key); if (found == end()) return { found, found }; else return { found, std::next(found) }; } std::pair<const_iterator, const_iterator> equal_range(const FindKey & key) const { const_iterator found = find(key); if (found == end()) return { found, found }; else return { found, std::next(found) }; } template<typename Key, typename... Args> inline std::pair<iterator, bool> emplace(Key && key, Args &&... args) { size_t index = hash_object(key); size_t num_slots_minus_one = this->num_slots_minus_one; BlockPointer entries = this->entries; index = hash_policy.index_for_hash(index, num_slots_minus_one); bool first = true; for (;;) { size_t block_index = index / BlockSize; int index_in_block = index % BlockSize; BlockPointer block = entries + block_index; int8_t metadata = block->control_bytes[index_in_block]; if (first) { if ((metadata & Constants::bits_for_direct_hit) != Constants::magic_for_direct_hit) return emplace_direct_hit({ index, block }, std::forward<Key>(key), std::forward<Args>(args)...); first = false; } if (compares_equal(key, block->data[index_in_block])) return { { block, index }, false }; int8_t to_next_index = metadata & Constants::bits_for_distance; if (to_next_index == 0) return emplace_new_key({ index, block }, std::forward<Key>(key), std::forward<Args>(args)...); index += Constants::jump_distances[to_next_index]; index = hash_policy.keep_in_range(index, num_slots_minus_one); } } std::pair<iterator, bool> insert(const value_type & value) { return emplace(value); } std::pair<iterator, bool> insert(value_type && value) { return emplace(std::move(value)); } template<typename... Args> iterator emplace_hint(const_iterator, Args &&... args) { return emplace(std::forward<Args>(args)...).first; } iterator insert(const_iterator, const value_type & value) { return emplace(value).first; } iterator insert(const_iterator, value_type && value) { return emplace(std::move(value)).first; } template<typename It> void insert(It begin, It end) { for (; begin != end; ++begin) { emplace(*begin); } } void insert(std::initializer_list<value_type> il) { insert(il.begin(), il.end()); } void rehash(size_t num_items) { num_items = std::max(num_items, static_cast<size_t>(std::ceil(num_elements / static_cast<double>(_max_load_factor)))); if (num_items == 0) { reset_to_empty_state(); return; } auto new_prime_index = hash_policy.next_size_over(num_items); if (num_items == num_slots_minus_one + 1) return; size_t num_blocks = num_items / BlockSize; if (num_items % BlockSize) ++num_blocks; size_t memory_requirement = calculate_memory_requirement(num_blocks); unsigned char * new_memory = &*AllocatorTraits::allocate(*this, memory_requirement); BlockPointer new_buckets = reinterpret_cast<BlockPointer>(new_memory); BlockPointer special_end_item = new_buckets + num_blocks; for (BlockPointer it = new_buckets; it <= special_end_item; ++it) it->fill_control_bytes(Constants::magic_for_empty); using std::swap; swap(entries, new_buckets); swap(num_slots_minus_one, num_items); --num_slots_minus_one; hash_policy.commit(new_prime_index); num_elements = 0; if (num_items) ++num_items; size_t old_num_blocks = num_items / BlockSize; if (num_items % BlockSize) ++old_num_blocks; for (BlockPointer it = new_buckets, end = new_buckets + old_num_blocks; it != end; ++it) { for (int i = 0; i < BlockSize; ++i) { int8_t metadata = it->control_bytes[i]; if (metadata != Constants::magic_for_empty && metadata != Constants::magic_for_reserved) { emplace(std::move(it->data[i])); AllocatorTraits::destroy(*this, it->data + i); } } } deallocate_data(new_buckets, num_items - 1); } void reserve(size_t num_elements) { size_t required_buckets = num_buckets_for_reserve(num_elements); if (required_buckets > bucket_count()) rehash(required_buckets); } // the return value is a type that can be converted to an iterator // the reason for doing this is that it's not free to find the // iterator pointing at the next element. if you care about the // next iterator, turn the return value into an iterator convertible_to_iterator erase(const_iterator to_erase) { LinkedListIt current = { to_erase.index, to_erase.current }; if (current.has_next()) { LinkedListIt previous = current; LinkedListIt next = current.next(*this); while (next.has_next()) { previous = next; next = next.next(*this); } AllocatorTraits::destroy(*this, std::addressof(*current)); AllocatorTraits::construct(*this, std::addressof(*current), std::move(*next)); AllocatorTraits::destroy(*this, std::addressof(*next)); next.set_metadata(Constants::magic_for_empty); previous.clear_next(); } else { if (!current.is_direct_hit()) find_parent_block(current).clear_next(); AllocatorTraits::destroy(*this, std::addressof(*current)); current.set_metadata(Constants::magic_for_empty); } --num_elements; return { to_erase.current, to_erase.index }; } iterator erase(const_iterator begin_it, const_iterator end_it) { if (begin_it == end_it) return { begin_it.current, begin_it.index }; if (std::next(begin_it) == end_it) return erase(begin_it); if (begin_it == begin() && end_it == end()) { clear(); return { end_it.current, end_it.index }; } std::vector<std::pair<int, LinkedListIt>> depth_in_chain; for (const_iterator it = begin_it; it != end_it; ++it) { LinkedListIt list_it(it.index, it.current); if (list_it.is_direct_hit()) depth_in_chain.emplace_back(0, list_it); else { LinkedListIt root = find_direct_hit(list_it); int distance = 1; for (;;) { LinkedListIt next = root.next(*this); if (next == list_it) break; ++distance; root = next; } depth_in_chain.emplace_back(distance, list_it); } } std::sort(depth_in_chain.begin(), depth_in_chain.end(), [](const auto & a, const auto & b) { return a.first < b.first; }); for (auto it = depth_in_chain.rbegin(), end = depth_in_chain.rend(); it != end; ++it) { erase(it->second.it()); } if (begin_it.current->control_bytes[begin_it.index % BlockSize] == Constants::magic_for_empty) return ++iterator{ begin_it.current, begin_it.index }; else return { begin_it.current, begin_it.index }; } size_t erase(const FindKey & key) { auto found = find(key); if (found == end()) return 0; else { erase(found); return 1; } } void clear() { if (!num_slots_minus_one) return; size_t num_slots = num_slots_minus_one + 1; size_t num_blocks = num_slots / BlockSize; if (num_slots % BlockSize) ++num_blocks; for (BlockPointer it = entries, end = it + num_blocks; it != end; ++it) { for (int i = 0; i < BlockSize; ++i) { if (it->control_bytes[i] != Constants::magic_for_empty) { AllocatorTraits::destroy(*this, std::addressof(it->data[i])); it->control_bytes[i] = Constants::magic_for_empty; } } } num_elements = 0; } void shrink_to_fit() { rehash_for_other_container(*this); } void swap(sherwood_v8_table & other) { using std::swap; swap_pointers(other); swap(static_cast<ArgumentHash &>(*this), static_cast<ArgumentHash &>(other)); swap(static_cast<ArgumentEqual &>(*this), static_cast<ArgumentEqual &>(other)); if (AllocatorTraits::propagate_on_container_swap::value) swap(static_cast<ByteAlloc &>(*this), static_cast<ByteAlloc &>(other)); } size_t size() const { return num_elements; } size_t max_size() const { return (AllocatorTraits::max_size(*this)) / sizeof(T); } size_t bucket_count() const { return num_slots_minus_one ? num_slots_minus_one + 1 : 0; } size_type max_bucket_count() const { return (AllocatorTraits::max_size(*this)) / sizeof(T); } size_t bucket(const FindKey & key) const { return hash_policy.index_for_hash(hash_object(key), num_slots_minus_one); } float load_factor() const { return static_cast<double>(num_elements) / (num_slots_minus_one + 1); } void max_load_factor(float value) { _max_load_factor = value; } float max_load_factor() const { return _max_load_factor; } bool empty() const { return num_elements == 0; } private: BlockPointer entries = BlockType::empty_block(); size_t num_slots_minus_one = 0; typename HashPolicySelector<ArgumentHash>::type hash_policy; float _max_load_factor = 0.9375f; size_t num_elements = 0; size_t num_buckets_for_reserve(size_t num_elements) const { return static_cast<size_t>(std::ceil(num_elements / static_cast<double>(_max_load_factor))); } void rehash_for_other_container(const sherwood_v8_table & other) { rehash(std::min(num_buckets_for_reserve(other.size()), other.bucket_count())); } bool is_full() const { if (!num_slots_minus_one) return true; else return num_elements + 1 > (num_slots_minus_one + 1) * static_cast<double>(_max_load_factor); } void swap_pointers(sherwood_v8_table & other) { using std::swap; swap(hash_policy, other.hash_policy); swap(entries, other.entries); swap(num_slots_minus_one, other.num_slots_minus_one); swap(num_elements, other.num_elements); swap(_max_load_factor, other._max_load_factor); } struct LinkedListIt { size_t index = 0; BlockPointer block = nullptr; LinkedListIt() { } LinkedListIt(size_t index, BlockPointer block) : index(index), block(block) { } iterator it() const { return { block, index }; } int index_in_block() const { return index % BlockSize; } bool is_direct_hit() const { return (metadata() & Constants::bits_for_direct_hit) == Constants::magic_for_direct_hit; } bool is_empty() const { return metadata() == Constants::magic_for_empty; } bool has_next() const { return jump_index() != 0; } int8_t jump_index() const { return Constants::distance_from_metadata(metadata()); } int8_t metadata() const { return block->control_bytes[index_in_block()]; } void set_metadata(int8_t metadata) { block->control_bytes[index_in_block()] = metadata; } LinkedListIt next(sherwood_v8_table & table) const { int8_t distance = jump_index(); size_t next_index = table.hash_policy.keep_in_range(index + Constants::jump_distances[distance], table.num_slots_minus_one); return { next_index, table.entries + next_index / BlockSize }; } void set_next(int8_t jump_index) { int8_t & metadata = block->control_bytes[index_in_block()]; metadata = (metadata & ~Constants::bits_for_distance) | jump_index; } void clear_next() { set_next(0); } value_type & operator*() const { return block->data[index_in_block()]; } bool operator!() const { return !block; } explicit operator bool() const { return block != nullptr; } bool operator==(const LinkedListIt & other) const { return index == other.index; } bool operator!=(const LinkedListIt & other) const { return !(*this == other); } }; template<typename... Args> SKA_NOINLINE(std::pair<iterator, bool>) emplace_direct_hit(LinkedListIt block, Args &&... args) { using std::swap; if (is_full()) { grow(); return emplace(std::forward<Args>(args)...); } if (block.metadata() == Constants::magic_for_empty) { AllocatorTraits::construct(*this, std::addressof(*block), std::forward<Args>(args)...); block.set_metadata(Constants::magic_for_direct_hit); ++num_elements; return { block.it(), true }; } else { LinkedListIt parent_block = find_parent_block(block); std::pair<int8_t, LinkedListIt> free_block = find_free_index(parent_block); if (!free_block.first) { grow(); return emplace(std::forward<Args>(args)...); } value_type new_value(std::forward<Args>(args)...); for (LinkedListIt it = block;;) { AllocatorTraits::construct(*this, std::addressof(*free_block.second), std::move(*it)); AllocatorTraits::destroy(*this, std::addressof(*it)); parent_block.set_next(free_block.first); free_block.second.set_metadata(Constants::magic_for_list_entry); if (!it.has_next()) { it.set_metadata(Constants::magic_for_empty); break; } LinkedListIt next = it.next(*this); it.set_metadata(Constants::magic_for_empty); block.set_metadata(Constants::magic_for_reserved); it = next; parent_block = free_block.second; free_block = find_free_index(free_block.second); if (!free_block.first) { grow(); return emplace(std::move(new_value)); } } AllocatorTraits::construct(*this, std::addressof(*block), std::move(new_value)); block.set_metadata(Constants::magic_for_direct_hit); ++num_elements; return { block.it(), true }; } } template<typename... Args> SKA_NOINLINE(std::pair<iterator, bool>) emplace_new_key(LinkedListIt parent, Args &&... args) { if (is_full()) { grow(); return emplace(std::forward<Args>(args)...); } std::pair<int8_t, LinkedListIt> free_block = find_free_index(parent); if (!free_block.first) { grow(); return emplace(std::forward<Args>(args)...); } AllocatorTraits::construct(*this, std::addressof(*free_block.second), std::forward<Args>(args)...); free_block.second.set_metadata(Constants::magic_for_list_entry); parent.set_next(free_block.first); ++num_elements; return { free_block.second.it(), true }; } LinkedListIt find_direct_hit(LinkedListIt child) const { size_t to_move_hash = hash_object(*child); size_t to_move_index = hash_policy.index_for_hash(to_move_hash, num_slots_minus_one); return { to_move_index, entries + to_move_index / BlockSize }; } LinkedListIt find_parent_block(LinkedListIt child) { LinkedListIt parent_block = find_direct_hit(child); for (;;) { LinkedListIt next = parent_block.next(*this); if (next == child) return parent_block; parent_block = next; } } std::pair<int8_t, LinkedListIt> find_free_index(LinkedListIt parent) const { for (int8_t jump_index = 1; jump_index < Constants::num_jump_distances; ++jump_index) { size_t index = hash_policy.keep_in_range(parent.index + Constants::jump_distances[jump_index], num_slots_minus_one); BlockPointer block = entries + index / BlockSize; if (block->control_bytes[index % BlockSize] == Constants::magic_for_empty) return { jump_index, { index, block } }; } return { 0, {} }; } void grow() { rehash(std::max(size_t(10), 2 * bucket_count())); } size_t calculate_memory_requirement(size_t num_blocks) { size_t memory_required = sizeof(BlockType) * num_blocks; memory_required += BlockSize; // for metadata of past-the-end pointer return memory_required; } void deallocate_data(BlockPointer begin, size_t num_slots_minus_one) { if (begin == BlockType::empty_block()) return; ++num_slots_minus_one; size_t num_blocks = num_slots_minus_one / BlockSize; if (num_slots_minus_one % BlockSize) ++num_blocks; size_t memory = calculate_memory_requirement(num_blocks); unsigned char * as_byte_pointer = reinterpret_cast<unsigned char *>(begin); AllocatorTraits::deallocate(*this, typename AllocatorTraits::pointer(as_byte_pointer), memory); } void reset_to_empty_state() { deallocate_data(entries, num_slots_minus_one); entries = BlockType::empty_block(); num_slots_minus_one = 0; hash_policy.reset(); } template<typename U> size_t hash_object(const U & key) { return static_cast<Hasher &>(*this)(key); } template<typename U> size_t hash_object(const U & key) const { return static_cast<const Hasher &>(*this)(key); } template<typename L, typename R> bool compares_equal(const L & lhs, const R & rhs) { return static_cast<Equal &>(*this)(lhs, rhs); } struct convertible_to_iterator { BlockPointer it; size_t index; operator iterator() { if (it->control_bytes[index % BlockSize] == Constants::magic_for_empty) return ++iterator{it, index}; else return { it, index }; } operator const_iterator() { if (it->control_bytes[index % BlockSize] == Constants::magic_for_empty) return ++iterator{it, index}; else return { it, index }; } }; }; template<typename T, typename Enable = void> struct AlignmentOr8Bytes { static constexpr size_t value = 8; }; template<typename T> struct AlignmentOr8Bytes<T, typename std::enable_if<alignof(T) >= 1>::type> { static constexpr size_t value = alignof(T); }; template<typename... Args> struct CalculateBytellBlockSize; template<typename First, typename... More> struct CalculateBytellBlockSize<First, More...> { static constexpr size_t this_value = AlignmentOr8Bytes<First>::value; static constexpr size_t base_value = CalculateBytellBlockSize<More...>::value; static constexpr size_t value = this_value > base_value ? this_value : base_value; }; template<> struct CalculateBytellBlockSize<> { static constexpr size_t value = 8; }; } template<typename K, typename V, typename H = std::hash<K>, typename E = std::equal_to<K>, typename A = std::allocator<std::pair<K, V> > > class bytell_hash_map : public detailv8::sherwood_v8_table < std::pair<K, V>, K, H, detailv8::KeyOrValueHasher<K, std::pair<K, V>, H>, E, detailv8::KeyOrValueEquality<K, std::pair<K, V>, E>, A, typename std::allocator_traits<A>::template rebind_alloc<unsigned char>, detailv8::CalculateBytellBlockSize<K, V>::value > { using Table = detailv8::sherwood_v8_table < std::pair<K, V>, K, H, detailv8::KeyOrValueHasher<K, std::pair<K, V>, H>, E, detailv8::KeyOrValueEquality<K, std::pair<K, V>, E>, A, typename std::allocator_traits<A>::template rebind_alloc<unsigned char>, detailv8::CalculateBytellBlockSize<K, V>::value >; public: using key_type = K; using mapped_type = V; using Table::Table; bytell_hash_map() { } inline V & operator[](const K & key) { return emplace(key, convertible_to_value()).first->second; } inline V & operator[](K && key) { return emplace(std::move(key), convertible_to_value()).first->second; } V & at(const K & key) { auto found = this->find(key); if (found == this->end()) throw std::out_of_range("Argument passed to at() was not in the map."); return found->second; } const V & at(const K & key) const { auto found = this->find(key); if (found == this->end()) throw std::out_of_range("Argument passed to at() was not in the map."); return found->second; } using Table::emplace; std::pair<typename Table::iterator, bool> emplace() { return emplace(key_type(), convertible_to_value()); } template<typename M> std::pair<typename Table::iterator, bool> insert_or_assign(const key_type & key, M && m) { auto emplace_result = emplace(key, std::forward<M>(m)); if (!emplace_result.second) emplace_result.first->second = std::forward<M>(m); return emplace_result; } template<typename M> std::pair<typename Table::iterator, bool> insert_or_assign(key_type && key, M && m) { auto emplace_result = emplace(std::move(key), std::forward<M>(m)); if (!emplace_result.second) emplace_result.first->second = std::forward<M>(m); return emplace_result; } template<typename M> typename Table::iterator insert_or_assign(typename Table::const_iterator, const key_type & key, M && m) { return insert_or_assign(key, std::forward<M>(m)).first; } template<typename M> typename Table::iterator insert_or_assign(typename Table::const_iterator, key_type && key, M && m) { return insert_or_assign(std::move(key), std::forward<M>(m)).first; } friend bool operator==(const bytell_hash_map & lhs, const bytell_hash_map & rhs) { if (lhs.size() != rhs.size()) return false; for (const typename Table::value_type & value : lhs) { auto found = rhs.find(value.first); if (found == rhs.end()) return false; else if (value.second != found->second) return false; } return true; } friend bool operator!=(const bytell_hash_map & lhs, const bytell_hash_map & rhs) { return !(lhs == rhs); } private: struct convertible_to_value { operator V() const { return V(); } }; }; template<typename T, typename H = std::hash<T>, typename E = std::equal_to<T>, typename A = std::allocator<T> > class bytell_hash_set : public detailv8::sherwood_v8_table < T, T, H, detailv8::functor_storage<size_t, H>, E, detailv8::functor_storage<bool, E>, A, typename std::allocator_traits<A>::template rebind_alloc<unsigned char>, detailv8::CalculateBytellBlockSize<T>::value > { using Table = detailv8::sherwood_v8_table < T, T, H, detailv8::functor_storage<size_t, H>, E, detailv8::functor_storage<bool, E>, A, typename std::allocator_traits<A>::template rebind_alloc<unsigned char>, detailv8::CalculateBytellBlockSize<T>::value >; public: using key_type = T; using Table::Table; bytell_hash_set() { } template<typename... Args> std::pair<typename Table::iterator, bool> emplace(Args &&... args) { return Table::emplace(T(std::forward<Args>(args)...)); } std::pair<typename Table::iterator, bool> emplace(const key_type & arg) { return Table::emplace(arg); } std::pair<typename Table::iterator, bool> emplace(key_type & arg) { return Table::emplace(arg); } std::pair<typename Table::iterator, bool> emplace(const key_type && arg) { return Table::emplace(std::move(arg)); } std::pair<typename Table::iterator, bool> emplace(key_type && arg) { return Table::emplace(std::move(arg)); } friend bool operator==(const bytell_hash_set & lhs, const bytell_hash_set & rhs) { if (lhs.size() != rhs.size()) return false; for (const T & value : lhs) { if (rhs.find(value) == rhs.end()) return false; } return true; } friend bool operator!=(const bytell_hash_set & lhs, const bytell_hash_set & rhs) { return !(lhs == rhs); } }; } // end namespace ska // ______ _____ ______ _________ // ______________ ___ /_ ___(_)_______ ___ /_ ______ ______ ______ / // __ ___/_ __ \__ __ \__ / __ __ \ __ __ \_ __ \_ __ \_ __ / // _ / / /_/ /_ /_/ /_ / _ / / / _ / / // /_/ // /_/ // /_/ / // /_/ \____/ /_.___/ /_/ /_/ /_/ ________/_/ /_/ \____/ \____/ \__,_/ // _/_____/ // // Fast & memory efficient hashtable based on robin hood hashing for C++11/14/17/20 // https://github.com/martinus/robin-hood-hashing // // Licensed under the MIT License <http://opensource.org/licenses/MIT>. // SPDX-License-Identifier: MIT // Copyright (c) 2018-2021 Martin Ankerl <http://martin.ankerl.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 ROBIN_HOOD_H_INCLUDED #define ROBIN_HOOD_H_INCLUDED // see https://semver.org/ #define ROBIN_HOOD_VERSION_MAJOR 3 // for incompatible API changes #define ROBIN_HOOD_VERSION_MINOR 11 // for adding functionality in a backwards-compatible manner #define ROBIN_HOOD_VERSION_PATCH 4 // for backwards-compatible bug fixes #include <algorithm> #include <cstdlib> #include <cstring> #include <functional> #include <limits> #include <memory> // only to support hash of smart pointers #include <stdexcept> #include <string> #include <type_traits> #include <utility> #if __cplusplus >= 201703L # include <string_view> #endif // #define ROBIN_HOOD_LOG_ENABLED #ifdef ROBIN_HOOD_LOG_ENABLED # include <iostream> # define ROBIN_HOOD_LOG(...) \ std::cout << __FUNCTION__ << "@" << __LINE__ << ": " << __VA_ARGS__ << std::endl; #else # define ROBIN_HOOD_LOG(x) #endif // #define ROBIN_HOOD_TRACE_ENABLED #ifdef ROBIN_HOOD_TRACE_ENABLED # include <iostream> # define ROBIN_HOOD_TRACE(...) \ std::cout << __FUNCTION__ << "@" << __LINE__ << ": " << __VA_ARGS__ << std::endl; #else # define ROBIN_HOOD_TRACE(x) #endif // #define ROBIN_HOOD_COUNT_ENABLED #ifdef ROBIN_HOOD_COUNT_ENABLED # include <iostream> # define ROBIN_HOOD_COUNT(x) ++counts().x; namespace robin_hood { struct Counts { uint64_t shiftUp{}; uint64_t shiftDown{}; }; inline std::ostream& operator<<(std::ostream& os, Counts const& c) { return os << c.shiftUp << " shiftUp" << std::endl << c.shiftDown << " shiftDown" << std::endl; } static Counts& counts() { static Counts counts{}; return counts; } } // namespace robin_hood #else # define ROBIN_HOOD_COUNT(x) #endif // all non-argument macros should use this facility. See // https://www.fluentcpp.com/2019/05/28/better-macros-better-flags/ #define ROBIN_HOOD(x) ROBIN_HOOD_PRIVATE_DEFINITION_##x() // mark unused members with this macro #define ROBIN_HOOD_UNUSED(identifier) // bitness #if SIZE_MAX == UINT32_MAX # define ROBIN_HOOD_PRIVATE_DEFINITION_BITNESS() 32 #elif SIZE_MAX == UINT64_MAX # define ROBIN_HOOD_PRIVATE_DEFINITION_BITNESS() 64 #else # error Unsupported bitness #endif // endianess #ifdef _MSC_VER # define ROBIN_HOOD_PRIVATE_DEFINITION_LITTLE_ENDIAN() 1 # define ROBIN_HOOD_PRIVATE_DEFINITION_BIG_ENDIAN() 0 #else # define ROBIN_HOOD_PRIVATE_DEFINITION_LITTLE_ENDIAN() \ (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) # define ROBIN_HOOD_PRIVATE_DEFINITION_BIG_ENDIAN() (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) #endif // inline #ifdef _MSC_VER # define ROBIN_HOOD_PRIVATE_DEFINITION_NOINLINE() __declspec(noinline) #else # define ROBIN_HOOD_PRIVATE_DEFINITION_NOINLINE() __attribute__((noinline)) #endif // exceptions #if !defined(__cpp_exceptions) && !defined(__EXCEPTIONS) && !defined(_CPPUNWIND) # define ROBIN_HOOD_PRIVATE_DEFINITION_HAS_EXCEPTIONS() 0 #else # define ROBIN_HOOD_PRIVATE_DEFINITION_HAS_EXCEPTIONS() 1 #endif // count leading/trailing bits #if !defined(ROBIN_HOOD_DISABLE_INTRINSICS) # ifdef _MSC_VER # if ROBIN_HOOD(BITNESS) == 32 # define ROBIN_HOOD_PRIVATE_DEFINITION_BITSCANFORWARD() _BitScanForward # else # define ROBIN_HOOD_PRIVATE_DEFINITION_BITSCANFORWARD() _BitScanForward64 # endif # include <intrin.h> # pragma intrinsic(ROBIN_HOOD(BITSCANFORWARD)) # define ROBIN_HOOD_COUNT_TRAILING_ZEROES(x) \ [](size_t mask) noexcept -> int { \ unsigned long index; \ return ROBIN_HOOD(BITSCANFORWARD)(&index, mask) ? static_cast<int>(index) \ : ROBIN_HOOD(BITNESS); \ }(x) # else # if ROBIN_HOOD(BITNESS) == 32 # define ROBIN_HOOD_PRIVATE_DEFINITION_CTZ() __builtin_ctzl # define ROBIN_HOOD_PRIVATE_DEFINITION_CLZ() __builtin_clzl # else # define ROBIN_HOOD_PRIVATE_DEFINITION_CTZ() __builtin_ctzll # define ROBIN_HOOD_PRIVATE_DEFINITION_CLZ() __builtin_clzll # endif # define ROBIN_HOOD_COUNT_LEADING_ZEROES(x) ((x) ? ROBIN_HOOD(CLZ)(x) : ROBIN_HOOD(BITNESS)) # define ROBIN_HOOD_COUNT_TRAILING_ZEROES(x) ((x) ? ROBIN_HOOD(CTZ)(x) : ROBIN_HOOD(BITNESS)) # endif #endif // fallthrough #ifndef __has_cpp_attribute // For backwards compatibility # define __has_cpp_attribute(x) 0 #endif #if __has_cpp_attribute(clang::fallthrough) # define ROBIN_HOOD_PRIVATE_DEFINITION_FALLTHROUGH() [[clang::fallthrough]] #elif __has_cpp_attribute(gnu::fallthrough) # define ROBIN_HOOD_PRIVATE_DEFINITION_FALLTHROUGH() [[gnu::fallthrough]] #else # define ROBIN_HOOD_PRIVATE_DEFINITION_FALLTHROUGH() #endif // likely/unlikely #ifdef _MSC_VER # define ROBIN_HOOD_LIKELY(condition) condition # define ROBIN_HOOD_UNLIKELY(condition) condition #else # define ROBIN_HOOD_LIKELY(condition) __builtin_expect(condition, 1) # define ROBIN_HOOD_UNLIKELY(condition) __builtin_expect(condition, 0) #endif // detect if native wchar_t type is availiable in MSVC #ifdef _MSC_VER # ifdef _NATIVE_WCHAR_T_DEFINED # define ROBIN_HOOD_PRIVATE_DEFINITION_HAS_NATIVE_WCHART() 1 # else # define ROBIN_HOOD_PRIVATE_DEFINITION_HAS_NATIVE_WCHART() 0 # endif #else # define ROBIN_HOOD_PRIVATE_DEFINITION_HAS_NATIVE_WCHART() 1 #endif // detect if MSVC supports the pair(std::piecewise_construct_t,...) consructor being constexpr #ifdef _MSC_VER # if _MSC_VER <= 1900 # define ROBIN_HOOD_PRIVATE_DEFINITION_BROKEN_CONSTEXPR() 1 # else # define ROBIN_HOOD_PRIVATE_DEFINITION_BROKEN_CONSTEXPR() 0 # endif #else # define ROBIN_HOOD_PRIVATE_DEFINITION_BROKEN_CONSTEXPR() 0 #endif // workaround missing "is_trivially_copyable" in g++ < 5.0 // See https://stackoverflow.com/a/31798726/48181 #if defined(__GNUC__) && __GNUC__ < 5 # define ROBIN_HOOD_IS_TRIVIALLY_COPYABLE(...) __has_trivial_copy(__VA_ARGS__) #else # define ROBIN_HOOD_IS_TRIVIALLY_COPYABLE(...) std::is_trivially_copyable<__VA_ARGS__>::value #endif // helpers for C++ versions, see https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html #define ROBIN_HOOD_PRIVATE_DEFINITION_CXX() __cplusplus #define ROBIN_HOOD_PRIVATE_DEFINITION_CXX98() 199711L #define ROBIN_HOOD_PRIVATE_DEFINITION_CXX11() 201103L #define ROBIN_HOOD_PRIVATE_DEFINITION_CXX14() 201402L #define ROBIN_HOOD_PRIVATE_DEFINITION_CXX17() 201703L #if ROBIN_HOOD(CXX) >= ROBIN_HOOD(CXX17) # define ROBIN_HOOD_PRIVATE_DEFINITION_NODISCARD() [[nodiscard]] #else # define ROBIN_HOOD_PRIVATE_DEFINITION_NODISCARD() #endif namespace robin_hood { #if ROBIN_HOOD(CXX) >= ROBIN_HOOD(CXX14) # define ROBIN_HOOD_STD std #else // c++11 compatibility layer namespace ROBIN_HOOD_STD { template <class T> struct alignment_of : std::integral_constant<std::size_t, alignof(typename std::remove_all_extents<T>::type)> {}; template <class T, T... Ints> class integer_sequence { public: using value_type = T; static_assert(std::is_integral<value_type>::value, "not integral type"); static constexpr std::size_t size() noexcept { return sizeof...(Ints); } }; template <std::size_t... Inds> using index_sequence = integer_sequence<std::size_t, Inds...>; namespace detail_ { template <class T, T Begin, T End, bool> struct IntSeqImpl { using TValue = T; static_assert(std::is_integral<TValue>::value, "not integral type"); static_assert(Begin >= 0 && Begin < End, "unexpected argument (Begin<0 || Begin<=End)"); template <class, class> struct IntSeqCombiner; template <TValue... Inds0, TValue... Inds1> struct IntSeqCombiner<integer_sequence<TValue, Inds0...>, integer_sequence<TValue, Inds1...>> { using TResult = integer_sequence<TValue, Inds0..., Inds1...>; }; using TResult = typename IntSeqCombiner<typename IntSeqImpl<TValue, Begin, Begin + (End - Begin) / 2, (End - Begin) / 2 == 1>::TResult, typename IntSeqImpl<TValue, Begin + (End - Begin) / 2, End, (End - Begin + 1) / 2 == 1>::TResult>::TResult; }; template <class T, T Begin> struct IntSeqImpl<T, Begin, Begin, false> { using TValue = T; static_assert(std::is_integral<TValue>::value, "not integral type"); static_assert(Begin >= 0, "unexpected argument (Begin<0)"); using TResult = integer_sequence<TValue>; }; template <class T, T Begin, T End> struct IntSeqImpl<T, Begin, End, true> { using TValue = T; static_assert(std::is_integral<TValue>::value, "not integral type"); static_assert(Begin >= 0, "unexpected argument (Begin<0)"); using TResult = integer_sequence<TValue, Begin>; }; } // namespace detail_ template <class T, T N> using make_integer_sequence = typename detail_::IntSeqImpl<T, 0, N, (N - 0) == 1>::TResult; template <std::size_t N> using make_index_sequence = make_integer_sequence<std::size_t, N>; template <class... T> using index_sequence_for = make_index_sequence<sizeof...(T)>; } // namespace ROBIN_HOOD_STD #endif namespace detail { // make sure we static_cast to the correct type for hash_int #if ROBIN_HOOD(BITNESS) == 64 using SizeT = uint64_t; #else using SizeT = uint32_t; #endif template <typename T> T rotr(T x, unsigned k) { return (x >> k) | (x << (8U * sizeof(T) - k)); } // This cast gets rid of warnings like "cast from 'uint8_t*' {aka 'unsigned char*'} to // 'uint64_t*' {aka 'long unsigned int*'} increases required alignment of target type". Use with // care! template <typename T> inline T reinterpret_cast_no_cast_align_warning(void* ptr) noexcept { return reinterpret_cast<T>(ptr); } template <typename T> inline T reinterpret_cast_no_cast_align_warning(void const* ptr) noexcept { return reinterpret_cast<T>(ptr); } // make sure this is not inlined as it is slow and dramatically enlarges code, thus making other // inlinings more difficult. Throws are also generally the slow path. template <typename E, typename... Args> [[noreturn]] ROBIN_HOOD(NOINLINE) #if ROBIN_HOOD(HAS_EXCEPTIONS) void doThrow(Args&&... args) { // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay) throw E(std::forward<Args>(args)...); } #else void doThrow(Args&&... ROBIN_HOOD_UNUSED(args) /*unused*/) { abort(); } #endif template <typename E, typename T, typename... Args> T* assertNotNull(T* t, Args&&... args) { if (ROBIN_HOOD_UNLIKELY(nullptr == t)) { doThrow<E>(std::forward<Args>(args)...); } return t; } template <typename T> inline T unaligned_load(void const* ptr) noexcept { // using memcpy so we don't get into unaligned load problems. // compiler should optimize this very well anyways. T t; std::memcpy(&t, ptr, sizeof(T)); return t; } // Allocates bulks of memory for objects of type T. This deallocates the memory in the destructor, // and keeps a linked list of the allocated memory around. Overhead per allocation is the size of a // pointer. template <typename T, size_t MinNumAllocs = 4, size_t MaxNumAllocs = 256> class BulkPoolAllocator { public: BulkPoolAllocator() noexcept = default; // does not copy anything, just creates a new allocator. BulkPoolAllocator(const BulkPoolAllocator& ROBIN_HOOD_UNUSED(o) /*unused*/) noexcept : mHead(nullptr) , mListForFree(nullptr) {} BulkPoolAllocator(BulkPoolAllocator&& o) noexcept : mHead(o.mHead) , mListForFree(o.mListForFree) { o.mListForFree = nullptr; o.mHead = nullptr; } BulkPoolAllocator& operator=(BulkPoolAllocator&& o) noexcept { reset(); mHead = o.mHead; mListForFree = o.mListForFree; o.mListForFree = nullptr; o.mHead = nullptr; return *this; } BulkPoolAllocator& // NOLINTNEXTLINE(bugprone-unhandled-self-assignment,cert-oop54-cpp) operator=(const BulkPoolAllocator& ROBIN_HOOD_UNUSED(o) /*unused*/) noexcept { // does not do anything return *this; } ~BulkPoolAllocator() noexcept { reset(); } // Deallocates all allocated memory. void reset() noexcept { while (mListForFree) { T* tmp = *mListForFree; ROBIN_HOOD_LOG("std::free") std::free(mListForFree); mListForFree = reinterpret_cast_no_cast_align_warning<T**>(tmp); } mHead = nullptr; } // allocates, but does NOT initialize. Use in-place new constructor, e.g. // T* obj = pool.allocate(); // ::new (static_cast<void*>(obj)) T(); T* allocate() { T* tmp = mHead; if (!tmp) { tmp = performAllocation(); } mHead = *reinterpret_cast_no_cast_align_warning<T**>(tmp); return tmp; } // does not actually deallocate but puts it in store. // make sure you have already called the destructor! e.g. with // obj->~T(); // pool.deallocate(obj); void deallocate(T* obj) noexcept { *reinterpret_cast_no_cast_align_warning<T**>(obj) = mHead; mHead = obj; } // Adds an already allocated block of memory to the allocator. This allocator is from now on // responsible for freeing the data (with free()). If the provided data is not large enough to // make use of, it is immediately freed. Otherwise it is reused and freed in the destructor. void addOrFree(void* ptr, const size_t numBytes) noexcept { // calculate number of available elements in ptr if (numBytes < ALIGNMENT + ALIGNED_SIZE) { // not enough data for at least one element. Free and return. ROBIN_HOOD_LOG("std::free") std::free(ptr); } else { ROBIN_HOOD_LOG("add to buffer") add(ptr, numBytes); } } void swap(BulkPoolAllocator<T, MinNumAllocs, MaxNumAllocs>& other) noexcept { using std::swap; swap(mHead, other.mHead); swap(mListForFree, other.mListForFree); } private: // iterates the list of allocated memory to calculate how many to alloc next. // Recalculating this each time saves us a size_t member. // This ignores the fact that memory blocks might have been added manually with addOrFree. In // practice, this should not matter much. ROBIN_HOOD(NODISCARD) size_t calcNumElementsToAlloc() const noexcept { auto tmp = mListForFree; size_t numAllocs = MinNumAllocs; while (numAllocs * 2 <= MaxNumAllocs && tmp) { auto x = reinterpret_cast<T***>(tmp); tmp = *x; numAllocs *= 2; } return numAllocs; } // WARNING: Underflow if numBytes < ALIGNMENT! This is guarded in addOrFree(). void add(void* ptr, const size_t numBytes) noexcept { const size_t numElements = (numBytes - ALIGNMENT) / ALIGNED_SIZE; auto data = reinterpret_cast<T**>(ptr); // link free list auto x = reinterpret_cast<T***>(data); *x = mListForFree; mListForFree = data; // create linked list for newly allocated data auto* const headT = reinterpret_cast_no_cast_align_warning<T*>(reinterpret_cast<char*>(ptr) + ALIGNMENT); auto* const head = reinterpret_cast<char*>(headT); // Visual Studio compiler automatically unrolls this loop, which is pretty cool for (size_t i = 0; i < numElements; ++i) { *reinterpret_cast_no_cast_align_warning<char**>(head + i * ALIGNED_SIZE) = head + (i + 1) * ALIGNED_SIZE; } // last one points to 0 *reinterpret_cast_no_cast_align_warning<T**>(head + (numElements - 1) * ALIGNED_SIZE) = mHead; mHead = headT; } // Called when no memory is available (mHead == 0). // Don't inline this slow path. ROBIN_HOOD(NOINLINE) T* performAllocation() { size_t const numElementsToAlloc = calcNumElementsToAlloc(); // alloc new memory: [prev |T, T, ... T] size_t const bytes = ALIGNMENT + ALIGNED_SIZE * numElementsToAlloc; ROBIN_HOOD_LOG("std::malloc " << bytes << " = " << ALIGNMENT << " + " << ALIGNED_SIZE << " * " << numElementsToAlloc) add(assertNotNull<std::bad_alloc>(std::malloc(bytes)), bytes); return mHead; } // enforce byte alignment of the T's #if ROBIN_HOOD(CXX) >= ROBIN_HOOD(CXX14) static constexpr size_t ALIGNMENT = (std::max)(std::alignment_of<T>::value, std::alignment_of<T*>::value); #else static const size_t ALIGNMENT = (ROBIN_HOOD_STD::alignment_of<T>::value > ROBIN_HOOD_STD::alignment_of<T*>::value) ? ROBIN_HOOD_STD::alignment_of<T>::value : +ROBIN_HOOD_STD::alignment_of<T*>::value; // the + is for walkarround #endif static constexpr size_t ALIGNED_SIZE = ((sizeof(T) - 1) / ALIGNMENT + 1) * ALIGNMENT; static_assert(MinNumAllocs >= 1, "MinNumAllocs"); static_assert(MaxNumAllocs >= MinNumAllocs, "MaxNumAllocs"); static_assert(ALIGNED_SIZE >= sizeof(T*), "ALIGNED_SIZE"); static_assert(0 == (ALIGNED_SIZE % sizeof(T*)), "ALIGNED_SIZE mod"); static_assert(ALIGNMENT >= sizeof(T*), "ALIGNMENT"); T* mHead{nullptr}; T** mListForFree{nullptr}; }; template <typename T, size_t MinSize, size_t MaxSize, bool IsFlat> struct NodeAllocator; // dummy allocator that does nothing template <typename T, size_t MinSize, size_t MaxSize> struct NodeAllocator<T, MinSize, MaxSize, true> { // we are not using the data, so just free it. void addOrFree(void* ptr, size_t ROBIN_HOOD_UNUSED(numBytes) /*unused*/) noexcept { ROBIN_HOOD_LOG("std::free") std::free(ptr); } }; template <typename T, size_t MinSize, size_t MaxSize> struct NodeAllocator<T, MinSize, MaxSize, false> : public BulkPoolAllocator<T, MinSize, MaxSize> {}; // c++14 doesn't have is_nothrow_swappable, and clang++ 6.0.1 doesn't like it either, so I'm making // my own here. namespace swappable { #if ROBIN_HOOD(CXX) < ROBIN_HOOD(CXX17) using std::swap; template <typename T> struct nothrow { static const bool value = noexcept(swap(std::declval<T&>(), std::declval<T&>())); }; #else template <typename T> struct nothrow { static const bool value = std::is_nothrow_swappable<T>::value; }; #endif } // namespace swappable } // namespace detail struct is_transparent_tag {}; // A custom pair implementation is used in the map because std::pair is not is_trivially_copyable, // which means it would not be allowed to be used in std::memcpy. This struct is copyable, which is // also tested. template <typename T1, typename T2> struct pair { using first_type = T1; using second_type = T2; template <typename U1 = T1, typename U2 = T2, typename = typename std::enable_if<std::is_default_constructible<U1>::value && std::is_default_constructible<U2>::value>::type> constexpr pair() noexcept(noexcept(U1()) && noexcept(U2())) : first() , second() {} // pair constructors are explicit so we don't accidentally call this ctor when we don't have to. explicit constexpr pair(std::pair<T1, T2> const& o) noexcept( noexcept(T1(std::declval<T1 const&>())) && noexcept(T2(std::declval<T2 const&>()))) : first(o.first) , second(o.second) {} // pair constructors are explicit so we don't accidentally call this ctor when we don't have to. explicit constexpr pair(std::pair<T1, T2>&& o) noexcept(noexcept( T1(std::move(std::declval<T1&&>()))) && noexcept(T2(std::move(std::declval<T2&&>())))) : first(std::move(o.first)) , second(std::move(o.second)) {} constexpr pair(T1&& a, T2&& b) noexcept(noexcept( T1(std::move(std::declval<T1&&>()))) && noexcept(T2(std::move(std::declval<T2&&>())))) : first(std::move(a)) , second(std::move(b)) {} template <typename U1, typename U2> constexpr pair(U1&& a, U2&& b) noexcept(noexcept(T1(std::forward<U1>( std::declval<U1&&>()))) && noexcept(T2(std::forward<U2>(std::declval<U2&&>())))) : first(std::forward<U1>(a)) , second(std::forward<U2>(b)) {} template <typename... U1, typename... U2> // MSVC 2015 produces error "C2476: ‘constexpr’ constructor does not initialize all members" // if this constructor is constexpr #if !ROBIN_HOOD(BROKEN_CONSTEXPR) constexpr #endif pair(std::piecewise_construct_t /*unused*/, std::tuple<U1...> a, std::tuple<U2...> b) noexcept(noexcept(pair(std::declval<std::tuple<U1...>&>(), std::declval<std::tuple<U2...>&>(), ROBIN_HOOD_STD::index_sequence_for<U1...>(), ROBIN_HOOD_STD::index_sequence_for<U2...>()))) : pair(a, b, ROBIN_HOOD_STD::index_sequence_for<U1...>(), ROBIN_HOOD_STD::index_sequence_for<U2...>()) { } // constructor called from the std::piecewise_construct_t ctor template <typename... U1, size_t... I1, typename... U2, size_t... I2> pair(std::tuple<U1...>& a, std::tuple<U2...>& b, ROBIN_HOOD_STD::index_sequence<I1...> /*unused*/, ROBIN_HOOD_STD::index_sequence<I2...> /*unused*/) noexcept( noexcept(T1(std::forward<U1>(std::get<I1>( std::declval<std::tuple< U1...>&>()))...)) && noexcept(T2(std:: forward<U2>(std::get<I2>( std::declval<std::tuple<U2...>&>()))...))) : first(std::forward<U1>(std::get<I1>(a))...) , second(std::forward<U2>(std::get<I2>(b))...) { // make visual studio compiler happy about warning about unused a & b. // Visual studio's pair implementation disables warning 4100. (void)a; (void)b; } void swap(pair<T1, T2>& o) noexcept((detail::swappable::nothrow<T1>::value) && (detail::swappable::nothrow<T2>::value)) { using std::swap; swap(first, o.first); swap(second, o.second); } T1 first; // NOLINT(misc-non-private-member-variables-in-classes) T2 second; // NOLINT(misc-non-private-member-variables-in-classes) }; template <typename A, typename B> inline void swap(pair<A, B>& a, pair<A, B>& b) noexcept( noexcept(std::declval<pair<A, B>&>().swap(std::declval<pair<A, B>&>()))) { a.swap(b); } template <typename A, typename B> inline constexpr bool operator==(pair<A, B> const& x, pair<A, B> const& y) { return (x.first == y.first) && (x.second == y.second); } template <typename A, typename B> inline constexpr bool operator!=(pair<A, B> const& x, pair<A, B> const& y) { return !(x == y); } template <typename A, typename B> inline constexpr bool operator<(pair<A, B> const& x, pair<A, B> const& y) noexcept(noexcept( std::declval<A const&>() < std::declval<A const&>()) && noexcept(std::declval<B const&>() < std::declval<B const&>())) { return x.first < y.first || (!(y.first < x.first) && x.second < y.second); } template <typename A, typename B> inline constexpr bool operator>(pair<A, B> const& x, pair<A, B> const& y) { return y < x; } template <typename A, typename B> inline constexpr bool operator<=(pair<A, B> const& x, pair<A, B> const& y) { return !(x > y); } template <typename A, typename B> inline constexpr bool operator>=(pair<A, B> const& x, pair<A, B> const& y) { return !(x < y); } inline size_t hash_bytes(void const* ptr, size_t len) noexcept { static constexpr uint64_t m = UINT64_C(0xc6a4a7935bd1e995); static constexpr uint64_t seed = UINT64_C(0xe17a1465); static constexpr unsigned int r = 47; auto const* const data64 = static_cast<uint64_t const*>(ptr); uint64_t h = seed ^ (len * m); size_t const n_blocks = len / 8; for (size_t i = 0; i < n_blocks; ++i) { auto k = detail::unaligned_load<uint64_t>(data64 + i); k *= m; k ^= k >> r; k *= m; h ^= k; h *= m; } auto const* const data8 = reinterpret_cast<uint8_t const*>(data64 + n_blocks); switch (len & 7U) { case 7: h ^= static_cast<uint64_t>(data8[6]) << 48U; ROBIN_HOOD(FALLTHROUGH); // FALLTHROUGH case 6: h ^= static_cast<uint64_t>(data8[5]) << 40U; ROBIN_HOOD(FALLTHROUGH); // FALLTHROUGH case 5: h ^= static_cast<uint64_t>(data8[4]) << 32U; ROBIN_HOOD(FALLTHROUGH); // FALLTHROUGH case 4: h ^= static_cast<uint64_t>(data8[3]) << 24U; ROBIN_HOOD(FALLTHROUGH); // FALLTHROUGH case 3: h ^= static_cast<uint64_t>(data8[2]) << 16U; ROBIN_HOOD(FALLTHROUGH); // FALLTHROUGH case 2: h ^= static_cast<uint64_t>(data8[1]) << 8U; ROBIN_HOOD(FALLTHROUGH); // FALLTHROUGH case 1: h ^= static_cast<uint64_t>(data8[0]); h *= m; ROBIN_HOOD(FALLTHROUGH); // FALLTHROUGH default: break; } h ^= h >> r; // not doing the final step here, because this will be done by keyToIdx anyways // h *= m; // h ^= h >> r; return static_cast<size_t>(h); } inline size_t hash_int(uint64_t x) noexcept { // tried lots of different hashes, let's stick with murmurhash3. It's simple, fast, well tested, // and doesn't need any special 128bit operations. x ^= x >> 33U; x *= UINT64_C(0xff51afd7ed558ccd); x ^= x >> 33U; // not doing the final step here, because this will be done by keyToIdx anyways // x *= UINT64_C(0xc4ceb9fe1a85ec53); // x ^= x >> 33U; return static_cast<size_t>(x); } // A thin wrapper around std::hash, performing an additional simple mixing step of the result. template <typename T, typename Enable = void> struct hash : public std::hash<T> { size_t operator()(T const& obj) const noexcept(noexcept(std::declval<std::hash<T>>().operator()(std::declval<T const&>()))) { // call base hash auto result = std::hash<T>::operator()(obj); // return mixed of that, to be save against identity has return hash_int(static_cast<detail::SizeT>(result)); } }; template <typename CharT> struct hash<std::basic_string<CharT>> { size_t operator()(std::basic_string<CharT> const& str) const noexcept { return hash_bytes(str.data(), sizeof(CharT) * str.size()); } }; #if ROBIN_HOOD(CXX) >= ROBIN_HOOD(CXX17) template <typename CharT> struct hash<std::basic_string_view<CharT>> { size_t operator()(std::basic_string_view<CharT> const& sv) const noexcept { return hash_bytes(sv.data(), sizeof(CharT) * sv.size()); } }; #endif template <class T> struct hash<T*> { size_t operator()(T* ptr) const noexcept { return hash_int(reinterpret_cast<detail::SizeT>(ptr)); } }; template <class T> struct hash<std::unique_ptr<T>> { size_t operator()(std::unique_ptr<T> const& ptr) const noexcept { return hash_int(reinterpret_cast<detail::SizeT>(ptr.get())); } }; template <class T> struct hash<std::shared_ptr<T>> { size_t operator()(std::shared_ptr<T> const& ptr) const noexcept { return hash_int(reinterpret_cast<detail::SizeT>(ptr.get())); } }; template <typename Enum> struct hash<Enum, typename std::enable_if<std::is_enum<Enum>::value>::type> { size_t operator()(Enum e) const noexcept { using Underlying = typename std::underlying_type<Enum>::type; return hash<Underlying>{}(static_cast<Underlying>(e)); } }; #define ROBIN_HOOD_HASH_INT(T) \ template <> \ struct hash<T> { \ size_t operator()(T const& obj) const noexcept { \ return hash_int(static_cast<uint64_t>(obj)); \ } \ } #if defined(__GNUC__) && !defined(__clang__) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wuseless-cast" #endif // see https://en.cppreference.com/w/cpp/utility/hash ROBIN_HOOD_HASH_INT(bool); ROBIN_HOOD_HASH_INT(char); ROBIN_HOOD_HASH_INT(signed char); ROBIN_HOOD_HASH_INT(unsigned char); ROBIN_HOOD_HASH_INT(char16_t); ROBIN_HOOD_HASH_INT(char32_t); #if ROBIN_HOOD(HAS_NATIVE_WCHART) ROBIN_HOOD_HASH_INT(wchar_t); #endif ROBIN_HOOD_HASH_INT(short); ROBIN_HOOD_HASH_INT(unsigned short); ROBIN_HOOD_HASH_INT(int); ROBIN_HOOD_HASH_INT(unsigned int); ROBIN_HOOD_HASH_INT(long); ROBIN_HOOD_HASH_INT(long long); ROBIN_HOOD_HASH_INT(unsigned long); ROBIN_HOOD_HASH_INT(unsigned long long); #if defined(__GNUC__) && !defined(__clang__) # pragma GCC diagnostic pop #endif namespace detail { template <typename T> struct void_type { using type = void; }; template <typename T, typename = void> struct has_is_transparent : public std::false_type {}; template <typename T> struct has_is_transparent<T, typename void_type<typename T::is_transparent>::type> : public std::true_type {}; // using wrapper classes for hash and key_equal prevents the diamond problem when the same type // is used. see https://stackoverflow.com/a/28771920/48181 template <typename T> struct WrapHash : public T { WrapHash() = default; explicit WrapHash(T const& o) noexcept(noexcept(T(std::declval<T const&>()))) : T(o) {} }; template <typename T> struct WrapKeyEqual : public T { WrapKeyEqual() = default; explicit WrapKeyEqual(T const& o) noexcept(noexcept(T(std::declval<T const&>()))) : T(o) {} }; // A highly optimized hashmap implementation, using the Robin Hood algorithm. // // In most cases, this map should be usable as a drop-in replacement for std::unordered_map, but // be about 2x faster in most cases and require much less allocations. // // This implementation uses the following memory layout: // // [Node, Node, ... Node | info, info, ... infoSentinel ] // // * Node: either a DataNode that directly has the std::pair<key, val> as member, // or a DataNode with a pointer to std::pair<key,val>. Which DataNode representation to use // depends on how fast the swap() operation is. Heuristically, this is automatically choosen // based on sizeof(). there are always 2^n Nodes. // // * info: Each Node in the map has a corresponding info byte, so there are 2^n info bytes. // Each byte is initialized to 0, meaning the corresponding Node is empty. Set to 1 means the // corresponding node contains data. Set to 2 means the corresponding Node is filled, but it // actually belongs to the previous position and was pushed out because that place is already // taken. // // * infoSentinel: Sentinel byte set to 1, so that iterator's ++ can stop at end() without the // need for a idx variable. // // According to STL, order of templates has effect on throughput. That's why I've moved the // boolean to the front. // https://www.reddit.com/r/cpp/comments/ahp6iu/compile_time_binary_size_reductions_and_cs_future/eeguck4/ template <bool IsFlat, size_t MaxLoadFactor100, typename Key, typename T, typename Hash, typename KeyEqual> class Table : public WrapHash<Hash>, public WrapKeyEqual<KeyEqual>, detail::NodeAllocator< typename std::conditional< std::is_void<T>::value, Key, robin_hood::pair<typename std::conditional<IsFlat, Key, Key const>::type, T>>::type, 4, 16384, IsFlat> { public: static constexpr bool is_flat = IsFlat; static constexpr bool is_map = !std::is_void<T>::value; static constexpr bool is_set = !is_map; static constexpr bool is_transparent = has_is_transparent<Hash>::value && has_is_transparent<KeyEqual>::value; using key_type = Key; using mapped_type = T; using value_type = typename std::conditional< is_set, Key, robin_hood::pair<typename std::conditional<is_flat, Key, Key const>::type, T>>::type; using size_type = size_t; using hasher = Hash; using key_equal = KeyEqual; using Self = Table<IsFlat, MaxLoadFactor100, key_type, mapped_type, hasher, key_equal>; private: static_assert(MaxLoadFactor100 > 10 && MaxLoadFactor100 < 100, "MaxLoadFactor100 needs to be >10 && < 100"); using WHash = WrapHash<Hash>; using WKeyEqual = WrapKeyEqual<KeyEqual>; // configuration defaults // make sure we have 8 elements, needed to quickly rehash mInfo static constexpr size_t InitialNumElements = sizeof(uint64_t); static constexpr uint32_t InitialInfoNumBits = 5; static constexpr uint8_t InitialInfoInc = 1U << InitialInfoNumBits; static constexpr size_t InfoMask = InitialInfoInc - 1U; static constexpr uint8_t InitialInfoHashShift = 0; using DataPool = detail::NodeAllocator<value_type, 4, 16384, IsFlat>; // type needs to be wider than uint8_t. using InfoType = uint32_t; // DataNode //////////////////////////////////////////////////////// // Primary template for the data node. We have special implementations for small and big // objects. For large objects it is assumed that swap() is fairly slow, so we allocate these // on the heap so swap merely swaps a pointer. template <typename M, bool> class DataNode {}; // Small: just allocate on the stack. template <typename M> class DataNode<M, true> final { public: template <typename... Args> explicit DataNode(M& ROBIN_HOOD_UNUSED(map) /*unused*/, Args&&... args) noexcept( noexcept(value_type(std::forward<Args>(args)...))) : mData(std::forward<Args>(args)...) {} DataNode(M& ROBIN_HOOD_UNUSED(map) /*unused*/, DataNode<M, true>&& n) noexcept( std::is_nothrow_move_constructible<value_type>::value) : mData(std::move(n.mData)) {} // doesn't do anything void destroy(M& ROBIN_HOOD_UNUSED(map) /*unused*/) noexcept {} void destroyDoNotDeallocate() noexcept {} value_type const* operator->() const noexcept { return &mData; } value_type* operator->() noexcept { return &mData; } const value_type& operator*() const noexcept { return mData; } value_type& operator*() noexcept { return mData; } template <typename VT = value_type> ROBIN_HOOD(NODISCARD) typename std::enable_if<is_map, typename VT::first_type&>::type getFirst() noexcept { return mData.first; } template <typename VT = value_type> ROBIN_HOOD(NODISCARD) typename std::enable_if<is_set, VT&>::type getFirst() noexcept { return mData; } template <typename VT = value_type> ROBIN_HOOD(NODISCARD) typename std::enable_if<is_map, typename VT::first_type const&>::type getFirst() const noexcept { return mData.first; } template <typename VT = value_type> ROBIN_HOOD(NODISCARD) typename std::enable_if<is_set, VT const&>::type getFirst() const noexcept { return mData; } template <typename MT = mapped_type> ROBIN_HOOD(NODISCARD) typename std::enable_if<is_map, MT&>::type getSecond() noexcept { return mData.second; } template <typename MT = mapped_type> ROBIN_HOOD(NODISCARD) typename std::enable_if<is_set, MT const&>::type getSecond() const noexcept { return mData.second; } void swap(DataNode<M, true>& o) noexcept( noexcept(std::declval<value_type>().swap(std::declval<value_type>()))) { mData.swap(o.mData); } private: value_type mData; }; // big object: allocate on heap. template <typename M> class DataNode<M, false> { public: template <typename... Args> explicit DataNode(M& map, Args&&... args) : mData(map.allocate()) { ::new (static_cast<void*>(mData)) value_type(std::forward<Args>(args)...); } DataNode(M& ROBIN_HOOD_UNUSED(map) /*unused*/, DataNode<M, false>&& n) noexcept : mData(std::move(n.mData)) {} void destroy(M& map) noexcept { // don't deallocate, just put it into list of datapool. mData->~value_type(); map.deallocate(mData); } void destroyDoNotDeallocate() noexcept { mData->~value_type(); } value_type const* operator->() const noexcept { return mData; } value_type* operator->() noexcept { return mData; } const value_type& operator*() const { return *mData; } value_type& operator*() { return *mData; } template <typename VT = value_type> ROBIN_HOOD(NODISCARD) typename std::enable_if<is_map, typename VT::first_type&>::type getFirst() noexcept { return mData->first; } template <typename VT = value_type> ROBIN_HOOD(NODISCARD) typename std::enable_if<is_set, VT&>::type getFirst() noexcept { return *mData; } template <typename VT = value_type> ROBIN_HOOD(NODISCARD) typename std::enable_if<is_map, typename VT::first_type const&>::type getFirst() const noexcept { return mData->first; } template <typename VT = value_type> ROBIN_HOOD(NODISCARD) typename std::enable_if<is_set, VT const&>::type getFirst() const noexcept { return *mData; } template <typename MT = mapped_type> ROBIN_HOOD(NODISCARD) typename std::enable_if<is_map, MT&>::type getSecond() noexcept { return mData->second; } template <typename MT = mapped_type> ROBIN_HOOD(NODISCARD) typename std::enable_if<is_map, MT const&>::type getSecond() const noexcept { return mData->second; } void swap(DataNode<M, false>& o) noexcept { using std::swap; swap(mData, o.mData); } private: value_type* mData; }; using Node = DataNode<Self, IsFlat>; // helpers for insertKeyPrepareEmptySpot: extract first entry (only const required) ROBIN_HOOD(NODISCARD) key_type const& getFirstConst(Node const& n) const noexcept { return n.getFirst(); } // in case we have void mapped_type, we are not using a pair, thus we just route k through. // No need to disable this because it's just not used if not applicable. ROBIN_HOOD(NODISCARD) key_type const& getFirstConst(key_type const& k) const noexcept { return k; } // in case we have non-void mapped_type, we have a standard robin_hood::pair template <typename Q = mapped_type> ROBIN_HOOD(NODISCARD) typename std::enable_if<!std::is_void<Q>::value, key_type const&>::type getFirstConst(value_type const& vt) const noexcept { return vt.first; } // Cloner ////////////////////////////////////////////////////////// template <typename M, bool UseMemcpy> struct Cloner; // fast path: Just copy data, without allocating anything. template <typename M> struct Cloner<M, true> { void operator()(M const& source, M& target) const { auto const* const src = reinterpret_cast<char const*>(source.mKeyVals); auto* tgt = reinterpret_cast<char*>(target.mKeyVals); auto const numElementsWithBuffer = target.calcNumElementsWithBuffer(target.mMask + 1); std::copy(src, src + target.calcNumBytesTotal(numElementsWithBuffer), tgt); } }; template <typename M> struct Cloner<M, false> { void operator()(M const& s, M& t) const { auto const numElementsWithBuffer = t.calcNumElementsWithBuffer(t.mMask + 1); std::copy(s.mInfo, s.mInfo + t.calcNumBytesInfo(numElementsWithBuffer), t.mInfo); for (size_t i = 0; i < numElementsWithBuffer; ++i) { if (t.mInfo[i]) { ::new (static_cast<void*>(t.mKeyVals + i)) Node(t, *s.mKeyVals[i]); } } } }; // Destroyer /////////////////////////////////////////////////////// template <typename M, bool IsFlatAndTrivial> struct Destroyer {}; template <typename M> struct Destroyer<M, true> { void nodes(M& m) const noexcept { m.mNumElements = 0; } void nodesDoNotDeallocate(M& m) const noexcept { m.mNumElements = 0; } }; template <typename M> struct Destroyer<M, false> { void nodes(M& m) const noexcept { m.mNumElements = 0; // clear also resets mInfo to 0, that's sometimes not necessary. auto const numElementsWithBuffer = m.calcNumElementsWithBuffer(m.mMask + 1); for (size_t idx = 0; idx < numElementsWithBuffer; ++idx) { if (0 != m.mInfo[idx]) { Node& n = m.mKeyVals[idx]; n.destroy(m); n.~Node(); } } } void nodesDoNotDeallocate(M& m) const noexcept { m.mNumElements = 0; // clear also resets mInfo to 0, that's sometimes not necessary. auto const numElementsWithBuffer = m.calcNumElementsWithBuffer(m.mMask + 1); for (size_t idx = 0; idx < numElementsWithBuffer; ++idx) { if (0 != m.mInfo[idx]) { Node& n = m.mKeyVals[idx]; n.destroyDoNotDeallocate(); n.~Node(); } } } }; // Iter //////////////////////////////////////////////////////////// struct fast_forward_tag {}; // generic iterator for both const_iterator and iterator. template <bool IsConst> // NOLINTNEXTLINE(hicpp-special-member-functions,cppcoreguidelines-special-member-functions) class Iter { private: using NodePtr = typename std::conditional<IsConst, Node const*, Node*>::type; public: using difference_type = std::ptrdiff_t; using value_type = typename Self::value_type; using reference = typename std::conditional<IsConst, value_type const&, value_type&>::type; using pointer = typename std::conditional<IsConst, value_type const*, value_type*>::type; using iterator_category = std::forward_iterator_tag; // default constructed iterator can be compared to itself, but WON'T return true when // compared to end(). Iter() = default; // Rule of zero: nothing specified. The conversion constructor is only enabled for // iterator to const_iterator, so it doesn't accidentally work as a copy ctor. // Conversion constructor from iterator to const_iterator. template <bool OtherIsConst, typename = typename std::enable_if<IsConst && !OtherIsConst>::type> // NOLINTNEXTLINE(hicpp-explicit-conversions) Iter(Iter<OtherIsConst> const& other) noexcept : mKeyVals(other.mKeyVals) , mInfo(other.mInfo) {} Iter(NodePtr valPtr, uint8_t const* infoPtr) noexcept : mKeyVals(valPtr) , mInfo(infoPtr) {} Iter(NodePtr valPtr, uint8_t const* infoPtr, fast_forward_tag ROBIN_HOOD_UNUSED(tag) /*unused*/) noexcept : mKeyVals(valPtr) , mInfo(infoPtr) { fastForward(); } template <bool OtherIsConst, typename = typename std::enable_if<IsConst && !OtherIsConst>::type> Iter& operator=(Iter<OtherIsConst> const& other) noexcept { mKeyVals = other.mKeyVals; mInfo = other.mInfo; return *this; } // prefix increment. Undefined behavior if we are at end()! Iter& operator++() noexcept { mInfo++; mKeyVals++; fastForward(); return *this; } Iter operator++(int) noexcept { Iter tmp = *this; ++(*this); return tmp; } reference operator*() const { return **mKeyVals; } pointer operator->() const { return &**mKeyVals; } template <bool O> bool operator==(Iter<O> const& o) const noexcept { return mKeyVals == o.mKeyVals; } template <bool O> bool operator!=(Iter<O> const& o) const noexcept { return mKeyVals != o.mKeyVals; } private: // fast forward to the next non-free info byte // I've tried a few variants that don't depend on intrinsics, but unfortunately they are // quite a bit slower than this one. So I've reverted that change again. See map_benchmark. void fastForward() noexcept { size_t n = 0; while (0U == (n = detail::unaligned_load<size_t>(mInfo))) { mInfo += sizeof(size_t); mKeyVals += sizeof(size_t); } #if defined(ROBIN_HOOD_DISABLE_INTRINSICS) // we know for certain that within the next 8 bytes we'll find a non-zero one. if (ROBIN_HOOD_UNLIKELY(0U == detail::unaligned_load<uint32_t>(mInfo))) { mInfo += 4; mKeyVals += 4; } if (ROBIN_HOOD_UNLIKELY(0U == detail::unaligned_load<uint16_t>(mInfo))) { mInfo += 2; mKeyVals += 2; } if (ROBIN_HOOD_UNLIKELY(0U == *mInfo)) { mInfo += 1; mKeyVals += 1; } #else # if ROBIN_HOOD(LITTLE_ENDIAN) auto inc = ROBIN_HOOD_COUNT_TRAILING_ZEROES(n) / 8; # else auto inc = ROBIN_HOOD_COUNT_LEADING_ZEROES(n) / 8; # endif mInfo += inc; mKeyVals += inc; #endif } friend class Table<IsFlat, MaxLoadFactor100, key_type, mapped_type, hasher, key_equal>; NodePtr mKeyVals{nullptr}; uint8_t const* mInfo{nullptr}; }; //////////////////////////////////////////////////////////////////// // highly performance relevant code. // Lower bits are used for indexing into the array (2^n size) // The upper 1-5 bits need to be a reasonable good hash, to save comparisons. template <typename HashKey> void keyToIdx(HashKey&& key, size_t* idx, InfoType* info) const { // In addition to whatever hash is used, add another mul & shift so we get better hashing. // This serves as a bad hash prevention, if the given data is // badly mixed. auto h = static_cast<uint64_t>(WHash::operator()(key)); h *= mHashMultiplier; h ^= h >> 33U; // the lower InitialInfoNumBits are reserved for info. *info = mInfoInc + static_cast<InfoType>((h & InfoMask) >> mInfoHashShift); *idx = (static_cast<size_t>(h) >> InitialInfoNumBits) & mMask; } // forwards the index by one, wrapping around at the end void next(InfoType* info, size_t* idx) const noexcept { *idx = *idx + 1; *info += mInfoInc; } void nextWhileLess(InfoType* info, size_t* idx) const noexcept { // unrolling this by hand did not bring any speedups. while (*info < mInfo[*idx]) { next(info, idx); } } // Shift everything up by one element. Tries to move stuff around. void shiftUp(size_t startIdx, size_t const insertion_idx) noexcept(std::is_nothrow_move_assignable<Node>::value) { auto idx = startIdx; ::new (static_cast<void*>(mKeyVals + idx)) Node(std::move(mKeyVals[idx - 1])); while (--idx != insertion_idx) { mKeyVals[idx] = std::move(mKeyVals[idx - 1]); } idx = startIdx; while (idx != insertion_idx) { ROBIN_HOOD_COUNT(shiftUp) mInfo[idx] = static_cast<uint8_t>(mInfo[idx - 1] + mInfoInc); if (ROBIN_HOOD_UNLIKELY(mInfo[idx] + mInfoInc > 0xFF)) { mMaxNumElementsAllowed = 0; } --idx; } } void shiftDown(size_t idx) noexcept(std::is_nothrow_move_assignable<Node>::value) { // until we find one that is either empty or has zero offset. // TODO(martinus) we don't need to move everything, just the last one for the same // bucket. mKeyVals[idx].destroy(*this); // until we find one that is either empty or has zero offset. while (mInfo[idx + 1] >= 2 * mInfoInc) { ROBIN_HOOD_COUNT(shiftDown) mInfo[idx] = static_cast<uint8_t>(mInfo[idx + 1] - mInfoInc); mKeyVals[idx] = std::move(mKeyVals[idx + 1]); ++idx; } mInfo[idx] = 0; // don't destroy, we've moved it // mKeyVals[idx].destroy(*this); mKeyVals[idx].~Node(); } // copy of find(), except that it returns iterator instead of const_iterator. template <typename Other> ROBIN_HOOD(NODISCARD) size_t findIdx(Other const& key) const { size_t idx{}; InfoType info{}; keyToIdx(key, &idx, &info); do { // unrolling this twice gives a bit of a speedup. More unrolling did not help. if (info == mInfo[idx] && ROBIN_HOOD_LIKELY(WKeyEqual::operator()(key, mKeyVals[idx].getFirst()))) { return idx; } next(&info, &idx); if (info == mInfo[idx] && ROBIN_HOOD_LIKELY(WKeyEqual::operator()(key, mKeyVals[idx].getFirst()))) { return idx; } next(&info, &idx); } while (info <= mInfo[idx]); // nothing found! return mMask == 0 ? 0 : static_cast<size_t>(std::distance( mKeyVals, reinterpret_cast_no_cast_align_warning<Node*>(mInfo))); } void cloneData(const Table& o) { Cloner<Table, IsFlat && ROBIN_HOOD_IS_TRIVIALLY_COPYABLE(Node)>()(o, *this); } // inserts a keyval that is guaranteed to be new, e.g. when the hashmap is resized. // @return True on success, false if something went wrong void insert_move(Node&& keyval) { // we don't retry, fail if overflowing // don't need to check max num elements if (0 == mMaxNumElementsAllowed && !try_increase_info()) { throwOverflowError(); } size_t idx{}; InfoType info{}; keyToIdx(keyval.getFirst(), &idx, &info); // skip forward. Use <= because we are certain that the element is not there. while (info <= mInfo[idx]) { idx = idx + 1; info += mInfoInc; } // key not found, so we are now exactly where we want to insert it. auto const insertion_idx = idx; auto const insertion_info = static_cast<uint8_t>(info); if (ROBIN_HOOD_UNLIKELY(insertion_info + mInfoInc > 0xFF)) { mMaxNumElementsAllowed = 0; } // find an empty spot while (0 != mInfo[idx]) { next(&info, &idx); } auto& l = mKeyVals[insertion_idx]; if (idx == insertion_idx) { ::new (static_cast<void*>(&l)) Node(std::move(keyval)); } else { shiftUp(idx, insertion_idx); l = std::move(keyval); } // put at empty spot mInfo[insertion_idx] = insertion_info; ++mNumElements; } public: using iterator = Iter<false>; using const_iterator = Iter<true>; Table() noexcept(noexcept(Hash()) && noexcept(KeyEqual())) : WHash() , WKeyEqual() { ROBIN_HOOD_TRACE(this) } // Creates an empty hash map. Nothing is allocated yet, this happens at the first insert. // This tremendously speeds up ctor & dtor of a map that never receives an element. The // penalty is payed at the first insert, and not before. Lookup of this empty map works // because everybody points to DummyInfoByte::b. parameter bucket_count is dictated by the // standard, but we can ignore it. explicit Table( size_t ROBIN_HOOD_UNUSED(bucket_count) /*unused*/, const Hash& h = Hash{}, const KeyEqual& equal = KeyEqual{}) noexcept(noexcept(Hash(h)) && noexcept(KeyEqual(equal))) : WHash(h) , WKeyEqual(equal) { ROBIN_HOOD_TRACE(this) } template <typename Iter> Table(Iter first, Iter last, size_t ROBIN_HOOD_UNUSED(bucket_count) /*unused*/ = 0, const Hash& h = Hash{}, const KeyEqual& equal = KeyEqual{}) : WHash(h) , WKeyEqual(equal) { ROBIN_HOOD_TRACE(this) insert(first, last); } Table(std::initializer_list<value_type> initlist, size_t ROBIN_HOOD_UNUSED(bucket_count) /*unused*/ = 0, const Hash& h = Hash{}, const KeyEqual& equal = KeyEqual{}) : WHash(h) , WKeyEqual(equal) { ROBIN_HOOD_TRACE(this) insert(initlist.begin(), initlist.end()); } Table(Table&& o) noexcept : WHash(std::move(static_cast<WHash&>(o))) , WKeyEqual(std::move(static_cast<WKeyEqual&>(o))) , DataPool(std::move(static_cast<DataPool&>(o))) { ROBIN_HOOD_TRACE(this) if (o.mMask) { mHashMultiplier = std::move(o.mHashMultiplier); mKeyVals = std::move(o.mKeyVals); mInfo = std::move(o.mInfo); mNumElements = std::move(o.mNumElements); mMask = std::move(o.mMask); mMaxNumElementsAllowed = std::move(o.mMaxNumElementsAllowed); mInfoInc = std::move(o.mInfoInc); mInfoHashShift = std::move(o.mInfoHashShift); // set other's mask to 0 so its destructor won't do anything o.init(); } } Table& operator=(Table&& o) noexcept { ROBIN_HOOD_TRACE(this) if (&o != this) { if (o.mMask) { // only move stuff if the other map actually has some data destroy(); mHashMultiplier = std::move(o.mHashMultiplier); mKeyVals = std::move(o.mKeyVals); mInfo = std::move(o.mInfo); mNumElements = std::move(o.mNumElements); mMask = std::move(o.mMask); mMaxNumElementsAllowed = std::move(o.mMaxNumElementsAllowed); mInfoInc = std::move(o.mInfoInc); mInfoHashShift = std::move(o.mInfoHashShift); WHash::operator=(std::move(static_cast<WHash&>(o))); WKeyEqual::operator=(std::move(static_cast<WKeyEqual&>(o))); DataPool::operator=(std::move(static_cast<DataPool&>(o))); o.init(); } else { // nothing in the other map => just clear us. clear(); } } return *this; } Table(const Table& o) : WHash(static_cast<const WHash&>(o)) , WKeyEqual(static_cast<const WKeyEqual&>(o)) , DataPool(static_cast<const DataPool&>(o)) { ROBIN_HOOD_TRACE(this) if (!o.empty()) { // not empty: create an exact copy. it is also possible to just iterate through all // elements and insert them, but copying is probably faster. auto const numElementsWithBuffer = calcNumElementsWithBuffer(o.mMask + 1); auto const numBytesTotal = calcNumBytesTotal(numElementsWithBuffer); ROBIN_HOOD_LOG("std::malloc " << numBytesTotal << " = calcNumBytesTotal(" << numElementsWithBuffer << ")") mHashMultiplier = o.mHashMultiplier; mKeyVals = static_cast<Node*>( detail::assertNotNull<std::bad_alloc>(std::malloc(numBytesTotal))); // no need for calloc because clonData does memcpy mInfo = reinterpret_cast<uint8_t*>(mKeyVals + numElementsWithBuffer); mNumElements = o.mNumElements; mMask = o.mMask; mMaxNumElementsAllowed = o.mMaxNumElementsAllowed; mInfoInc = o.mInfoInc; mInfoHashShift = o.mInfoHashShift; cloneData(o); } } // Creates a copy of the given map. Copy constructor of each entry is used. // Not sure why clang-tidy thinks this doesn't handle self assignment, it does // NOLINTNEXTLINE(bugprone-unhandled-self-assignment,cert-oop54-cpp) Table& operator=(Table const& o) { ROBIN_HOOD_TRACE(this) if (&o == this) { // prevent assigning of itself return *this; } // we keep using the old allocator and not assign the new one, because we want to keep // the memory available. when it is the same size. if (o.empty()) { if (0 == mMask) { // nothing to do, we are empty too return *this; } // not empty: destroy what we have there // clear also resets mInfo to 0, that's sometimes not necessary. destroy(); init(); WHash::operator=(static_cast<const WHash&>(o)); WKeyEqual::operator=(static_cast<const WKeyEqual&>(o)); DataPool::operator=(static_cast<DataPool const&>(o)); return *this; } // clean up old stuff Destroyer<Self, IsFlat && std::is_trivially_destructible<Node>::value>{}.nodes(*this); if (mMask != o.mMask) { // no luck: we don't have the same array size allocated, so we need to realloc. if (0 != mMask) { // only deallocate if we actually have data! ROBIN_HOOD_LOG("std::free") std::free(mKeyVals); } auto const numElementsWithBuffer = calcNumElementsWithBuffer(o.mMask + 1); auto const numBytesTotal = calcNumBytesTotal(numElementsWithBuffer); ROBIN_HOOD_LOG("std::malloc " << numBytesTotal << " = calcNumBytesTotal(" << numElementsWithBuffer << ")") mKeyVals = static_cast<Node*>( detail::assertNotNull<std::bad_alloc>(std::malloc(numBytesTotal))); // no need for calloc here because cloneData performs a memcpy. mInfo = reinterpret_cast<uint8_t*>(mKeyVals + numElementsWithBuffer); // sentinel is set in cloneData } WHash::operator=(static_cast<const WHash&>(o)); WKeyEqual::operator=(static_cast<const WKeyEqual&>(o)); DataPool::operator=(static_cast<DataPool const&>(o)); mHashMultiplier = o.mHashMultiplier; mNumElements = o.mNumElements; mMask = o.mMask; mMaxNumElementsAllowed = o.mMaxNumElementsAllowed; mInfoInc = o.mInfoInc; mInfoHashShift = o.mInfoHashShift; cloneData(o); return *this; } // Swaps everything between the two maps. void swap(Table& o) { ROBIN_HOOD_TRACE(this) using std::swap; swap(o, *this); } // Clears all data, without resizing. void clear() { ROBIN_HOOD_TRACE(this) if (empty()) { // don't do anything! also important because we don't want to write to // DummyInfoByte::b, even though we would just write 0 to it. return; } Destroyer<Self, IsFlat && std::is_trivially_destructible<Node>::value>{}.nodes(*this); auto const numElementsWithBuffer = calcNumElementsWithBuffer(mMask + 1); // clear everything, then set the sentinel again uint8_t const z = 0; std::fill(mInfo, mInfo + calcNumBytesInfo(numElementsWithBuffer), z); mInfo[numElementsWithBuffer] = 1; mInfoInc = InitialInfoInc; mInfoHashShift = InitialInfoHashShift; } // Destroys the map and all it's contents. ~Table() { ROBIN_HOOD_TRACE(this) destroy(); } // Checks if both tables contain the same entries. Order is irrelevant. bool operator==(const Table& other) const { ROBIN_HOOD_TRACE(this) if (other.size() != size()) { return false; } for (auto const& otherEntry : other) { if (!has(otherEntry)) { return false; } } return true; } bool operator!=(const Table& other) const { ROBIN_HOOD_TRACE(this) return !operator==(other); } template <typename Q = mapped_type> typename std::enable_if<!std::is_void<Q>::value, Q&>::type operator[](const key_type& key) { ROBIN_HOOD_TRACE(this) auto idxAndState = insertKeyPrepareEmptySpot(key); switch (idxAndState.second) { case InsertionState::key_found: break; case InsertionState::new_node: ::new (static_cast<void*>(&mKeyVals[idxAndState.first])) Node(*this, std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple()); break; case InsertionState::overwrite_node: mKeyVals[idxAndState.first] = Node(*this, std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple()); break; case InsertionState::overflow_error: throwOverflowError(); } return mKeyVals[idxAndState.first].getSecond(); } template <typename Q = mapped_type> typename std::enable_if<!std::is_void<Q>::value, Q&>::type operator[](key_type&& key) { ROBIN_HOOD_TRACE(this) auto idxAndState = insertKeyPrepareEmptySpot(key); switch (idxAndState.second) { case InsertionState::key_found: break; case InsertionState::new_node: ::new (static_cast<void*>(&mKeyVals[idxAndState.first])) Node(*this, std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::forward_as_tuple()); break; case InsertionState::overwrite_node: mKeyVals[idxAndState.first] = Node(*this, std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::forward_as_tuple()); break; case InsertionState::overflow_error: throwOverflowError(); } return mKeyVals[idxAndState.first].getSecond(); } template <typename Iter> void insert(Iter first, Iter last) { for (; first != last; ++first) { // value_type ctor needed because this might be called with std::pair's insert(value_type(*first)); } } void insert(std::initializer_list<value_type> ilist) { for (auto&& vt : ilist) { insert(std::move(vt)); } } template <typename... Args> std::pair<iterator, bool> emplace(Args&&... args) { ROBIN_HOOD_TRACE(this) Node n{*this, std::forward<Args>(args)...}; auto idxAndState = insertKeyPrepareEmptySpot(getFirstConst(n)); switch (idxAndState.second) { case InsertionState::key_found: n.destroy(*this); break; case InsertionState::new_node: ::new (static_cast<void*>(&mKeyVals[idxAndState.first])) Node(*this, std::move(n)); break; case InsertionState::overwrite_node: mKeyVals[idxAndState.first] = std::move(n); break; case InsertionState::overflow_error: n.destroy(*this); throwOverflowError(); break; } return std::make_pair(iterator(mKeyVals + idxAndState.first, mInfo + idxAndState.first), InsertionState::key_found != idxAndState.second); } template <typename... Args> iterator emplace_hint(const_iterator position, Args&&... args) { (void)position; return emplace(std::forward<Args>(args)...).first; } template <typename... Args> std::pair<iterator, bool> try_emplace(const key_type& key, Args&&... args) { return try_emplace_impl(key, std::forward<Args>(args)...); } template <typename... Args> std::pair<iterator, bool> try_emplace(key_type&& key, Args&&... args) { return try_emplace_impl(std::move(key), std::forward<Args>(args)...); } template <typename... Args> iterator try_emplace(const_iterator hint, const key_type& key, Args&&... args) { (void)hint; return try_emplace_impl(key, std::forward<Args>(args)...).first; } template <typename... Args> iterator try_emplace(const_iterator hint, key_type&& key, Args&&... args) { (void)hint; return try_emplace_impl(std::move(key), std::forward<Args>(args)...).first; } template <typename Mapped> std::pair<iterator, bool> insert_or_assign(const key_type& key, Mapped&& obj) { return insertOrAssignImpl(key, std::forward<Mapped>(obj)); } template <typename Mapped> std::pair<iterator, bool> insert_or_assign(key_type&& key, Mapped&& obj) { return insertOrAssignImpl(std::move(key), std::forward<Mapped>(obj)); } template <typename Mapped> iterator insert_or_assign(const_iterator hint, const key_type& key, Mapped&& obj) { (void)hint; return insertOrAssignImpl(key, std::forward<Mapped>(obj)).first; } template <typename Mapped> iterator insert_or_assign(const_iterator hint, key_type&& key, Mapped&& obj) { (void)hint; return insertOrAssignImpl(std::move(key), std::forward<Mapped>(obj)).first; } std::pair<iterator, bool> insert(const value_type& keyval) { ROBIN_HOOD_TRACE(this) return emplace(keyval); } iterator insert(const_iterator hint, const value_type& keyval) { (void)hint; return emplace(keyval).first; } std::pair<iterator, bool> insert(value_type&& keyval) { return emplace(std::move(keyval)); } iterator insert(const_iterator hint, value_type&& keyval) { (void)hint; return emplace(std::move(keyval)).first; } // Returns 1 if key is found, 0 otherwise. size_t count(const key_type& key) const { // NOLINT(modernize-use-nodiscard) ROBIN_HOOD_TRACE(this) auto kv = mKeyVals + findIdx(key); if (kv != reinterpret_cast_no_cast_align_warning<Node*>(mInfo)) { return 1; } return 0; } template <typename OtherKey, typename Self_ = Self> // NOLINTNEXTLINE(modernize-use-nodiscard) typename std::enable_if<Self_::is_transparent, size_t>::type count(const OtherKey& key) const { ROBIN_HOOD_TRACE(this) auto kv = mKeyVals + findIdx(key); if (kv != reinterpret_cast_no_cast_align_warning<Node*>(mInfo)) { return 1; } return 0; } bool contains(const key_type& key) const { // NOLINT(modernize-use-nodiscard) return 1U == count(key); } template <typename OtherKey, typename Self_ = Self> // NOLINTNEXTLINE(modernize-use-nodiscard) typename std::enable_if<Self_::is_transparent, bool>::type contains(const OtherKey& key) const { return 1U == count(key); } // Returns a reference to the value found for key. // Throws std::out_of_range if element cannot be found template <typename Q = mapped_type> // NOLINTNEXTLINE(modernize-use-nodiscard) typename std::enable_if<!std::is_void<Q>::value, Q&>::type at(key_type const& key) { ROBIN_HOOD_TRACE(this) auto kv = mKeyVals + findIdx(key); if (kv == reinterpret_cast_no_cast_align_warning<Node*>(mInfo)) { doThrow<std::out_of_range>("key not found"); } return kv->getSecond(); } // Returns a reference to the value found for key. // Throws std::out_of_range if element cannot be found template <typename Q = mapped_type> // NOLINTNEXTLINE(modernize-use-nodiscard) typename std::enable_if<!std::is_void<Q>::value, Q const&>::type at(key_type const& key) const { ROBIN_HOOD_TRACE(this) auto kv = mKeyVals + findIdx(key); if (kv == reinterpret_cast_no_cast_align_warning<Node*>(mInfo)) { doThrow<std::out_of_range>("key not found"); } return kv->getSecond(); } const_iterator find(const key_type& key) const { // NOLINT(modernize-use-nodiscard) ROBIN_HOOD_TRACE(this) const size_t idx = findIdx(key); return const_iterator{mKeyVals + idx, mInfo + idx}; } template <typename OtherKey> const_iterator find(const OtherKey& key, is_transparent_tag /*unused*/) const { ROBIN_HOOD_TRACE(this) const size_t idx = findIdx(key); return const_iterator{mKeyVals + idx, mInfo + idx}; } template <typename OtherKey, typename Self_ = Self> typename std::enable_if<Self_::is_transparent, // NOLINT(modernize-use-nodiscard) const_iterator>::type // NOLINT(modernize-use-nodiscard) find(const OtherKey& key) const { // NOLINT(modernize-use-nodiscard) ROBIN_HOOD_TRACE(this) const size_t idx = findIdx(key); return const_iterator{mKeyVals + idx, mInfo + idx}; } iterator find(const key_type& key) { ROBIN_HOOD_TRACE(this) const size_t idx = findIdx(key); return iterator{mKeyVals + idx, mInfo + idx}; } template <typename OtherKey> iterator find(const OtherKey& key, is_transparent_tag /*unused*/) { ROBIN_HOOD_TRACE(this) const size_t idx = findIdx(key); return iterator{mKeyVals + idx, mInfo + idx}; } template <typename OtherKey, typename Self_ = Self> typename std::enable_if<Self_::is_transparent, iterator>::type find(const OtherKey& key) { ROBIN_HOOD_TRACE(this) const size_t idx = findIdx(key); return iterator{mKeyVals + idx, mInfo + idx}; } iterator begin() { ROBIN_HOOD_TRACE(this) if (empty()) { return end(); } return iterator(mKeyVals, mInfo, fast_forward_tag{}); } const_iterator begin() const { // NOLINT(modernize-use-nodiscard) ROBIN_HOOD_TRACE(this) return cbegin(); } const_iterator cbegin() const { // NOLINT(modernize-use-nodiscard) ROBIN_HOOD_TRACE(this) if (empty()) { return cend(); } return const_iterator(mKeyVals, mInfo, fast_forward_tag{}); } iterator end() { ROBIN_HOOD_TRACE(this) // no need to supply valid info pointer: end() must not be dereferenced, and only node // pointer is compared. return iterator{reinterpret_cast_no_cast_align_warning<Node*>(mInfo), nullptr}; } const_iterator end() const { // NOLINT(modernize-use-nodiscard) ROBIN_HOOD_TRACE(this) return cend(); } const_iterator cend() const { // NOLINT(modernize-use-nodiscard) ROBIN_HOOD_TRACE(this) return const_iterator{reinterpret_cast_no_cast_align_warning<Node*>(mInfo), nullptr}; } iterator erase(const_iterator pos) { ROBIN_HOOD_TRACE(this) // its safe to perform const cast here // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) return erase(iterator{const_cast<Node*>(pos.mKeyVals), const_cast<uint8_t*>(pos.mInfo)}); } // Erases element at pos, returns iterator to the next element. iterator erase(iterator pos) { ROBIN_HOOD_TRACE(this) // we assume that pos always points to a valid entry, and not end(). auto const idx = static_cast<size_t>(pos.mKeyVals - mKeyVals); shiftDown(idx); --mNumElements; if (*pos.mInfo) { // we've backward shifted, return this again return pos; } // no backward shift, return next element return ++pos; } size_t erase(const key_type& key) { ROBIN_HOOD_TRACE(this) size_t idx{}; InfoType info{}; keyToIdx(key, &idx, &info); // check while info matches with the source idx do { if (info == mInfo[idx] && WKeyEqual::operator()(key, mKeyVals[idx].getFirst())) { shiftDown(idx); --mNumElements; return 1; } next(&info, &idx); } while (info <= mInfo[idx]); // nothing found to delete return 0; } // reserves space for the specified number of elements. Makes sure the old data fits. // exactly the same as reserve(c). void rehash(size_t c) { // forces a reserve reserve(c, true); } // reserves space for the specified number of elements. Makes sure the old data fits. // Exactly the same as rehash(c). Use rehash(0) to shrink to fit. void reserve(size_t c) { // reserve, but don't force rehash reserve(c, false); } // If possible reallocates the map to a smaller one. This frees the underlying table. // Does not do anything if load_factor is too large for decreasing the table's size. void compact() { ROBIN_HOOD_TRACE(this) auto newSize = InitialNumElements; while (calcMaxNumElementsAllowed(newSize) < mNumElements && newSize != 0) { newSize *= 2; } if (ROBIN_HOOD_UNLIKELY(newSize == 0)) { throwOverflowError(); } ROBIN_HOOD_LOG("newSize > mMask + 1: " << newSize << " > " << mMask << " + 1") // only actually do anything when the new size is bigger than the old one. This prevents to // continuously allocate for each reserve() call. if (newSize < mMask + 1) { rehashPowerOfTwo(newSize, true); } } size_type size() const noexcept { // NOLINT(modernize-use-nodiscard) ROBIN_HOOD_TRACE(this) return mNumElements; } size_type max_size() const noexcept { // NOLINT(modernize-use-nodiscard) ROBIN_HOOD_TRACE(this) return static_cast<size_type>(-1); } ROBIN_HOOD(NODISCARD) bool empty() const noexcept { ROBIN_HOOD_TRACE(this) return 0 == mNumElements; } float max_load_factor() const noexcept { // NOLINT(modernize-use-nodiscard) ROBIN_HOOD_TRACE(this) return MaxLoadFactor100 / 100.0F; } // Average number of elements per bucket. Since we allow only 1 per bucket float load_factor() const noexcept { // NOLINT(modernize-use-nodiscard) ROBIN_HOOD_TRACE(this) return static_cast<float>(size()) / static_cast<float>(mMask + 1); } ROBIN_HOOD(NODISCARD) size_t mask() const noexcept { ROBIN_HOOD_TRACE(this) return mMask; } ROBIN_HOOD(NODISCARD) size_t calcMaxNumElementsAllowed(size_t maxElements) const noexcept { if (ROBIN_HOOD_LIKELY(maxElements <= (std::numeric_limits<size_t>::max)() / 100)) { return maxElements * MaxLoadFactor100 / 100; } // we might be a bit inprecise, but since maxElements is quite large that doesn't matter return (maxElements / 100) * MaxLoadFactor100; } ROBIN_HOOD(NODISCARD) size_t calcNumBytesInfo(size_t numElements) const noexcept { // we add a uint64_t, which houses the sentinel (first byte) and padding so we can load // 64bit types. return numElements + sizeof(uint64_t); } ROBIN_HOOD(NODISCARD) size_t calcNumElementsWithBuffer(size_t numElements) const noexcept { auto maxNumElementsAllowed = calcMaxNumElementsAllowed(numElements); return numElements + (std::min)(maxNumElementsAllowed, (static_cast<size_t>(0xFF))); } // calculation only allowed for 2^n values ROBIN_HOOD(NODISCARD) size_t calcNumBytesTotal(size_t numElements) const { #if ROBIN_HOOD(BITNESS) == 64 return numElements * sizeof(Node) + calcNumBytesInfo(numElements); #else // make sure we're doing 64bit operations, so we are at least safe against 32bit overflows. auto const ne = static_cast<uint64_t>(numElements); auto const s = static_cast<uint64_t>(sizeof(Node)); auto const infos = static_cast<uint64_t>(calcNumBytesInfo(numElements)); auto const total64 = ne * s + infos; auto const total = static_cast<size_t>(total64); if (ROBIN_HOOD_UNLIKELY(static_cast<uint64_t>(total) != total64)) { throwOverflowError(); } return total; #endif } private: template <typename Q = mapped_type> ROBIN_HOOD(NODISCARD) typename std::enable_if<!std::is_void<Q>::value, bool>::type has(const value_type& e) const { ROBIN_HOOD_TRACE(this) auto it = find(e.first); return it != end() && it->second == e.second; } template <typename Q = mapped_type> ROBIN_HOOD(NODISCARD) typename std::enable_if<std::is_void<Q>::value, bool>::type has(const value_type& e) const { ROBIN_HOOD_TRACE(this) return find(e) != end(); } void reserve(size_t c, bool forceRehash) { ROBIN_HOOD_TRACE(this) auto const minElementsAllowed = (std::max)(c, mNumElements); auto newSize = InitialNumElements; while (calcMaxNumElementsAllowed(newSize) < minElementsAllowed && newSize != 0) { newSize *= 2; } if (ROBIN_HOOD_UNLIKELY(newSize == 0)) { throwOverflowError(); } ROBIN_HOOD_LOG("newSize > mMask + 1: " << newSize << " > " << mMask << " + 1") // only actually do anything when the new size is bigger than the old one. This prevents to // continuously allocate for each reserve() call. if (forceRehash || newSize > mMask + 1) { rehashPowerOfTwo(newSize, false); } } // reserves space for at least the specified number of elements. // only works if numBuckets if power of two // True on success, false otherwise void rehashPowerOfTwo(size_t numBuckets, bool forceFree) { ROBIN_HOOD_TRACE(this) Node* const oldKeyVals = mKeyVals; uint8_t const* const oldInfo = mInfo; const size_t oldMaxElementsWithBuffer = calcNumElementsWithBuffer(mMask + 1); // resize operation: move stuff initData(numBuckets); if (oldMaxElementsWithBuffer > 1) { for (size_t i = 0; i < oldMaxElementsWithBuffer; ++i) { if (oldInfo[i] != 0) { // might throw an exception, which is really bad since we are in the middle of // moving stuff. insert_move(std::move(oldKeyVals[i])); // destroy the node but DON'T destroy the data. oldKeyVals[i].~Node(); } } // this check is not necessary as it's guarded by the previous if, but it helps // silence g++'s overeager "attempt to free a non-heap object 'map' // [-Werror=free-nonheap-object]" warning. if (oldKeyVals != reinterpret_cast_no_cast_align_warning<Node*>(&mMask)) { // don't destroy old data: put it into the pool instead if (forceFree) { std::free(oldKeyVals); } else { DataPool::addOrFree(oldKeyVals, calcNumBytesTotal(oldMaxElementsWithBuffer)); } } } } ROBIN_HOOD(NOINLINE) void throwOverflowError() const { #if ROBIN_HOOD(HAS_EXCEPTIONS) throw std::overflow_error("robin_hood::map overflow"); #else abort(); #endif } template <typename OtherKey, typename... Args> std::pair<iterator, bool> try_emplace_impl(OtherKey&& key, Args&&... args) { ROBIN_HOOD_TRACE(this) auto idxAndState = insertKeyPrepareEmptySpot(key); switch (idxAndState.second) { case InsertionState::key_found: break; case InsertionState::new_node: ::new (static_cast<void*>(&mKeyVals[idxAndState.first])) Node( *this, std::piecewise_construct, std::forward_as_tuple(std::forward<OtherKey>(key)), std::forward_as_tuple(std::forward<Args>(args)...)); break; case InsertionState::overwrite_node: mKeyVals[idxAndState.first] = Node(*this, std::piecewise_construct, std::forward_as_tuple(std::forward<OtherKey>(key)), std::forward_as_tuple(std::forward<Args>(args)...)); break; case InsertionState::overflow_error: throwOverflowError(); break; } return std::make_pair(iterator(mKeyVals + idxAndState.first, mInfo + idxAndState.first), InsertionState::key_found != idxAndState.second); } template <typename OtherKey, typename Mapped> std::pair<iterator, bool> insertOrAssignImpl(OtherKey&& key, Mapped&& obj) { ROBIN_HOOD_TRACE(this) auto idxAndState = insertKeyPrepareEmptySpot(key); switch (idxAndState.second) { case InsertionState::key_found: mKeyVals[idxAndState.first].getSecond() = std::forward<Mapped>(obj); break; case InsertionState::new_node: ::new (static_cast<void*>(&mKeyVals[idxAndState.first])) Node( *this, std::piecewise_construct, std::forward_as_tuple(std::forward<OtherKey>(key)), std::forward_as_tuple(std::forward<Mapped>(obj))); break; case InsertionState::overwrite_node: mKeyVals[idxAndState.first] = Node(*this, std::piecewise_construct, std::forward_as_tuple(std::forward<OtherKey>(key)), std::forward_as_tuple(std::forward<Mapped>(obj))); break; case InsertionState::overflow_error: throwOverflowError(); break; } return std::make_pair(iterator(mKeyVals + idxAndState.first, mInfo + idxAndState.first), InsertionState::key_found != idxAndState.second); } void initData(size_t max_elements) { mNumElements = 0; mMask = max_elements - 1; mMaxNumElementsAllowed = calcMaxNumElementsAllowed(max_elements); auto const numElementsWithBuffer = calcNumElementsWithBuffer(max_elements); // calloc also zeroes everything auto const numBytesTotal = calcNumBytesTotal(numElementsWithBuffer); ROBIN_HOOD_LOG("std::calloc " << numBytesTotal << " = calcNumBytesTotal(" << numElementsWithBuffer << ")") mKeyVals = reinterpret_cast<Node*>( detail::assertNotNull<std::bad_alloc>(std::calloc(1, numBytesTotal))); mInfo = reinterpret_cast<uint8_t*>(mKeyVals + numElementsWithBuffer); // set sentinel mInfo[numElementsWithBuffer] = 1; mInfoInc = InitialInfoInc; mInfoHashShift = InitialInfoHashShift; } enum class InsertionState { overflow_error, key_found, new_node, overwrite_node }; // Finds key, and if not already present prepares a spot where to pot the key & value. // This potentially shifts nodes out of the way, updates mInfo and number of inserted // elements, so the only operation left to do is create/assign a new node at that spot. template <typename OtherKey> std::pair<size_t, InsertionState> insertKeyPrepareEmptySpot(OtherKey&& key) { for (int i = 0; i < 256; ++i) { size_t idx{}; InfoType info{}; keyToIdx(key, &idx, &info); nextWhileLess(&info, &idx); // while we potentially have a match while (info == mInfo[idx]) { if (WKeyEqual::operator()(key, mKeyVals[idx].getFirst())) { // key already exists, do NOT insert. // see http://en.cppreference.com/w/cpp/container/unordered_map/insert return std::make_pair(idx, InsertionState::key_found); } next(&info, &idx); } // unlikely that this evaluates to true if (ROBIN_HOOD_UNLIKELY(mNumElements >= mMaxNumElementsAllowed)) { if (!increase_size()) { return std::make_pair(size_t(0), InsertionState::overflow_error); } continue; } // key not found, so we are now exactly where we want to insert it. auto const insertion_idx = idx; auto const insertion_info = info; if (ROBIN_HOOD_UNLIKELY(insertion_info + mInfoInc > 0xFF)) { mMaxNumElementsAllowed = 0; } // find an empty spot while (0 != mInfo[idx]) { next(&info, &idx); } if (idx != insertion_idx) { shiftUp(idx, insertion_idx); } // put at empty spot mInfo[insertion_idx] = static_cast<uint8_t>(insertion_info); ++mNumElements; return std::make_pair(insertion_idx, idx == insertion_idx ? InsertionState::new_node : InsertionState::overwrite_node); } // enough attempts failed, so finally give up. return std::make_pair(size_t(0), InsertionState::overflow_error); } bool try_increase_info() { ROBIN_HOOD_LOG("mInfoInc=" << mInfoInc << ", numElements=" << mNumElements << ", maxNumElementsAllowed=" << calcMaxNumElementsAllowed(mMask + 1)) if (mInfoInc <= 2) { // need to be > 2 so that shift works (otherwise undefined behavior!) return false; } // we got space left, try to make info smaller mInfoInc = static_cast<uint8_t>(mInfoInc >> 1U); // remove one bit of the hash, leaving more space for the distance info. // This is extremely fast because we can operate on 8 bytes at once. ++mInfoHashShift; auto const numElementsWithBuffer = calcNumElementsWithBuffer(mMask + 1); for (size_t i = 0; i < numElementsWithBuffer; i += 8) { auto val = unaligned_load<uint64_t>(mInfo + i); val = (val >> 1U) & UINT64_C(0x7f7f7f7f7f7f7f7f); std::memcpy(mInfo + i, &val, sizeof(val)); } // update sentinel, which might have been cleared out! mInfo[numElementsWithBuffer] = 1; mMaxNumElementsAllowed = calcMaxNumElementsAllowed(mMask + 1); return true; } // True if resize was possible, false otherwise bool increase_size() { // nothing allocated yet? just allocate InitialNumElements if (0 == mMask) { initData(InitialNumElements); return true; } auto const maxNumElementsAllowed = calcMaxNumElementsAllowed(mMask + 1); if (mNumElements < maxNumElementsAllowed && try_increase_info()) { return true; } ROBIN_HOOD_LOG("mNumElements=" << mNumElements << ", maxNumElementsAllowed=" << maxNumElementsAllowed << ", load=" << (static_cast<double>(mNumElements) * 100.0 / (static_cast<double>(mMask) + 1))) if (mNumElements * 2 < calcMaxNumElementsAllowed(mMask + 1)) { // we have to resize, even though there would still be plenty of space left! // Try to rehash instead. Delete freed memory so we don't steadyily increase mem in case // we have to rehash a few times nextHashMultiplier(); rehashPowerOfTwo(mMask + 1, true); } else { // we've reached the capacity of the map, so the hash seems to work nice. Keep using it. rehashPowerOfTwo((mMask + 1) * 2, false); } return true; } void nextHashMultiplier() { // adding an *even* number, so that the multiplier will always stay odd. This is necessary // so that the hash stays a mixing function (and thus doesn't have any information loss). mHashMultiplier += UINT64_C(0xc4ceb9fe1a85ec54); } void destroy() { if (0 == mMask) { // don't deallocate! return; } Destroyer<Self, IsFlat && std::is_trivially_destructible<Node>::value>{} .nodesDoNotDeallocate(*this); // This protection against not deleting mMask shouldn't be needed as it's sufficiently // protected with the 0==mMask check, but I have this anyways because g++ 7 otherwise // reports a compile error: attempt to free a non-heap object 'fm' // [-Werror=free-nonheap-object] if (mKeyVals != reinterpret_cast_no_cast_align_warning<Node*>(&mMask)) { ROBIN_HOOD_LOG("std::free") std::free(mKeyVals); } } void init() noexcept { mKeyVals = reinterpret_cast_no_cast_align_warning<Node*>(&mMask); mInfo = reinterpret_cast<uint8_t*>(&mMask); mNumElements = 0; mMask = 0; mMaxNumElementsAllowed = 0; mInfoInc = InitialInfoInc; mInfoHashShift = InitialInfoHashShift; } // members are sorted so no padding occurs uint64_t mHashMultiplier = UINT64_C(0xc4ceb9fe1a85ec53); // 8 byte 8 Node* mKeyVals = reinterpret_cast_no_cast_align_warning<Node*>(&mMask); // 8 byte 16 uint8_t* mInfo = reinterpret_cast<uint8_t*>(&mMask); // 8 byte 24 size_t mNumElements = 0; // 8 byte 32 size_t mMask = 0; // 8 byte 40 size_t mMaxNumElementsAllowed = 0; // 8 byte 48 InfoType mInfoInc = InitialInfoInc; // 4 byte 52 InfoType mInfoHashShift = InitialInfoHashShift; // 4 byte 56 // 16 byte 56 if NodeAllocator }; } // namespace detail // map template <typename Key, typename T, typename Hash = hash<Key>, typename KeyEqual = std::equal_to<Key>, size_t MaxLoadFactor100 = 80> using unordered_flat_map = detail::Table<true, MaxLoadFactor100, Key, T, Hash, KeyEqual>; template <typename Key, typename T, typename Hash = hash<Key>, typename KeyEqual = std::equal_to<Key>, size_t MaxLoadFactor100 = 80> using unordered_node_map = detail::Table<false, MaxLoadFactor100, Key, T, Hash, KeyEqual>; template <typename Key, typename T, typename Hash = hash<Key>, typename KeyEqual = std::equal_to<Key>, size_t MaxLoadFactor100 = 80> using unordered_map = detail::Table<sizeof(robin_hood::pair<Key, T>) <= sizeof(size_t) * 6 && std::is_nothrow_move_constructible<robin_hood::pair<Key, T>>::value && std::is_nothrow_move_assignable<robin_hood::pair<Key, T>>::value, MaxLoadFactor100, Key, T, Hash, KeyEqual>; // set template <typename Key, typename Hash = hash<Key>, typename KeyEqual = std::equal_to<Key>, size_t MaxLoadFactor100 = 80> using unordered_flat_set = detail::Table<true, MaxLoadFactor100, Key, void, Hash, KeyEqual>; template <typename Key, typename Hash = hash<Key>, typename KeyEqual = std::equal_to<Key>, size_t MaxLoadFactor100 = 80> using unordered_node_set = detail::Table<false, MaxLoadFactor100, Key, void, Hash, KeyEqual>; template <typename Key, typename Hash = hash<Key>, typename KeyEqual = std::equal_to<Key>, size_t MaxLoadFactor100 = 80> using unordered_set = detail::Table<sizeof(Key) <= sizeof(size_t) * 6 && std::is_nothrow_move_constructible<Key>::value && std::is_nothrow_move_assignable<Key>::value, MaxLoadFactor100, Key, void, Hash, KeyEqual>; } // namespace robin_hood #endif /** * 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> 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>}}; } // namespace detail /** * 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."); }; } // namespace ah } // namespace tsl #endif /** * 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_HASH_H #define TSL_ARRAY_HASH_H #include <algorithm> #include <cassert> #include <cmath> #include <cstddef> #include <cstdint> #include <cstdlib> #include <cstring> #include <iterator> #include <limits> #include <memory> #include <stdexcept> #include <type_traits> #include <utility> #include <vector> /* * __has_include is a bit useless * (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79433), check also __cplusplus * version. */ #ifdef __has_include #if __has_include(<string_view>) && __cplusplus >= 201703L #define TSL_AH_HAS_STRING_VIEW #endif #endif #ifdef TSL_AH_HAS_STRING_VIEW #include <string_view> #endif #ifdef TSL_DEBUG #define tsl_ah_assert(expr) assert(expr) #else #define tsl_ah_assert(expr) (static_cast<void>(0)) #endif /** * Implementation of the array hash structure described in the * "Cache-conscious collision resolution in string hash tables." (Askitis * Nikolas and Justin Zobel, 2005) paper. */ namespace tsl { namespace ah { template <class CharT> struct str_hash { #ifdef TSL_AH_HAS_STRING_VIEW std::size_t operator()(const CharT* key, std::size_t key_size) const { return std::hash<std::basic_string_view<CharT>>()( std::basic_string_view<CharT>(key, key_size)); } #else /** * FNV-1a hash */ std::size_t operator()(const CharT* key, std::size_t key_size) const { static const std::size_t init = std::size_t( (sizeof(std::size_t) == 8) ? 0xcbf29ce484222325 : 0x811c9dc5); static const std::size_t multiplier = std::size_t((sizeof(std::size_t) == 8) ? 0x100000001b3 : 0x1000193); std::size_t hash = init; for (std::size_t i = 0; i < key_size; ++i) { hash ^= key[i]; hash *= multiplier; } return hash; } #endif }; template <class CharT> struct str_equal { bool operator()(const CharT* key_lhs, std::size_t key_size_lhs, const CharT* key_rhs, std::size_t key_size_rhs) const { if (key_size_lhs != key_size_rhs) { return false; } else { return std::memcmp(key_lhs, key_rhs, key_size_lhs * sizeof(CharT)) == 0; } } }; } // namespace ah namespace detail_array_hash { template <typename T, typename = void> struct is_iterator : std::false_type {}; template <typename T> struct is_iterator<T, typename std::enable_if<!std::is_same< typename std::iterator_traits<T>::iterator_category, void>::value>::type> : std::true_type {}; static constexpr bool is_power_of_two(std::size_t value) { return value != 0 && (value & (value - 1)) == 0; } template <typename T, typename U> static T numeric_cast(U value, const char* error_message = "numeric_cast() failed.") { T ret = static_cast<T>(value); if (static_cast<U>(ret) != value) { throw std::runtime_error(error_message); } const bool is_same_signedness = (std::is_unsigned<T>::value && std::is_unsigned<U>::value) || (std::is_signed<T>::value && std::is_signed<U>::value); if (!is_same_signedness && (ret < T{}) != (value < U{})) { throw std::runtime_error(error_message); } return ret; } /** * Fixed size type used to represent size_type values on serialization. Need to * be big enough to represent a std::size_t on 32 and 64 bits platforms, and * must be the same size on both platforms. */ using slz_size_type = std::uint64_t; template <class T, class Deserializer> static T deserialize_value(Deserializer& deserializer) { // MSVC < 2017 is not conformant, circumvent the problem by removing the // template keyword #if defined(_MSC_VER) && _MSC_VER < 1910 return deserializer.Deserializer::operator()<T>(); #else return deserializer.Deserializer::template operator()<T>(); #endif } /** * For each string in the bucket, store the size of the string, the chars of the * string and T, if it's not void. T should be either void or an unsigned type. * * End the buffer with END_OF_BUCKET flag. END_OF_BUCKET has the same type as * the string size variable. * * m_buffer (CharT*): * | size of str1 (KeySizeT) | str1 (const CharT*) | value (T if T != void) | * ... | | size of strN (KeySizeT) | strN (const CharT*) | value (T if T != * void) | END_OF_BUCKET (KeySizeT) | * * m_buffer is null if there is no string in the bucket. * * KeySizeT and T are extended to be a multiple of CharT when stored in the * buffer. * * Use std::malloc and std::free instead of new and delete so we can have access * to std::realloc. */ template <class CharT, class T, class KeyEqual, class KeySizeT, bool StoreNullTerminator> class array_bucket { template <typename U> using has_mapped_type = typename std::integral_constant<bool, !std::is_same<U, void>::value>; static_assert(!has_mapped_type<T>::value || std::is_unsigned<T>::value, "T should be either void or an unsigned type."); static_assert(std::is_unsigned<KeySizeT>::value, "KeySizeT should be an unsigned type."); public: template <bool IsConst> class array_bucket_iterator; using char_type = CharT; using key_size_type = KeySizeT; using mapped_type = T; using size_type = std::size_t; using key_equal = KeyEqual; using iterator = array_bucket_iterator<false>; using const_iterator = array_bucket_iterator<true>; static_assert(sizeof(KeySizeT) <= sizeof(size_type), "sizeof(KeySizeT) should be <= sizeof(std::size_t;)"); static_assert(std::is_unsigned<size_type>::value, ""); private: /** * Return how much space in bytes the type U will take when stored in the * buffer. As the buffer is of type CharT, U may take more space than * sizeof(U). * * Example: sizeof(CharT) = 4, sizeof(U) = 2 => U will take 4 bytes in the * buffer instead of 2. */ template <typename U> static constexpr size_type sizeof_in_buff() noexcept { static_assert(is_power_of_two(sizeof(U)), "sizeof(U) should be a power of two."); static_assert(is_power_of_two(sizeof(CharT)), "sizeof(CharT) should be a power of two."); return std::max(sizeof(U), sizeof(CharT)); } /** * Same as sizeof_in_buff<U>, but instead of returning the size in bytes * return it in term of sizeof(CharT). */ template <typename U> static constexpr size_type size_as_char_t() noexcept { return sizeof_in_buff<U>() / sizeof(CharT); } static key_size_type read_key_size(const CharT* buffer) noexcept { key_size_type key_size; std::memcpy(&key_size, buffer, sizeof(key_size)); return key_size; } static mapped_type read_value(const CharT* buffer) noexcept { mapped_type value; std::memcpy(&value, buffer, sizeof(value)); return value; } static bool is_end_of_bucket(const CharT* buffer) noexcept { return read_key_size(buffer) == END_OF_BUCKET; } public: /** * Return the size required for an entry with a key of size 'key_size'. */ template <class U = T, typename std::enable_if< !has_mapped_type<U>::value>::type* = nullptr> static size_type entry_required_bytes(size_type key_size) noexcept { return sizeof_in_buff<key_size_type>() + (key_size + KEY_EXTRA_SIZE) * sizeof(CharT); } template <class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr> static size_type entry_required_bytes(size_type key_size) noexcept { return sizeof_in_buff<key_size_type>() + (key_size + KEY_EXTRA_SIZE) * sizeof(CharT) + sizeof_in_buff<mapped_type>(); } private: /** * Return the size of the current entry in buffer. */ static size_type entry_size_bytes(const CharT* buffer) noexcept { return entry_required_bytes(read_key_size(buffer)); } public: template <bool IsConst> class array_bucket_iterator { friend class array_bucket; using buffer_type = typename std::conditional<IsConst, const CharT, CharT>::type; explicit array_bucket_iterator(buffer_type* position) noexcept : m_position(position) {} public: using iterator_category = std::forward_iterator_tag; using value_type = void; using difference_type = std::ptrdiff_t; using reference = void; using pointer = void; public: array_bucket_iterator() noexcept : m_position(nullptr) {} const CharT* key() const { return m_position + size_as_char_t<key_size_type>(); } size_type key_size() const { return read_key_size(m_position); } template <class U = T, typename std::enable_if< has_mapped_type<U>::value>::type* = nullptr> U value() const { return read_value(m_position + size_as_char_t<key_size_type>() + key_size() + KEY_EXTRA_SIZE); } template <class U = T, typename std::enable_if< has_mapped_type<U>::value && !IsConst && std::is_same<U, T>::value>::type* = nullptr> void set_value(U value) noexcept { std::memcpy(m_position + size_as_char_t<key_size_type>() + key_size() + KEY_EXTRA_SIZE, &value, sizeof(value)); } array_bucket_iterator& operator++() { m_position += entry_size_bytes(m_position) / sizeof(CharT); if (is_end_of_bucket(m_position)) { m_position = nullptr; } return *this; } array_bucket_iterator operator++(int) { array_bucket_iterator tmp(*this); ++*this; return tmp; } friend bool operator==(const array_bucket_iterator& lhs, const array_bucket_iterator& rhs) { return lhs.m_position == rhs.m_position; } friend bool operator!=(const array_bucket_iterator& lhs, const array_bucket_iterator& rhs) { return !(lhs == rhs); } private: buffer_type* m_position; }; static iterator end_it() noexcept { return iterator(nullptr); } static const_iterator cend_it() noexcept { return const_iterator(nullptr); } public: array_bucket() : m_buffer(nullptr) {} /** * Reserve 'size' in the buffer of the bucket. The created bucket is empty. */ array_bucket(std::size_t size) : m_buffer(nullptr) { if (size == 0) { return; } m_buffer = static_cast<CharT*>(std::malloc( size * sizeof(CharT) + sizeof_in_buff<decltype(END_OF_BUCKET)>())); if (m_buffer == nullptr) { throw std::bad_alloc(); } const auto end_of_bucket = END_OF_BUCKET; std::memcpy(m_buffer, &end_of_bucket, sizeof(end_of_bucket)); } ~array_bucket() { clear(); } array_bucket(const array_bucket& other) { if (other.m_buffer == nullptr) { m_buffer = nullptr; return; } const size_type other_buffer_size = other.size(); m_buffer = static_cast<CharT*>( std::malloc(other_buffer_size * sizeof(CharT) + sizeof_in_buff<decltype(END_OF_BUCKET)>())); if (m_buffer == nullptr) { throw std::bad_alloc(); } std::memcpy(m_buffer, other.m_buffer, other_buffer_size * sizeof(CharT)); const auto end_of_bucket = END_OF_BUCKET; std::memcpy(m_buffer + other_buffer_size, &end_of_bucket, sizeof(end_of_bucket)); } array_bucket(array_bucket&& other) noexcept : m_buffer(other.m_buffer) { other.m_buffer = nullptr; } array_bucket& operator=(array_bucket other) noexcept { other.swap(*this); return *this; } void swap(array_bucket& other) noexcept { std::swap(m_buffer, other.m_buffer); } iterator begin() noexcept { return iterator(m_buffer); } iterator end() noexcept { return iterator(nullptr); } const_iterator begin() const noexcept { return cbegin(); } const_iterator end() const noexcept { return cend(); } const_iterator cbegin() const noexcept { return const_iterator(m_buffer); } const_iterator cend() const noexcept { return const_iterator(nullptr); } /** * Return an iterator pointing to the key entry if presents or, if not there, * to the position past the last element of the bucket. Return end() if the * bucket has not be initialized yet. * * The boolean of the pair is set to true if the key is there, false * otherwise. */ std::pair<const_iterator, bool> find_or_end_of_bucket( const CharT* key, size_type key_size) const noexcept { if (m_buffer == nullptr) { return std::make_pair(cend(), false); } const CharT* buffer_ptr_in_out = m_buffer; const bool found = find_or_end_of_bucket_impl(key, key_size, buffer_ptr_in_out); return std::make_pair(const_iterator(buffer_ptr_in_out), found); } /** * Append the element 'key' with its potential value at the end of the bucket. * 'end_of_bucket' should point past the end of the last element in the * bucket, end() if the bucket was not initialized yet. You usually get this * value from find_or_end_of_bucket. * * Return the position where the element was actually inserted. */ template <class... ValueArgs> const_iterator append(const_iterator end_of_bucket, const CharT* key, size_type key_size, ValueArgs&&... value) { const key_size_type key_sz = as_key_size_type(key_size); if (end_of_bucket == cend()) { tsl_ah_assert(m_buffer == nullptr); const size_type buffer_size = entry_required_bytes(key_sz) + sizeof_in_buff<decltype(END_OF_BUCKET)>(); m_buffer = static_cast<CharT*>(std::malloc(buffer_size)); if (m_buffer == nullptr) { throw std::bad_alloc(); } append_impl(key, key_sz, m_buffer, std::forward<ValueArgs>(value)...); return const_iterator(m_buffer); } else { tsl_ah_assert(is_end_of_bucket(end_of_bucket.m_position)); const size_type current_size = ((end_of_bucket.m_position + size_as_char_t<decltype(END_OF_BUCKET)>()) - m_buffer) * sizeof(CharT); const size_type new_size = current_size + entry_required_bytes(key_sz); CharT* new_buffer = static_cast<CharT*>(std::realloc(m_buffer, new_size)); if (new_buffer == nullptr) { throw std::bad_alloc(); } m_buffer = new_buffer; CharT* buffer_append_pos = m_buffer + current_size / sizeof(CharT) - size_as_char_t<decltype(END_OF_BUCKET)>(); append_impl(key, key_sz, buffer_append_pos, std::forward<ValueArgs>(value)...); return const_iterator(buffer_append_pos); } } const_iterator erase(const_iterator position) noexcept { tsl_ah_assert(position.m_position != nullptr && !is_end_of_bucket(position.m_position)); // get mutable pointers CharT* start_entry = m_buffer + (position.m_position - m_buffer); CharT* start_next_entry = start_entry + entry_size_bytes(start_entry) / sizeof(CharT); CharT* end_buffer_ptr = start_next_entry; while (!is_end_of_bucket(end_buffer_ptr)) { end_buffer_ptr += entry_size_bytes(end_buffer_ptr) / sizeof(CharT); } end_buffer_ptr += size_as_char_t<decltype(END_OF_BUCKET)>(); const size_type size_to_move = (end_buffer_ptr - start_next_entry) * sizeof(CharT); std::memmove(start_entry, start_next_entry, size_to_move); if (is_end_of_bucket(m_buffer)) { clear(); return cend(); } else if (is_end_of_bucket(start_entry)) { return cend(); } else { return const_iterator(start_entry); } } /** * Return true if an element has been erased */ bool erase(const CharT* key, size_type key_size) noexcept { if (m_buffer == nullptr) { return false; } const CharT* entry_buffer_ptr_in_out = m_buffer; bool found = find_or_end_of_bucket_impl(key, key_size, entry_buffer_ptr_in_out); if (found) { erase(const_iterator(entry_buffer_ptr_in_out)); return true; } else { return false; } } /** * Bucket should be big enough and there is no check to see if the key already * exists. No check on key_size. */ template <class... ValueArgs> void append_in_reserved_bucket_no_check(const CharT* key, size_type key_size, ValueArgs&&... value) noexcept { CharT* buffer_ptr = m_buffer; while (!is_end_of_bucket(buffer_ptr)) { buffer_ptr += entry_size_bytes(buffer_ptr) / sizeof(CharT); } append_impl(key, key_size_type(key_size), buffer_ptr, std::forward<ValueArgs>(value)...); } bool empty() const noexcept { return m_buffer == nullptr || is_end_of_bucket(m_buffer); } void clear() noexcept { std::free(m_buffer); m_buffer = nullptr; } iterator mutable_iterator(const_iterator pos) noexcept { return iterator(m_buffer + (pos.m_position - m_buffer)); } template <class Serializer> void serialize(Serializer& serializer) const { const slz_size_type bucket_size = size(); tsl_ah_assert(m_buffer != nullptr || bucket_size == 0); serializer(bucket_size); serializer(m_buffer, bucket_size); } template <class Deserializer> static array_bucket deserialize(Deserializer& deserializer) { array_bucket bucket; const slz_size_type bucket_size_ds = deserialize_value<slz_size_type>(deserializer); if (bucket_size_ds == 0) { return bucket; } const std::size_t bucket_size = numeric_cast<std::size_t>( bucket_size_ds, "Deserialized bucket_size is too big."); bucket.m_buffer = static_cast<CharT*>( std::malloc(bucket_size * sizeof(CharT) + sizeof_in_buff<decltype(END_OF_BUCKET)>())); if (bucket.m_buffer == nullptr) { throw std::bad_alloc(); } deserializer(bucket.m_buffer, bucket_size); const auto end_of_bucket = END_OF_BUCKET; std::memcpy(bucket.m_buffer + bucket_size, &end_of_bucket, sizeof(end_of_bucket)); tsl_ah_assert(bucket.size() == bucket_size); return bucket; } private: key_size_type as_key_size_type(size_type key_size) const { if (key_size > MAX_KEY_SIZE) { throw std::length_error("Key is too long."); } return key_size_type(key_size); } /* * Return true if found, false otherwise. * If true, buffer_ptr_in_out points to the start of the entry matching 'key'. * If false, buffer_ptr_in_out points to where the 'key' should be inserted. * * Start search from buffer_ptr_in_out. */ bool find_or_end_of_bucket_impl(const CharT* key, size_type key_size, const CharT*& buffer_ptr_in_out) const noexcept { while (!is_end_of_bucket(buffer_ptr_in_out)) { const key_size_type buffer_key_size = read_key_size(buffer_ptr_in_out); const CharT* buffer_str = buffer_ptr_in_out + size_as_char_t<key_size_type>(); if (KeyEqual()(buffer_str, buffer_key_size, key, key_size)) { return true; } buffer_ptr_in_out += entry_size_bytes(buffer_ptr_in_out) / sizeof(CharT); } return false; } template <typename U = T, typename std::enable_if< !has_mapped_type<U>::value>::type* = nullptr> void append_impl(const CharT* key, key_size_type key_size, CharT* buffer_append_pos) noexcept { std::memcpy(buffer_append_pos, &key_size, sizeof(key_size)); buffer_append_pos += size_as_char_t<key_size_type>(); std::memcpy(buffer_append_pos, key, key_size * sizeof(CharT)); buffer_append_pos += key_size; const CharT zero = 0; std::memcpy(buffer_append_pos, &zero, KEY_EXTRA_SIZE * sizeof(CharT)); buffer_append_pos += KEY_EXTRA_SIZE; const auto end_of_bucket = END_OF_BUCKET; std::memcpy(buffer_append_pos, &end_of_bucket, sizeof(end_of_bucket)); } template <typename U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr> void append_impl( const CharT* key, key_size_type key_size, CharT* buffer_append_pos, typename array_bucket<CharT, U, KeyEqual, KeySizeT, StoreNullTerminator>::mapped_type value) noexcept { std::memcpy(buffer_append_pos, &key_size, sizeof(key_size)); buffer_append_pos += size_as_char_t<key_size_type>(); std::memcpy(buffer_append_pos, key, key_size * sizeof(CharT)); buffer_append_pos += key_size; const CharT zero = 0; std::memcpy(buffer_append_pos, &zero, KEY_EXTRA_SIZE * sizeof(CharT)); buffer_append_pos += KEY_EXTRA_SIZE; std::memcpy(buffer_append_pos, &value, sizeof(value)); buffer_append_pos += size_as_char_t<mapped_type>(); const auto end_of_bucket = END_OF_BUCKET; std::memcpy(buffer_append_pos, &end_of_bucket, sizeof(end_of_bucket)); } /** * Return the number of CharT in m_buffer. As the size of the buffer is not * stored to gain some space, the method need to find the EOF marker and is * thus in O(n). */ size_type size() const noexcept { if (m_buffer == nullptr) { return 0; } CharT* buffer_ptr = m_buffer; while (!is_end_of_bucket(buffer_ptr)) { buffer_ptr += entry_size_bytes(buffer_ptr) / sizeof(CharT); } return buffer_ptr - m_buffer; } private: static const key_size_type END_OF_BUCKET = std::numeric_limits<key_size_type>::max(); static const key_size_type KEY_EXTRA_SIZE = StoreNullTerminator ? 1 : 0; CharT* m_buffer; public: static const key_size_type MAX_KEY_SIZE = // -1 for END_OF_BUCKET key_size_type(std::numeric_limits<key_size_type>::max() - KEY_EXTRA_SIZE - 1); }; template <class T> class value_container { public: void clear() noexcept { m_values.clear(); } void reserve(std::size_t new_cap) { m_values.reserve(new_cap); } void shrink_to_fit() { m_values.shrink_to_fit(); } friend void swap(value_container& lhs, value_container& rhs) { lhs.m_values.swap(rhs.m_values); } protected: static constexpr float VECTOR_GROWTH_RATE = 1.5f; // TODO use a sparse array? or a std::deque std::vector<T> m_values; }; template <> class value_container<void> { public: void clear() noexcept {} void shrink_to_fit() {} void reserve(std::size_t /*new_cap*/) {} }; /** * If there is no value in the array_hash (in the case of a set for example), T * should be void. * * The size of a key string is limited to std::numeric_limits<KeySizeT>::max() * - 1. * * The number of elements in the map is limited to * std::numeric_limits<IndexSizeT>::max(). */ template <class CharT, class T, class Hash, class KeyEqual, bool StoreNullTerminator, class KeySizeT, class IndexSizeT, class GrowthPolicy> class array_hash : private value_container<T>, private Hash, private GrowthPolicy { private: template <typename U> using has_mapped_type = typename std::integral_constant<bool, !std::is_same<U, void>::value>; /** * If there is a mapped type in array_hash, we store the values in m_values of * value_container class and we store an index to m_values in the bucket. The * index is of type IndexSizeT. */ using array_bucket = tsl::detail_array_hash::array_bucket< CharT, typename std::conditional<has_mapped_type<T>::value, IndexSizeT, void>::type, KeyEqual, KeySizeT, StoreNullTerminator>; public: template <bool IsConst> class array_hash_iterator; using char_type = CharT; using key_size_type = KeySizeT; using index_size_type = IndexSizeT; using size_type = std::size_t; using hasher = Hash; using key_equal = KeyEqual; using iterator = array_hash_iterator<false>; using const_iterator = array_hash_iterator<true>; /* * Iterator classes */ public: template <bool IsConst> class array_hash_iterator { friend class array_hash; private: using iterator_array_bucket = typename array_bucket::const_iterator; using iterator_buckets = typename std::conditional< IsConst, typename std::vector<array_bucket>::const_iterator, typename std::vector<array_bucket>::iterator>::type; using array_hash_ptr = typename std::conditional<IsConst, const array_hash*, array_hash*>::type; public: using iterator_category = std::forward_iterator_tag; using value_type = typename std::conditional<has_mapped_type<T>::value, T, void>::type; using difference_type = std::ptrdiff_t; using reference = typename std::conditional< has_mapped_type<T>::value, typename std::conditional< IsConst, typename std::add_lvalue_reference<const T>::type, typename std::add_lvalue_reference<T>::type>::type, void>::type; using pointer = typename std::conditional< has_mapped_type<T>::value, typename std::conditional<IsConst, const T*, T*>::type, void>::type; private: array_hash_iterator(iterator_buckets buckets_iterator, iterator_array_bucket array_bucket_iterator, array_hash_ptr array_hash_p) noexcept : m_buckets_iterator(buckets_iterator), m_array_bucket_iterator(array_bucket_iterator), m_array_hash(array_hash_p) { tsl_ah_assert(m_array_hash != nullptr); } public: array_hash_iterator() noexcept : m_array_hash(nullptr) {} template <bool TIsConst = IsConst, typename std::enable_if<TIsConst>::type* = nullptr> array_hash_iterator(const array_hash_iterator<!TIsConst>& other) noexcept : m_buckets_iterator(other.m_buckets_iterator), m_array_bucket_iterator(other.m_array_bucket_iterator), m_array_hash(other.m_array_hash) {} array_hash_iterator(const array_hash_iterator& other) = default; array_hash_iterator(array_hash_iterator&& other) = default; array_hash_iterator& operator=(const array_hash_iterator& other) = default; array_hash_iterator& operator=(array_hash_iterator&& other) = default; const CharT* key() const { return m_array_bucket_iterator.key(); } size_type key_size() const { return m_array_bucket_iterator.key_size(); } #ifdef TSL_AH_HAS_STRING_VIEW std::basic_string_view<CharT> key_sv() const { return std::basic_string_view<CharT>(key(), key_size()); } #endif template <class U = T, typename std::enable_if< has_mapped_type<U>::value>::type* = nullptr> reference value() const { return this->m_array_hash->m_values[value_position()]; } template <class U = T, typename std::enable_if< has_mapped_type<U>::value>::type* = nullptr> reference operator*() const { return value(); } template <class U = T, typename std::enable_if< has_mapped_type<U>::value>::type* = nullptr> pointer operator->() const { return std::addressof(value()); } array_hash_iterator& operator++() { tsl_ah_assert(m_buckets_iterator != m_array_hash->m_buckets_data.end()); tsl_ah_assert(m_array_bucket_iterator != m_buckets_iterator->cend()); ++m_array_bucket_iterator; if (m_array_bucket_iterator == m_buckets_iterator->cend()) { do { ++m_buckets_iterator; } while (m_buckets_iterator != m_array_hash->m_buckets_data.end() && m_buckets_iterator->empty()); if (m_buckets_iterator != m_array_hash->m_buckets_data.end()) { m_array_bucket_iterator = m_buckets_iterator->cbegin(); } } return *this; } array_hash_iterator operator++(int) { array_hash_iterator tmp(*this); ++*this; return tmp; } friend bool operator==(const array_hash_iterator& lhs, const array_hash_iterator& rhs) { return lhs.m_buckets_iterator == rhs.m_buckets_iterator && lhs.m_array_bucket_iterator == rhs.m_array_bucket_iterator && lhs.m_array_hash == rhs.m_array_hash; } friend bool operator!=(const array_hash_iterator& lhs, const array_hash_iterator& rhs) { return !(lhs == rhs); } private: template <class U = T, typename std::enable_if< has_mapped_type<U>::value>::type* = nullptr> IndexSizeT value_position() const { return this->m_array_bucket_iterator.value(); } private: iterator_buckets m_buckets_iterator; iterator_array_bucket m_array_bucket_iterator; array_hash_ptr m_array_hash; }; public: array_hash(size_type bucket_count, const Hash& hash, float max_load_factor) : value_container<T>(), Hash(hash), GrowthPolicy(bucket_count), m_buckets_data(bucket_count > max_bucket_count() ? throw std::length_error( "The map exceeds its maximum bucket count.") : bucket_count), m_buckets(m_buckets_data.empty() ? static_empty_bucket_ptr() : m_buckets_data.data()), m_nb_elements(0) { this->max_load_factor(max_load_factor); } array_hash(const array_hash& other) : value_container<T>(other), Hash(other), GrowthPolicy(other), m_buckets_data(other.m_buckets_data), m_buckets(m_buckets_data.empty() ? static_empty_bucket_ptr() : m_buckets_data.data()), m_nb_elements(other.m_nb_elements), m_max_load_factor(other.m_max_load_factor), m_load_threshold(other.m_load_threshold) {} array_hash(array_hash&& other) noexcept( std::is_nothrow_move_constructible<value_container<T>>::value&& std::is_nothrow_move_constructible<Hash>::value&& std::is_nothrow_move_constructible<GrowthPolicy>::value&& std::is_nothrow_move_constructible< std::vector<array_bucket>>::value) : value_container<T>(std::move(other)), Hash(std::move(other)), GrowthPolicy(std::move(other)), m_buckets_data(std::move(other.m_buckets_data)), m_buckets(m_buckets_data.empty() ? static_empty_bucket_ptr() : m_buckets_data.data()), m_nb_elements(other.m_nb_elements), m_max_load_factor(other.m_max_load_factor), m_load_threshold(other.m_load_threshold) { other.value_container<T>::clear(); other.GrowthPolicy::clear(); other.m_buckets_data.clear(); other.m_buckets = static_empty_bucket_ptr(); other.m_nb_elements = 0; other.m_load_threshold = 0; } array_hash& operator=(const array_hash& other) { if (&other != this) { value_container<T>::operator=(other); Hash::operator=(other); GrowthPolicy::operator=(other); m_buckets_data = other.m_buckets_data; m_buckets = m_buckets_data.empty() ? static_empty_bucket_ptr() : m_buckets_data.data(); m_nb_elements = other.m_nb_elements; m_max_load_factor = other.m_max_load_factor; m_load_threshold = other.m_load_threshold; } return *this; } array_hash& operator=(array_hash&& other) { other.swap(*this); other.clear(); return *this; } /* * Iterators */ iterator begin() noexcept { auto begin = m_buckets_data.begin(); while (begin != m_buckets_data.end() && begin->empty()) { ++begin; } return (begin != m_buckets_data.end()) ? iterator(begin, begin->cbegin(), this) : end(); } const_iterator begin() const noexcept { return cbegin(); } const_iterator cbegin() const noexcept { auto begin = m_buckets_data.cbegin(); while (begin != m_buckets_data.cend() && begin->empty()) { ++begin; } return (begin != m_buckets_data.cend()) ? const_iterator(begin, begin->cbegin(), this) : cend(); } iterator end() noexcept { return iterator(m_buckets_data.end(), array_bucket::cend_it(), this); } const_iterator end() const noexcept { return cend(); } const_iterator cend() const noexcept { return const_iterator(m_buckets_data.end(), array_bucket::cend_it(), this); } /* * Capacity */ bool empty() const noexcept { return m_nb_elements == 0; } size_type size() const noexcept { return m_nb_elements; } size_type max_size() const noexcept { return std::numeric_limits<IndexSizeT>::max(); } size_type max_key_size() const noexcept { return MAX_KEY_SIZE; } void shrink_to_fit() { clear_old_erased_values(); value_container<T>::shrink_to_fit(); rehash_impl(size_type(std::ceil(float(size()) / max_load_factor()))); } /* * Modifiers */ void clear() noexcept { value_container<T>::clear(); for (auto& bucket : m_buckets_data) { bucket.clear(); } m_nb_elements = 0; } template <class... ValueArgs> std::pair<iterator, bool> emplace(const CharT* key, size_type key_size, ValueArgs&&... value_args) { const std::size_t hash = hash_key(key, key_size); std::size_t ibucket = bucket_for_hash(hash); auto it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size); if (it_find.second) { return std::make_pair( iterator(m_buckets_data.begin() + ibucket, it_find.first, this), false); } if (grow_on_high_load()) { ibucket = bucket_for_hash(hash); it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size); } return emplace_impl(ibucket, it_find.first, key, key_size, std::forward<ValueArgs>(value_args)...); } template <class M> std::pair<iterator, bool> insert_or_assign(const CharT* key, size_type key_size, M&& obj) { auto it = emplace(key, key_size, std::forward<M>(obj)); if (!it.second) { it.first.value() = std::forward<M>(obj); } return it; } iterator erase(const_iterator pos) { if (should_clear_old_erased_values()) { clear_old_erased_values(); } return erase_from_bucket(mutable_iterator(pos)); } iterator erase(const_iterator first, const_iterator last) { if (first == last) { return mutable_iterator(first); } /** * When erasing an element from a bucket with erase_from_bucket, it * invalidates all the iterators in the array bucket of the element * (m_array_bucket_iterator) but not the iterators of the buckets itself * (m_buckets_iterator). * * So first erase all the values between first and last which are not part * of the bucket of last, and then erase carefully the values in last's * bucket. */ auto to_delete = mutable_iterator(first); while (to_delete.m_buckets_iterator != last.m_buckets_iterator) { to_delete = erase_from_bucket(to_delete); } std::size_t nb_elements_until_last = std::distance( to_delete.m_array_bucket_iterator, last.m_array_bucket_iterator); while (nb_elements_until_last > 0) { to_delete = erase_from_bucket(to_delete); nb_elements_until_last--; } if (should_clear_old_erased_values()) { clear_old_erased_values(); } return to_delete; } size_type erase(const CharT* key, size_type key_size) { return erase(key, key_size, hash_key(key, key_size)); } size_type erase(const CharT* key, size_type key_size, std::size_t hash) { if (should_clear_old_erased_values()) { clear_old_erased_values(); } const std::size_t ibucket = bucket_for_hash(hash); if (m_buckets[ibucket].erase(key, key_size)) { m_nb_elements--; return 1; } else { return 0; } } void swap(array_hash& other) { using std::swap; swap(static_cast<value_container<T>&>(*this), static_cast<value_container<T>&>(other)); swap(static_cast<Hash&>(*this), static_cast<Hash&>(other)); swap(static_cast<GrowthPolicy&>(*this), static_cast<GrowthPolicy&>(other)); swap(m_buckets_data, other.m_buckets_data); swap(m_buckets, other.m_buckets); swap(m_nb_elements, other.m_nb_elements); swap(m_max_load_factor, other.m_max_load_factor); swap(m_load_threshold, other.m_load_threshold); } /* * Lookup */ template <class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr> U& at(const CharT* key, size_type key_size) { return at(key, key_size, hash_key(key, key_size)); } template <class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr> const U& at(const CharT* key, size_type key_size) const { return at(key, key_size, hash_key(key, key_size)); } template <class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr> U& at(const CharT* key, size_type key_size, std::size_t hash) { return const_cast<U&>( static_cast<const array_hash*>(this)->at(key, key_size, hash)); } template <class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr> const U& at(const CharT* key, size_type key_size, std::size_t hash) const { const std::size_t ibucket = bucket_for_hash(hash); auto it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size); if (it_find.second) { return this->m_values[it_find.first.value()]; } else { throw std::out_of_range("Couldn't find key."); } } template <class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr> U& access_operator(const CharT* key, size_type key_size) { const std::size_t hash = hash_key(key, key_size); std::size_t ibucket = bucket_for_hash(hash); auto it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size); if (it_find.second) { return this->m_values[it_find.first.value()]; } else { if (grow_on_high_load()) { ibucket = bucket_for_hash(hash); it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size); } return emplace_impl(ibucket, it_find.first, key, key_size, U{}) .first.value(); } } size_type count(const CharT* key, size_type key_size) const { return count(key, key_size, hash_key(key, key_size)); } size_type count(const CharT* key, size_type key_size, std::size_t hash) const { const std::size_t ibucket = bucket_for_hash(hash); auto it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size); if (it_find.second) { return 1; } else { return 0; } } iterator find(const CharT* key, size_type key_size) { return find(key, key_size, hash_key(key, key_size)); } const_iterator find(const CharT* key, size_type key_size) const { return find(key, key_size, hash_key(key, key_size)); } iterator find(const CharT* key, size_type key_size, std::size_t hash) { const std::size_t ibucket = bucket_for_hash(hash); auto it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size); if (it_find.second) { return iterator(m_buckets_data.begin() + ibucket, it_find.first, this); } else { return end(); } } const_iterator find(const CharT* key, size_type key_size, std::size_t hash) const { const std::size_t ibucket = bucket_for_hash(hash); auto it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size); if (it_find.second) { return const_iterator(m_buckets_data.cbegin() + ibucket, it_find.first, this); } else { return cend(); } } std::pair<iterator, iterator> equal_range(const CharT* key, size_type key_size) { return equal_range(key, key_size, hash_key(key, key_size)); } std::pair<const_iterator, const_iterator> equal_range( const CharT* key, size_type key_size) const { return equal_range(key, key_size, hash_key(key, key_size)); } std::pair<iterator, iterator> equal_range(const CharT* key, size_type key_size, std::size_t hash) { iterator it = find(key, key_size, hash); return std::make_pair(it, (it == end()) ? it : std::next(it)); } std::pair<const_iterator, const_iterator> equal_range( const CharT* key, size_type key_size, std::size_t hash) const { const_iterator it = find(key, key_size, hash); return std::make_pair(it, (it == cend()) ? it : std::next(it)); } /* * Bucket interface */ size_type bucket_count() const { return m_buckets_data.size(); } size_type max_bucket_count() const { return std::min(GrowthPolicy::max_bucket_count(), m_buckets_data.max_size()); } /* * Hash policy */ float load_factor() const { if (bucket_count() == 0) { return 0; } return float(m_nb_elements) / float(bucket_count()); } float max_load_factor() const { return m_max_load_factor; } void max_load_factor(float ml) { m_max_load_factor = std::max(0.1f, ml); m_load_threshold = size_type(float(bucket_count()) * m_max_load_factor); } void rehash(size_type count) { count = std::max(count, size_type(std::ceil(float(size()) / max_load_factor()))); rehash_impl(count); } void reserve(size_type count) { rehash(size_type(std::ceil(float(count) / max_load_factor()))); } /* * Observers */ hasher hash_function() const { return static_cast<const hasher&>(*this); } // TODO add support for statefull KeyEqual key_equal key_eq() const { return KeyEqual(); } /* * Other */ iterator mutable_iterator(const_iterator it) noexcept { auto it_bucket = m_buckets_data.begin() + std::distance(m_buckets_data.cbegin(), it.m_buckets_iterator); return iterator(it_bucket, it.m_array_bucket_iterator, this); } template <class Serializer> void serialize(Serializer& serializer) const { serialize_impl(serializer); } template <class Deserializer> void deserialize(Deserializer& deserializer, bool hash_compatible) { deserialize_impl(deserializer, hash_compatible); } private: std::size_t hash_key(const CharT* key, size_type key_size) const { return Hash::operator()(key, key_size); } std::size_t bucket_for_hash(std::size_t hash) const { return GrowthPolicy::bucket_for_hash(hash); } /** * If there is a mapped_type, the mapped value in m_values is not erased now. * It will be erased 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 (see clear_old_erased_values). */ iterator erase_from_bucket(iterator pos) noexcept { auto array_bucket_next_it = pos.m_buckets_iterator->erase(pos.m_array_bucket_iterator); m_nb_elements--; if (array_bucket_next_it != pos.m_buckets_iterator->cend()) { return iterator(pos.m_buckets_iterator, array_bucket_next_it, this); } else { do { ++pos.m_buckets_iterator; } while (pos.m_buckets_iterator != m_buckets_data.end() && pos.m_buckets_iterator->empty()); if (pos.m_buckets_iterator != m_buckets_data.end()) { return iterator(pos.m_buckets_iterator, pos.m_buckets_iterator->cbegin(), this); } else { return end(); } } } template <class U = T, typename std::enable_if< !has_mapped_type<U>::value>::type* = nullptr> bool should_clear_old_erased_values( float /*threshold*/ = DEFAULT_CLEAR_OLD_ERASED_VALUE_THRESHOLD) const { return false; } template <class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr> bool should_clear_old_erased_values( float threshold = DEFAULT_CLEAR_OLD_ERASED_VALUE_THRESHOLD) const { if (this->m_values.size() == 0) { return false; } return float(m_nb_elements) / float(this->m_values.size()) < threshold; } template <class U = T, typename std::enable_if< !has_mapped_type<U>::value>::type* = nullptr> void clear_old_erased_values() {} template <class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr> void clear_old_erased_values() { static_assert(std::is_nothrow_move_constructible<U>::value || std::is_copy_constructible<U>::value, "mapped_value must be either copy constructible or nothrow " "move constructible."); if (m_nb_elements == this->m_values.size()) { return; } std::vector<T> new_values; new_values.reserve(size()); for (auto it = begin(); it != end(); ++it) { new_values.push_back(std::move_if_noexcept(it.value())); } IndexSizeT ivalue = 0; for (auto it = begin(); it != end(); ++it) { auto it_array_bucket = it.m_buckets_iterator->mutable_iterator(it.m_array_bucket_iterator); it_array_bucket.set_value(ivalue); ivalue++; } new_values.swap(this->m_values); tsl_ah_assert(m_nb_elements == this->m_values.size()); } /** * Return true if a rehash occurred. */ bool grow_on_high_load() { if (size() >= m_load_threshold) { rehash_impl(GrowthPolicy::next_bucket_count()); return true; } return false; } template <class... ValueArgs, class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr> std::pair<iterator, bool> emplace_impl( std::size_t ibucket, typename array_bucket::const_iterator end_of_bucket, const CharT* key, size_type key_size, ValueArgs&&... value_args) { if (this->m_values.size() >= max_size()) { // Try to clear old erased values lingering in m_values. Throw if it // doesn't change anything. clear_old_erased_values(); if (this->m_values.size() >= max_size()) { throw std::length_error( "Can't insert value, too much values in the map."); } } if (this->m_values.size() == this->m_values.capacity()) { this->m_values.reserve( std::size_t(float(this->m_values.size()) * value_container<T>::VECTOR_GROWTH_RATE)); } this->m_values.emplace_back(std::forward<ValueArgs>(value_args)...); try { auto it = m_buckets[ibucket].append( end_of_bucket, key, key_size, IndexSizeT(this->m_values.size() - 1)); m_nb_elements++; return std::make_pair( iterator(m_buckets_data.begin() + ibucket, it, this), true); } catch (...) { // Rollback this->m_values.pop_back(); throw; } } template <class U = T, typename std::enable_if< !has_mapped_type<U>::value>::type* = nullptr> std::pair<iterator, bool> emplace_impl( std::size_t ibucket, typename array_bucket::const_iterator end_of_bucket, const CharT* key, size_type key_size) { if (m_nb_elements >= max_size()) { throw std::length_error( "Can't insert value, too much values in the map."); } auto it = m_buckets[ibucket].append(end_of_bucket, key, key_size); m_nb_elements++; return std::make_pair(iterator(m_buckets_data.begin() + ibucket, it, this), true); } void rehash_impl(size_type bucket_count) { GrowthPolicy new_growth_policy(bucket_count); if (bucket_count == this->bucket_count()) { return; } if (should_clear_old_erased_values( REHASH_CLEAR_OLD_ERASED_VALUE_THRESHOLD)) { clear_old_erased_values(); } std::vector<std::size_t> required_size_for_bucket(bucket_count, 0); std::vector<std::size_t> bucket_for_ivalue(size(), 0); std::size_t ivalue = 0; for (auto it = begin(); it != end(); ++it) { const std::size_t hash = hash_key(it.key(), it.key_size()); const std::size_t ibucket = new_growth_policy.bucket_for_hash(hash); bucket_for_ivalue[ivalue] = ibucket; required_size_for_bucket[ibucket] += array_bucket::entry_required_bytes(it.key_size()); ivalue++; } std::vector<array_bucket> new_buckets; new_buckets.reserve(bucket_count); for (std::size_t ibucket = 0; ibucket < bucket_count; ibucket++) { new_buckets.emplace_back(required_size_for_bucket[ibucket]); } ivalue = 0; for (auto it = begin(); it != end(); ++it) { const std::size_t ibucket = bucket_for_ivalue[ivalue]; append_iterator_in_reserved_bucket_no_check(new_buckets[ibucket], it); ivalue++; } using std::swap; swap(static_cast<GrowthPolicy&>(*this), new_growth_policy); m_buckets_data.swap(new_buckets); m_buckets = !m_buckets_data.empty() ? m_buckets_data.data() : static_empty_bucket_ptr(); // Call max_load_factor to change m_load_threshold max_load_factor(m_max_load_factor); } template <class U = T, typename std::enable_if< !has_mapped_type<U>::value>::type* = nullptr> void append_iterator_in_reserved_bucket_no_check(array_bucket& bucket, iterator it) { bucket.append_in_reserved_bucket_no_check(it.key(), it.key_size()); } template <class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr> void append_iterator_in_reserved_bucket_no_check(array_bucket& bucket, iterator it) { bucket.append_in_reserved_bucket_no_check(it.key(), it.key_size(), it.value_position()); } /** * On serialization the values of each bucket (if has_mapped_type is true) are * serialized next to the bucket. The potential old erased values in * value_container are thus not serialized. * * On deserialization, when hash_compatible is true, we reaffect the value * index (IndexSizeT) of each bucket with set_value as the position of each * value is no more the same in value_container compared to when they were * serialized. * * It's done this way as we can't call clear_old_erased_values() because we * want the serialize method to remain const and we don't want to * serialize/deserialize old erased values. As we may not serialize all the * values in value_container, the values we keep can change of index. We thus * have to modify the value indexes in the buckets. */ template <class Serializer> void serialize_impl(Serializer& serializer) const { const slz_size_type version = SERIALIZATION_PROTOCOL_VERSION; serializer(version); const slz_size_type bucket_count = m_buckets_data.size(); serializer(bucket_count); const slz_size_type nb_elements = m_nb_elements; serializer(nb_elements); const float max_load_factor = m_max_load_factor; serializer(max_load_factor); for (const array_bucket& bucket : m_buckets_data) { bucket.serialize(serializer); serialize_bucket_values(serializer, bucket); } } template < class Serializer, class U = T, typename std::enable_if<!has_mapped_type<U>::value>::type* = nullptr> void serialize_bucket_values(Serializer& /*serializer*/, const array_bucket& /*bucket*/) const {} template <class Serializer, class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr> void serialize_bucket_values(Serializer& serializer, const array_bucket& bucket) const { for (auto it = bucket.begin(); it != bucket.end(); ++it) { serializer(this->m_values[it.value()]); } } template <class Deserializer> void deserialize_impl(Deserializer& deserializer, bool hash_compatible) { tsl_ah_assert(m_buckets_data.empty()); // Current hash table must be empty const slz_size_type version = deserialize_value<slz_size_type>(deserializer); // For now we only have one version of the serialization protocol. // If it doesn't match there is a problem with the file. if (version != SERIALIZATION_PROTOCOL_VERSION) { throw std::runtime_error( "Can't deserialize the array_map/set. The protocol version header is " "invalid."); } const slz_size_type bucket_count_ds = deserialize_value<slz_size_type>(deserializer); const slz_size_type nb_elements = deserialize_value<slz_size_type>(deserializer); const float max_load_factor = deserialize_value<float>(deserializer); m_nb_elements = numeric_cast<IndexSizeT>( nb_elements, "Deserialized nb_elements is too big."); size_type bucket_count = numeric_cast<size_type>( bucket_count_ds, "Deserialized bucket_count is too big."); GrowthPolicy::operator=(GrowthPolicy(bucket_count)); this->max_load_factor(max_load_factor); value_container<T>::reserve(m_nb_elements); if (hash_compatible) { if (bucket_count != bucket_count_ds) { throw std::runtime_error( "The GrowthPolicy is not the same even though hash_compatible is " "true."); } m_buckets_data.reserve(bucket_count); for (size_type i = 0; i < bucket_count; i++) { m_buckets_data.push_back(array_bucket::deserialize(deserializer)); deserialize_bucket_values(deserializer, m_buckets_data.back()); } } else { m_buckets_data.resize(bucket_count); for (size_type i = 0; i < bucket_count; i++) { // TODO use buffer to avoid reallocation on each deserialization. array_bucket bucket = array_bucket::deserialize(deserializer); deserialize_bucket_values(deserializer, bucket); for (auto it_val = bucket.cbegin(); it_val != bucket.cend(); ++it_val) { const std::size_t ibucket = bucket_for_hash(hash_key(it_val.key(), it_val.key_size())); auto it_find = m_buckets_data[ibucket].find_or_end_of_bucket( it_val.key(), it_val.key_size()); if (it_find.second) { throw std::runtime_error( "Error on deserialization, the same key is presents multiple " "times."); } append_array_bucket_iterator_in_bucket(m_buckets_data[ibucket], it_find.first, it_val); } } } m_buckets = m_buckets_data.data(); if (load_factor() > this->max_load_factor()) { throw std::runtime_error( "Invalid max_load_factor. Check that the serializer and deserializer " "support " "floats correctly as they can be converted implicitely to ints."); } } template < class Deserializer, class U = T, typename std::enable_if<!has_mapped_type<U>::value>::type* = nullptr> void deserialize_bucket_values(Deserializer& /*deserializer*/, array_bucket& /*bucket*/) {} template <class Deserializer, class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr> void deserialize_bucket_values(Deserializer& deserializer, array_bucket& bucket) { for (auto it = bucket.begin(); it != bucket.end(); ++it) { this->m_values.emplace_back(deserialize_value<U>(deserializer)); tsl_ah_assert(this->m_values.size() - 1 <= std::numeric_limits<IndexSizeT>::max()); it.set_value(static_cast<IndexSizeT>(this->m_values.size() - 1)); } } template <class U = T, typename std::enable_if< !has_mapped_type<U>::value>::type* = nullptr> void append_array_bucket_iterator_in_bucket( array_bucket& bucket, typename array_bucket::const_iterator end_of_bucket, typename array_bucket::const_iterator it_val) { bucket.append(end_of_bucket, it_val.key(), it_val.key_size()); } template <class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr> void append_array_bucket_iterator_in_bucket( array_bucket& bucket, typename array_bucket::const_iterator end_of_bucket, typename array_bucket::const_iterator it_val) { bucket.append(end_of_bucket, it_val.key(), it_val.key_size(), it_val.value()); } public: static const size_type DEFAULT_INIT_BUCKET_COUNT = 0; static constexpr float DEFAULT_MAX_LOAD_FACTOR = 2.0f; static const size_type MAX_KEY_SIZE = array_bucket::MAX_KEY_SIZE; private: /** * Protocol version currenlty used for serialization. */ static const slz_size_type SERIALIZATION_PROTOCOL_VERSION = 1; static constexpr float DEFAULT_CLEAR_OLD_ERASED_VALUE_THRESHOLD = 0.6f; static constexpr float REHASH_CLEAR_OLD_ERASED_VALUE_THRESHOLD = 0.9f; /** * Return an always valid pointer to a static empty array_bucket. */ array_bucket* static_empty_bucket_ptr() { static array_bucket empty_bucket; return &empty_bucket; } private: std::vector<array_bucket> m_buckets_data; /** * Points to m_buckets_data.data() if !m_buckets_data.empty() otherwise points * to static_empty_bucket_ptr. This variable is useful to avoid the cost of * checking if m_buckets_data is empty when trying to find an element. * * TODO Remove m_buckets_data and only use a pointer+size instead of a * pointer+vector to save some space in the array_hash object. */ array_bucket* m_buckets; IndexSizeT m_nb_elements; float m_max_load_factor; size_type m_load_threshold; }; } // end namespace detail_array_hash } // end namespace tsl #endif /** * 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> 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 /** * 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> 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 static unsigned long xorshf96() { /* A George Marsaglia generator, period 2^96-1 */ static unsigned long x = 123456789, y = 362436069, z = 521288629; unsigned long t; x ^= x << 16; x ^= x >> 5; x ^= x << 1; t = x; x = y; y = z; z = t ^ x ^ y; return z; } static inline unsigned long _random() { return xorshf96(); } template<class M> static void BenchUnOrderSetInt(benchmark::State &state) { M m; m.reserve(65536); for (auto i = 0; i < 65536; i++) { m.insert(i); } for (auto _ : state) { auto c = m.find(_random() % 65536); benchmark::DoNotOptimize(c); } } #include <unordered_set> // BENCHMARK_TEMPLATE(BenchUnOrderSetInt, std::unordered_set<int>); // BENCHMARK_TEMPLATE(BenchUnOrderSetInt, ska::unordered_set<int>); // BENCHMARK_TEMPLATE(BenchUnOrderSetInt, ska::flat_hash_set<int>); // BENCHMARK_TEMPLATE(BenchUnOrderSetInt, ska::bytell_hash_set<int>); // BENCHMARK_TEMPLATE(BenchUnOrderSetInt, robin_hood::unordered_flat_set<int>); template<class M> static void BenchUnOrderSetString(benchmark::State &state) { M m; m.reserve(65536); std::vector<std::string> keys(65536); for (auto i = 0; i < 65536; i++) { keys[i] = std::to_string(_random() % 65536); auto sKey = std::to_string(i); m.insert(sKey); } for (auto _ : state) { auto kIndex = _random() % 65536; auto c = m.find(keys[kIndex]); benchmark::DoNotOptimize(c); } } template<class M> static void BenchCharKeySet(benchmark::State &state) { M m; std::vector<std::string> keys(65536); for (auto i = 0; i < 65536; i++) { keys[i] = std::to_string(_random() % 65536); auto sKey = std::to_string(i); m.insert(sKey); } for (auto _ : state) { auto kIndex = _random() % 65536; auto c = m.find(keys[kIndex]); benchmark::DoNotOptimize(c); } } BENCHMARK_TEMPLATE(BenchUnOrderSetString, std::unordered_set<std::string>); BENCHMARK_TEMPLATE(BenchUnOrderSetString, ska::unordered_set<std::string>); BENCHMARK_TEMPLATE(BenchUnOrderSetString, ska::flat_hash_set<std::string>); // BENCHMARK_TEMPLATE(BenchUnOrderSetString, ska::bytell_hash_set<std::string>); // BENCHMARK_TEMPLATE(BenchUnOrderSetString, robin_hood::unordered_flat_set<std::string>); // BENCHMARK_TEMPLATE(BenchCharKeySet,tsl::array_set<char>); int main(int argc, char **argv) { benchmark::Initialize(&argc, argv); benchmark::RunSpecifiedBenchmarks(); tsl::array_map<char,int> m; return 0; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree