-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.hpp
More file actions
1270 lines (1029 loc) · 30 KB
/
Copy pathshared.hpp
File metadata and controls
1270 lines (1029 loc) · 30 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2026, Jiaming Meng (jackm@exoad.net)
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <cstdint>
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <climits>
#include <cassert>
#include <memory>
#include <string>
#include <string_view>
#include <functional>
#include <optional>
#include <variant>
#include <expected>
#include <span>
#include <array>
#include <vector>
#include <deque>
#include <list>
#include <forward_list>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <stack>
#include <queue>
#include <bitset>
#include <tuple>
#include <pair>
#include <type_traits>
#include <concepts>
#include <utility>
#include <limits>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <ranges>
#include <compare>
#include <atomic>
#include <mutex>
#include <shared_mutex>
#include <condition_variable>
#include <thread>
#include <future>
#include <semaphore>
#include <latch>
#include <barrier>
#include <chrono>
#include <ratio>
#include <stdexcept>
#include <exception>
#include <system_error>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <filesystem>
#include <regex>
#include <random>
#include <complex>
#include <valarray>
#include <any>
#include <typeindex>
#include <typeinfo>
#include <source_location>
#include <bit>
#include <numbers>
#include <version>
using Int8 = int8_t;
using Int16 = int16_t;
using Int32 = int32_t;
using Int64 = int64_t;
using UInt8 = uint8_t;
using UInt16 = uint16_t;
using UInt32 = uint32_t;
using UInt64 = uint64_t;
using Float32 = float;
using Float64 = double;
using Float128 = long double;
using Utf8 = char;
using Utf16 = char16_t;
using Utf32 = char32_t;
using WChar = wchar_t;
using Bool = bool;
using Byte = std::byte;
using Size = std::size_t;
using PtrDiff = std::ptrdiff_t;
using UPtr = std::uintptr_t;
using IPtr = std::intptr_t;
using Void = void;
using Any = void*;
using CFile = std::FILE;
using NullPtr = std::nullptr_t;
using Str = std::string;
using StrView = std::string_view;
using WStr = std::wstring;
using WStrView = std::wstring_view;
using Str16 = std::u16string;
using Str32 = std::u32string;
using Str8 = std::u8string;
template<typename T>
using UniqPtr = std::unique_ptr<T>;
template<typename T>
using SharedPtr = std::shared_ptr<T>;
template<typename T>
using WeakPtr = std::weak_ptr<T>;
template<typename T>
using Opt = std::optional<T>;
template<typename... Ts>
using Variant = std::variant<Ts...>;
template<typename T, typename E>
using Expected = std::expected<T, E>;
template<typename T>
using Span = std::span<T>;
template<typename T, Size N>
using Arr = std::array<T, N>;
template<typename F, typename S>
using Pair = std::pair<F, S>;
template<typename... Ts>
using Tuple = std::tuple<Ts...>;
template<typename Sig>
using Fn = std::function<Sig>;
template<typename T>
using Complex = std::complex<T>;
template<typename T>
using ValArr = std::valarray<T>;
template<Size N>
using BitSet = std::bitset<N>;
using AnyVal = std::any;
using TypeIdx = std::type_index;
using TypeInfo = std::type_info;
using Monostate = std::monostate;
using SrcLoc = std::source_location;
using ErrCode = std::error_code;
using ErrCond = std::error_condition;
using ErrCat = std::error_category;
template<typename T>
using Vec = std::vector<T>;
template<typename T>
using Deque = std::deque<T>;
template<typename T>
using List = std::list<T>;
template<typename T>
using FwdList = std::forward_list<T>;
template<typename K, typename V>
using Map = std::map<K, V>;
template<typename K, typename V>
using MultiMap = std::multimap<K, V>;
template<typename K, typename V>
using HashMap = std::unordered_map<K, V>;
template<typename K, typename V>
using HashMultiMap = std::unordered_multimap<K, V>;
template<typename T>
using Set = std::set<T>;
template<typename T>
using MultiSet = std::multiset<T>;
template<typename T>
using HashSet = std::unordered_set<T>;
template<typename T>
using HashMultiSet = std::unordered_multiset<T>;
template<typename T>
using Stack = std::stack<T>;
template<typename T>
using Queue = std::queue<T>;
template<typename T>
using PrioQueue = std::priority_queue<T>;
template<typename T>
using InitList = std::initializer_list<T>;
template<typename Iterator>
using IterTraits = std::iterator_traits<Iterator>;
template<typename T>
using ReverseIter = std::reverse_iterator<T>;
using StrongOrdering = std::strong_ordering;
using WeakOrdering = std::weak_ordering;
using PartialOrdering = std::partial_ordering;
using Thread = std::thread;
using JThread = std::jthread;
using Mutex = std::mutex;
using RMutex = std::recursive_mutex;
using TMutex = std::timed_mutex;
using RTMutex = std::recursive_timed_mutex;
using SRMutex = std::shared_mutex;
using SRTMutex = std::shared_timed_mutex;
using CondVar = std::condition_variable;
using CondVarAny = std::condition_variable_any;
using OnceFlag = std::once_flag;
template<typename T>
using Atomic = std::atomic<T>;
template<typename T>
using Future = std::future<T>;
template<typename T>
using SharedFuture = std::shared_future<T>;
template<typename T>
using Promise = std::promise<T>;
template<typename T>
using PackagedTask = std::packaged_task<T>;
template<std::ptrdiff_t LEAST_MAX>
using CountingSem = std::counting_semaphore<LEAST_MAX>;
using BinarySem = std::binary_semaphore;
using Latch = std::latch;
template<typename CompletionFn = std::ptrdiff_t>
using Barrier = std::barrier<CompletionFn>;
template<typename T>
using LockGuard = std::lock_guard<T>;
template<typename T>
using UniqLock = std::unique_lock<T>;
template<typename T>
using SharedLock = std::shared_lock<T>;
template<typename... Mutexes>
using ScopedLock = std::scoped_lock<Mutexes...>;
using SysClock = std::chrono::system_clock;
using SteadyClock = std::chrono::steady_clock;
using HiResClock = std::chrono::high_resolution_clock;
using Nanoseconds = std::chrono::nanoseconds;
using Microseconds = std::chrono::microseconds;
using Milliseconds = std::chrono::milliseconds;
using Seconds = std::chrono::seconds;
using Minutes = std::chrono::minutes;
using Hours = std::chrono::hours;
using Days = std::chrono::days;
using Weeks = std::chrono::weeks;
using Months = std::chrono::months;
using Years = std::chrono::years;
template<typename Rep, typename Period = std::ratio<1>>
using Duration = std::chrono::duration<Rep, Period>;
template<typename Clock, typename Duration = typename Clock::duration>
using TimePoint = std::chrono::time_point<Clock, Duration>;
using IStream = std::istream;
using OStream = std::ostream;
using IOStream = std::iostream;
using IFStream = std::ifstream;
using OFStream = std::ofstream;
using FStream = std::fstream;
using IStrStream = std::istringstream;
using OStrStream = std::ostringstream;
using StrStream = std::stringstream;
using Path = std::filesystem::path;
using DirEntry = std::filesystem::directory_entry;
using DirIter = std::filesystem::directory_iterator;
using RecDirIter = std::filesystem::recursive_directory_iterator;
using FileStatus = std::filesystem::file_status;
using SpaceInfo = std::filesystem::space_info;
using FileTime = std::filesystem::file_time_type;
using FsErrCode = std::filesystem::filesystem_error;
using FileType = std::filesystem::file_type;
using Perms = std::filesystem::perms;
using CopyOpts = std::filesystem::copy_options;
using RandDev = std::random_device;
using Mt19937 = std::mt19937;
using Mt19937_64 = std::mt19937_64;
template<typename IntType = Int32>
using UniformIntDist = std::uniform_int_distribution<IntType>;
template<typename RealType = Float64>
using UniformRealDist = std::uniform_real_distribution<RealType>;
template<typename RealType = Float64>
using NormalDist = std::normal_distribution<RealType>;
template<typename RealType = Float64>
using BernoulliDist = std::bernoulli_distribution;
template<typename IntType = Int32>
using PoissonDist = std::poisson_distribution<IntType>;
using Regex = std::regex;
using SMatch = std::smatch;
using SSMatch = std::ssub_match;
using RegexErr = std::regex_error;
using RegexFlags = std::regex_constants::syntax_option_type;
using Exception = std::exception;
using RuntimeErr = std::runtime_error;
using LogicErr = std::logic_error;
using RangeErr = std::range_error;
using OverflowErr = std::overflow_error;
using UnderflowErr = std::underflow_error;
using OutOfRangeErr = std::out_of_range;
using InvalidArgErr = std::invalid_argument;
using LengthErr = std::length_error;
using DomainErr = std::domain_error;
using BadAlloc = std::bad_alloc;
using BadCast = std::bad_cast;
using BadTypeid = std::typeid;
using BadOptAccess = std::bad_optional_access;
using BadVariantAccess = std::bad_variant_access;
using BadAnyAccess = std::bad_any_cast;
using BadWeakPtr = std::bad_weak_ptr;
using BadFnCall = std::bad_function_call;
using SysErr = std::system_error;
using ExceptionPtr = std::exception_ptr;
using NestedExc = std::nested_exception;
template<typename T>
using RemoveRef = std::remove_reference_t<T>;
template<typename T>
using RemoveConst = std::remove_const_t<T>;
template<typename T>
using RemoveCV = std::remove_cv_t<T>;
template<typename T>
using RemoveCVRef = std::remove_cvref_t<T>;
template<typename T>
using RemovePtr = std::remove_pointer_t<T>;
template<typename T>
using RemoveExtent = std::remove_extent_t<T>;
template<typename T>
using RemoveAllExtents = std::remove_all_extents_t<T>;
template<typename T>
using AddConst = std::add_const_t<T>;
template<typename T>
using AddLRef = std::add_lvalue_reference_t<T>;
template<typename T>
using AddRRef = std::add_rvalue_reference_t<T>;
template<typename T>
using AddPtr = std::add_pointer_t<T>;
template<typename T>
using MakeSigned = std::make_signed_t<T>;
template<typename T>
using MakeUnsigned = std::make_unsigned_t<T>;
template<typename T>
using Decay = std::decay_t<T>;
template<typename T>
using Underlying = std::underlying_type_t<T>;
template<typename T>
using TypeIdentity = std::type_identity_t<T>;
template<Bool COND, typename T = void>
using EnableIf = std::enable_if_t<COND, T>;
template<Bool COND, typename T, typename F>
using Conditional = std::conditional_t<COND, T, F>;
template<typename... Ts>
using CommonType = std::common_type_t<Ts...>;
template<typename T>
using InvokeResult = std::invoke_result_t<T>;
template<typename... Ts>
using VoidT = std::void_t<Ts...>;
template<typename T>
using NumLimits = std::numeric_limits<T>;
using Nano = std::nano;
using Micro = std::micro;
using Milli = std::milli;
using Centi = std::centi;
using Deci = std::deci;
using Kilo = std::kilo;
using Mega = std::mega;
using Giga = std::giga;
using Tera = std::tera;
using Peta = std::peta;
using Exa = std::exa;
constexpr Float64 PI = std::numbers::pi;
constexpr Float64 E = std::numbers::e;
constexpr Float64 PHI = std::numbers::phi;
constexpr Float64 SQRT2 = std::numbers::sqrt2;
constexpr Float64 SQRT3 = std::numbers::sqrt3;
constexpr Float64 LN2 = std::numbers::ln2;
constexpr Float64 LN10 = std::numbers::ln10;
constexpr Float64 LOG2E = std::numbers::log2e;
constexpr Float64 LOG10E = std::numbers::log10e;
constexpr Float64 INV_PI = std::numbers::inv_pi;
constexpr Float64 INV_SQRT_PI = std::numbers::inv_sqrtpi;
constexpr Float64 EGAMMA = std::numbers::egamma;
template<typename T, typename... Args>
UniqPtr<T> makeUniq(Args&&... args)
{
return std::make_unique<T>(std::forward<Args>(args)...);
}
template<typename T, typename... Args>
SharedPtr<T> makeShared(Args&&... args)
{
return std::make_shared<T>(std::forward<Args>(args)...);
}
template<typename T>
SharedPtr<T> staticPtrCast(const SharedPtr<auto>& ptr) noexcept
{
return std::static_pointer_cast<T>(ptr);
}
template<typename T>
SharedPtr<T> dynamicPtrCast(const SharedPtr<auto>& ptr) noexcept
{
return std::dynamic_pointer_cast<T>(ptr);
}
template<typename T>
SharedPtr<T> constPtrCast(const SharedPtr<auto>& ptr) noexcept
{
return std::const_pointer_cast<T>(ptr);
}
template<typename T>
constexpr RemoveRef<T>&& mv(T&& t) noexcept
{
return std::move(t);
}
template<typename T>
constexpr T&& fwd(RemoveRef<T>& t) noexcept
{
return std::forward<T>(t);
}
template<typename T>
constexpr T&& fwd(RemoveRef<T>&& t) noexcept
{
return std::forward<T>(t);
}
template<typename T>
constexpr Decay<T> decay(T&& t) noexcept(noexcept(Decay<T>(std::forward<T>(t))))
{
return std::forward<T>(t);
}
template<typename T>
void swp(T& a, T& b) noexcept(std::is_nothrow_swappable_v<T>)
{
std::swap(a, b);
}
template<typename T, typename U = T>
T exchange(T& obj, U&& newVal) noexcept(
std::is_nothrow_move_constructible_v<T> &&
std::is_nothrow_assignable_v<T&, U>)
{
return std::exchange(obj, std::forward<U>(newVal));
}
template<typename T>
constexpr Opt<Decay<T>> some(T&& value)
{
return std::make_optional<Decay<T>>(std::forward<T>(value));
}
constexpr std::nullopt_t none = std::nullopt;
template<typename T, typename... Ts>
constexpr Bool holdsAlt(const Variant<Ts...>& v) noexcept
{
return std::holds_alternative<T>(v);
}
template<typename T, typename... Ts>
constexpr T& getAlt(Variant<Ts...>& v)
{
return std::get<T>(v);
}
template<typename T, typename... Ts>
constexpr const T& getAlt(const Variant<Ts...>& v)
{
return std::get<T>(v);
}
template<typename T, typename... Ts>
constexpr T* getIf(Variant<Ts...>* v) noexcept
{
return std::get_if<T>(v);
}
template<typename T, typename... Ts>
constexpr const T* getIf(const Variant<Ts...>* v) noexcept
{
return std::get_if<T>(v);
}
template<typename... Visitors>
struct Overload : Visitors...
{
using Visitors::operator()...;
};
template<typename... Visitors>
Overload(Visitors...) -> Overload<Visitors...>;
template<typename Var, typename... Visitors>
decltype(auto) visit(Var&& var, Visitors&&... visitors)
{
return std::visit(
Overload{ std::forward<Visitors>(visitors)... },
std::forward<Var>(var)
);
}
template<typename... Ts>
constexpr Tuple<Decay<Ts>...> makeTuple(Ts&&... args)
{
return std::make_tuple(std::forward<Ts>(args)...);
}
template<typename... Ts>
constexpr Tuple<Ts&...> tieAll(Ts&... args) noexcept
{
return std::tie(args...);
}
template<typename... Tuples>
constexpr auto tupleCat(Tuples&&... tuples)
{
return std::tuple_cat(std::forward<Tuples>(tuples)...);
}
template<Size I, typename Tup>
constexpr auto& tupleGet(Tup&& tup) noexcept
{
return std::get<I>(std::forward<Tup>(tup));
}
template<typename F, typename S>
constexpr Pair<Decay<F>, Decay<S>> makePair(F&& first, S&& second)
{
return std::make_pair(std::forward<F>(first), std::forward<S>(second));
}
template<typename T>
constexpr T absVal(T x) noexcept
{
return std::abs(x);
}
template<typename T>
constexpr T minVal(T a, T b) noexcept
{
return std::min(a, b);
}
template<typename T>
constexpr T maxVal(T a, T b) noexcept
{
return std::max(a, b);
}
template<typename T>
constexpr T clampVal(T value, T lo, T hi) noexcept
{
return std::clamp(value, lo, hi);
}
template<typename T>
constexpr Pair<T, T> minMaxVal(T a, T b) noexcept
{
return std::minmax(a, b);
}
template<typename T>
constexpr T gcdVal(T a, T b) noexcept
{
return std::gcd(a, b);
}
template<typename T>
constexpr T lcmVal(T a, T b) noexcept
{
return std::lcm(a, b);
}
template<typename T>
constexpr T midpointVal(T a, T b) noexcept
{
return std::midpoint(a, b);
}
template<typename T>
constexpr T lerpVal(T a, T b, T t) noexcept
{
return std::lerp(a, b, t);
}
template<std::integral T>
constexpr Bool hasOneBit(T x) noexcept
{
return std::has_single_bit(x);
}
template<std::integral T>
constexpr T bitCeil(T x) noexcept
{
return std::bit_ceil(x);
}
template<std::integral T>
constexpr T bitFloor(T x) noexcept
{
return std::bit_floor(x);
}
template<std::integral T>
constexpr Int32 bitWidth(T x) noexcept
{
return std::bit_width(x);
}
template<std::integral T>
constexpr T rotl(T x, Int32 s) noexcept
{
return std::rotl(x, s);
}
template<std::integral T>
constexpr T rotr(T x, Int32 s) noexcept
{
return std::rotr(x, s);
}
template<std::integral T>
constexpr Int32 countl0(T x) noexcept
{
return std::countl_zero(x);
}
template<std::integral T>
constexpr Int32 countl1(T x) noexcept
{
return std::countl_one(x);
}
template<std::integral T>
constexpr Int32 countr0(T x) noexcept
{
return std::countr_zero(x);
}
template<std::integral T>
constexpr Int32 countr1(T x) noexcept
{
return std::countr_one(x);
}
template<std::integral T>
constexpr Int32 popCount(T x) noexcept
{
return std::popcount(x);
}
template<typename To, typename From>
constexpr To bitCast(const From& from) noexcept
{
return std::bit_cast<To>(from);
}
template<typename Range, typename T>
auto findIn(Range&& r, const T& value)
{
return std::find(std::begin(r), std::end(r), value);
}
template<typename Range, typename Pred>
auto findIf(Range&& r, Pred&& pred)
{
return std::find_if(std::begin(r), std::end(r), std::forward<Pred>(pred));
}
template<typename Range, typename Pred>
Bool anyOf(Range&& r, Pred&& pred)
{
return std::any_of(std::begin(r), std::end(r), std::forward<Pred>(pred));
}
template<typename Range, typename Pred>
Bool allOf(Range&& r, Pred&& pred)
{
return std::all_of(std::begin(r), std::end(r), std::forward<Pred>(pred));
}
template<typename Range, typename Pred>
Bool noneOf(Range&& r, Pred&& pred)
{
return std::none_of(std::begin(r), std::end(r), std::forward<Pred>(pred));
}
template<typename Range, typename Fn>
Fn forEach(Range&& r, Fn&& fn)
{
return std::for_each(std::begin(r), std::end(r), std::forward<Fn>(fn));
}
template<typename Range>
void sortRange(Range&& r)
{
std::sort(std::begin(r), std::end(r));
}
template<typename Range, typename Cmp>
void sortRange(Range&& r, Cmp&& cmp)
{
std::sort(std::begin(r), std::end(r), std::forward<Cmp>(cmp));
}
template<typename Range>
void stableSort(Range&& r)
{
std::stable_sort(std::begin(r), std::end(r));
}
template<typename Range, typename Cmp>
void stableSort(Range&& r, Cmp&& cmp)
{
std::stable_sort(std::begin(r), std::end(r), std::forward<Cmp>(cmp));
}
template<typename Range, typename T>
Bool binSearch(Range&& r, const T& value)
{
return std::binary_search(std::begin(r), std::end(r), value);
}
template<typename Range, typename T>
auto lowerBound(Range&& r, const T& value)
{
return std::lower_bound(std::begin(r), std::end(r), value);
}
template<typename Range, typename T>
auto upperBound(Range&& r, const T& value)
{
return std::upper_bound(std::begin(r), std::end(r), value);
}
template<typename Range, typename T>
Size countIn(Range&& r, const T& value)
{
return static_cast<Size>(std::count(std::begin(r), std::end(r), value));
}
template<typename Range, typename Pred>
Size countIf(Range&& r, Pred&& pred)
{
return static_cast<Size>(std::count_if(std::begin(r), std::end(r), std::forward<Pred>(pred)));
}
template<typename Range>
auto minElem(Range&& r)
{
return std::min_element(std::begin(r), std::end(r));
}
template<typename Range>
auto maxElem(Range&& r)
{
return std::max_element(std::begin(r), std::end(r));
}
template<typename Range>
auto minMaxElem(Range&& r)
{
return std::minmax_element(std::begin(r), std::end(r));
}
template<typename Range, typename T>
void fillRange(Range&& r, const T& value)
{
std::fill(std::begin(r), std::end(r), value);
}
template<typename Range>
void reverseRange(Range&& r)
{
std::reverse(std::begin(r), std::end(r));
}
template<typename Range>
void uniqueRange(Range&& r)
{
std::unique(std::begin(r), std::end(r));
}
template<typename Range, typename T>
void removeFrom(Range&& r, const T& value)
{
std::remove(std::begin(r), std::end(r), value);
}
template<typename Range, typename Pred>
void removeIf(Range&& r, Pred&& pred)
{
std::remove_if(std::begin(r), std::end(r), std::forward<Pred>(pred));
}
template<typename Range, typename T>
T accumulate(Range&& r, T init)
{
return std::accumulate(std::begin(r), std::end(r), init);
}
template<typename Range, typename T, typename BinOp>
T accumulate(Range&& r, T init, BinOp&& op)
{
return std::accumulate(std::begin(r), std::end(r), init, std::forward<BinOp>(op));
}
template<typename Range, typename T>
T reduce(Range&& r, T init)
{
return std::reduce(std::begin(r), std::end(r), init);
}
inline Str toStr(Int32 v) { return std::to_string(v); }
inline Str toStr(Int64 v) { return std::to_string(v); }
inline Str toStr(UInt32 v) { return std::to_string(v); }
inline Str toStr(UInt64 v) { return std::to_string(v); }
inline Str toStr(Float32 v) { return std::to_string(v); }
inline Str toStr(Float64 v) { return std::to_string(v); }
inline Str toStr(Float128 v) { return std::to_string(v); }
inline Int32 toInt(const Str& s) { return std::stoi(s); }
inline Int64 toLong(const Str& s) { return std::stoll(s); }
inline UInt64 toULong(const Str& s) { return std::stoull(s); }
inline Float32 toFloat(const Str& s) { return std::stof(s); }
inline Float64 toDouble(const Str& s) { return std::stod(s); }
inline void memCopy(Void* dst, const Void* src, Size n) noexcept
{
std::memcpy(dst, src, n);
}
inline void memMove(Void* dst, const Void* src, Size n) noexcept
{
std::memmove(dst, src, n);
}
inline void memSet(Void* dst, Int32 value, Size n) noexcept
{
std::memset(dst, value, n);
}
inline Int32 memCmp(const Void* a, const Void* b, Size n) noexcept
{
return std::memcmp(a, b, n);
}
template<typename T>
T* addressOf(T& ref) noexcept
{
return std::addressof(ref);
}
template<typename T>
void destroyAt(T* ptr)
{
std::destroy_at(ptr);
}
template<typename T, typename... Args>
T* constructAt(T* ptr, Args&&... args)
{
return std::construct_at(ptr, std::forward<Args>(args)...);
}
template<typename T, typename U>
constexpr Bool isSame = std::is_same_v<T, U>;
template<typename T>
constexpr Bool isConst = std::is_const_v<T>;
template<typename T>
constexpr Bool isRef = std::is_reference_v<T>;
template<typename T>
constexpr Bool isLRef = std::is_lvalue_reference_v<T>;
template<typename T>
constexpr Bool isRRef = std::is_rvalue_reference_v<T>;
template<typename T>
constexpr Bool isPtr = std::is_pointer_v<T>;
template<typename T>
constexpr Bool isArr = std::is_array_v<T>;
template<typename T>
constexpr Bool isEnum = std::is_enum_v<T>;
template<typename T>
constexpr Bool isClass = std::is_class_v<T>;
template<typename T>
constexpr Bool isUnion = std::is_union_v<T>;
template<typename T>
constexpr Bool isIntegral = std::is_integral_v<T>;
template<typename T>
constexpr Bool isFloat = std::is_floating_point_v<T>;
template<typename T>
constexpr Bool isArithmetic = std::is_arithmetic_v<T>;
template<typename T>
constexpr Bool isSigned = std::is_signed_v<T>;
template<typename T>
constexpr Bool isUnsigned = std::is_unsigned_v<T>;
template<typename T>
constexpr Bool isVoid = std::is_void_v<T>;
template<typename T>
constexpr Bool isNullPtr = std::is_null_pointer_v<T>;
template<typename T>
constexpr Bool isTrivial = std::is_trivial_v<T>;