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
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 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 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 (WINE)
ARM msvc v19.10 (WINE)
ARM msvc v19.14 (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 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 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 (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 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 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
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
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
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 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 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 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 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 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 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 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 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 gcc 12.2.0
SPARC gcc 12.3.0
SPARC gcc 12.4.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
SPARC64 gcc 12.2.0
SPARC64 gcc 12.3.0
SPARC64 gcc 12.4.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
TI C6x gcc 12.2.0
TI C6x gcc 12.3.0
TI C6x gcc 12.4.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 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 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 9.0.0
armv8-a clang 9.0.1
clad trunk (clang 20.1.0)
clad v1.10 (clang 20.1.0)
clad v1.8 (clang 18.1.0)
clad v1.9 (clang 19.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 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
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 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 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 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 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 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 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 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 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
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 gcc 12.1.0
mipsel gcc 12.2.0
mipsel gcc 12.3.0
mipsel gcc 12.4.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 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 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 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 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 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 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 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 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
sh gcc 12.2.0
sh gcc 12.3.0
sh gcc 12.4.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 4.9.4
sh gcc 9.5.0
vast (trunk)
x64 msvc v19.0 (WINE)
x64 msvc v19.10 (WINE)
x64 msvc v19.14 (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 (WINE)
x86 msvc v19.10 (WINE)
x86 msvc v19.14 (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-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 P2996)
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 metaprogramming - P2632)
x86-64 clang (old concepts branch)
x86-64 clang (p1974)
x86-64 clang (pattern matching - P2688)
x86-64 clang (reflection)
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.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 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 (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 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 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.1.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.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 <algorithm> #include <cassert> #include <chrono> #include <compare> #include <concepts> #include <cstddef> #include <functional> #include <iterator> #include <limits> #include <memory> #include <optional> #include <ranges> #include <sstream> #include <type_traits> #include <utility> #include <variant> namespace flex { /* * Helpers */ #define FLEX_FWD(x) static_cast<decltype(x)&&>(x) using int_t = std::ptrdiff_t; struct immovable { immovable() = default; immovable(immovable const&) = delete; immovable& operator=(immovable const&) = delete; }; template <typename T> using with_ref = T&; template <typename T> concept can_ref = requires { typename with_ref<T>; }; template <typename T, typename U> concept same_decayed = std::same_as<std::decay_t<T>, U>; /* * MARK: Collection concepts */ template <typename P> concept position = std::regular<P> && std::three_way_comparable<P, std::strong_ordering>; template <typename C> struct collection_traits { }; template <typename C> using coll_traits_t = collection_traits<std::remove_cvref_t<C>>; template <typename C> using position_t = decltype(coll_traits_t<C>::start_pos(std::declval<C&>())); template <typename C> using collection_element_t = decltype(coll_traits_t<C>::read_at_unchecked( std::declval<C&>(), std::declval<position_t<C> const&>())); template <typename C, typename Traits = coll_traits_t<C>> concept collection_concept = requires(C const& self) { { Traits::start_pos(self) } -> position; { Traits::end_pos(self) } -> std::same_as<position_t<C>>; } && requires(C const& self, position_t<C>& pos) { { Traits::inc_pos(self, pos) }; } && requires(C const& self, position_t<C> const& pos) { { Traits::read_at_unchecked(self, pos) } -> can_ref; }; template <typename C, typename Traits = coll_traits_t<C>> concept mutable_collection_concept = requires(C& self, position_t<C> const& pos) { { Traits::read_at_unchecked(self, pos) } -> can_ref; { Traits::swap_at(self, pos, pos) }; }; template <typename C, typename Traits = coll_traits_t<C>> concept bidirectional_collection_concept = requires(C const& self, position_t<C>& pos) { { Traits::dec_pos(self, pos) }; }; template <typename C, typename Traits = coll_traits_t<C>> concept random_access_collection_concept = requires(C const& self, position_t<C> const& pos) { { Traits::distance(self, pos, pos) } -> std::signed_integral; } && requires(C const& self, position_t<C>& pos, int_t offset) { { Traits::offset_pos(self, pos, offset) }; }; template <typename T> concept has_nested_collection_traits = requires { typename T::flex_collection_traits; } && std::is_class_v<typename T::flex_collection_traits>; template <has_nested_collection_traits T> struct collection_traits<T> : T::flex_collection_traits { }; struct start_pos_t { template <collection_concept C> constexpr auto operator()(C const& self) const -> position_t<C> { return coll_traits_t<C>::start_pos(self); } }; inline constexpr start_pos_t start_pos{}; struct end_pos_t { template <collection_concept C> constexpr auto operator()(C const& self) const -> position_t<C> { return coll_traits_t<C>::end_pos(self); } }; inline constexpr end_pos_t end_pos{}; struct inc_pos_t { template <collection_concept C> constexpr auto operator()(C const& self, position_t<C>& pos) const -> position_t<C>& { coll_traits_t<C>::inc_pos(self, pos); return pos; } }; inline constexpr inc_pos_t inc_pos{}; struct read_at_unchecked_t { template <collection_concept C> constexpr auto operator()(C& self, position_t<C> const& pos) const -> decltype(auto) { return coll_traits_t<C>::read_at_unchecked(self, pos); } }; inline constexpr read_at_unchecked_t read_at_unchecked{}; struct is_readable_position_t { template <collection_concept C> constexpr auto operator()(C const& self, position_t<C> const& pos) const -> bool { return pos >= start_pos(self) && pos < end_pos(self); } }; inline constexpr is_readable_position_t is_readable_position{}; struct read_at_t { template <collection_concept C> constexpr auto operator()(C& self, position_t<C> const& pos) const -> collection_element_t<C> { if (!is_readable_position(self, pos)) { #ifdef _MSC_VER std::abort(); #else __builtin_trap(); #endif } return read_at_unchecked(self, pos); } }; inline constexpr read_at_t read_at{}; struct swap_at_t { template <mutable_collection_concept C> constexpr auto operator()(C& self, position_t<C> const& pos1, position_t<C> const& pos2) const -> void { coll_traits_t<C>::swap_at(self, pos1, pos2); } }; inline constexpr swap_at_t swap_at{}; struct dec_pos_t { template <bidirectional_collection_concept C> constexpr auto operator()(C const& self, position_t<C>& pos) const -> position_t<C>& { coll_traits_t<C>::dec_pos(self, pos); return pos; } }; inline constexpr dec_pos_t dec_pos{}; struct distance_t { template <random_access_collection_concept C> constexpr auto operator()(C const& self, position_t<C> const& from, position_t<C> const& to) const -> int_t { return static_cast<int_t>(coll_traits_t<C>::distance(self, from, to)); } }; inline constexpr distance_t distance; struct offset_pos_t { template <random_access_collection_concept C> constexpr auto operator()(C const& self, position_t<C>& pos, int_t offset) const -> position_t<C>& { coll_traits_t<C>::offset_pos(self, pos, offset); return pos; } }; inline constexpr offset_pos_t offset_pos{}; template <typename C, typename Traits = coll_traits_t<C>> concept has_overloaded_iterate_slice = requires(C& self, position_t<C> pos, bool (*pred)(collection_element_t<C>)) { { Traits::iterate_slice(self, pos, pos, pred) } -> std::same_as<position_t<C>>; }; struct iterate_slice_t { template <collection_concept C, typename Pred> constexpr auto operator()(C& self, position_t<C> from, position_t<C> to, Pred&& pred) const -> position_t<C> { if constexpr (has_overloaded_iterate_slice<C>) { return coll_traits_t<C>::iterate_slice(self, std::move(from), std::move(to), FLEX_FWD(pred)); } else { using Traits = coll_traits_t<C>; auto bounds_check = [&](position_t<C> const& pos) { return pos >= Traits::start_pos(self) && pos <= Traits::end_pos(self); }; if (!bounds_check(from) || !bounds_check(to)) { #ifdef _MSC_VER std::abort(); #else __builtin_trap(); #endif } while (from < to) { if (!std::invoke(pred, Traits::read_at_unchecked(self, from))) { break; } Traits::inc_pos(self, from); } return from; } } }; inline constexpr iterate_slice_t iterate_slice{}; template <collection_concept Base> struct slice_collection { Base base; position_t<Base> from; position_t<Base> to; struct flex_collection_traits { using self_t = slice_collection; static constexpr auto start_pos(self_t const& self) { return self.from; } static constexpr auto end_pos(self_t& self) { return self.to; } static constexpr auto inc_pos(self_t& self, position_t<Base>& pos) { flex::inc_pos(self.base, pos); } static constexpr auto dec_pos(self_t& self, position_t<Base>& pos) requires bidirectional_collection_concept<Base> { flex::dec_pos(self.base, pos); } static constexpr auto read_at_unchecked(auto& self, position_t<Base> const& pos) -> decltype(flex::read_at_unchecked(self.base, pos)) { return flex::read_at_unchecked(self.base, pos); } static constexpr auto swap_at(auto& self, position_t<Base> const& pos1, position_t<Base> const& pos2) requires mutable_collection_concept<Base> { flex::swap_at(self.base, pos1, pos2); } static constexpr auto distance(auto& self, position_t<Base> const& from, position_t<Base> const& to) -> int_t requires random_access_collection_concept<Base> { return flex::distance(self.base, from, to); } }; }; /* * MARK: Iteration context concepts */ enum class iteration_result : bool { incomplete = false, complete = true }; constexpr bool loop_continue = true; constexpr bool loop_break = false; template <typename Ctx> concept iteration_context = requires { typename Ctx::element_type; } && requires(Ctx& ctx, bool (*pred)(typename Ctx::element_type)) { { ctx.run_while(pred) } -> std::same_as<iteration_result>; }; struct run_while_t { template <typename T> constexpr auto operator()(T&&) = delete; template <iteration_context Ctx, typename Pred> constexpr auto operator()(Ctx& self, Pred&& pred) const -> iteration_result { return self.run_while(FLEX_FWD(pred)); } }; inline constexpr run_while_t run_while{}; struct step_t { template <iteration_context Ctx, typename Fn> constexpr auto operator()(Ctx& ctx, Fn&& fn) const { using R = std::invoke_result_t<Fn, typename Ctx::element_type>; if constexpr (std::is_void_v<R>) { return ctx.run_while([&](auto&& elem) { (void)std::invoke(FLEX_FWD(fn), FLEX_FWD(elem)); return loop_break; }) != iteration_result::complete; } else if constexpr (std::is_lvalue_reference_v<R>) { using Ptr = std::add_pointer_t<R>; Ptr ptr = nullptr; run_while(ctx, [&](auto&& elem) { ptr = std::addressof(std::invoke(FLEX_FWD(fn), FLEX_FWD(elem))); return loop_break; }); return ptr; } else { using Opt = std::optional<std::remove_reference_t<R>>; Opt opt = std::nullopt; run_while(ctx, [&](auto&& elem) { opt.emplace(std::invoke(FLEX_FWD(fn), FLEX_FWD(elem))); return loop_break; }); return opt; } } template <iteration_context Ctx> constexpr auto operator()(Ctx& self) const { return step_t{}(self, std::identity{}); } }; inline constexpr step_t step; /* * MARK: Sequence concepts */ template <typename Seq> concept has_member_iterate = requires(Seq& seq) { { seq.iterate() } -> iteration_context; }; template <collection_concept C> struct collection_iteration_context : immovable { C& coll; position_t<C> pos = start_pos(coll); position_t<C> const last = end_pos(coll); using element_type = collection_element_t<C>; constexpr auto run_while(auto&& pred) -> iteration_result { pos = flex::iterate_slice(coll, pos, last, pred); if (pos != last) { inc_pos(coll, pos); return iteration_result::incomplete; } else { return iteration_result::complete; } } }; template <typename Seq> struct sequence_traits { template <collection_concept C> static constexpr auto iterate(C& self) -> collection_iteration_context<C> { return collection_iteration_context<C>{.coll = self}; } }; template <typename Seq> using seq_traits_t = sequence_traits<std::remove_cvref_t<Seq>>; template <typename Seq, typename Traits = seq_traits_t<Seq>> concept sequence = requires(Seq& seq) { { Traits::iterate(seq) } -> iteration_context; }; struct iterate_t { template <sequence Seq> constexpr auto operator()(Seq& seq) const -> iteration_context auto { return seq_traits_t<Seq>::iterate(seq); } }; inline constexpr iterate_t iterate{}; template <typename Seq> using iteration_context_t = decltype(iterate(std::declval<Seq&>())); template <typename Seq> using sequence_element_t = typename iteration_context_t<Seq>::element_type; template <has_member_iterate Seq> struct sequence_traits<Seq> { static constexpr auto iterate(Seq& self) { return self.iterate(); } static constexpr auto iterate(Seq const& self) requires has_member_iterate<Seq const> { return self.iterate(); } }; template <typename C> concept collection = sequence<C> && collection_concept<C>; template <typename C> concept mutable_collection = collection<C> && mutable_collection_concept<C>; template <typename C> concept bidirectional_collection = collection<C> && bidirectional_collection_concept<C>; template <typename C> concept random_access_collection = bidirectional_collection<C> && random_access_collection_concept<C>; template <sequence Seq> struct sequence_iterator { private: Seq* ptr_; iteration_context_t<Seq> ctx_; using opt_t = decltype(flex::step(ctx_)); opt_t elem_{}; public: using reference = typename iteration_context_t<Seq>::element_type; using value_type = std::remove_cvref_t<reference>; using difference_type = int_t; constexpr explicit sequence_iterator(Seq& seq) : ptr_(std::addressof(seq)), ctx_(flex::iterate(*ptr_)), elem_(flex::step(ctx_)) { } constexpr auto operator*() const -> reference { return *elem_; } constexpr auto operator++() -> sequence_iterator& { elem_ = flex::step(ctx_); return *this; } constexpr void operator++(int) { ++*this; } constexpr auto operator==(std::default_sentinel_t) const -> bool { return !static_cast<bool>(elem_); } }; template <collection C> struct collection_iterator { private: C* ptr_ = nullptr; position_t<C> pos_{}; public: using reference = collection_element_t<C>; using value_type = std::remove_cvref_t<reference>; using iterator_category = std::forward_iterator_tag; using difference_type = int_t; collection_iterator() = default; constexpr explicit collection_iterator(C& coll, position_t<C> pos) : ptr_(std::addressof(coll)), pos_(std::move(pos)) { } constexpr auto operator*() const -> reference { return read_at(*ptr_, pos_); } constexpr auto operator++() -> collection_iterator& { inc_pos(*ptr_, pos_); return *this; } constexpr auto operator++(int) -> collection_iterator { auto temp = *this; ++*this; return temp; } constexpr auto operator<=>(collection_iterator const&) const = default; }; template <typename Derived> struct sequence_adaptor_base { constexpr auto derived() -> Derived& { return static_cast<Derived&>(*this); } constexpr auto derived() const -> Derived const& { return static_cast<Derived const&>(*this); } constexpr auto begin() { if constexpr (collection<Derived>) { return collection_iterator<Derived>(derived(), start_pos(derived())); } else { return sequence_iterator<Derived>(derived()); } } constexpr auto begin() const requires sequence<Derived const> { auto& self = static_cast<Derived const&>(*this); if constexpr (collection<Derived const>) { return collection_iterator<Derived const>(self, start_pos(self)); } else { return sequence_iterator<Derived const>(self); } } constexpr auto end() requires collection<Derived> { auto& self = static_cast<Derived&>(*this); return collection_iterator<Derived>(self, end_pos(self)); } constexpr auto end() const { return std::default_sentinel; } constexpr auto end() const requires collection<Derived const> { auto& self = static_cast<Derived const&>(*this); return collection_iterator<Derived const>(self, end_pos(self)); } }; template <sequence Seq> struct sequence_range : std::ranges::view_interface<sequence_range<Seq>> { Seq seq; iteration_context_t<Seq> ctx = flex::iterate(seq); struct iterator { using context_type = iteration_context_t<Seq>; context_type* ctx; using optional_t = decltype(step(*ctx)); optional_t elem{}; using reference = typename context_type::element_type; using value_type = std::remove_cvref_t<reference>; using difference_type = std::ptrdiff_t; iterator() = default; explicit iterator(context_type* ctx) : ctx(ctx), elem(step(*ctx)) { } constexpr auto operator*() const -> reference { return *elem; } constexpr auto operator++() -> iterator& { elem = step(*ctx); } constexpr auto operator++(int) -> void { ++*this; } constexpr bool operator==(std::default_sentinel_t) const { return !static_cast<bool>(elem); } }; constexpr auto begin() -> iterator { return iterator(std::addressof(ctx)); } static constexpr auto end() -> std::default_sentinel_t { return {}; } }; template <collection C> struct collection_range : std::ranges::view_interface<collection_range<C>> { C coll; constexpr auto begin() { return collection_iterator<C>(coll, start_pos(coll)); } constexpr auto begin() const requires collection<C const> { return collection_iterator<C const>(coll, start_pos(coll)); } constexpr auto end() { return collection_iterator<C>(coll, end_pos(coll)); } constexpr auto end() const requires collection<C const> { return collection_iterator<C const>(coll, end_pos(coll)); } }; struct as_range_t { template <sequence Seq> constexpr auto operator()(Seq&& seq) const -> decltype(auto) { if constexpr (std::ranges::range<Seq>) { return std::views::all(FLEX_FWD(seq)); } else if constexpr (std::is_lvalue_reference_v<Seq>) { if constexpr (collection<Seq>) { return collection_range{.coll = std::ref(seq)}; } else { return sequence_range{.seq = std::ref(seq)}; } } else { if constexpr (collection<Seq>) { return collection_range{.coll = std::move(seq)}; } else { return sequence_range{.seq = std::move(seq)}; } } } }; inline namespace operation { inline constexpr as_range_t as_range{}; } /* * Implementation for reference_wrapper */ template <sequence Seq> struct sequence_traits<std::reference_wrapper<Seq>> { static constexpr auto iterate(std::reference_wrapper<Seq> const& self) { return flex::iterate(self.get()); } }; template <collection C> struct collection_traits<std::reference_wrapper<C>> { using self_t = std::reference_wrapper<C>; static constexpr auto start_pos(self_t self) -> position_t<C> { return flex::start_pos(self.get()); } static constexpr auto end_pos(self_t self) -> position_t<C> { return flex::end_pos(self.get()); } static constexpr auto inc_pos(self_t self, position_t<C>& pos) { flex::inc_pos(self.get(), pos); } static constexpr auto read_at_unchecked(self_t self, position_t<C> const& pos) -> collection_element_t<C> { return flex::read_at_unchecked(self.get(), pos); } static constexpr auto swap_at(self_t self, position_t<C> const& pos1, position_t<C> const& pos2) requires mutable_collection<C> { flex::swap_at(self.get(), pos1, pos2); } static constexpr auto dec_pos(self_t self, position_t<C>& pos) requires bidirectional_collection<C> { flex::dec_pos(self.get(), pos); } static constexpr auto distance(self_t self, position_t<C> const& from, position_t<C> const& to) -> int_t requires random_access_collection<C> { return flex::distance(self.get(), from, to); } static constexpr auto offset_pos(self_t self, position_t<C>& pos, int_t offset) requires random_access_collection<C> { flex::offset_pos(self.get(), pos, offset); } static constexpr auto iterate_slice(self_t self, position_t<C> from, position_t<C> to, auto&& pred) -> position_t<C> { return flex::iterate_slice(self.get(), from, to, pred); } }; /* * Implementation for ranges */ template <std::ranges::input_range R> struct range_iteration_context : immovable { using element_type = std::ranges::range_reference_t<R>; std::ranges::iterator_t<R> iter; std::ranges::sentinel_t<R> last; bool inc_next = false; template <typename Pred> constexpr auto run_while(Pred&& pred) -> iteration_result { if (inc_next) { assert(iter != last); inc_next = false; ++iter; } while (iter != last) { if (!pred(*iter)) { inc_next = true; return iteration_result::incomplete; } ++iter; } return iteration_result::complete; } }; template <std::ranges::forward_range R> struct range_iteration_context<R> : immovable { using element_type = std::ranges::range_reference_t<R>; std::ranges::iterator_t<R> iter; std::ranges::sentinel_t<R> last; constexpr auto run_while(auto&& pred) -> iteration_result { while (iter != last) { if (!pred(*iter)) { ++iter; return iteration_result::incomplete; } ++iter; } return iteration_result::complete; } }; template <typename T, typename U> requires(!std::same_as<T, sequence_adaptor_base<U>>) void derived_from_sequence_adaptor_base_test(T const&, sequence_adaptor_base<U> const&); template <typename T> concept derived_from_sequence_adaptor_base = requires(T t) { derived_from_sequence_adaptor_base_test(t, t); }; template <typename R> requires(!derived_from_sequence_adaptor_base<R>) && std::ranges::input_range<R> struct sequence_traits<R> { static constexpr auto iterate(R& rng) { return range_iteration_context<R>{.iter = std::ranges::begin(rng), .last = std::ranges::end(rng)}; } static constexpr auto iterate(R const& rng) requires std::ranges::input_range<R const> { return range_iteration_context<R const>{.iter = std::ranges::begin(rng), .last = std::ranges::end(rng)}; } }; template <typename R> requires(!derived_from_sequence_adaptor_base<R>) && std::ranges::random_access_range<R> && std::ranges::sized_range<R> struct collection_traits<R> { static constexpr auto start_pos(R const&) -> std::size_t { return 0; } static constexpr auto end_pos(R const& self) -> std::size_t { return std::ranges::size(self); } static constexpr auto inc_pos(R const&, std::size_t& idx) { ++idx; } static constexpr auto read_at_unchecked(R& self, std::size_t idx) -> decltype(auto) { if constexpr (std::ranges::contiguous_range<R>) { return std::ranges::data(self)[idx]; } else { return std::ranges::begin(self)[idx]; } } static constexpr auto read_at_unchecked(R const& self, std::size_t idx) -> decltype(auto) { if constexpr (std::ranges::contiguous_range<R>) { return std::ranges::cdata(self)[idx]; } else { return std::ranges::cbegin(self)[idx]; } } static constexpr auto swap_at(R& self, std::size_t idx1, std::size_t idx2) requires std::indirectly_swappable<std::ranges::iterator_t<R>> { std::ranges::iter_swap(std::ranges::begin(self) + idx1, std::ranges::begin(self) + idx2); } static constexpr auto dec_pos(auto&, std::size_t& idx) { --idx; } }; /* * MARK: Algorithms */ namespace pipeable_detail { struct sequence_adaptor_object { template <sequence LHS, typename RHS> requires std::derived_from<std::decay_t<RHS>, sequence_adaptor_object> friend constexpr auto operator|(LHS&& lhs, RHS&& rhs) -> decltype(FLEX_FWD(rhs)(FLEX_FWD(lhs))) { return FLEX_FWD(rhs)(FLEX_FWD(lhs)); } }; struct sequence_consumer_object { template <sequence LHS, typename RHS> requires std::derived_from<std::decay_t<RHS>, sequence_consumer_object> friend constexpr auto operator|(LHS&& lhs, RHS&& rhs) -> decltype(FLEX_FWD(rhs)(FLEX_FWD(lhs))) { return FLEX_FWD(rhs)(FLEX_FWD(lhs)); } }; template <typename LHS, typename RHS> struct pipe : std::conditional_t<std::derived_from<RHS, sequence_adaptor_object>, sequence_adaptor_object, sequence_consumer_object> { LHS lhs; RHS rhs; constexpr auto operator()(sequence auto&& seq) -> decltype(rhs(lhs(FLEX_FWD(seq)))) { return rhs(lhs(FLEX_FWD(seq))); } }; template <typename LHS, typename RHS> requires std::derived_from<std::decay_t<LHS>, sequence_adaptor_object> && (std::derived_from<std::decay_t<RHS>, sequence_adaptor_object> || std::derived_from<std::decay_t<RHS>, sequence_consumer_object>) constexpr auto operator|(LHS&& lhs, RHS&& rhs) -> pipe<std::decay_t<LHS>, std::decay_t<RHS>> { return {.lhs = FLEX_FWD(lhs), .rhs = FLEX_FWD(rhs)}; } } // namespace pipeable_detail using pipeable_detail::sequence_adaptor_object; using pipeable_detail::sequence_consumer_object; template <typename Lambda> struct make_sequence_adaptor_object : Lambda, sequence_adaptor_object { }; template <typename L> make_sequence_adaptor_object(L) -> make_sequence_adaptor_object<L>; template <typename Lambda> struct make_sequence_consumer_object : Lambda, sequence_consumer_object { }; template <typename L> make_sequence_consumer_object(L) -> make_sequence_consumer_object<L>; struct for_each_t { template <sequence Seq, typename Fn> constexpr auto operator()(Seq&& seq, Fn fn) const -> Fn { auto ctx = flex::iterate(seq); ctx.run_while([&fn](auto&&... elems) { std::invoke(fn, FLEX_FWD(elems)...); return true; }); return fn; } template <typename Fn> constexpr auto operator()(Fn&& fn) const { return make_sequence_consumer_object([fn = FLEX_FWD(fn)](auto&& seq) mutable { return for_each_t{}(FLEX_FWD(seq), std::move(fn)); }); } }; inline namespace operation { inline constexpr for_each_t for_each{}; } struct fold_t { template <sequence Seq, typename FoldFn, typename Init> constexpr auto operator()(Seq&& seq, FoldFn fold_fn, Init init) const -> Init { for_each(seq, [&](auto&&... elems) { init = fold_fn(std::move(init), FLEX_FWD(elems)...); }); return init; } }; inline namespace operation { inline constexpr fold_t fold{}; } struct sum_t : sequence_consumer_object { template <sequence Seq> constexpr auto operator()(Seq&& seq) const -> int_t { return fold(seq, std::plus{}, int_t{0}); } }; inline namespace operation { inline constexpr auto sum = sum_t{}; } struct all_t { template <sequence Seq, typename Pred> constexpr auto operator()(Seq&& seq, Pred&& pred) const -> bool { auto ctx = iterate(seq); return ctx.run_while(pred) == iteration_result::complete; } constexpr auto operator()(auto&& pred) const { return make_sequence_consumer_object( [pred = FLEX_FWD(pred)]( sequence auto&& seq) mutable -> decltype(all_t{}(FLEX_FWD(seq), std::move(pred))) { return all_t{}(FLEX_FWD(seq), std::move(pred)); }); } }; inline constexpr all_t all{}; /* * MARK: Adaptors */ template <sequence Base, typename FilterFn> struct filter_adaptor { [[no_unique_address]] Base base; [[no_unique_address]] FilterFn filter_fn; template <typename BaseCtx, typename F> struct context { BaseCtx base_ctx; F filter_fn; using element_type = BaseCtx::element_type; constexpr auto run_while(auto&& pred) -> iteration_result { return base_ctx.run_while([&](auto&&... elems) { if (std::invoke(filter_fn, std::as_const(elems)...)) { if (!pred(FLEX_FWD(elems)...)) { return loop_break; } } return loop_continue; }); } }; constexpr auto iterate() { return context{.base_ctx = flex::iterate(base), .filter_fn = std::ref(filter_fn)}; } constexpr auto iterate() const requires sequence<Base const> { return context{.base_ctx = flex::iterate(base), .filter_fn = std::ref(filter_fn)}; } }; struct filter_t { template <sequence Seq, typename FilterFn> constexpr auto operator()(Seq seq, FilterFn filter_fn) const -> sequence auto { return filter_adaptor<Seq, FilterFn>{.base = std::move(seq), .filter_fn = std::move(filter_fn)}; } template <typename FilterFn> constexpr auto operator()(FilterFn&& filter_fn) const { return make_sequence_adaptor_object([f = FLEX_FWD(filter_fn)](sequence auto&& seq) mutable { return filter_t{}(FLEX_FWD(seq), std::move(f)); }); } }; inline namespace operation { inline constexpr filter_t filter{}; } template <sequence Base> struct enumerate_adaptor { Base base; template <typename BaseState> struct state_type { BaseState base_state; int_t counter = 0; using element_type = std::tuple<int_t, typename BaseState::element_type>; constexpr auto run_while(auto&& pred) -> iteration_result { return base_state.run_while( [&](auto&&... elems) { return pred(counter++, FLEX_FWD(elems)...); }); } }; constexpr auto iterate() { return state_type{.base_state = flex::iterate(base)}; } constexpr auto iterate() const requires sequence<Base const> { return state_type{.base_state = flex::iterate(base)}; } }; struct enumerate_t : sequence_adaptor_object { template <sequence Seq> constexpr auto operator()(Seq seq) const -> sequence auto { return enumerate_adaptor<Seq>{.base = std::move(seq)}; } }; inline namespace operation { inline constexpr enumerate_t enumerate{}; } template <sequence Base, typename MapFn> struct map_adaptor : sequence_adaptor_base<map_adaptor<Base, MapFn>> { Base base; MapFn map_fn; template <typename BaseContext, typename Fn> struct context { BaseContext base_context; Fn fn; using element_type = std::invoke_result_t<Fn&, typename BaseContext::element_type>; constexpr auto run_while(auto&& pred) -> iteration_result { return base_context.run_while( [&](auto&&... elems) { return pred(std::invoke(fn, FLEX_FWD(elems)...)); }); } }; constexpr auto iterate() { return context{.base_context = flex::iterate(base), .fn = std::ref(map_fn)}; } constexpr auto iterate() const requires sequence<Base const> { return context{.base_context = flex::iterate(base), .fn = std::cref(map_fn)}; } struct flex_collection_traits { static constexpr auto start_pos(auto& self) requires collection<decltype(self.base)> { return flex::start_pos(self.base); } static constexpr auto end_pos(auto& self) requires collection<decltype(self.base)> { return flex::end_pos(self.base); } static constexpr auto inc_pos(auto& self, position_t<Base>& pos) { return flex::inc_pos(self.base, pos); } static constexpr auto read_at_unchecked(auto& self, position_t<Base> const& pos) { return std::invoke(self.map_fn, flex::read_at_unchecked(self.base, pos)); } }; }; struct map_t { template <sequence Seq, typename MapFn> requires std::invocable<MapFn&, sequence_element_t<Seq>> constexpr auto operator()(Seq seq, MapFn map_fn) const -> sequence auto { return map_adaptor<Seq, MapFn>{.base = std::move(seq), .map_fn = std::move(map_fn)}; } template <typename MapFn> constexpr auto operator()(MapFn&& map_fn) const { return make_sequence_adaptor_object([fn = FLEX_FWD(map_fn)](sequence auto&& seq) mutable { return map_t{}(FLEX_FWD(seq), std::move(fn)); }); } }; inline namespace operation { inline constexpr map_t map{}; } namespace detail { template <typename Fun> struct emplace_from { Fun fun; using type = decltype(std::move(fun)()); constexpr operator type() && noexcept { return std::move(fun)(); } constexpr type operator()() && { return std::move(fun)(); } }; } // namespace detail template <sequence... Bases> struct chain_adaptor : sequence_adaptor_base<chain_adaptor<Bases...>> { std::tuple<Bases...> bases; static constexpr std::size_t Last = sizeof...(Bases) - 1; template <typename Parent, typename... BaseContexts> struct iteration_context { Parent* parent; std::variant<BaseContexts...> base_context; using element_type = std::common_reference_t<typename BaseContexts::element_type...>; template <std::size_t I> constexpr auto run_while_impl(auto& pred) -> iteration_result { if constexpr (I < Last) { if (base_context.index() == I) { auto& ctx = std::get<I>(base_context); auto result = flex::run_while(ctx, pred); if (result == iteration_result::incomplete) { return result; } else { base_context.template emplace<I + 1>(detail::emplace_from{ [&] { return flex::iterate(std::get<I + 1>(parent->bases)); }}); return run_while_impl<I + 1>(pred); } } else { return run_while_impl<I + 1>(pred); } } else { if (base_context.index() == I) { return std::get<I>(base_context).run_while(pred); } else { #ifdef _MSC_VER __assume(false); #else __builtin_unreachable(); #endif } } } constexpr auto run_while(auto&& pred) -> iteration_result { auto call_with_common_ref = [&pred](auto&& elem) { return pred(static_cast<element_type>(FLEX_FWD(elem))); }; return run_while_impl<0>(call_with_common_ref); } }; constexpr auto iterate() { return iteration_context<chain_adaptor, iteration_context_t<Bases>...>{ .parent = this, .base_context = std::variant<iteration_context_t<Bases>...>( std::in_place_index<0>, detail::emplace_from([&] { return flex::iterate(std::get<0>(bases)); }))}; } constexpr auto iterate() const requires(sequence<Bases const> && ...) { return iteration_context<chain_adaptor const, iteration_context_t<Bases const>...>{ .parent = this, .base_context = std::variant<iteration_context_t<Bases const>...>( std::in_place_index<0>, detail::emplace_from([&] { return flex::iterate(std::get<0>(bases)); }))}; } struct flex_collection_traits { using position_type = std::variant<position_t<Bases>...>; template <typename... B> using element_type = std::common_reference_t<collection_element_t<B>...>; static constexpr auto start_pos(auto& self) { return position_type(std::in_place_index<0>, flex::start_pos(std::get<0>(self.bases))); } static constexpr auto end_pos(auto& self) { return position_type(std::in_place_index<Last>, flex::end_pos(std::get<Last>(self.bases))); } template <std::size_t I> static constexpr auto inc_pos_impl(auto& self, position_type& pos) { if constexpr (I < Last) { if (pos.index() == I) { auto& base = std::get<I>(self.bases); auto& base_pos = std::get<I>(pos); flex::inc_pos(base, base_pos); if (base_pos == flex::end_pos(base)) { pos.template emplace<I + 1>(flex::start_pos(std::get<I + 1>(self.bases))); } } else { return inc_pos_impl<I + 1>(self, pos); } } else { if (pos.index() == I) { flex::inc_pos(std::get<I>(self.bases), std::get<I>(pos)); } else { #ifdef _MSC_VER __assume(false); #else __builtin_unreachable(); #endif } } } static constexpr auto inc_pos(auto& self, position_type& pos) { return inc_pos_impl<0>(self, pos); } template <std::size_t I> static constexpr auto read_at_unchecked_impl(auto& self, position_type const& pos) -> std::common_reference_t<collection_element_t<Bases>...> { if (pos.index() == I) { return flex::read_at_unchecked(std::get<I>(self.bases), std::get<I>(pos)); } else { if constexpr (I < Last) { return read_at_unchecked_impl<I + 1>(self, pos); } else { #ifdef _MSC_VER __assume(false); #else __builtin_unreachable(); #endif } } } static constexpr auto read_at_unchecked(auto& self, position_type const& pos) -> std::common_reference_t<collection_element_t<Bases>...> { return read_at_unchecked_impl<0>(self, pos); } }; }; struct chain_t { template <sequence... Seqs> constexpr auto operator()(Seqs... seqs) const -> chain_adaptor<Seqs...> { return chain_adaptor<Seqs...>{.bases = std::tuple<Seqs...>(std::move(seqs)...)}; } }; inline namespace operation { inline constexpr chain_t chain{}; } template <typename Base> struct chunk_adaptor : sequence_adaptor_base<chunk_adaptor<Base>> { Base base; int_t chunk_sz; template <typename BaseCtx> struct outer_context_type { BaseCtx base_ctx; using opt_t = decltype(flex::step(base_ctx)); int_t chunk_sz; opt_t elem{}; int_t remaining = 0; bool base_done = false; struct inner_sequence { outer_context_type* outer_ctx; struct inner_context_type : immovable { outer_context_type* outer_ctx; using element_type = typename BaseCtx::element_type; constexpr auto run_while(auto&& inner_pred) -> iteration_result { while (true) { if (outer_ctx->remaining == 0) { return iteration_result::complete; } if (!outer_ctx->elem) { if (!outer_ctx->read_next()) { return iteration_result::complete; } } --outer_ctx->remaining; auto r = inner_pred(*std::move(outer_ctx->elem)); outer_ctx->elem = {}; if (!r) { return iteration_result::incomplete; } } } }; constexpr auto iterate() const -> inner_context_type { return inner_context_type{.outer_ctx = outer_ctx}; } }; using element_type = inner_sequence; constexpr bool read_next() { elem = flex::step(base_ctx); return static_cast<bool>(elem); } constexpr auto run_while(auto&& outer_pred) -> iteration_result { while (true) { while (remaining-- > 0) { if (!read_next()) { return iteration_result::complete; } } if (!read_next()) { return iteration_result::complete; } remaining = chunk_sz; if (!outer_pred(inner_sequence{.outer_ctx = this})) { return iteration_result::incomplete; } } } }; constexpr auto iterate() { return outer_context_type<iteration_context_t<Base>>{.base_ctx = flex::iterate(base), .chunk_sz = chunk_sz}; } }; struct chunk_t { template <sequence Seq> constexpr auto operator()(Seq seq, int_t chunk_sz) const { return chunk_adaptor<Seq>{.base = std::move(seq), .chunk_sz = chunk_sz}; } }; inline namespace operation { inline constexpr chunk_t chunk{}; } template <typename Base> struct reverse_adaptor : sequence_adaptor_base<reverse_adaptor<Base>> { Base base; struct flex_collection_traits { struct position_type { position_t<Base> base_pos; friend auto operator==(position_type const&, position_type const&) -> bool = default; friend constexpr auto operator<=>(position_type const& lhs, position_type const& rhs) -> std::strong_ordering { return rhs.base_pos <=> lhs.base_pos; } }; static constexpr auto start_pos(auto& self) -> position_type { return {.base_pos = flex::end_pos(self.base)}; } static constexpr auto end_pos(auto& self) -> position_type { return {.base_pos = flex::start_pos(self.base)}; } static constexpr auto inc_pos(auto& self, position_type& pos) { flex::dec_pos(self.base, pos.base_pos); } static constexpr auto dec_pos(auto& self, position_type& pos) { flex::inc_pos(self.base, pos.base_pos); } static constexpr auto read_at_unchecked(auto& self, position_type pos) -> collection_element_t<decltype(self.base)> { flex::dec_pos(self.base, pos.base_pos); return flex::read_at_unchecked(self.base, pos.base_pos); } static constexpr auto iterate_slice(auto& self, position_type from, position_type to, auto&& pred) -> position_type requires bidirectional_collection<decltype(self.base)> { while (from < to) { flex::dec_pos(self.base, from.base_pos); if (!pred(flex::read_at(self.base, from.base_pos))) { flex::inc_pos(self.base, from.base_pos); break; } } return from; } }; }; struct reverse_t : sequence_adaptor_object { template <bidirectional_collection C> constexpr auto operator()(C coll) const -> reverse_adaptor<C> { return reverse_adaptor<C>{.base = std::move(coll)}; } }; inline namespace operation { inline constexpr reverse_t reverse{}; } template <typename Base> struct rotate_adaptor : sequence_adaptor_base<rotate_adaptor<Base>> { Base base; position_t<Base> middle; struct flex_collection_traits { struct position_type { position_t<Base> base_pos; bool looped = false; friend bool operator==(position_type const&, position_type const&) = default; friend constexpr std::strong_ordering operator<=>(position_type const& lhs, position_type const& rhs) { if (lhs.looped == rhs.looped) { return lhs.base_pos <=> rhs.base_pos; } else if (lhs.looped) { return std::strong_ordering::greater; } else { return std::strong_ordering::less; } } }; static constexpr auto start_pos(auto& self) -> position_type { return {.base_pos = self.middle}; } static constexpr auto end_pos(auto& self) -> position_type { return {.base_pos = self.middle, .looped = true}; } static constexpr auto inc_pos(auto& self, position_type& pos) { flex::inc_pos(self.base, pos.base_pos); if (pos.base_pos == flex::end_pos(self.base)) { pos.base_pos = flex::start_pos(self.base); pos.looped = true; } } static constexpr auto dec_pos(auto& self, position_type& pos) -> void requires bidirectional_collection<decltype(self.base)> { if (pos.base_pos == flex::start_pos(self.base)) { pos.base_pos = flex::end_pos(self.base); pos.looped = false; } flex::dec_pos(self.base, pos.base_pos); } static constexpr auto read_at_unchecked(auto& self, position_type const& pos) -> collection_element_t<decltype(self.base)> { return flex::read_at_unchecked(self.base, pos.base_pos); } }; }; struct rotate_t { template <collection C> constexpr auto operator()(C coll, position_t<C> mid) const { if (!is_readable_position(coll, mid) || mid == end_pos(coll)) { #ifdef _MSC_VER std::abort(); #else __builtin_trap(); #endif } return rotate_adaptor<C>{.base = coll, .middle = mid}; } }; struct rotate_by_t { template <collection C> constexpr auto operator()(C coll, std::size_t places) const { auto pos = start_pos(coll); while (places-- > 0) { inc_pos(coll, pos); } return rotate_t{}(std::move(coll), pos); } constexpr auto operator()(std::size_t places) const { return make_sequence_adaptor_object( [places](auto&& coll) { return rotate_by_t{}(FLEX_FWD(coll), places); }); } }; inline namespace operation { inline constexpr rotate_t rotate{}; inline constexpr rotate_by_t rotate_by{}; } // namespace operation template <sequence Base> struct take_adaptor : sequence_adaptor_base<take_adaptor<Base>> { Base base; std::size_t count; template <typename BaseCtx> struct iteration_context { BaseCtx base_ctx; std::size_t remaining; using element_type = typename BaseCtx::element_type; constexpr auto run_while(auto&& pred) -> iteration_result { if (remaining > 0) { auto res = base_ctx.run_while([&](auto&& elem) { --remaining; return pred(FLEX_FWD(elem)) && (remaining > 0); }); return static_cast<iteration_result>(static_cast<bool>(res) || (remaining == 0)); } else { return iteration_result::complete; } } }; constexpr auto iterate() { return iteration_context{.base_ctx = flex::iterate(base), .remaining = count}; } constexpr auto iterate() const requires sequence<Base const> { return iteration_context{.base_ctx = flex::iterate(base), .remaining = count}; } }; template <typename Base> requires collection<Base> struct collection_traits<take_adaptor<Base>> { struct position_type { position_t<Base> base_pos{}; std::size_t remaining = 0; constexpr bool operator==(position_type const& rhs) const& { return remaining == rhs.remaining; } constexpr auto operator<=>(position_type const& rhs) const& -> std::strong_ordering { // This is backwards because we are counting down the remaining elements return rhs.remaining <=> this->remaining; } }; static constexpr auto start_pos(auto& self) -> position_type { return position_type{.base_pos = flex::start_pos(self.base), .remaining = self.count}; } static constexpr auto end_pos(auto&) -> position_type { return position_type{.remaining = 0}; } static constexpr auto inc_pos(auto& self, position_type& pos) -> void { if (pos.remaining > 0) { flex::inc_pos(self.base, pos.base_pos); if (pos.base_pos == flex::end_pos(self.base)) { pos.remaining = 0; } else { --pos.remaining; } } } static constexpr auto read_at_unchecked(auto& self, position_type const& pos) -> collection_element_t<decltype(self.base)> { return flex::read_at_unchecked(self.base, pos.base_pos); } }; struct take_t { template <sequence Seq> constexpr auto operator()(Seq seq, std::size_t count) const { #if 0 if constexpr (random_access_collection<Seq>) { auto limit = start_pos(seq); auto size = distance(seq, start_pos(seq), end_pos(seq)); offset_pos(seq, limit, std::min(size, static_cast<int_t>(count))); return slice_collection<Seq>{ .base = std::move(seq), .from = start_pos(seq), .to = std::move(limit)}; } else { return take_adaptor<Seq>{.base = std::move(seq), .count = count}; } #else return take_adaptor<Seq>{.base = std::move(seq), .count = count}; #endif } constexpr auto operator()(std::size_t count) const { return make_sequence_adaptor_object( [count](auto&& seq) { return take_t{}(FLEX_FWD(seq), count); }); } }; inline namespace operation { inline constexpr take_t take; } template <typename Base> struct flatten_adaptor { Base base; template <typename BaseContext> struct context_type { BaseContext base_ctx; using opt_t = decltype(flex::step(base_ctx)); using inner_ctx_t = iteration_context_t<typename BaseContext::element_type>; opt_t inner_elem{}; std::optional<inner_ctx_t> inner_ctx{}; using element_type = typename inner_ctx_t::element_type; constexpr auto run_while(auto&& pred) -> iteration_result { while (true) { if (!inner_ctx) { inner_elem = flex::step(base_ctx); if (!inner_elem) { return iteration_result::complete; } inner_ctx.emplace( detail::emplace_from([&] { return flex::iterate(*inner_elem); })); } auto r = flex::run_while(*inner_ctx, pred); if (r == iteration_result::incomplete) { return iteration_result::incomplete; } else { inner_ctx.reset(); } } } }; constexpr auto iterate() { return context_type{.base_ctx = flex::iterate(base)}; } }; struct flatten_t : sequence_adaptor_object { template <sequence Seq> requires sequence<typename iteration_context_t<Seq>::element_type> constexpr auto operator()(Seq seq) const { return flatten_adaptor<Seq>{.base = std::move(seq)}; } }; inline namespace operation { inline constexpr flatten_t flatten{}; } /* * MARK: Collection algorithms */ struct find_if_t { template <collection C, typename Pred> constexpr auto operator()(C&& coll, Pred pred) const -> position_t<C> { return iterate_slice(coll, start_pos(coll), end_pos(coll), std::not_fn(pred)); } template <typename Pred> constexpr auto operator()(Pred pred) const { return make_sequence_consumer_object([pred = std::move(pred)](auto&& coll) mutable { return find_if_t{}(FLEX_FWD(coll), std::move(pred)); }); } }; inline namespace operation { inline constexpr find_if_t find_if{}; } /* * MARK: Factories */ template <typename T> concept incrementable = std::regular<T> && requires(T t) { { ++t } -> std::same_as<T&>; { t++ } -> std::same_as<T>; }; template <typename T> concept decrementable = incrementable<T> && requires(T t) { { --t } -> std::same_as<T&>; { t-- } -> std::same_as<T>; }; template <typename T> concept advancable = decrementable<T> && std::weakly_incrementable<T> && requires(T t, T const u, std::iter_difference_t<T> o) { { t += o } -> std::same_as<T&>; { t -= o } -> std::same_as<T&>; T(u + o); T(o + u); T(u - o); { u - u } -> std::convertible_to<int_t>; }; template <incrementable T> struct iota_sequence { T from; struct iteration_context { T value; using element_type = T; auto run_while(auto&& pred) -> iteration_result { while (!pred(value++)) { } return iteration_result::incomplete; } }; constexpr auto iterate() const -> iteration_context { return iteration_context{.value = from}; } }; template <incrementable T> struct bounded_iota_sequence { T from; T to; struct iteration_context { T value; T last; using element_type = T; auto run_while(auto&& pred) -> iteration_result { while (value != last) { if (!pred(value++)) { return iteration_result::incomplete; } } return iteration_result::complete; } }; constexpr auto iterate() const { return iteration_context{.value = from, .last = to}; }; }; template <incrementable T> requires std::totally_ordered<T> struct iota_collection : sequence_adaptor_base<iota_collection<T>> { T from; T to; struct flex_collection_traits { private: using self_t = iota_collection; struct position_type { T current; constexpr auto operator<=>(position_type const&) const -> std::strong_ordering = default; }; public: static constexpr auto start_pos(self_t const& self) { return position_type{self.from}; } static constexpr auto end_pos(self_t const& self) { return position_type{self.to}; } static constexpr auto inc_pos(self_t const&, position_type& pos) { ++pos.current; } static constexpr auto dec_pos(self_t const&, position_type& pos) -> void requires decrementable<T> { --pos.current; } static constexpr auto read_at_unchecked(self_t const&, position_type const& pos) -> T { return pos.current; } static constexpr auto offset_pos(self_t const&, position_type& pos, int_t offset) -> void requires advancable<T> { pos.current += offset; } static constexpr auto distance(self_t const&, position_type const& from, position_type const& to) -> int_t { return static_cast<int_t>(to.current - from.current); } static constexpr auto iterate_slice(self_t const&, position_type from, position_type to, auto&& pred) -> position_type { while (from < to) { if (!pred(T(from.current))) { break; } ++from.current; } return from; } }; }; struct iota_t { template <incrementable I> constexpr auto operator()(I from, I to) const -> sequence auto { if constexpr (std::three_way_comparable<I, std::strong_ordering>) { return iota_collection<I>{.from = from, .to = to}; } else { return bounded_iota_sequence<I>{.from = from, .to = to}; } } template <incrementable I> constexpr auto operator()(I from) const -> sequence auto { if constexpr (std::is_arithmetic_v<I>) { return iota_t{}(from, std::numeric_limits<I>::max()); } else { return iota_sequence<I>{.from = from}; } } }; inline namespace operation { inline constexpr iota_t iota{}; template <typename T, typename CharT, typename Traits> struct istream_sequence : sequence_adaptor_base<istream_sequence<T, CharT, Traits>> { std::basic_istream<CharT, Traits>* is; struct context : immovable { std::basic_istream<CharT, Traits>* is; T value{}; using element_type = T const&; auto run_while(auto&& pred) -> iteration_result { while (*is >> value) { if (!pred(std::as_const(value))) { return iteration_result::incomplete; } } return iteration_result::complete; } }; auto iterate() -> context { return {.is = is}; } }; template <std::default_initializable T> struct from_istream_t { template <typename CharT, typename Traits> constexpr auto operator()(std::basic_istream<CharT, Traits>& stream) const { return istream_sequence<T, CharT, Traits>{.is = std::addressof(stream)}; } }; template <typename T> inline constexpr from_istream_t<T> from_istream; } // namespace operation } // namespace flex /* * MARK: main() */ constexpr auto print_int = [](int i) { std::printf("%d\n", i); }; int main() { std::vector<int> vec{1, 2, 3, 4, 5}; std::cref(vec) | flex::map([](int* i) { return i; }); //flex::map(std::cref(vec), [](int* i) { return i; }); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree