C++ Standard Template Library browser.
1/* ========================================================================
2 *
3 * Filename: prelude.hpp
4 * Description: C++ sensible prelude - Declarations
5 * Author: Diego A. Estrada Rivera
6 * Version: 0.0.4
7 *
8 * ======================================================================== */
9#pragma once
10
11/* === Generic includes === */
12#include <list>
13
14#include <queue>
15#include <span>
16#include <stack>
17#include <unordered_map>
18#include <unordered_set>
19
20#include <algorithm>
21#include <numeric>
22#include <ranges>
23
24#include <format>
25#include <fstream>
26
27/* === Numeric Types === */
28#include <cstdint>
29#include <limits.h>
30using Size = std::size_t;
31
32/* Floating Point */
33// brain floating point
34// see https://en.wikipedia.org/wiki/Bfloat16_floating-point_format
35typedef __bf16 BF16;
36typedef _Float16 F16;
37typedef float F32;
38static_assert(sizeof(F32) * CHAR_BIT == 32); //
39typedef double F64;
40static_assert(sizeof(F64) * CHAR_BIT == 64);
41#if __float80_max < __float128_max
42typedef __float80 F80; // sometimes is the same as F128
43#endif
44typedef __float128 F128;
45
46// decimal floating points
47// see decimal32/64/128 in https://en.wikipedia.org/wiki/IEEE_754
48#if 0
49typedef float __attribute__((mode(SD))) D32;
50typedef float __attribute__((mode(DD))) D64;
51typedef float __attribute__((mode(TD))) D128;
52#endif
53
54/* Integer */
55typedef uint8_t U8;
56typedef uint16_t U16;
57typedef uint32_t U32;
58typedef uint64_t U64;
59typedef __uint128_t U128;
60
61typedef int8_t I8;
62typedef int16_t I16;
63typedef int32_t I32;
64typedef int64_t I64;
65typedef __int128_t I128;
66
67// generic Integer type
68typedef int_fast32_t Int;
69
70/* === Other Types === */
71typedef bool Bool;
72typedef char Char;
73typedef const char* CString;
74
75#include <vector>
76template <typename T> using Vector = std::vector<T>;
77
78#include <string>
79using String = std::string;
80using namespace std::string_literals;
81
82namespace ra = std::ranges;
83namespace vi = std::ranges::views;
84
85// Unit type
86enum class Unit { unit };
87
88// Void type
89enum class Void {};
90
91// Pointer types
92template <typename T> using Ref = T&;
93
94template <typename T> using Handle = T*;
95
96/* === Keywords === */
97#define let auto const
98#define var auto
99#define fn [[nodiscard, gnu::const]] auto
100#define proc [[nodiscard]] auto
101
102/* This is included as a static member variable of <tuple>.
103 * However, there is no way to bring the std::ignore namespace static variable
104 * into the current scope. This is a way to do that.
105 * https://en.cppreference.com/w/cpp/utility/tuple/ignore
106 */
107namespace detail {
108struct ignore_t {
109 template <typename T>
110 constexpr // required since C++14
111 void
112 operator=(T&&) const noexcept
113 {
114 }
115};
116} // namespace detail
117inline constinit const detail::ignore_t ignore; // changed to constinit
118
119// manage tuples better
120#define fst std::get<0>
121#define snd std::get<1>
122#define thr std::get<2>
123
124/* === Miscellaneous Functions */
125template <class D, class C> fn memoize(let& op) noexcept
126{
127 static var mp = std::unordered_map<D, C>();
128 return [=](D const& x) noexcept {
129 return mp.contains(x) ? mp.at(x) : mp[x] = op(x);
130 };
131}
132
133/* Sensible IO */
134#include <iostream>
135proc getLine() noexcept -> String;
136
137proc getChar() noexcept -> Char;
138
139proc print(const std::string_view& f) noexcept -> Unit;
140
141proc println(const std::string_view& f) noexcept -> Unit;