summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--README.md130
-rw-r--r--benchmarks/others/clist_v1.h2
-rw-r--r--benchmarks/others/csmap_v1.h8
-rw-r--r--benchmarks/others/pcg_extras.hpp664
-rw-r--r--benchmarks/others/pcg_random.hpp1947
-rw-r--r--benchmarks/others/pcg_uint128.hpp884
-rw-r--r--benchmarks/others/sstr.h8
-rw-r--r--docs/ccommon_api.md54
-rw-r--r--docs/cdeq_api.md2
-rw-r--r--docs/clist_api.md2
-rw-r--r--docs/cmap_api.md4
-rw-r--r--docs/cset_api.md4
-rw-r--r--docs/csmap_api.md12
-rw-r--r--docs/csset_api.md4
-rw-r--r--docs/cstr_api.md17
-rw-r--r--docs/csview_api.md10
-rw-r--r--docs/cvec_api.md2
-rw-r--r--examples/csmap_erase.c154
-rw-r--r--examples/csmap_find.c133
-rw-r--r--examples/cstr_match.c2
-rw-r--r--examples/ex_gauss1.c22
-rw-r--r--examples/mmap.c18
-rw-r--r--examples/priority.c2
-rw-r--r--include/stc/ccommon.h16
-rw-r--r--include/stc/cdeq.h2
-rw-r--r--include/stc/clist.h2
-rw-r--r--include/stc/cmap.h36
-rw-r--r--include/stc/cset.h6
-rw-r--r--include/stc/csmap.h33
-rw-r--r--include/stc/csptr.h8
-rw-r--r--include/stc/csset.h5
-rw-r--r--include/stc/cstr.h17
-rw-r--r--include/stc/csview.h10
-rw-r--r--include/stc/cvec.h2
34 files changed, 382 insertions, 3840 deletions
diff --git a/README.md b/README.md
index a77cdf96..d6738938 100644
--- a/README.md
+++ b/README.md
@@ -39,7 +39,7 @@ Highlights
- **Fully memory managed** - All containers will destruct keys/values via destructor passed as macro parameters to the ***using***-declaration. Also, shared pointers are supported and can be stored in containers, see ***csptr***.
- **Fully type safe** - Because of templating, it avoids error-prone casting of container types and elements back and forth from the containers.
- **Uniform, easy-to-learn API** - Methods to ***construct***, ***initialize***, ***iterate*** and ***destruct*** have uniform and intuitive usage across the various containers.
-- **Small footprint** - Small source code and generated executables. The executable from the example below using six different containers is *27 kb in size* compiled with TinyC.
+- **Small footprint** - Small source code and generated executables. The executable from the example below with six different containers is *22 kb in size* compiled with gcc -Os on linux.
- **Dual mode compilation** - By default it is a simple header-only library with inline and static methods only, but you can easily switch to create a traditional library with shared symbols, without changing existing source files. See the Installation section.
- **No callback functions** - All passed template argument functions/macros are directly called from the implementation, no slow callbacks which requires storage.
- **Compiles with C++ and C99** - C code can be compiled with C++.
@@ -64,18 +64,18 @@ templates in c++. A basic usage example:
```c
#include <stc/cvec.h>
-using_cvec(i, int);
+using_cvec(f32, float);
int main(void) {
- cvec_i vec = cvec_i_init();
- cvec_i_push_back(&vec, 10);
- cvec_i_push_back(&vec, 20);
- cvec_i_push_back(&vec, 30);
+ cvec_f32 vec = cvec_f32_init();
+ cvec_f32_push_back(&vec, 10.f);
+ cvec_f32_push_back(&vec, 20.f);
+ cvec_f32_push_back(&vec, 30.f);
- c_foreach (i, cvec_i, vec)
- printf(" %d", *i.ref);
+ c_foreach (i, cvec_f32, vec)
+ printf(" %g", *i.ref);
- cvec_i_del(&vec);
+ cvec_f32_del(&vec);
}
```
With six different containers:
@@ -91,66 +91,68 @@ With six different containers:
struct Point { float x, y; };
int Point_compare(const struct Point* a, const struct Point* b) {
- if (a->x != b->x) return 1 - 2*(a->x < b->x);
- return (a->y > b->y) - (a->y < b->y);
+ int cmp = c_default_compare(&a->x, &b->x);
+ return cmp ? cmp : c_default_compare(&a->y, &b->y);
}
// declare container types
-using_cset(i, int); // unordered set
-using_cvec(p, struct Point, Point_compare); // vector, struct as elements
-using_cdeq(i, int); // deque
-using_clist(i, int); // singly linked list
-using_cqueue(i, cdeq_i); // queue, using deque as adapter
-using_csmap(i, int, int); // sorted map
+using_cset(i32, int); // unordered set
+using_cvec(pnt, struct Point, Point_compare); // vector, struct as elements
+using_cdeq(i32, int); // deque
+using_clist(i32, int); // singly linked list
+using_cqueue(i32, cdeq_i32); // queue, using deque as adapter
+using_csmap(i32, int, int); // sorted map
int main(void) {
- // shorthand for define + initialize containers with c_var():
- c_var (cset_i, set, {10, 20, 30});
- c_var (cvec_p, vec, { {10, 1}, {20, 2}, {30, 3} });
- c_var (cdeq_i, deq, {10, 20, 30});
- c_var (clist_i, lst, {10, 20, 30});
- c_var (cqueue_i, que, {10, 20, 30});
- c_var (csmap_i, map, { {20, 2}, {10, 1}, {30, 3} });
-
- // add one more element to each container
- cset_i_insert(&set, 40);
- cvec_p_push_back(&vec, (struct Point) {40, 4});
- cdeq_i_push_front(&deq, 5);
- clist_i_push_front(&lst, 5);
- cqueue_i_push(&que, 40);
- csmap_i_emplace(&map, 40, 4);
-
- // find an element in each container
- cset_i_iter_t i1 = cset_i_find(&set, 20);
- cvec_p_iter_t i2 = cvec_p_find(&vec, (struct Point) {20, 2});
- cdeq_i_iter_t i3 = cdeq_i_find(&deq, 20);
- clist_i_iter_t i4 = clist_i_find(&lst, 20);
- csmap_i_iter_t i5 = csmap_i_find(&map, 20);
- printf("\nFound: %d, (%g, %g), %d, %d, [%d: %d]\n", *i1.ref, i2.ref->x, i2.ref->y,
- *i3.ref, *i4.ref,
- i5.ref->first, i5.ref->second);
- // erase the elements found
- cset_i_erase_at(&set, i1);
- cvec_p_erase_at(&vec, i2);
- cdeq_i_erase_at(&deq, i3);
- clist_i_erase_at(&lst, i4);
- csmap_i_erase_at(&map, i5);
-
- printf("After erasing elements found:");
- printf("\n set:"); c_foreach (i, cset_i, set) printf(" %d", *i.ref);
- printf("\n vec:"); c_foreach (i, cvec_p, vec) printf(" (%g, %g)", i.ref->x, i.ref->y);
- printf("\n deq:"); c_foreach (i, cdeq_i, deq) printf(" %d", *i.ref);
- printf("\n lst:"); c_foreach (i, clist_i, lst) printf(" %d", *i.ref);
- printf("\n que:"); c_foreach (i, cqueue_i, que) printf(" %d", *i.ref);
- printf("\n map:"); c_foreach (i, csmap_i, map) printf(" [%d: %d]", i.ref->first, i.ref->second);
-
- // cleanup
- cset_i_del(&set);
- cvec_p_del(&vec);
- cdeq_i_del(&deq);
- clist_i_del(&lst);
- cqueue_i_del(&que);
- csmap_i_del(&map);
+ // define six containers with automatic call of init and del (destruction after scope exit)
+ c_forauto (cset_i32, set)
+ c_forauto (cvec_pnt, vec)
+ c_forauto (cdeq_i32, deq)
+ c_forauto (clist_i32, lst)
+ c_forauto (cqueue_i32, que)
+ c_forauto (csmap_i32, map)
+ {
+ // add some elements to each container
+ c_emplace(cset_i32, set, {10, 20, 30});
+ c_emplace(cvec_pnt, vec, { {10, 1}, {20, 2}, {30, 3} });
+ c_emplace(cdeq_i32, deq, {10, 20, 30});
+ c_emplace(clist_i32, lst, {10, 20, 30});
+ c_emplace(cqueue_i32, que, {10, 20, 30});
+ c_emplace(csmap_i32, map, { {20, 2}, {10, 1}, {30, 3} });
+
+ // add one more element to each container
+ cset_i32_insert(&set, 40);
+ cvec_pnt_push_back(&vec, (struct Point) {40, 4});
+ cdeq_i32_push_front(&deq, 5);
+ clist_i32_push_front(&lst, 5);
+ cqueue_i32_push(&que, 40);
+ csmap_i32_emplace(&map, 40, 4);
+
+ // find an element in each container
+ cset_i32_iter_t i1 = cset_i32_find(&set, 20);
+ cvec_pnt_iter_t i2 = cvec_pnt_find(&vec, (struct Point) {20, 2});
+ cdeq_i32_iter_t i3 = cdeq_i32_find(&deq, 20);
+ clist_i32_iter_t i4 = clist_i32_find(&lst, 20);
+ csmap_i32_iter_t i5 = csmap_i32_find(&map, 20);
+ printf("\nFound: %d, (%g, %g), %d, %d, [%d: %d]\n", *i1.ref, i2.ref->x, i2.ref->y,
+ *i3.ref, *i4.ref,
+ i5.ref->first, i5.ref->second);
+ // erase the elements found
+ cset_i32_erase_at(&set, i1);
+ cvec_pnt_erase_at(&vec, i2);
+ cdeq_i32_erase_at(&deq, i3);
+ clist_i32_erase_at(&lst, i4);
+ csmap_i32_erase_at(&map, i5);
+
+ printf("After erasing elements found:");
+ printf("\n set:"); c_foreach (i, cset_i32, set) printf(" %d", *i.ref);
+ printf("\n vec:"); c_foreach (i, cvec_pnt, vec) printf(" (%g, %g)", i.ref->x, i.ref->y);
+ printf("\n deq:"); c_foreach (i, cdeq_i32, deq) printf(" %d", *i.ref);
+ printf("\n lst:"); c_foreach (i, clist_i32, lst) printf(" %d", *i.ref);
+ printf("\n que:"); c_foreach (i, cqueue_i32, que) printf(" %d", *i.ref);
+ printf("\n map:"); c_foreach (i, csmap_i32, map) printf(" [%d: %d]", i.ref->first,
+ i.ref->second);
+ }
}
```
Output
diff --git a/benchmarks/others/clist_v1.h b/benchmarks/others/clist_v1.h
index 00e04e9b..0dd8feef 100644
--- a/benchmarks/others/clist_v1.h
+++ b/benchmarks/others/clist_v1.h
@@ -69,7 +69,7 @@
_c_using_clist(clist_##X, Value, valueCompare, valueDel, valueFromRaw, valueToRaw, RawValue)
#define using_clist_str() \
- _c_using_clist(clist_str, cstr, c_rawstr_compare, cstr_del, cstr_from, cstr_toraw, const char*)
+ _c_using_clist(clist_str, cstr, c_rawstr_compare, cstr_del, cstr_from, cstr_str, const char*)
#define _c_using_clist_types(CX, Value) \
diff --git a/benchmarks/others/csmap_v1.h b/benchmarks/others/csmap_v1.h
index 14114150..69f3f06d 100644
--- a/benchmarks/others/csmap_v1.h
+++ b/benchmarks/others/csmap_v1.h
@@ -87,8 +87,8 @@ int main(void) {
#define using_csmap_str() \
_c_using_aatree(csmap_str, csmap_, cstr, cstr, c_rawstr_compare, \
- cstr_del, cstr_from, cstr_toraw, const char*, \
- cstr_del, cstr_from, cstr_toraw, const char*)
+ cstr_del, cstr_from, cstr_str, const char*, \
+ cstr_del, cstr_from, cstr_str, const char*)
#define using_csmap_strkey(...) c_MACRO_OVERLOAD(using_csmap_strkey, __VA_ARGS__)
@@ -105,7 +105,7 @@ int main(void) {
#define _c_using_aatree_strkey(X, C, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) \
_c_using_aatree(C##X, C, cstr, Mapped, c_rawstr_compare, \
mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \
- cstr_del, cstr_from, cstr_toraw, const char*)
+ cstr_del, cstr_from, cstr_str, const char*)
#define using_csmap_strval(...) c_MACRO_OVERLOAD(using_csmap_strval, __VA_ARGS__)
@@ -121,7 +121,7 @@ int main(void) {
#define using_csmap_strval_7(X, Key, keyCompareRaw, keyDel, keyFromRaw, keyToRaw, RawKey) \
_c_using_aatree(csmap_##X, csmap_, Key, cstr, keyCompareRaw, \
- cstr_del, cstr_from, cstr_toraw, const char*, \
+ cstr_del, cstr_from, cstr_str, const char*, \
keyDel, keyFromRaw, keyToRaw, RawKey)
#define SET_ONLY_csmap_(...)
diff --git a/benchmarks/others/pcg_extras.hpp b/benchmarks/others/pcg_extras.hpp
deleted file mode 100644
index 8445ca24..00000000
--- a/benchmarks/others/pcg_extras.hpp
+++ /dev/null
@@ -1,664 +0,0 @@
-/*
- * PCG Random Number Generation for C++
- *
- * Copyright 2014-2017 Melissa O'Neill <[email protected]>,
- * and the PCG Project contributors.
- *
- * SPDX-License-Identifier: (Apache-2.0 OR MIT)
- *
- * Licensed under the Apache License, Version 2.0 (provided in
- * LICENSE-APACHE.txt and at http://www.apache.org/licenses/LICENSE-2.0)
- * or under the MIT license (provided in LICENSE-MIT.txt and at
- * http://opensource.org/licenses/MIT), at your option. This file may not
- * be copied, modified, or distributed except according to those terms.
- *
- * Distributed on an "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, either
- * express or implied. See your chosen license for details.
- *
- * For additional information about the PCG random number generation scheme,
- * visit http://www.pcg-random.org/.
- */
-
-/*
- * This file provides support code that is useful for random-number generation
- * but not specific to the PCG generation scheme, including:
- * - 128-bit int support for platforms where it isn't available natively
- * - bit twiddling operations
- * - I/O of 128-bit and 8-bit integers
- * - Handling the evilness of SeedSeq
- * - Support for efficiently producing random numbers less than a given
- * bound
- */
-
-#ifndef PCG_EXTRAS_HPP_INCLUDED
-#define PCG_EXTRAS_HPP_INCLUDED 1
-
-#include <cinttypes>
-#include <cstddef>
-#include <cstdlib>
-#include <cstring>
-#include <cassert>
-#include <limits>
-#include <iostream>
-#include <type_traits>
-#include <utility>
-#include <locale>
-#include <iterator>
-
-#ifdef __GNUC__
- #include <cxxabi.h>
-#endif
-
-/*
- * Abstractions for compiler-specific directives
- */
-
-#ifdef __GNUC__
- #define PCG_NOINLINE __attribute__((noinline))
-#else
- #define PCG_NOINLINE
-#endif
-
-/*
- * Some members of the PCG library use 128-bit math. When compiling on 64-bit
- * platforms, both GCC and Clang provide 128-bit integer types that are ideal
- * for the job.
- *
- * On 32-bit platforms (or with other compilers), we fall back to a C++
- * class that provides 128-bit unsigned integers instead. It may seem
- * like we're reinventing the wheel here, because libraries already exist
- * that support large integers, but most existing libraries provide a very
- * generic multiprecision code, but here we're operating at a fixed size.
- * Also, most other libraries are fairly heavyweight. So we use a direct
- * implementation. Sadly, it's much slower than hand-coded assembly or
- * direct CPU support.
- *
- */
-#if __SIZEOF_INT128__
- namespace pcg_extras {
- typedef __uint128_t pcg128_t;
- }
- #define PCG_128BIT_CONSTANT(high,low) \
- ((pcg_extras::pcg128_t(high) << 64) + low)
-#else
- #include "pcg_uint128.hpp"
- namespace pcg_extras {
- typedef pcg_extras::uint_x4<uint32_t,uint64_t> pcg128_t;
- }
- #define PCG_128BIT_CONSTANT(high,low) \
- pcg_extras::pcg128_t(high,low)
- #define PCG_EMULATED_128BIT_MATH 1
-#endif
-
-
-namespace pcg_extras {
-
-/*
- * We often need to represent a "number of bits". When used normally, these
- * numbers are never greater than 128, so an unsigned char is plenty.
- * If you're using a nonstandard generator of a larger size, you can set
- * PCG_BITCOUNT_T to have it define it as a larger size. (Some compilers
- * might produce faster code if you set it to an unsigned int.)
- */
-
-#ifndef PCG_BITCOUNT_T
- typedef uint8_t bitcount_t;
-#else
- typedef PCG_BITCOUNT_T bitcount_t;
-#endif
-
-/*
- * C++ requires us to be able to serialize RNG state by printing or reading
- * it from a stream. Because we use 128-bit ints, we also need to be able
- * ot print them, so here is code to do so.
- *
- * This code provides enough functionality to print 128-bit ints in decimal
- * and zero-padded in hex. It's not a full-featured implementation.
- */
-
-template <typename CharT, typename Traits>
-std::basic_ostream<CharT,Traits>&
-operator<<(std::basic_ostream<CharT,Traits>& out, pcg128_t value)
-{
- auto desired_base = out.flags() & out.basefield;
- bool want_hex = desired_base == out.hex;
-
- if (want_hex) {
- uint64_t highpart = uint64_t(value >> 64);
- uint64_t lowpart = uint64_t(value);
- auto desired_width = out.width();
- if (desired_width > 16) {
- out.width(desired_width - 16);
- }
- if (highpart != 0 || desired_width > 16)
- out << highpart;
- CharT oldfill = '\0';
- if (highpart != 0) {
- out.width(16);
- oldfill = out.fill('0');
- }
- auto oldflags = out.setf(decltype(desired_base){}, out.showbase);
- out << lowpart;
- out.setf(oldflags);
- if (highpart != 0) {
- out.fill(oldfill);
- }
- return out;
- }
- constexpr size_t MAX_CHARS_128BIT = 40;
-
- char buffer[MAX_CHARS_128BIT];
- char* pos = buffer+sizeof(buffer);
- *(--pos) = '\0';
- constexpr auto BASE = pcg128_t(10ULL);
- do {
- auto div = value / BASE;
- auto mod = uint32_t(value - (div * BASE));
- *(--pos) = '0' + char(mod);
- value = div;
- } while(value != pcg128_t(0ULL));
- return out << pos;
-}
-
-template <typename CharT, typename Traits>
-std::basic_istream<CharT,Traits>&
-operator>>(std::basic_istream<CharT,Traits>& in, pcg128_t& value)
-{
- typename std::basic_istream<CharT,Traits>::sentry s(in);
-
- if (!s)
- return in;
-
- constexpr auto BASE = pcg128_t(10ULL);
- pcg128_t current(0ULL);
- bool did_nothing = true;
- bool overflow = false;
- for(;;) {
- CharT wide_ch = in.get();
- if (!in.good())
- break;
- auto ch = in.narrow(wide_ch, '\0');
- if (ch < '0' || ch > '9') {
- in.unget();
- break;
- }
- did_nothing = false;
- pcg128_t digit(uint32_t(ch - '0'));
- pcg128_t timesbase = current*BASE;
- overflow = overflow || timesbase < current;
- current = timesbase + digit;
- overflow = overflow || current < digit;
- }
-
- if (did_nothing || overflow) {
- in.setstate(std::ios::failbit);
- if (overflow)
- current = ~pcg128_t(0ULL);
- }
-
- value = current;
-
- return in;
-}
-
-/*
- * Likewise, if people use tiny rngs, we'll be serializing uint8_t.
- * If we just used the provided IO operators, they'd read/write chars,
- * not ints, so we need to define our own. We *can* redefine this operator
- * here because we're in our own namespace.
- */
-
-template <typename CharT, typename Traits>
-std::basic_ostream<CharT,Traits>&
-operator<<(std::basic_ostream<CharT,Traits>&out, uint8_t value)
-{
- return out << uint32_t(value);
-}
-
-template <typename CharT, typename Traits>
-std::basic_istream<CharT,Traits>&
-operator>>(std::basic_istream<CharT,Traits>& in, uint8_t& target)
-{
- uint32_t value = 0xdecea5edU;
- in >> value;
- if (!in && value == 0xdecea5edU)
- return in;
- if (value > uint8_t(~0)) {
- in.setstate(std::ios::failbit);
- value = ~0U;
- }
- target = uint8_t(value);
- return in;
-}
-
-/* Unfortunately, the above functions don't get found in preference to the
- * built in ones, so we create some more specific overloads that will.
- * Ugh.
- */
-
-inline std::ostream& operator<<(std::ostream& out, uint8_t value)
-{
- return pcg_extras::operator<< <char>(out, value);
-}
-
-inline std::istream& operator>>(std::istream& in, uint8_t& value)
-{
- return pcg_extras::operator>> <char>(in, value);
-}
-
-
-
-/*
- * Useful bitwise operations.
- */
-
-/*
- * XorShifts are invertable, but they are someting of a pain to invert.
- * This function backs them out. It's used by the whacky "inside out"
- * generator defined later.
- */
-
-template <typename itype>
-inline itype unxorshift(itype x, bitcount_t bits, bitcount_t shift)
-{
- if (2*shift >= bits) {
- return x ^ (x >> shift);
- }
- itype lowmask1 = (itype(1U) << (bits - shift*2)) - 1;
- itype highmask1 = ~lowmask1;
- itype top1 = x;
- itype bottom1 = x & lowmask1;
- top1 ^= top1 >> shift;
- top1 &= highmask1;
- x = top1 | bottom1;
- itype lowmask2 = (itype(1U) << (bits - shift)) - 1;
- itype bottom2 = x & lowmask2;
- bottom2 = unxorshift(bottom2, bits - shift, shift);
- bottom2 &= lowmask1;
- return top1 | bottom2;
-}
-
-/*
- * Rotate left and right.
- *
- * In ideal world, compilers would spot idiomatic rotate code and convert it
- * to a rotate instruction. Of course, opinions vary on what the correct
- * idiom is and how to spot it. For clang, sometimes it generates better
- * (but still crappy) code if you define PCG_USE_ZEROCHECK_ROTATE_IDIOM.
- */
-
-template <typename itype>
-inline itype rotl(itype value, bitcount_t rot)
-{
- constexpr bitcount_t bits = sizeof(itype) * 8;
- constexpr bitcount_t mask = bits - 1;
-#if PCG_USE_ZEROCHECK_ROTATE_IDIOM
- return rot ? (value << rot) | (value >> (bits - rot)) : value;
-#else
- return (value << rot) | (value >> ((- rot) & mask));
-#endif
-}
-
-template <typename itype>
-inline itype rotr(itype value, bitcount_t rot)
-{
- constexpr bitcount_t bits = sizeof(itype) * 8;
- constexpr bitcount_t mask = bits - 1;
-#if PCG_USE_ZEROCHECK_ROTATE_IDIOM
- return rot ? (value >> rot) | (value << (bits - rot)) : value;
-#else
- return (value >> rot) | (value << ((- rot) & mask));
-#endif
-}
-
-/* Unfortunately, both Clang and GCC sometimes perform poorly when it comes
- * to properly recognizing idiomatic rotate code, so for we also provide
- * assembler directives (enabled with PCG_USE_INLINE_ASM). Boo, hiss.
- * (I hope that these compilers get better so that this code can die.)
- *
- * These overloads will be preferred over the general template code above.
- */
-#if PCG_USE_INLINE_ASM && __GNUC__ && (__x86_64__ || __i386__)
-
-inline uint8_t rotr(uint8_t value, bitcount_t rot)
-{
- asm ("rorb %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
- return value;
-}
-
-inline uint16_t rotr(uint16_t value, bitcount_t rot)
-{
- asm ("rorw %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
- return value;
-}
-
-inline uint32_t rotr(uint32_t value, bitcount_t rot)
-{
- asm ("rorl %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
- return value;
-}
-
-#if __x86_64__
-inline uint64_t rotr(uint64_t value, bitcount_t rot)
-{
- asm ("rorq %%cl, %0" : "=r" (value) : "0" (value), "c" (rot));
- return value;
-}
-#endif // __x86_64__
-
-#elif defined(_MSC_VER)
- // Use MSVC++ bit rotation intrinsics
-
-#pragma intrinsic(_rotr, _rotr64, _rotr8, _rotr16)
-
-inline uint8_t rotr(uint8_t value, bitcount_t rot)
-{
- return _rotr8(value, rot);
-}
-
-inline uint16_t rotr(uint16_t value, bitcount_t rot)
-{
- return _rotr16(value, rot);
-}
-
-inline uint32_t rotr(uint32_t value, bitcount_t rot)
-{
- return _rotr(value, rot);
-}
-
-inline uint64_t rotr(uint64_t value, bitcount_t rot)
-{
- return _rotr64(value, rot);
-}
-
-#endif // PCG_USE_INLINE_ASM
-
-
-/*
- * The C++ SeedSeq concept (modelled by seed_seq) can fill an array of
- * 32-bit integers with seed data, but sometimes we want to produce
- * larger or smaller integers.
- *
- * The following code handles this annoyance.
- *
- * uneven_copy will copy an array of 32-bit ints to an array of larger or
- * smaller ints (actually, the code is general it only needing forward
- * iterators). The copy is identical to the one that would be performed if
- * we just did memcpy on a standard little-endian machine, but works
- * regardless of the endian of the machine (or the weirdness of the ints
- * involved).
- *
- * generate_to initializes an array of integers using a SeedSeq
- * object. It is given the size as a static constant at compile time and
- * tries to avoid memory allocation. If we're filling in 32-bit constants
- * we just do it directly. If we need a separate buffer and it's small,
- * we allocate it on the stack. Otherwise, we fall back to heap allocation.
- * Ugh.
- *
- * generate_one produces a single value of some integral type using a
- * SeedSeq object.
- */
-
- /* uneven_copy helper, case where destination ints are less than 32 bit. */
-
-template<class SrcIter, class DestIter>
-SrcIter uneven_copy_impl(
- SrcIter src_first, DestIter dest_first, DestIter dest_last,
- std::true_type)
-{
- typedef typename std::iterator_traits<SrcIter>::value_type src_t;
- typedef typename std::iterator_traits<DestIter>::value_type dest_t;
-
- constexpr bitcount_t SRC_SIZE = sizeof(src_t);
- constexpr bitcount_t DEST_SIZE = sizeof(dest_t);
- constexpr bitcount_t DEST_BITS = DEST_SIZE * 8;
- constexpr bitcount_t SCALE = SRC_SIZE / DEST_SIZE;
-
- size_t count = 0;
- src_t value = 0;
-
- while (dest_first != dest_last) {
- if ((count++ % SCALE) == 0)
- value = *src_first++; // Get more bits
- else
- value >>= DEST_BITS; // Move down bits
-
- *dest_first++ = dest_t(value); // Truncates, ignores high bits.
- }
- return src_first;
-}
-
- /* uneven_copy helper, case where destination ints are more than 32 bit. */
-
-template<class SrcIter, class DestIter>
-SrcIter uneven_copy_impl(
- SrcIter src_first, DestIter dest_first, DestIter dest_last,
- std::false_type)
-{
- typedef typename std::iterator_traits<SrcIter>::value_type src_t;
- typedef typename std::iterator_traits<DestIter>::value_type dest_t;
-
- constexpr auto SRC_SIZE = sizeof(src_t);
- constexpr auto SRC_BITS = SRC_SIZE * 8;
- constexpr auto DEST_SIZE = sizeof(dest_t);
- constexpr auto SCALE = (DEST_SIZE+SRC_SIZE-1) / SRC_SIZE;
-
- while (dest_first != dest_last) {
- dest_t value(0UL);
- unsigned int shift = 0;
-
- for (size_t i = 0; i < SCALE; ++i) {
- value |= dest_t(*src_first++) << shift;
- shift += SRC_BITS;
- }
-
- *dest_first++ = value;
- }
- return src_first;
-}
-
-/* uneven_copy, call the right code for larger vs. smaller */
-
-template<class SrcIter, class DestIter>
-inline SrcIter uneven_copy(SrcIter src_first,
- DestIter dest_first, DestIter dest_last)
-{
- typedef typename std::iterator_traits<SrcIter>::value_type src_t;
- typedef typename std::iterator_traits<DestIter>::value_type dest_t;
-
- constexpr bool DEST_IS_SMALLER = sizeof(dest_t) < sizeof(src_t);
-
- return uneven_copy_impl(src_first, dest_first, dest_last,
- std::integral_constant<bool, DEST_IS_SMALLER>{});
-}
-
-/* generate_to, fill in a fixed-size array of integral type using a SeedSeq
- * (actually works for any random-access iterator)
- */
-
-template <size_t size, typename SeedSeq, typename DestIter>
-inline void generate_to_impl(SeedSeq&& generator, DestIter dest,
- std::true_type)
-{
- generator.generate(dest, dest+size);
-}
-
-template <size_t size, typename SeedSeq, typename DestIter>
-void generate_to_impl(SeedSeq&& generator, DestIter dest,
- std::false_type)
-{
- typedef typename std::iterator_traits<DestIter>::value_type dest_t;
- constexpr auto DEST_SIZE = sizeof(dest_t);
- constexpr auto GEN_SIZE = sizeof(uint32_t);
-
- constexpr bool GEN_IS_SMALLER = GEN_SIZE < DEST_SIZE;
- constexpr size_t FROM_ELEMS =
- GEN_IS_SMALLER
- ? size * ((DEST_SIZE+GEN_SIZE-1) / GEN_SIZE)
- : (size + (GEN_SIZE / DEST_SIZE) - 1)
- / ((GEN_SIZE / DEST_SIZE) + GEN_IS_SMALLER);
- // this odd code ^^^^^^^^^^^^^^^^^ is work-around for
- // a bug: http://llvm.org/bugs/show_bug.cgi?id=21287
-
- if (FROM_ELEMS <= 1024) {
- uint32_t buffer[FROM_ELEMS];
- generator.generate(buffer, buffer+FROM_ELEMS);
- uneven_copy(buffer, dest, dest+size);
- } else {
- uint32_t* buffer = static_cast<uint32_t*>(malloc(GEN_SIZE * FROM_ELEMS));
- generator.generate(buffer, buffer+FROM_ELEMS);
- uneven_copy(buffer, dest, dest+size);
- free(static_cast<void*>(buffer));
- }
-}
-
-template <size_t size, typename SeedSeq, typename DestIter>
-inline void generate_to(SeedSeq&& generator, DestIter dest)
-{
- typedef typename std::iterator_traits<DestIter>::value_type dest_t;
- constexpr bool IS_32BIT = sizeof(dest_t) == sizeof(uint32_t);
-
- generate_to_impl<size>(std::forward<SeedSeq>(generator), dest,
- std::integral_constant<bool, IS_32BIT>{});
-}
-
-/* generate_one, produce a value of integral type using a SeedSeq
- * (optionally, we can have it produce more than one and pick which one
- * we want)
- */
-
-template <typename UInt, size_t i = 0UL, size_t N = i+1UL, typename SeedSeq>
-inline UInt generate_one(SeedSeq&& generator)
-{
- UInt result[N];
- generate_to<N>(std::forward<SeedSeq>(generator), result);
- return result[i];
-}
-
-template <typename RngType>
-auto bounded_rand(RngType& rng, typename RngType::result_type upper_bound)
- -> typename RngType::result_type
-{
- typedef typename RngType::result_type rtype;
- rtype threshold = (RngType::max() - RngType::min() + rtype(1) - upper_bound)
- % upper_bound;
- for (;;) {
- rtype r = rng() - RngType::min();
- if (r >= threshold)
- return r % upper_bound;
- }
-}
-
-template <typename Iter, typename RandType>
-void shuffle(Iter from, Iter to, RandType&& rng)
-{
- typedef typename std::iterator_traits<Iter>::difference_type delta_t;
- typedef typename std::remove_reference<RandType>::type::result_type result_t;
- auto count = to - from;
- while (count > 1) {
- delta_t chosen = delta_t(bounded_rand(rng, result_t(count)));
- --count;
- --to;
- using std::swap;
- swap(*(from + chosen), *to);
- }
-}
-
-/*
- * Although std::seed_seq is useful, it isn't everything. Often we want to
- * initialize a random-number generator some other way, such as from a random
- * device.
- *
- * Technically, it does not meet the requirements of a SeedSequence because
- * it lacks some of the rarely-used member functions (some of which would
- * be impossible to provide). However the C++ standard is quite specific
- * that actual engines only called the generate method, so it ought not to be
- * a problem in practice.
- */
-
-template <typename RngType>
-class seed_seq_from {
-private:
- RngType rng_;
-
- typedef uint_least32_t result_type;
-
-public:
- template<typename... Args>
- seed_seq_from(Args&&... args) :
- rng_(std::forward<Args>(args)...)
- {
- // Nothing (else) to do...
- }
-
- template<typename Iter>
- void generate(Iter start, Iter finish)
- {
- for (auto i = start; i != finish; ++i)
- *i = result_type(rng_());
- }
-
- constexpr size_t size() const
- {
- return (sizeof(typename RngType::result_type) > sizeof(result_type)
- && RngType::max() > ~size_t(0UL))
- ? ~size_t(0UL)
- : size_t(RngType::max());
- }
-};
-
-/*
- * Sometimes you might want a distinct seed based on when the program
- * was compiled. That way, a particular instance of the program will
- * behave the same way, but when recompiled it'll produce a different
- * value.
- */
-
-template <typename IntType>
-struct static_arbitrary_seed {
-private:
- static constexpr IntType fnv(IntType hash, const char* pos) {
- return *pos == '\0'
- ? hash
- : fnv((hash * IntType(16777619U)) ^ *pos, (pos+1));
- }
-
-public:
- static constexpr IntType value = fnv(IntType(2166136261U ^ sizeof(IntType)),
- __DATE__ __TIME__ __FILE__);
-};
-
-// Sometimes, when debugging or testing, it's handy to be able print the name
-// of a (in human-readable form). This code allows the idiom:
-//
-// cout << printable_typename<my_foo_type_t>()
-//
-// to print out my_foo_type_t (or its concrete type if it is a synonym)
-
-#if __cpp_rtti || __GXX_RTTI
-
-template <typename T>
-struct printable_typename {};
-
-template <typename T>
-std::ostream& operator<<(std::ostream& out, printable_typename<T>) {
- const char *implementation_typename = typeid(T).name();
-#ifdef __GNUC__
- int status;
- char* pretty_name =
- abi::__cxa_demangle(implementation_typename, nullptr, nullptr, &status);
- if (status == 0)
- out << pretty_name;
- free(static_cast<void*>(pretty_name));
- if (status == 0)
- return out;
-#endif
- out << implementation_typename;
- return out;
-}
-
-#endif // __cpp_rtti || __GXX_RTTI
-
-} // namespace pcg_extras
-
-#endif // PCG_EXTRAS_HPP_INCLUDED
diff --git a/benchmarks/others/pcg_random.hpp b/benchmarks/others/pcg_random.hpp
deleted file mode 100644
index 4ab37cc4..00000000
--- a/benchmarks/others/pcg_random.hpp
+++ /dev/null
@@ -1,1947 +0,0 @@
-/*
- * PCG Random Number Generation for C++
- *
- * Copyright 2014-2019 Melissa O'Neill <[email protected]>,
- * and the PCG Project contributors.
- *
- * SPDX-License-Identifier: (Apache-2.0 OR MIT)
- *
- * Licensed under the Apache License, Version 2.0 (provided in
- * LICENSE-APACHE.txt and at http://www.apache.org/licenses/LICENSE-2.0)
- * or under the MIT license (provided in LICENSE-MIT.txt and at
- * http://opensource.org/licenses/MIT), at your option. This file may not
- * be copied, modified, or distributed except according to those terms.
- *
- * Distributed on an "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, either
- * express or implied. See your chosen license for details.
- *
- * For additional information about the PCG random number generation scheme,
- * visit http://www.pcg-random.org/.
- */
-
-/*
- * This code provides the reference implementation of the PCG family of
- * random number generators. The code is complex because it implements
- *
- * - several members of the PCG family, specifically members corresponding
- * to the output functions:
- * - XSH RR (good for 64-bit state, 32-bit output)
- * - XSH RS (good for 64-bit state, 32-bit output)
- * - XSL RR (good for 128-bit state, 64-bit output)
- * - RXS M XS (statistically most powerful generator)
- * - XSL RR RR (good for 128-bit state, 128-bit output)
- * - and RXS, RXS M, XSH, XSL (mostly for testing)
- * - at potentially *arbitrary* bit sizes
- * - with four different techniques for random streams (MCG, one-stream
- * LCG, settable-stream LCG, unique-stream LCG)
- * - and the extended generation schemes allowing arbitrary periods
- * - with all features of C++11 random number generation (and more),
- * some of which are somewhat painful, including
- * - initializing with a SeedSequence which writes 32-bit values
- * to memory, even though the state of the generator may not
- * use 32-bit values (it might use smaller or larger integers)
- * - I/O for RNGs and a prescribed format, which needs to handle
- * the issue that 8-bit and 128-bit integers don't have working
- * I/O routines (e.g., normally 8-bit = char, not integer)
- * - equality and inequality for RNGs
- * - and a number of convenience typedefs to mask all the complexity
- *
- * The code employes a fairly heavy level of abstraction, and has to deal
- * with various C++ minutia. If you're looking to learn about how the PCG
- * scheme works, you're probably best of starting with one of the other
- * codebases (see www.pcg-random.org). But if you're curious about the
- * constants for the various output functions used in those other, simpler,
- * codebases, this code shows how they are calculated.
- *
- * On the positive side, at least there are convenience typedefs so that you
- * can say
- *
- * pcg32 myRNG;
- *
- * rather than:
- *
- * pcg_detail::engine<
- * uint32_t, // Output Type
- * uint64_t, // State Type
- * pcg_detail::xsh_rr_mixin<uint32_t, uint64_t>, true, // Output Func
- * pcg_detail::specific_stream<uint64_t>, // Stream Kind
- * pcg_detail::default_multiplier<uint64_t> // LCG Mult
- * > myRNG;
- *
- */
-
-#ifndef PCG_RAND_HPP_INCLUDED
-#define PCG_RAND_HPP_INCLUDED 1
-
-#include <algorithm>
-#include <cinttypes>
-#include <cstddef>
-#include <cstdlib>
-#include <cstring>
-#include <cassert>
-#include <limits>
-#include <iostream>
-#include <iterator>
-#include <type_traits>
-#include <utility>
-#include <locale>
-#include <new>
-#include <stdexcept>
-
-#ifdef _MSC_VER
- #pragma warning(disable:4146)
-#endif
-
-#ifdef _MSC_VER
- #define PCG_ALWAYS_INLINE __forceinline
-#elif __GNUC__
- #define PCG_ALWAYS_INLINE __attribute__((always_inline))
-#else
- #define PCG_ALWAYS_INLINE inline
-#endif
-
-/*
- * The pcg_extras namespace contains some support code that is likley to
- * be useful for a variety of RNGs, including:
- * - 128-bit int support for platforms where it isn't available natively
- * - bit twiddling operations
- * - I/O of 128-bit and 8-bit integers
- * - Handling the evilness of SeedSeq
- * - Support for efficiently producing random numbers less than a given
- * bound
- */
-
-#include "pcg_extras.hpp"
-
-namespace pcg_detail {
-
-using namespace pcg_extras;
-
-/*
- * The LCG generators need some constants to function. This code lets you
- * look up the constant by *type*. For example
- *
- * default_multiplier<uint32_t>::multiplier()
- *
- * gives you the default multipler for 32-bit integers. We use the name
- * of the constant and not a generic word like value to allow these classes
- * to be used as mixins.
- */
-
-template <typename T>
-struct default_multiplier {
- // Not defined for an arbitrary type
-};
-
-template <typename T>
-struct default_increment {
- // Not defined for an arbitrary type
-};
-
-#define PCG_DEFINE_CONSTANT(type, what, kind, constant) \
- template <> \
- struct what ## _ ## kind<type> { \
- static constexpr type kind() { \
- return constant; \
- } \
- };
-
-PCG_DEFINE_CONSTANT(uint8_t, default, multiplier, 141U)
-PCG_DEFINE_CONSTANT(uint8_t, default, increment, 77U)
-
-PCG_DEFINE_CONSTANT(uint16_t, default, multiplier, 12829U)
-PCG_DEFINE_CONSTANT(uint16_t, default, increment, 47989U)
-
-PCG_DEFINE_CONSTANT(uint32_t, default, multiplier, 747796405U)
-PCG_DEFINE_CONSTANT(uint32_t, default, increment, 2891336453U)
-
-PCG_DEFINE_CONSTANT(uint64_t, default, multiplier, 6364136223846793005ULL)
-PCG_DEFINE_CONSTANT(uint64_t, default, increment, 1442695040888963407ULL)
-
-PCG_DEFINE_CONSTANT(pcg128_t, default, multiplier,
- PCG_128BIT_CONSTANT(2549297995355413924ULL,4865540595714422341ULL))
-PCG_DEFINE_CONSTANT(pcg128_t, default, increment,
- PCG_128BIT_CONSTANT(6364136223846793005ULL,1442695040888963407ULL))
-
-/* Alternative (cheaper) multipliers for 128-bit */
-
-template <typename T>
-struct cheap_multiplier : public default_multiplier<T> {
- // For most types just use the default.
-};
-
-template <>
-struct cheap_multiplier<pcg128_t> {
- static constexpr uint64_t multiplier() {
- return 0xda942042e4dd58b5ULL;
- }
-};
-
-
-/*
- * Each PCG generator is available in four variants, based on how it applies
- * the additive constant for its underlying LCG; the variations are:
- *
- * single stream - all instances use the same fixed constant, thus
- * the RNG always somewhere in same sequence
- * mcg - adds zero, resulting in a single stream and reduced
- * period
- * specific stream - the constant can be changed at any time, selecting
- * a different random sequence
- * unique stream - the constant is based on the memory address of the
- * object, thus every RNG has its own unique sequence
- *
- * This variation is provided though mixin classes which define a function
- * value called increment() that returns the nesessary additive constant.
- */
-
-
-
-/*
- * unique stream
- */
-
-
-template <typename itype>
-class unique_stream {
-protected:
- static constexpr bool is_mcg = false;
-
- // Is never called, but is provided for symmetry with specific_stream
- void set_stream(...)
- {
- abort();
- }
-
-public:
- typedef itype state_type;
-
- constexpr itype increment() const {
- return itype(reinterpret_cast<uintptr_t>(this) | 1);
- }
-
- constexpr itype stream() const
- {
- return increment() >> 1;
- }
-
- static constexpr bool can_specify_stream = false;
-
- static constexpr size_t streams_pow2()
- {
- return (sizeof(itype) < sizeof(size_t) ? sizeof(itype)
- : sizeof(size_t))*8 - 1u;
- }
-
-protected:
- constexpr unique_stream() = default;
-};
-
-
-/*
- * no stream (mcg)
- */
-
-template <typename itype>
-class no_stream {
-protected:
- static constexpr bool is_mcg = true;
-
- // Is never called, but is provided for symmetry with specific_stream
- void set_stream(...)
- {
- abort();
- }
-
-public:
- typedef itype state_type;
-
- static constexpr itype increment() {
- return 0;
- }
-
- static constexpr bool can_specify_stream = false;
-
- static constexpr size_t streams_pow2()
- {
- return 0u;
- }
-
-protected:
- constexpr no_stream() = default;
-};
-
-
-/*
- * single stream/sequence (oneseq)
- */
-
-template <typename itype>
-class oneseq_stream : public default_increment<itype> {
-protected:
- static constexpr bool is_mcg = false;
-
- // Is never called, but is provided for symmetry with specific_stream
- void set_stream(...)
- {
- abort();
- }
-
-public:
- typedef itype state_type;
-
- static constexpr itype stream()
- {
- return default_increment<itype>::increment() >> 1;
- }
-
- static constexpr bool can_specify_stream = false;
-
- static constexpr size_t streams_pow2()
- {
- return 0u;
- }
-
-protected:
- constexpr oneseq_stream() = default;
-};
-
-
-/*
- * specific stream
- */
-
-template <typename itype>
-class specific_stream {
-protected:
- static constexpr bool is_mcg = false;
-
- itype inc_ = default_increment<itype>::increment();
-
-public:
- typedef itype state_type;
- typedef itype stream_state;
-
- constexpr itype increment() const {
- return inc_;
- }
-
- itype stream()
- {
- return inc_ >> 1;
- }
-
- void set_stream(itype specific_seq)
- {
- inc_ = (specific_seq << 1) | 1;
- }
-
- static constexpr bool can_specify_stream = true;
-
- static constexpr size_t streams_pow2()
- {
- return (sizeof(itype)*8) - 1u;
- }
-
-protected:
- specific_stream() = default;
-
- specific_stream(itype specific_seq)
- : inc_(itype(specific_seq << 1) | itype(1U))
- {
- // Nothing (else) to do.
- }
-};
-
-
-/*
- * This is where it all comes together. This function joins together three
- * mixin classes which define
- * - the LCG additive constant (the stream)
- * - the LCG multiplier
- * - the output function
- * in addition, we specify the type of the LCG state, and the result type,
- * and whether to use the pre-advance version of the state for the output
- * (increasing instruction-level parallelism) or the post-advance version
- * (reducing register pressure).
- *
- * Given the high level of parameterization, the code has to use some
- * template-metaprogramming tricks to handle some of the suble variations
- * involved.
- */
-
-template <typename xtype, typename itype,
- typename output_mixin,
- bool output_previous = true,
- typename stream_mixin = oneseq_stream<itype>,
- typename multiplier_mixin = default_multiplier<itype> >
-class engine : protected output_mixin,
- public stream_mixin,
- protected multiplier_mixin {
-protected:
- itype state_;
-
- struct can_specify_stream_tag {};
- struct no_specifiable_stream_tag {};
-
- using stream_mixin::increment;
- using multiplier_mixin::multiplier;
-
-public:
- typedef xtype result_type;
- typedef itype state_type;
-
- static constexpr size_t period_pow2()
- {
- return sizeof(state_type)*8 - 2*stream_mixin::is_mcg;
- }
-
- // It would be nice to use std::numeric_limits for these, but
- // we can't be sure that it'd be defined for the 128-bit types.
-
- static constexpr result_type min()
- {
- return result_type(0UL);
- }
-
- static constexpr result_type max()
- {
- return result_type(~result_type(0UL));
- }
-
-protected:
- itype bump(itype state)
- {
- return state * multiplier() + increment();
- }
-
- itype base_generate()
- {
- return state_ = bump(state_);
- }
-
- itype base_generate0()
- {
- itype old_state = state_;
- state_ = bump(state_);
- return old_state;
- }
-
-public:
- result_type operator()()
- {
- if (output_previous)
- return this->output(base_generate0());
- else
- return this->output(base_generate());
- }
-
- result_type operator()(result_type upper_bound)
- {
- return bounded_rand(*this, upper_bound);
- }
-
-protected:
- static itype advance(itype state, itype delta,
- itype cur_mult, itype cur_plus);
-
- static itype distance(itype cur_state, itype newstate, itype cur_mult,
- itype cur_plus, itype mask = ~itype(0U));
-
- itype distance(itype newstate, itype mask = itype(~itype(0U))) const
- {
- return distance(state_, newstate, multiplier(), increment(), mask);
- }
-
-public:
- void advance(itype delta)
- {
- state_ = advance(state_, delta, this->multiplier(), this->increment());
- }
-
- void backstep(itype delta)
- {
- advance(-delta);
- }
-
- void discard(itype delta)
- {
- advance(delta);
- }
-
- bool wrapped()
- {
- if (stream_mixin::is_mcg) {
- // For MCGs, the low order two bits never change. In this
- // implementation, we keep them fixed at 3 to make this test
- // easier.
- return state_ == 3;
- } else {
- return state_ == 0;
- }
- }
-
- engine(itype state = itype(0xcafef00dd15ea5e5ULL))
- : state_(this->is_mcg ? state|state_type(3U)
- : bump(state + this->increment()))
- {
- // Nothing else to do.
- }
-
- // This function may or may not exist. It thus has to be a template
- // to use SFINAE; users don't have to worry about its template-ness.
-
- template <typename sm = stream_mixin>
- engine(itype state, typename sm::stream_state stream_seed)
- : stream_mixin(stream_seed),
- state_(this->is_mcg ? state|state_type(3U)
- : bump(state + this->increment()))
- {
- // Nothing else to do.
- }
-
- template<typename SeedSeq>
- engine(SeedSeq&& seedSeq, typename std::enable_if<
- !stream_mixin::can_specify_stream
- && !std::is_convertible<SeedSeq, itype>::value
- && !std::is_convertible<SeedSeq, engine>::value,
- no_specifiable_stream_tag>::type = {})
- : engine(generate_one<itype>(std::forward<SeedSeq>(seedSeq)))
- {
- // Nothing else to do.
- }
-
- template<typename SeedSeq>
- engine(SeedSeq&& seedSeq, typename std::enable_if<
- stream_mixin::can_specify_stream
- && !std::is_convertible<SeedSeq, itype>::value
- && !std::is_convertible<SeedSeq, engine>::value,
- can_specify_stream_tag>::type = {})
- : engine(generate_one<itype,1,2>(seedSeq),
- generate_one<itype,0,2>(seedSeq))
- {
- // Nothing else to do.
- }
-
-
- template<typename... Args>
- void seed(Args&&... args)
- {
- new (this) engine(std::forward<Args>(args)...);
- }
-
- template <typename xtype1, typename itype1,
- typename output_mixin1, bool output_previous1,
- typename stream_mixin_lhs, typename multiplier_mixin_lhs,
- typename stream_mixin_rhs, typename multiplier_mixin_rhs>
- friend bool operator==(const engine<xtype1,itype1,
- output_mixin1,output_previous1,
- stream_mixin_lhs, multiplier_mixin_lhs>&,
- const engine<xtype1,itype1,
- output_mixin1,output_previous1,
- stream_mixin_rhs, multiplier_mixin_rhs>&);
-
- template <typename xtype1, typename itype1,
- typename output_mixin1, bool output_previous1,
- typename stream_mixin_lhs, typename multiplier_mixin_lhs,
- typename stream_mixin_rhs, typename multiplier_mixin_rhs>
- friend itype1 operator-(const engine<xtype1,itype1,
- output_mixin1,output_previous1,
- stream_mixin_lhs, multiplier_mixin_lhs>&,
- const engine<xtype1,itype1,
- output_mixin1,output_previous1,
- stream_mixin_rhs, multiplier_mixin_rhs>&);
-
- template <typename CharT, typename Traits,
- typename xtype1, typename itype1,
- typename output_mixin1, bool output_previous1,
- typename stream_mixin1, typename multiplier_mixin1>
- friend std::basic_ostream<CharT,Traits>&
- operator<<(std::basic_ostream<CharT,Traits>& out,
- const engine<xtype1,itype1,
- output_mixin1,output_previous1,
- stream_mixin1, multiplier_mixin1>&);
-
- template <typename CharT, typename Traits,
- typename xtype1, typename itype1,
- typename output_mixin1, bool output_previous1,
- typename stream_mixin1, typename multiplier_mixin1>
- friend std::basic_istream<CharT,Traits>&
- operator>>(std::basic_istream<CharT,Traits>& in,
- engine<xtype1, itype1,
- output_mixin1, output_previous1,
- stream_mixin1, multiplier_mixin1>& rng);
-};
-
-template <typename CharT, typename Traits,
- typename xtype, typename itype,
- typename output_mixin, bool output_previous,
- typename stream_mixin, typename multiplier_mixin>
-std::basic_ostream<CharT,Traits>&
-operator<<(std::basic_ostream<CharT,Traits>& out,
- const engine<xtype,itype,
- output_mixin,output_previous,
- stream_mixin, multiplier_mixin>& rng)
-{
- using pcg_extras::operator<<;
-
- auto orig_flags = out.flags(std::ios_base::dec | std::ios_base::left);
- auto space = out.widen(' ');
- auto orig_fill = out.fill();
-
- out << rng.multiplier() << space
- << rng.increment() << space
- << rng.state_;
-
- out.flags(orig_flags);
- out.fill(orig_fill);
- return out;
-}
-
-
-template <typename CharT, typename Traits,
- typename xtype, typename itype,
- typename output_mixin, bool output_previous,
- typename stream_mixin, typename multiplier_mixin>
-std::basic_istream<CharT,Traits>&
-operator>>(std::basic_istream<CharT,Traits>& in,
- engine<xtype,itype,
- output_mixin,output_previous,
- stream_mixin, multiplier_mixin>& rng)
-{
- using pcg_extras::operator>>;
-
- auto orig_flags = in.flags(std::ios_base::dec | std::ios_base::skipws);
-
- itype multiplier, increment, state;
- in >> multiplier >> increment >> state;
-
- if (!in.fail()) {
- bool good = true;
- if (multiplier != rng.multiplier()) {
- good = false;
- } else if (rng.can_specify_stream) {
- rng.set_stream(increment >> 1);
- } else if (increment != rng.increment()) {
- good = false;
- }
- if (good) {
- rng.state_ = state;
- } else {
- in.clear(std::ios::failbit);
- }
- }
-
- in.flags(orig_flags);
- return in;
-}
-
-
-template <typename xtype, typename itype,
- typename output_mixin, bool output_previous,
- typename stream_mixin, typename multiplier_mixin>
-itype engine<xtype,itype,output_mixin,output_previous,stream_mixin,
- multiplier_mixin>::advance(
- itype state, itype delta, itype cur_mult, itype cur_plus)
-{
- // The method used here is based on Brown, "Random Number Generation
- // with Arbitrary Stride,", Transactions of the American Nuclear
- // Society (Nov. 1994). The algorithm is very similar to fast
- // exponentiation.
- //
- // Even though delta is an unsigned integer, we can pass a
- // signed integer to go backwards, it just goes "the long way round".
-
- constexpr itype ZERO = 0u; // itype may be a non-trivial types, so
- constexpr itype ONE = 1u; // we define some ugly constants.
- itype acc_mult = 1;
- itype acc_plus = 0;
- while (delta > ZERO) {
- if (delta & ONE) {
- acc_mult *= cur_mult;
- acc_plus = acc_plus*cur_mult + cur_plus;
- }
- cur_plus = (cur_mult+ONE)*cur_plus;
- cur_mult *= cur_mult;
- delta >>= 1;
- }
- return acc_mult * state + acc_plus;
-}
-
-template <typename xtype, typename itype,
- typename output_mixin, bool output_previous,
- typename stream_mixin, typename multiplier_mixin>
-itype engine<xtype,itype,output_mixin,output_previous,stream_mixin,
- multiplier_mixin>::distance(
- itype cur_state, itype newstate, itype cur_mult, itype cur_plus, itype mask)
-{
- constexpr itype ONE = 1u; // itype could be weird, so use constant
- bool is_mcg = cur_plus == itype(0);
- itype the_bit = is_mcg ? itype(4u) : itype(1u);
- itype distance = 0u;
- while ((cur_state & mask) != (newstate & mask)) {
- if ((cur_state & the_bit) != (newstate & the_bit)) {
- cur_state = cur_state * cur_mult + cur_plus;
- distance |= the_bit;
- }
- assert((cur_state & the_bit) == (newstate & the_bit));
- the_bit <<= 1;
- cur_plus = (cur_mult+ONE)*cur_plus;
- cur_mult *= cur_mult;
- }
- return is_mcg ? distance >> 2 : distance;
-}
-
-template <typename xtype, typename itype,
- typename output_mixin, bool output_previous,
- typename stream_mixin_lhs, typename multiplier_mixin_lhs,
- typename stream_mixin_rhs, typename multiplier_mixin_rhs>
-itype operator-(const engine<xtype,itype,
- output_mixin,output_previous,
- stream_mixin_lhs, multiplier_mixin_lhs>& lhs,
- const engine<xtype,itype,
- output_mixin,output_previous,
- stream_mixin_rhs, multiplier_mixin_rhs>& rhs)
-{
- static_assert(
- std::is_same<stream_mixin_lhs, stream_mixin_rhs>::value &&
- std::is_same<multiplier_mixin_lhs, multiplier_mixin_rhs>::value,
- "Incomparable generators");
- if (lhs.increment() == rhs.increment()) {
- return rhs.distance(lhs.state_);
- } else {
- constexpr itype ONE = 1u;
- itype lhs_diff = lhs.increment() + (lhs.multiplier()-ONE) * lhs.state_;
- itype rhs_diff = rhs.increment() + (rhs.multiplier()-ONE) * rhs.state_;
- if ((lhs_diff & itype(3u)) != (rhs_diff & itype(3u))) {
- rhs_diff = -rhs_diff;
- }
- return rhs.distance(rhs_diff, lhs_diff, rhs.multiplier(), itype(0u));
- }
-}
-
-
-template <typename xtype, typename itype,
- typename output_mixin, bool output_previous,
- typename stream_mixin_lhs, typename multiplier_mixin_lhs,
- typename stream_mixin_rhs, typename multiplier_mixin_rhs>
-bool operator==(const engine<xtype,itype,
- output_mixin,output_previous,
- stream_mixin_lhs, multiplier_mixin_lhs>& lhs,
- const engine<xtype,itype,
- output_mixin,output_previous,
- stream_mixin_rhs, multiplier_mixin_rhs>& rhs)
-{
- return (lhs.multiplier() == rhs.multiplier())
- && (lhs.increment() == rhs.increment())
- && (lhs.state_ == rhs.state_);
-}
-
-template <typename xtype, typename itype,
- typename output_mixin, bool output_previous,
- typename stream_mixin_lhs, typename multiplier_mixin_lhs,
- typename stream_mixin_rhs, typename multiplier_mixin_rhs>
-inline bool operator!=(const engine<xtype,itype,
- output_mixin,output_previous,
- stream_mixin_lhs, multiplier_mixin_lhs>& lhs,
- const engine<xtype,itype,
- output_mixin,output_previous,
- stream_mixin_rhs, multiplier_mixin_rhs>& rhs)
-{
- return !operator==(lhs,rhs);
-}
-
-
-template <typename xtype, typename itype,
- template<typename XT,typename IT> class output_mixin,
- bool output_previous = (sizeof(itype) <= 8),
- template<typename IT> class multiplier_mixin = default_multiplier>
-using oneseq_base = engine<xtype, itype,
- output_mixin<xtype, itype>, output_previous,
- oneseq_stream<itype>,
- multiplier_mixin<itype> >;
-
-template <typename xtype, typename itype,
- template<typename XT,typename IT> class output_mixin,
- bool output_previous = (sizeof(itype) <= 8),
- template<typename IT> class multiplier_mixin = default_multiplier>
-using unique_base = engine<xtype, itype,
- output_mixin<xtype, itype>, output_previous,
- unique_stream<itype>,
- multiplier_mixin<itype> >;
-
-template <typename xtype, typename itype,
- template<typename XT,typename IT> class output_mixin,
- bool output_previous = (sizeof(itype) <= 8),
- template<typename IT> class multiplier_mixin = default_multiplier>
-using setseq_base = engine<xtype, itype,
- output_mixin<xtype, itype>, output_previous,
- specific_stream<itype>,
- multiplier_mixin<itype> >;
-
-template <typename xtype, typename itype,
- template<typename XT,typename IT> class output_mixin,
- bool output_previous = (sizeof(itype) <= 8),
- template<typename IT> class multiplier_mixin = default_multiplier>
-using mcg_base = engine<xtype, itype,
- output_mixin<xtype, itype>, output_previous,
- no_stream<itype>,
- multiplier_mixin<itype> >;
-
-/*
- * OUTPUT FUNCTIONS.
- *
- * These are the core of the PCG generation scheme. They specify how to
- * turn the base LCG's internal state into the output value of the final
- * generator.
- *
- * They're implemented as mixin classes.
- *
- * All of the classes have code that is written to allow it to be applied
- * at *arbitrary* bit sizes, although in practice they'll only be used at
- * standard sizes supported by C++.
- */
-
-/*
- * XSH RS -- high xorshift, followed by a random shift
- *
- * Fast. A good performer.
- */
-
-template <typename xtype, typename itype>
-struct xsh_rs_mixin {
- static xtype output(itype internal)
- {
- constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
- constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
- constexpr bitcount_t sparebits = bits - xtypebits;
- constexpr bitcount_t opbits =
- sparebits-5 >= 64 ? 5
- : sparebits-4 >= 32 ? 4
- : sparebits-3 >= 16 ? 3
- : sparebits-2 >= 4 ? 2
- : sparebits-1 >= 1 ? 1
- : 0;
- constexpr bitcount_t mask = (1 << opbits) - 1;
- constexpr bitcount_t maxrandshift = mask;
- constexpr bitcount_t topspare = opbits;
- constexpr bitcount_t bottomspare = sparebits - topspare;
- constexpr bitcount_t xshift = topspare + (xtypebits+maxrandshift)/2;
- bitcount_t rshift =
- opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
- internal ^= internal >> xshift;
- xtype result = xtype(internal >> (bottomspare - maxrandshift + rshift));
- return result;
- }
-};
-
-/*
- * XSH RR -- high xorshift, followed by a random rotate
- *
- * Fast. A good performer. Slightly better statistically than XSH RS.
- */
-
-template <typename xtype, typename itype>
-struct xsh_rr_mixin {
- static xtype output(itype internal)
- {
- constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
- constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype)*8);
- constexpr bitcount_t sparebits = bits - xtypebits;
- constexpr bitcount_t wantedopbits =
- xtypebits >= 128 ? 7
- : xtypebits >= 64 ? 6
- : xtypebits >= 32 ? 5
- : xtypebits >= 16 ? 4
- : 3;
- constexpr bitcount_t opbits =
- sparebits >= wantedopbits ? wantedopbits
- : sparebits;
- constexpr bitcount_t amplifier = wantedopbits - opbits;
- constexpr bitcount_t mask = (1 << opbits) - 1;
- constexpr bitcount_t topspare = opbits;
- constexpr bitcount_t bottomspare = sparebits - topspare;
- constexpr bitcount_t xshift = (topspare + xtypebits)/2;
- bitcount_t rot = opbits ? bitcount_t(internal >> (bits - opbits)) & mask
- : 0;
- bitcount_t amprot = (rot << amplifier) & mask;
- internal ^= internal >> xshift;
- xtype result = xtype(internal >> bottomspare);
- result = rotr(result, amprot);
- return result;
- }
-};
-
-/*
- * RXS -- random xorshift
- */
-
-template <typename xtype, typename itype>
-struct rxs_mixin {
-static xtype output_rxs(itype internal)
- {
- constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
- constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype)*8);
- constexpr bitcount_t shift = bits - xtypebits;
- constexpr bitcount_t extrashift = (xtypebits - shift)/2;
- bitcount_t rshift = shift > 64+8 ? (internal >> (bits - 6)) & 63
- : shift > 32+4 ? (internal >> (bits - 5)) & 31
- : shift > 16+2 ? (internal >> (bits - 4)) & 15
- : shift > 8+1 ? (internal >> (bits - 3)) & 7
- : shift > 4+1 ? (internal >> (bits - 2)) & 3
- : shift > 2+1 ? (internal >> (bits - 1)) & 1
- : 0;
- internal ^= internal >> (shift + extrashift - rshift);
- xtype result = internal >> rshift;
- return result;
- }
-};
-
-/*
- * RXS M XS -- random xorshift, mcg multiply, fixed xorshift
- *
- * The most statistically powerful generator, but all those steps
- * make it slower than some of the others. We give it the rottenest jobs.
- *
- * Because it's usually used in contexts where the state type and the
- * result type are the same, it is a permutation and is thus invertable.
- * We thus provide a function to invert it. This function is used to
- * for the "inside out" generator used by the extended generator.
- */
-
-/* Defined type-based concepts for the multiplication step. They're actually
- * all derived by truncating the 128-bit, which was computed to be a good
- * "universal" constant.
- */
-
-template <typename T>
-struct mcg_multiplier {
- // Not defined for an arbitrary type
-};
-
-template <typename T>
-struct mcg_unmultiplier {
- // Not defined for an arbitrary type
-};
-
-PCG_DEFINE_CONSTANT(uint8_t, mcg, multiplier, 217U)
-PCG_DEFINE_CONSTANT(uint8_t, mcg, unmultiplier, 105U)
-
-PCG_DEFINE_CONSTANT(uint16_t, mcg, multiplier, 62169U)
-PCG_DEFINE_CONSTANT(uint16_t, mcg, unmultiplier, 28009U)
-
-PCG_DEFINE_CONSTANT(uint32_t, mcg, multiplier, 277803737U)
-PCG_DEFINE_CONSTANT(uint32_t, mcg, unmultiplier, 2897767785U)
-
-PCG_DEFINE_CONSTANT(uint64_t, mcg, multiplier, 12605985483714917081ULL)
-PCG_DEFINE_CONSTANT(uint64_t, mcg, unmultiplier, 15009553638781119849ULL)
-
-PCG_DEFINE_CONSTANT(pcg128_t, mcg, multiplier,
- PCG_128BIT_CONSTANT(17766728186571221404ULL, 12605985483714917081ULL))
-PCG_DEFINE_CONSTANT(pcg128_t, mcg, unmultiplier,
- PCG_128BIT_CONSTANT(14422606686972528997ULL, 15009553638781119849ULL))
-
-
-template <typename xtype, typename itype>
-struct rxs_m_xs_mixin {
- static xtype output(itype internal)
- {
- constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
- constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
- constexpr bitcount_t opbits = xtypebits >= 128 ? 6
- : xtypebits >= 64 ? 5
- : xtypebits >= 32 ? 4
- : xtypebits >= 16 ? 3
- : 2;
- constexpr bitcount_t shift = bits - xtypebits;
- constexpr bitcount_t mask = (1 << opbits) - 1;
- bitcount_t rshift =
- opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
- internal ^= internal >> (opbits + rshift);
- internal *= mcg_multiplier<itype>::multiplier();
- xtype result = internal >> shift;
- result ^= result >> ((2U*xtypebits+2U)/3U);
- return result;
- }
-
- static itype unoutput(itype internal)
- {
- constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
- constexpr bitcount_t opbits = bits >= 128 ? 6
- : bits >= 64 ? 5
- : bits >= 32 ? 4
- : bits >= 16 ? 3
- : 2;
- constexpr bitcount_t mask = (1 << opbits) - 1;
-
- internal = unxorshift(internal, bits, (2U*bits+2U)/3U);
-
- internal *= mcg_unmultiplier<itype>::unmultiplier();
-
- bitcount_t rshift = opbits ? (internal >> (bits - opbits)) & mask : 0;
- internal = unxorshift(internal, bits, opbits + rshift);
-
- return internal;
- }
-};
-
-
-/*
- * RXS M -- random xorshift, mcg multiply
- */
-
-template <typename xtype, typename itype>
-struct rxs_m_mixin {
- static xtype output(itype internal)
- {
- constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
- constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
- constexpr bitcount_t opbits = xtypebits >= 128 ? 6
- : xtypebits >= 64 ? 5
- : xtypebits >= 32 ? 4
- : xtypebits >= 16 ? 3
- : 2;
- constexpr bitcount_t shift = bits - xtypebits;
- constexpr bitcount_t mask = (1 << opbits) - 1;
- bitcount_t rshift = opbits ? (internal >> (bits - opbits)) & mask : 0;
- internal ^= internal >> (opbits + rshift);
- internal *= mcg_multiplier<itype>::multiplier();
- xtype result = internal >> shift;
- return result;
- }
-};
-
-
-/*
- * DXSM -- double xorshift multiply
- *
- * This is a new, more powerful output permutation (added in 2019). It's
- * a more comprehensive scrambling than RXS M, but runs faster on 128-bit
- * types. Although primarily intended for use at large sizes, also works
- * at smaller sizes as well.
- *
- * This permutation is similar to xorshift multiply hash functions, except
- * that one of the multipliers is the LCG multiplier (to avoid needing to
- * have a second constant) and the other is based on the low-order bits.
- * This latter aspect means that the scrambling applied to the high bits
- * depends on the low bits, and makes it (to my eye) impractical to back
- * out the permutation without having the low-order bits.
- */
-
-template <typename xtype, typename itype>
-struct dxsm_mixin {
- inline xtype output(itype internal)
- {
- constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
- constexpr bitcount_t itypebits = bitcount_t(sizeof(itype) * 8);
- static_assert(xtypebits <= itypebits/2,
- "Output type must be half the size of the state type.");
-
- xtype hi = xtype(internal >> (itypebits - xtypebits));
- xtype lo = xtype(internal);
-
- lo |= 1;
- hi ^= hi >> (xtypebits/2);
- hi *= xtype(cheap_multiplier<itype>::multiplier());
- hi ^= hi >> (3*(xtypebits/4));
- hi *= lo;
- return hi;
- }
-};
-
-
-/*
- * XSL RR -- fixed xorshift (to low bits), random rotate
- *
- * Useful for 128-bit types that are split across two CPU registers.
- */
-
-template <typename xtype, typename itype>
-struct xsl_rr_mixin {
- static xtype output(itype internal)
- {
- constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
- constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
- constexpr bitcount_t sparebits = bits - xtypebits;
- constexpr bitcount_t wantedopbits = xtypebits >= 128 ? 7
- : xtypebits >= 64 ? 6
- : xtypebits >= 32 ? 5
- : xtypebits >= 16 ? 4
- : 3;
- constexpr bitcount_t opbits = sparebits >= wantedopbits ? wantedopbits
- : sparebits;
- constexpr bitcount_t amplifier = wantedopbits - opbits;
- constexpr bitcount_t mask = (1 << opbits) - 1;
- constexpr bitcount_t topspare = sparebits;
- constexpr bitcount_t bottomspare = sparebits - topspare;
- constexpr bitcount_t xshift = (topspare + xtypebits) / 2;
-
- bitcount_t rot =
- opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
- bitcount_t amprot = (rot << amplifier) & mask;
- internal ^= internal >> xshift;
- xtype result = xtype(internal >> bottomspare);
- result = rotr(result, amprot);
- return result;
- }
-};
-
-
-/*
- * XSL RR RR -- fixed xorshift (to low bits), random rotate (both parts)
- *
- * Useful for 128-bit types that are split across two CPU registers.
- * If you really want an invertable 128-bit RNG, I guess this is the one.
- */
-
-template <typename T> struct halfsize_trait {};
-template <> struct halfsize_trait<pcg128_t> { typedef uint64_t type; };
-template <> struct halfsize_trait<uint64_t> { typedef uint32_t type; };
-template <> struct halfsize_trait<uint32_t> { typedef uint16_t type; };
-template <> struct halfsize_trait<uint16_t> { typedef uint8_t type; };
-
-template <typename xtype, typename itype>
-struct xsl_rr_rr_mixin {
- typedef typename halfsize_trait<itype>::type htype;
-
- static itype output(itype internal)
- {
- constexpr bitcount_t htypebits = bitcount_t(sizeof(htype) * 8);
- constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
- constexpr bitcount_t sparebits = bits - htypebits;
- constexpr bitcount_t wantedopbits = htypebits >= 128 ? 7
- : htypebits >= 64 ? 6
- : htypebits >= 32 ? 5
- : htypebits >= 16 ? 4
- : 3;
- constexpr bitcount_t opbits = sparebits >= wantedopbits ? wantedopbits
- : sparebits;
- constexpr bitcount_t amplifier = wantedopbits - opbits;
- constexpr bitcount_t mask = (1 << opbits) - 1;
- constexpr bitcount_t topspare = sparebits;
- constexpr bitcount_t xshift = (topspare + htypebits) / 2;
-
- bitcount_t rot =
- opbits ? bitcount_t(internal >> (bits - opbits)) & mask : 0;
- bitcount_t amprot = (rot << amplifier) & mask;
- internal ^= internal >> xshift;
- htype lowbits = htype(internal);
- lowbits = rotr(lowbits, amprot);
- htype highbits = htype(internal >> topspare);
- bitcount_t rot2 = lowbits & mask;
- bitcount_t amprot2 = (rot2 << amplifier) & mask;
- highbits = rotr(highbits, amprot2);
- return (itype(highbits) << topspare) ^ itype(lowbits);
- }
-};
-
-
-/*
- * XSH -- fixed xorshift (to high bits)
- *
- * You shouldn't use this at 64-bits or less.
- */
-
-template <typename xtype, typename itype>
-struct xsh_mixin {
- static xtype output(itype internal)
- {
- constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
- constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
- constexpr bitcount_t sparebits = bits - xtypebits;
- constexpr bitcount_t topspare = 0;
- constexpr bitcount_t bottomspare = sparebits - topspare;
- constexpr bitcount_t xshift = (topspare + xtypebits) / 2;
-
- internal ^= internal >> xshift;
- xtype result = internal >> bottomspare;
- return result;
- }
-};
-
-/*
- * XSL -- fixed xorshift (to low bits)
- *
- * You shouldn't use this at 64-bits or less.
- */
-
-template <typename xtype, typename itype>
-struct xsl_mixin {
- inline xtype output(itype internal)
- {
- constexpr bitcount_t xtypebits = bitcount_t(sizeof(xtype) * 8);
- constexpr bitcount_t bits = bitcount_t(sizeof(itype) * 8);
- constexpr bitcount_t sparebits = bits - xtypebits;
- constexpr bitcount_t topspare = sparebits;
- constexpr bitcount_t bottomspare = sparebits - topspare;
- constexpr bitcount_t xshift = (topspare + xtypebits) / 2;
-
- internal ^= internal >> xshift;
- xtype result = internal >> bottomspare;
- return result;
- }
-};
-
-
-/* ---- End of Output Functions ---- */
-
-
-template <typename baseclass>
-struct inside_out : private baseclass {
- inside_out() = delete;
-
- typedef typename baseclass::result_type result_type;
- typedef typename baseclass::state_type state_type;
- static_assert(sizeof(result_type) == sizeof(state_type),
- "Require a RNG whose output function is a permutation");
-
- static bool external_step(result_type& randval, size_t i)
- {
- state_type state = baseclass::unoutput(randval);
- state = state * baseclass::multiplier() + baseclass::increment()
- + state_type(i*2);
- result_type result = baseclass::output(state);
- randval = result;
- state_type zero =
- baseclass::is_mcg ? state & state_type(3U) : state_type(0U);
- return result == zero;
- }
-
- static bool external_advance(result_type& randval, size_t i,
- result_type delta, bool forwards = true)
- {
- state_type state = baseclass::unoutput(randval);
- state_type mult = baseclass::multiplier();
- state_type inc = baseclass::increment() + state_type(i*2);
- state_type zero =
- baseclass::is_mcg ? state & state_type(3U) : state_type(0U);
- state_type dist_to_zero = baseclass::distance(state, zero, mult, inc);
- bool crosses_zero =
- forwards ? dist_to_zero <= delta
- : (-dist_to_zero) <= delta;
- if (!forwards)
- delta = -delta;
- state = baseclass::advance(state, delta, mult, inc);
- randval = baseclass::output(state);
- return crosses_zero;
- }
-};
-
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2, typename baseclass, typename extvalclass, bool kdd = true>
-class extended : public baseclass {
-public:
- typedef typename baseclass::state_type state_type;
- typedef typename baseclass::result_type result_type;
- typedef inside_out<extvalclass> insideout;
-
-private:
- static constexpr bitcount_t rtypebits = sizeof(result_type)*8;
- static constexpr bitcount_t stypebits = sizeof(state_type)*8;
-
- static constexpr bitcount_t tick_limit_pow2 = 64U;
-
- static constexpr size_t table_size = 1UL << table_pow2;
- static constexpr size_t table_shift = stypebits - table_pow2;
- static constexpr state_type table_mask =
- (state_type(1U) << table_pow2) - state_type(1U);
-
- static constexpr bool may_tick =
- (advance_pow2 < stypebits) && (advance_pow2 < tick_limit_pow2);
- static constexpr size_t tick_shift = stypebits - advance_pow2;
- static constexpr state_type tick_mask =
- may_tick ? state_type(
- (uint64_t(1) << (advance_pow2*may_tick)) - 1)
- // ^-- stupidity to appease GCC warnings
- : ~state_type(0U);
-
- static constexpr bool may_tock = stypebits < tick_limit_pow2;
-
- result_type data_[table_size];
-
- PCG_NOINLINE void advance_table();
-
- PCG_NOINLINE void advance_table(state_type delta, bool isForwards = true);
-
- result_type& get_extended_value()
- {
- state_type state = this->state_;
- if (kdd && baseclass::is_mcg) {
- // The low order bits of an MCG are constant, so drop them.
- state >>= 2;
- }
- size_t index = kdd ? state & table_mask
- : state >> table_shift;
-
- if (may_tick) {
- bool tick = kdd ? (state & tick_mask) == state_type(0u)
- : (state >> tick_shift) == state_type(0u);
- if (tick)
- advance_table();
- }
- if (may_tock) {
- bool tock = state == state_type(0u);
- if (tock)
- advance_table();
- }
- return data_[index];
- }
-
-public:
- static constexpr size_t period_pow2()
- {
- return baseclass::period_pow2() + table_size*extvalclass::period_pow2();
- }
-
- PCG_ALWAYS_INLINE result_type operator()()
- {
- result_type rhs = get_extended_value();
- result_type lhs = this->baseclass::operator()();
- return lhs ^ rhs;
- }
-
- result_type operator()(result_type upper_bound)
- {
- return bounded_rand(*this, upper_bound);
- }
-
- void set(result_type wanted)
- {
- result_type& rhs = get_extended_value();
- result_type lhs = this->baseclass::operator()();
- rhs = lhs ^ wanted;
- }
-
- void advance(state_type distance, bool forwards = true);
-
- void backstep(state_type distance)
- {
- advance(distance, false);
- }
-
- extended(const result_type* data)
- : baseclass()
- {
- datainit(data);
- }
-
- extended(const result_type* data, state_type seed)
- : baseclass(seed)
- {
- datainit(data);
- }
-
- // This function may or may not exist. It thus has to be a template
- // to use SFINAE; users don't have to worry about its template-ness.
-
- template <typename bc = baseclass>
- extended(const result_type* data, state_type seed,
- typename bc::stream_state stream_seed)
- : baseclass(seed, stream_seed)
- {
- datainit(data);
- }
-
- extended()
- : baseclass()
- {
- selfinit();
- }
-
- extended(state_type seed)
- : baseclass(seed)
- {
- selfinit();
- }
-
- // This function may or may not exist. It thus has to be a template
- // to use SFINAE; users don't have to worry about its template-ness.
-
- template <typename bc = baseclass>
- extended(state_type seed, typename bc::stream_state stream_seed)
- : baseclass(seed, stream_seed)
- {
- selfinit();
- }
-
-private:
- void selfinit();
- void datainit(const result_type* data);
-
-public:
-
- template<typename SeedSeq, typename = typename std::enable_if<
- !std::is_convertible<SeedSeq, result_type>::value
- && !std::is_convertible<SeedSeq, extended>::value>::type>
- extended(SeedSeq&& seedSeq)
- : baseclass(seedSeq)
- {
- generate_to<table_size>(seedSeq, data_);
- }
-
- template<typename... Args>
- void seed(Args&&... args)
- {
- new (this) extended(std::forward<Args>(args)...);
- }
-
- template <bitcount_t table_pow2_, bitcount_t advance_pow2_,
- typename baseclass_, typename extvalclass_, bool kdd_>
- friend bool operator==(const extended<table_pow2_, advance_pow2_,
- baseclass_, extvalclass_, kdd_>&,
- const extended<table_pow2_, advance_pow2_,
- baseclass_, extvalclass_, kdd_>&);
-
- template <typename CharT, typename Traits,
- bitcount_t table_pow2_, bitcount_t advance_pow2_,
- typename baseclass_, typename extvalclass_, bool kdd_>
- friend std::basic_ostream<CharT,Traits>&
- operator<<(std::basic_ostream<CharT,Traits>& out,
- const extended<table_pow2_, advance_pow2_,
- baseclass_, extvalclass_, kdd_>&);
-
- template <typename CharT, typename Traits,
- bitcount_t table_pow2_, bitcount_t advance_pow2_,
- typename baseclass_, typename extvalclass_, bool kdd_>
- friend std::basic_istream<CharT,Traits>&
- operator>>(std::basic_istream<CharT,Traits>& in,
- extended<table_pow2_, advance_pow2_,
- baseclass_, extvalclass_, kdd_>&);
-
-};
-
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2,
- typename baseclass, typename extvalclass, bool kdd>
-void extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::datainit(
- const result_type* data)
-{
- for (size_t i = 0; i < table_size; ++i)
- data_[i] = data[i];
-}
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2,
- typename baseclass, typename extvalclass, bool kdd>
-void extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::selfinit()
-{
- // We need to fill the extended table with something, and we have
- // very little provided data, so we use the base generator to
- // produce values. Although not ideal (use a seed sequence, folks!),
- // unexpected correlations are mitigated by
- // - using XOR differences rather than the number directly
- // - the way the table is accessed, its values *won't* be accessed
- // in the same order the were written.
- // - any strange correlations would only be apparent if we
- // were to backstep the generator so that the base generator
- // was generating the same values again
- result_type lhs = baseclass::operator()();
- result_type rhs = baseclass::operator()();
- result_type xdiff = lhs - rhs;
- for (size_t i = 0; i < table_size; ++i) {
- data_[i] = baseclass::operator()() ^ xdiff;
- }
-}
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2,
- typename baseclass, typename extvalclass, bool kdd>
-bool operator==(const extended<table_pow2, advance_pow2,
- baseclass, extvalclass, kdd>& lhs,
- const extended<table_pow2, advance_pow2,
- baseclass, extvalclass, kdd>& rhs)
-{
- auto& base_lhs = static_cast<const baseclass&>(lhs);
- auto& base_rhs = static_cast<const baseclass&>(rhs);
- return base_lhs == base_rhs
- && std::equal(
- std::begin(lhs.data_), std::end(lhs.data_),
- std::begin(rhs.data_)
- );
-}
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2,
- typename baseclass, typename extvalclass, bool kdd>
-inline bool operator!=(const extended<table_pow2, advance_pow2,
- baseclass, extvalclass, kdd>& lhs,
- const extended<table_pow2, advance_pow2,
- baseclass, extvalclass, kdd>& rhs)
-{
- return !operator==(lhs, rhs);
-}
-
-template <typename CharT, typename Traits,
- bitcount_t table_pow2, bitcount_t advance_pow2,
- typename baseclass, typename extvalclass, bool kdd>
-std::basic_ostream<CharT,Traits>&
-operator<<(std::basic_ostream<CharT,Traits>& out,
- const extended<table_pow2, advance_pow2,
- baseclass, extvalclass, kdd>& rng)
-{
- auto orig_flags = out.flags(std::ios_base::dec | std::ios_base::left);
- auto space = out.widen(' ');
- auto orig_fill = out.fill();
-
- out << rng.multiplier() << space
- << rng.increment() << space
- << rng.state_;
-
- for (const auto& datum : rng.data_)
- out << space << datum;
-
- out.flags(orig_flags);
- out.fill(orig_fill);
- return out;
-}
-
-template <typename CharT, typename Traits,
- bitcount_t table_pow2, bitcount_t advance_pow2,
- typename baseclass, typename extvalclass, bool kdd>
-std::basic_istream<CharT,Traits>&
-operator>>(std::basic_istream<CharT,Traits>& in,
- extended<table_pow2, advance_pow2,
- baseclass, extvalclass, kdd>& rng)
-{
- extended<table_pow2, advance_pow2, baseclass, extvalclass> new_rng;
- auto& base_rng = static_cast<baseclass&>(new_rng);
- in >> base_rng;
-
- if (in.fail())
- return in;
-
- auto orig_flags = in.flags(std::ios_base::dec | std::ios_base::skipws);
-
- for (auto& datum : new_rng.data_) {
- in >> datum;
- if (in.fail())
- goto bail;
- }
-
- rng = new_rng;
-
-bail:
- in.flags(orig_flags);
- return in;
-}
-
-
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2,
- typename baseclass, typename extvalclass, bool kdd>
-void
-extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::advance_table()
-{
- bool carry = false;
- for (size_t i = 0; i < table_size; ++i) {
- if (carry) {
- carry = insideout::external_step(data_[i],i+1);
- }
- bool carry2 = insideout::external_step(data_[i],i+1);
- carry = carry || carry2;
- }
-}
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2,
- typename baseclass, typename extvalclass, bool kdd>
-void
-extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::advance_table(
- state_type delta, bool isForwards)
-{
- typedef typename baseclass::state_type base_state_t;
- typedef typename extvalclass::state_type ext_state_t;
- constexpr bitcount_t basebits = sizeof(base_state_t)*8;
- constexpr bitcount_t extbits = sizeof(ext_state_t)*8;
- static_assert(basebits <= extbits || advance_pow2 > 0,
- "Current implementation might overflow its carry");
-
- base_state_t carry = 0;
- for (size_t i = 0; i < table_size; ++i) {
- base_state_t total_delta = carry + delta;
- ext_state_t trunc_delta = ext_state_t(total_delta);
- if (basebits > extbits) {
- carry = total_delta >> extbits;
- } else {
- carry = 0;
- }
- carry +=
- insideout::external_advance(data_[i],i+1, trunc_delta, isForwards);
- }
-}
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2,
- typename baseclass, typename extvalclass, bool kdd>
-void extended<table_pow2,advance_pow2,baseclass,extvalclass,kdd>::advance(
- state_type distance, bool forwards)
-{
- static_assert(kdd,
- "Efficient advance is too hard for non-kdd extension. "
- "For a weak advance, cast to base class");
- state_type zero =
- baseclass::is_mcg ? this->state_ & state_type(3U) : state_type(0U);
- if (may_tick) {
- state_type ticks = distance >> (advance_pow2*may_tick);
- // ^-- stupidity to appease GCC
- // warnings
- state_type adv_mask =
- baseclass::is_mcg ? tick_mask << 2 : tick_mask;
- state_type next_advance_distance = this->distance(zero, adv_mask);
- if (!forwards)
- next_advance_distance = (-next_advance_distance) & tick_mask;
- if (next_advance_distance < (distance & tick_mask)) {
- ++ticks;
- }
- if (ticks)
- advance_table(ticks, forwards);
- }
- if (forwards) {
- if (may_tock && this->distance(zero) <= distance)
- advance_table();
- baseclass::advance(distance);
- } else {
- if (may_tock && -(this->distance(zero)) <= distance)
- advance_table(state_type(1U), false);
- baseclass::advance(-distance);
- }
-}
-
-} // namespace pcg_detail
-
-namespace pcg_engines {
-
-using namespace pcg_detail;
-
-/* Predefined types for XSH RS */
-
-typedef oneseq_base<uint8_t, uint16_t, xsh_rs_mixin> oneseq_xsh_rs_16_8;
-typedef oneseq_base<uint16_t, uint32_t, xsh_rs_mixin> oneseq_xsh_rs_32_16;
-typedef oneseq_base<uint32_t, uint64_t, xsh_rs_mixin> oneseq_xsh_rs_64_32;
-typedef oneseq_base<uint64_t, pcg128_t, xsh_rs_mixin> oneseq_xsh_rs_128_64;
-typedef oneseq_base<uint64_t, pcg128_t, xsh_rs_mixin, true, cheap_multiplier>
- cm_oneseq_xsh_rs_128_64;
-
-typedef unique_base<uint8_t, uint16_t, xsh_rs_mixin> unique_xsh_rs_16_8;
-typedef unique_base<uint16_t, uint32_t, xsh_rs_mixin> unique_xsh_rs_32_16;
-typedef unique_base<uint32_t, uint64_t, xsh_rs_mixin> unique_xsh_rs_64_32;
-typedef unique_base<uint64_t, pcg128_t, xsh_rs_mixin> unique_xsh_rs_128_64;
-typedef unique_base<uint64_t, pcg128_t, xsh_rs_mixin, true, cheap_multiplier>
- cm_unique_xsh_rs_128_64;
-
-typedef setseq_base<uint8_t, uint16_t, xsh_rs_mixin> setseq_xsh_rs_16_8;
-typedef setseq_base<uint16_t, uint32_t, xsh_rs_mixin> setseq_xsh_rs_32_16;
-typedef setseq_base<uint32_t, uint64_t, xsh_rs_mixin> setseq_xsh_rs_64_32;
-typedef setseq_base<uint64_t, pcg128_t, xsh_rs_mixin> setseq_xsh_rs_128_64;
-typedef setseq_base<uint64_t, pcg128_t, xsh_rs_mixin, true, cheap_multiplier>
- cm_setseq_xsh_rs_128_64;
-
-typedef mcg_base<uint8_t, uint16_t, xsh_rs_mixin> mcg_xsh_rs_16_8;
-typedef mcg_base<uint16_t, uint32_t, xsh_rs_mixin> mcg_xsh_rs_32_16;
-typedef mcg_base<uint32_t, uint64_t, xsh_rs_mixin> mcg_xsh_rs_64_32;
-typedef mcg_base<uint64_t, pcg128_t, xsh_rs_mixin> mcg_xsh_rs_128_64;
-typedef mcg_base<uint64_t, pcg128_t, xsh_rs_mixin, true, cheap_multiplier>
- cm_mcg_xsh_rs_128_64;
-
-/* Predefined types for XSH RR */
-
-typedef oneseq_base<uint8_t, uint16_t, xsh_rr_mixin> oneseq_xsh_rr_16_8;
-typedef oneseq_base<uint16_t, uint32_t, xsh_rr_mixin> oneseq_xsh_rr_32_16;
-typedef oneseq_base<uint32_t, uint64_t, xsh_rr_mixin> oneseq_xsh_rr_64_32;
-typedef oneseq_base<uint64_t, pcg128_t, xsh_rr_mixin> oneseq_xsh_rr_128_64;
-typedef oneseq_base<uint64_t, pcg128_t, xsh_rr_mixin, true, cheap_multiplier>
- cm_oneseq_xsh_rr_128_64;
-
-typedef unique_base<uint8_t, uint16_t, xsh_rr_mixin> unique_xsh_rr_16_8;
-typedef unique_base<uint16_t, uint32_t, xsh_rr_mixin> unique_xsh_rr_32_16;
-typedef unique_base<uint32_t, uint64_t, xsh_rr_mixin> unique_xsh_rr_64_32;
-typedef unique_base<uint64_t, pcg128_t, xsh_rr_mixin> unique_xsh_rr_128_64;
-typedef unique_base<uint64_t, pcg128_t, xsh_rr_mixin, true, cheap_multiplier>
- cm_unique_xsh_rr_128_64;
-
-typedef setseq_base<uint8_t, uint16_t, xsh_rr_mixin> setseq_xsh_rr_16_8;
-typedef setseq_base<uint16_t, uint32_t, xsh_rr_mixin> setseq_xsh_rr_32_16;
-typedef setseq_base<uint32_t, uint64_t, xsh_rr_mixin> setseq_xsh_rr_64_32;
-typedef setseq_base<uint64_t, pcg128_t, xsh_rr_mixin> setseq_xsh_rr_128_64;
-typedef setseq_base<uint64_t, pcg128_t, xsh_rr_mixin, true, cheap_multiplier>
- cm_setseq_xsh_rr_128_64;
-
-typedef mcg_base<uint8_t, uint16_t, xsh_rr_mixin> mcg_xsh_rr_16_8;
-typedef mcg_base<uint16_t, uint32_t, xsh_rr_mixin> mcg_xsh_rr_32_16;
-typedef mcg_base<uint32_t, uint64_t, xsh_rr_mixin> mcg_xsh_rr_64_32;
-typedef mcg_base<uint64_t, pcg128_t, xsh_rr_mixin> mcg_xsh_rr_128_64;
-typedef mcg_base<uint64_t, pcg128_t, xsh_rr_mixin, true, cheap_multiplier>
- cm_mcg_xsh_rr_128_64;
-
-
-/* Predefined types for RXS M XS */
-
-typedef oneseq_base<uint8_t, uint8_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_8_8;
-typedef oneseq_base<uint16_t, uint16_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_16_16;
-typedef oneseq_base<uint32_t, uint32_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_32_32;
-typedef oneseq_base<uint64_t, uint64_t, rxs_m_xs_mixin> oneseq_rxs_m_xs_64_64;
-typedef oneseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin>
- oneseq_rxs_m_xs_128_128;
-typedef oneseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin, true, cheap_multiplier>
- cm_oneseq_rxs_m_xs_128_128;
-
-typedef unique_base<uint8_t, uint8_t, rxs_m_xs_mixin> unique_rxs_m_xs_8_8;
-typedef unique_base<uint16_t, uint16_t, rxs_m_xs_mixin> unique_rxs_m_xs_16_16;
-typedef unique_base<uint32_t, uint32_t, rxs_m_xs_mixin> unique_rxs_m_xs_32_32;
-typedef unique_base<uint64_t, uint64_t, rxs_m_xs_mixin> unique_rxs_m_xs_64_64;
-typedef unique_base<pcg128_t, pcg128_t, rxs_m_xs_mixin> unique_rxs_m_xs_128_128;
-typedef unique_base<pcg128_t, pcg128_t, rxs_m_xs_mixin, true, cheap_multiplier>
- cm_unique_rxs_m_xs_128_128;
-
-typedef setseq_base<uint8_t, uint8_t, rxs_m_xs_mixin> setseq_rxs_m_xs_8_8;
-typedef setseq_base<uint16_t, uint16_t, rxs_m_xs_mixin> setseq_rxs_m_xs_16_16;
-typedef setseq_base<uint32_t, uint32_t, rxs_m_xs_mixin> setseq_rxs_m_xs_32_32;
-typedef setseq_base<uint64_t, uint64_t, rxs_m_xs_mixin> setseq_rxs_m_xs_64_64;
-typedef setseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin> setseq_rxs_m_xs_128_128;
-typedef setseq_base<pcg128_t, pcg128_t, rxs_m_xs_mixin, true, cheap_multiplier>
- cm_setseq_rxs_m_xs_128_128;
-
- // MCG versions don't make sense here, so aren't defined.
-
-/* Predefined types for RXS M */
-
-typedef oneseq_base<uint8_t, uint16_t, rxs_m_mixin> oneseq_rxs_m_16_8;
-typedef oneseq_base<uint16_t, uint32_t, rxs_m_mixin> oneseq_rxs_m_32_16;
-typedef oneseq_base<uint32_t, uint64_t, rxs_m_mixin> oneseq_rxs_m_64_32;
-typedef oneseq_base<uint64_t, pcg128_t, rxs_m_mixin> oneseq_rxs_m_128_64;
-typedef oneseq_base<uint64_t, pcg128_t, rxs_m_mixin, true, cheap_multiplier>
- cm_oneseq_rxs_m_128_64;
-
-typedef unique_base<uint8_t, uint16_t, rxs_m_mixin> unique_rxs_m_16_8;
-typedef unique_base<uint16_t, uint32_t, rxs_m_mixin> unique_rxs_m_32_16;
-typedef unique_base<uint32_t, uint64_t, rxs_m_mixin> unique_rxs_m_64_32;
-typedef unique_base<uint64_t, pcg128_t, rxs_m_mixin> unique_rxs_m_128_64;
-typedef unique_base<uint64_t, pcg128_t, rxs_m_mixin, true, cheap_multiplier>
- cm_unique_rxs_m_128_64;
-
-typedef setseq_base<uint8_t, uint16_t, rxs_m_mixin> setseq_rxs_m_16_8;
-typedef setseq_base<uint16_t, uint32_t, rxs_m_mixin> setseq_rxs_m_32_16;
-typedef setseq_base<uint32_t, uint64_t, rxs_m_mixin> setseq_rxs_m_64_32;
-typedef setseq_base<uint64_t, pcg128_t, rxs_m_mixin> setseq_rxs_m_128_64;
-typedef setseq_base<uint64_t, pcg128_t, rxs_m_mixin, true, cheap_multiplier>
- cm_setseq_rxs_m_128_64;
-
-typedef mcg_base<uint8_t, uint16_t, rxs_m_mixin> mcg_rxs_m_16_8;
-typedef mcg_base<uint16_t, uint32_t, rxs_m_mixin> mcg_rxs_m_32_16;
-typedef mcg_base<uint32_t, uint64_t, rxs_m_mixin> mcg_rxs_m_64_32;
-typedef mcg_base<uint64_t, pcg128_t, rxs_m_mixin> mcg_rxs_m_128_64;
-typedef mcg_base<uint64_t, pcg128_t, rxs_m_mixin, true, cheap_multiplier>
- cm_mcg_rxs_m_128_64;
-
-/* Predefined types for DXSM */
-
-typedef oneseq_base<uint8_t, uint16_t, dxsm_mixin> oneseq_dxsm_16_8;
-typedef oneseq_base<uint16_t, uint32_t, dxsm_mixin> oneseq_dxsm_32_16;
-typedef oneseq_base<uint32_t, uint64_t, dxsm_mixin> oneseq_dxsm_64_32;
-typedef oneseq_base<uint64_t, pcg128_t, dxsm_mixin> oneseq_dxsm_128_64;
-typedef oneseq_base<uint64_t, pcg128_t, dxsm_mixin, true, cheap_multiplier>
- cm_oneseq_dxsm_128_64;
-
-typedef unique_base<uint8_t, uint16_t, dxsm_mixin> unique_dxsm_16_8;
-typedef unique_base<uint16_t, uint32_t, dxsm_mixin> unique_dxsm_32_16;
-typedef unique_base<uint32_t, uint64_t, dxsm_mixin> unique_dxsm_64_32;
-typedef unique_base<uint64_t, pcg128_t, dxsm_mixin> unique_dxsm_128_64;
-typedef unique_base<uint64_t, pcg128_t, dxsm_mixin, true, cheap_multiplier>
- cm_unique_dxsm_128_64;
-
-typedef setseq_base<uint8_t, uint16_t, dxsm_mixin> setseq_dxsm_16_8;
-typedef setseq_base<uint16_t, uint32_t, dxsm_mixin> setseq_dxsm_32_16;
-typedef setseq_base<uint32_t, uint64_t, dxsm_mixin> setseq_dxsm_64_32;
-typedef setseq_base<uint64_t, pcg128_t, dxsm_mixin> setseq_dxsm_128_64;
-typedef setseq_base<uint64_t, pcg128_t, dxsm_mixin, true, cheap_multiplier>
- cm_setseq_dxsm_128_64;
-
-typedef mcg_base<uint8_t, uint16_t, dxsm_mixin> mcg_dxsm_16_8;
-typedef mcg_base<uint16_t, uint32_t, dxsm_mixin> mcg_dxsm_32_16;
-typedef mcg_base<uint32_t, uint64_t, dxsm_mixin> mcg_dxsm_64_32;
-typedef mcg_base<uint64_t, pcg128_t, dxsm_mixin> mcg_dxsm_128_64;
-typedef mcg_base<uint64_t, pcg128_t, dxsm_mixin, true, cheap_multiplier>
- cm_mcg_dxsm_128_64;
-
-/* Predefined types for XSL RR (only defined for "large" types) */
-
-typedef oneseq_base<uint32_t, uint64_t, xsl_rr_mixin> oneseq_xsl_rr_64_32;
-typedef oneseq_base<uint64_t, pcg128_t, xsl_rr_mixin> oneseq_xsl_rr_128_64;
-typedef oneseq_base<uint64_t, pcg128_t, xsl_rr_mixin, true, cheap_multiplier>
- cm_oneseq_xsl_rr_128_64;
-
-typedef unique_base<uint32_t, uint64_t, xsl_rr_mixin> unique_xsl_rr_64_32;
-typedef unique_base<uint64_t, pcg128_t, xsl_rr_mixin> unique_xsl_rr_128_64;
-typedef unique_base<uint64_t, pcg128_t, xsl_rr_mixin, true, cheap_multiplier>
- cm_unique_xsl_rr_128_64;
-
-typedef setseq_base<uint32_t, uint64_t, xsl_rr_mixin> setseq_xsl_rr_64_32;
-typedef setseq_base<uint64_t, pcg128_t, xsl_rr_mixin> setseq_xsl_rr_128_64;
-typedef setseq_base<uint64_t, pcg128_t, xsl_rr_mixin, true, cheap_multiplier>
- cm_setseq_xsl_rr_128_64;
-
-typedef mcg_base<uint32_t, uint64_t, xsl_rr_mixin> mcg_xsl_rr_64_32;
-typedef mcg_base<uint64_t, pcg128_t, xsl_rr_mixin> mcg_xsl_rr_128_64;
-typedef mcg_base<uint64_t, pcg128_t, xsl_rr_mixin, true, cheap_multiplier>
- cm_mcg_xsl_rr_128_64;
-
-
-/* Predefined types for XSL RR RR (only defined for "large" types) */
-
-typedef oneseq_base<uint64_t, uint64_t, xsl_rr_rr_mixin>
- oneseq_xsl_rr_rr_64_64;
-typedef oneseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin>
- oneseq_xsl_rr_rr_128_128;
-typedef oneseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin, true, cheap_multiplier>
- cm_oneseq_xsl_rr_rr_128_128;
-
-typedef unique_base<uint64_t, uint64_t, xsl_rr_rr_mixin>
- unique_xsl_rr_rr_64_64;
-typedef unique_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin>
- unique_xsl_rr_rr_128_128;
-typedef unique_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin, true, cheap_multiplier>
- cm_unique_xsl_rr_rr_128_128;
-
-typedef setseq_base<uint64_t, uint64_t, xsl_rr_rr_mixin>
- setseq_xsl_rr_rr_64_64;
-typedef setseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin>
- setseq_xsl_rr_rr_128_128;
-typedef setseq_base<pcg128_t, pcg128_t, xsl_rr_rr_mixin, true, cheap_multiplier>
- cm_setseq_xsl_rr_rr_128_128;
-
- // MCG versions don't make sense here, so aren't defined.
-
-/* Extended generators */
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2,
- typename BaseRNG, bool kdd = true>
-using ext_std8 = extended<table_pow2, advance_pow2, BaseRNG,
- oneseq_rxs_m_xs_8_8, kdd>;
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2,
- typename BaseRNG, bool kdd = true>
-using ext_std16 = extended<table_pow2, advance_pow2, BaseRNG,
- oneseq_rxs_m_xs_16_16, kdd>;
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2,
- typename BaseRNG, bool kdd = true>
-using ext_std32 = extended<table_pow2, advance_pow2, BaseRNG,
- oneseq_rxs_m_xs_32_32, kdd>;
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2,
- typename BaseRNG, bool kdd = true>
-using ext_std64 = extended<table_pow2, advance_pow2, BaseRNG,
- oneseq_rxs_m_xs_64_64, kdd>;
-
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
-using ext_oneseq_rxs_m_xs_32_32 =
- ext_std32<table_pow2, advance_pow2, oneseq_rxs_m_xs_32_32, kdd>;
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
-using ext_mcg_xsh_rs_64_32 =
- ext_std32<table_pow2, advance_pow2, mcg_xsh_rs_64_32, kdd>;
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
-using ext_oneseq_xsh_rs_64_32 =
- ext_std32<table_pow2, advance_pow2, oneseq_xsh_rs_64_32, kdd>;
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
-using ext_setseq_xsh_rr_64_32 =
- ext_std32<table_pow2, advance_pow2, setseq_xsh_rr_64_32, kdd>;
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
-using ext_mcg_xsl_rr_128_64 =
- ext_std64<table_pow2, advance_pow2, mcg_xsl_rr_128_64, kdd>;
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
-using ext_oneseq_xsl_rr_128_64 =
- ext_std64<table_pow2, advance_pow2, oneseq_xsl_rr_128_64, kdd>;
-
-template <bitcount_t table_pow2, bitcount_t advance_pow2, bool kdd = true>
-using ext_setseq_xsl_rr_128_64 =
- ext_std64<table_pow2, advance_pow2, setseq_xsl_rr_128_64, kdd>;
-
-} // namespace pcg_engines
-
-typedef pcg_engines::setseq_xsh_rr_64_32 pcg32;
-typedef pcg_engines::oneseq_xsh_rr_64_32 pcg32_oneseq;
-typedef pcg_engines::unique_xsh_rr_64_32 pcg32_unique;
-typedef pcg_engines::mcg_xsh_rs_64_32 pcg32_fast;
-
-typedef pcg_engines::setseq_xsl_rr_128_64 pcg64;
-typedef pcg_engines::oneseq_xsl_rr_128_64 pcg64_oneseq;
-typedef pcg_engines::unique_xsl_rr_128_64 pcg64_unique;
-typedef pcg_engines::mcg_xsl_rr_128_64 pcg64_fast;
-
-typedef pcg_engines::setseq_rxs_m_xs_8_8 pcg8_once_insecure;
-typedef pcg_engines::setseq_rxs_m_xs_16_16 pcg16_once_insecure;
-typedef pcg_engines::setseq_rxs_m_xs_32_32 pcg32_once_insecure;
-typedef pcg_engines::setseq_rxs_m_xs_64_64 pcg64_once_insecure;
-typedef pcg_engines::setseq_xsl_rr_rr_128_128 pcg128_once_insecure;
-
-typedef pcg_engines::oneseq_rxs_m_xs_8_8 pcg8_oneseq_once_insecure;
-typedef pcg_engines::oneseq_rxs_m_xs_16_16 pcg16_oneseq_once_insecure;
-typedef pcg_engines::oneseq_rxs_m_xs_32_32 pcg32_oneseq_once_insecure;
-typedef pcg_engines::oneseq_rxs_m_xs_64_64 pcg64_oneseq_once_insecure;
-typedef pcg_engines::oneseq_xsl_rr_rr_128_128 pcg128_oneseq_once_insecure;
-
-
-// These two extended RNGs provide two-dimensionally equidistributed
-// 32-bit generators. pcg32_k2_fast occupies the same space as pcg64,
-// and can be called twice to generate 64 bits, but does not required
-// 128-bit math; on 32-bit systems, it's faster than pcg64 as well.
-
-typedef pcg_engines::ext_setseq_xsh_rr_64_32<1,16,true> pcg32_k2;
-typedef pcg_engines::ext_oneseq_xsh_rs_64_32<1,32,true> pcg32_k2_fast;
-
-// These eight extended RNGs have about as much state as arc4random
-//
-// - the k variants are k-dimensionally equidistributed
-// - the c variants offer better crypographic security
-//
-// (just how good the cryptographic security is is an open question)
-
-typedef pcg_engines::ext_setseq_xsh_rr_64_32<6,16,true> pcg32_k64;
-typedef pcg_engines::ext_mcg_xsh_rs_64_32<6,32,true> pcg32_k64_oneseq;
-typedef pcg_engines::ext_oneseq_xsh_rs_64_32<6,32,true> pcg32_k64_fast;
-
-typedef pcg_engines::ext_setseq_xsh_rr_64_32<6,16,false> pcg32_c64;
-typedef pcg_engines::ext_oneseq_xsh_rs_64_32<6,32,false> pcg32_c64_oneseq;
-typedef pcg_engines::ext_mcg_xsh_rs_64_32<6,32,false> pcg32_c64_fast;
-
-typedef pcg_engines::ext_setseq_xsl_rr_128_64<5,16,true> pcg64_k32;
-typedef pcg_engines::ext_oneseq_xsl_rr_128_64<5,128,true> pcg64_k32_oneseq;
-typedef pcg_engines::ext_mcg_xsl_rr_128_64<5,128,true> pcg64_k32_fast;
-
-typedef pcg_engines::ext_setseq_xsl_rr_128_64<5,16,false> pcg64_c32;
-typedef pcg_engines::ext_oneseq_xsl_rr_128_64<5,128,false> pcg64_c32_oneseq;
-typedef pcg_engines::ext_mcg_xsl_rr_128_64<5,128,false> pcg64_c32_fast;
-
-// These eight extended RNGs have more state than the Mersenne twister
-//
-// - the k variants are k-dimensionally equidistributed
-// - the c variants offer better crypographic security
-//
-// (just how good the cryptographic security is is an open question)
-
-typedef pcg_engines::ext_setseq_xsh_rr_64_32<10,16,true> pcg32_k1024;
-typedef pcg_engines::ext_oneseq_xsh_rs_64_32<10,32,true> pcg32_k1024_fast;
-
-typedef pcg_engines::ext_setseq_xsh_rr_64_32<10,16,false> pcg32_c1024;
-typedef pcg_engines::ext_oneseq_xsh_rs_64_32<10,32,false> pcg32_c1024_fast;
-
-typedef pcg_engines::ext_setseq_xsl_rr_128_64<10,16,true> pcg64_k1024;
-typedef pcg_engines::ext_oneseq_xsl_rr_128_64<10,128,true> pcg64_k1024_fast;
-
-typedef pcg_engines::ext_setseq_xsl_rr_128_64<10,16,false> pcg64_c1024;
-typedef pcg_engines::ext_oneseq_xsl_rr_128_64<10,128,false> pcg64_c1024_fast;
-
-// These generators have an insanely huge period (2^524352), and is suitable
-// for silly party tricks, such as dumping out 64 KB ZIP files at an arbitrary
-// point in the future. [Actually, over the full period of the generator, it
-// will produce every 64 KB ZIP file 2^64 times!]
-
-typedef pcg_engines::ext_setseq_xsh_rr_64_32<14,16,true> pcg32_k16384;
-typedef pcg_engines::ext_oneseq_xsh_rs_64_32<14,32,true> pcg32_k16384_fast;
-
-#ifdef _MSC_VER
- #pragma warning(default:4146)
-#endif
-
-#endif // PCG_RAND_HPP_INCLUDED
diff --git a/benchmarks/others/pcg_uint128.hpp b/benchmarks/others/pcg_uint128.hpp
deleted file mode 100644
index 9131b21d..00000000
--- a/benchmarks/others/pcg_uint128.hpp
+++ /dev/null
@@ -1,884 +0,0 @@
-/*
- * PCG Random Number Generation for C++
- *
- * Copyright 2014-2017 Melissa O'Neill <[email protected]>,
- * and the PCG Project contributors.
- *
- * SPDX-License-Identifier: (Apache-2.0 OR MIT)
- *
- * Licensed under the Apache License, Version 2.0 (provided in
- * LICENSE-APACHE.txt and at http://www.apache.org/licenses/LICENSE-2.0)
- * or under the MIT license (provided in LICENSE-MIT.txt and at
- * http://opensource.org/licenses/MIT), at your option. This file may not
- * be copied, modified, or distributed except according to those terms.
- *
- * Distributed on an "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, either
- * express or implied. See your chosen license for details.
- *
- * For additional information about the PCG random number generation scheme,
- * visit http://www.pcg-random.org/.
- */
-
-/*
- * This code provides a a C++ class that can provide 128-bit (or higher)
- * integers. To produce 2K-bit integers, it uses two K-bit integers,
- * placed in a union that allowes the code to also see them as four K/2 bit
- * integers (and access them either directly name, or by index).
- *
- * It may seem like we're reinventing the wheel here, because several
- * libraries already exist that support large integers, but most existing
- * libraries provide a very generic multiprecision code, but here we're
- * operating at a fixed size. Also, most other libraries are fairly
- * heavyweight. So we use a direct implementation. Sadly, it's much slower
- * than hand-coded assembly or direct CPU support.
- */
-
-#ifndef PCG_UINT128_HPP_INCLUDED
-#define PCG_UINT128_HPP_INCLUDED 1
-
-#include <cstdint>
-#include <cstdio>
-#include <cassert>
-#include <climits>
-#include <utility>
-#include <initializer_list>
-#include <type_traits>
-
-#if defined(_MSC_VER) // Use MSVC++ intrinsics
-#include <intrin.h>
-#endif
-
-/*
- * We want to lay the type out the same way that a native type would be laid
- * out, which means we must know the machine's endian, at compile time.
- * This ugliness attempts to do so.
- */
-
-#ifndef PCG_LITTLE_ENDIAN
- #if defined(__BYTE_ORDER__)
- #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
- #define PCG_LITTLE_ENDIAN 1
- #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
- #define PCG_LITTLE_ENDIAN 0
- #else
- #error __BYTE_ORDER__ does not match a standard endian, pick a side
- #endif
- #elif __LITTLE_ENDIAN__ || _LITTLE_ENDIAN
- #define PCG_LITTLE_ENDIAN 1
- #elif __BIG_ENDIAN__ || _BIG_ENDIAN
- #define PCG_LITTLE_ENDIAN 0
- #elif __x86_64 || __x86_64__ || _M_X64 || __i386 || __i386__ || _M_IX86
- #define PCG_LITTLE_ENDIAN 1
- #elif __powerpc__ || __POWERPC__ || __ppc__ || __PPC__ \
- || __m68k__ || __mc68000__
- #define PCG_LITTLE_ENDIAN 0
- #else
- #error Unable to determine target endianness
- #endif
-#endif
-
-namespace pcg_extras {
-
-// Recent versions of GCC have intrinsics we can use to quickly calculate
-// the number of leading and trailing zeros in a number. If possible, we
-// use them, otherwise we fall back to old-fashioned bit twiddling to figure
-// them out.
-
-#ifndef PCG_BITCOUNT_T
- typedef uint8_t bitcount_t;
-#else
- typedef PCG_BITCOUNT_T bitcount_t;
-#endif
-
-/*
- * Provide some useful helper functions
- * * flog2 floor(log2(x))
- * * trailingzeros number of trailing zero bits
- */
-
-#if defined(__GNUC__) // Any GNU-compatible compiler supporting C++11 has
- // some useful intrinsics we can use.
-
-inline bitcount_t flog2(uint32_t v)
-{
- return 31 - __builtin_clz(v);
-}
-
-inline bitcount_t trailingzeros(uint32_t v)
-{
- return __builtin_ctz(v);
-}
-
-inline bitcount_t flog2(uint64_t v)
-{
-#if UINT64_MAX == ULONG_MAX
- return 63 - __builtin_clzl(v);
-#elif UINT64_MAX == ULLONG_MAX
- return 63 - __builtin_clzll(v);
-#else
- #error Cannot find a function for uint64_t
-#endif
-}
-
-inline bitcount_t trailingzeros(uint64_t v)
-{
-#if UINT64_MAX == ULONG_MAX
- return __builtin_ctzl(v);
-#elif UINT64_MAX == ULLONG_MAX
- return __builtin_ctzll(v);
-#else
- #error Cannot find a function for uint64_t
-#endif
-}
-
-#elif defined(_MSC_VER) // Use MSVC++ intrinsics
-
-#pragma intrinsic(_BitScanReverse, _BitScanForward)
-#if defined(_M_X64) || defined(_M_ARM) || defined(_M_ARM64)
-#pragma intrinsic(_BitScanReverse64, _BitScanForward64)
-#endif
-
-inline bitcount_t flog2(uint32_t v)
-{
- unsigned long i;
- _BitScanReverse(&i, v);
- return bitcount_t(i);
-}
-
-inline bitcount_t trailingzeros(uint32_t v)
-{
- unsigned long i;
- _BitScanForward(&i, v);
- return bitcount_t(i);
-}
-
-inline bitcount_t flog2(uint64_t v)
-{
-#if defined(_M_X64) || defined(_M_ARM) || defined(_M_ARM64)
- unsigned long i;
- _BitScanReverse64(&i, v);
- return bitcount_t(i);
-#else
- // 32-bit x86
- uint32_t high = v >> 32;
- uint32_t low = uint32_t(v);
- return high ? 32+flog2(high) : flog2(low);
-#endif
-}
-
-inline bitcount_t trailingzeros(uint64_t v)
-{
-#if defined(_M_X64) || defined(_M_ARM) || defined(_M_ARM64)
- unsigned long i;
- _BitScanForward64(&i, v);
- return bitcount_t(i);
-#else
- // 32-bit x86
- uint32_t high = v >> 32;
- uint32_t low = uint32_t(v);
- return low ? trailingzeros(low) : trailingzeros(high)+32;
-#endif
-}
-
-#else // Otherwise, we fall back to bit twiddling
- // implementations
-
-inline bitcount_t flog2(uint32_t v)
-{
- // Based on code by Eric Cole and Mark Dickinson, which appears at
- // https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn
-
- static const uint8_t multiplyDeBruijnBitPos[32] = {
- 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
- 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
- };
-
- v |= v >> 1; // first round down to one less than a power of 2
- v |= v >> 2;
- v |= v >> 4;
- v |= v >> 8;
- v |= v >> 16;
-
- return multiplyDeBruijnBitPos[(uint32_t)(v * 0x07C4ACDDU) >> 27];
-}
-
-inline bitcount_t trailingzeros(uint32_t v)
-{
- static const uint8_t multiplyDeBruijnBitPos[32] = {
- 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
- 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
- };
-
- return multiplyDeBruijnBitPos[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
-}
-
-inline bitcount_t flog2(uint64_t v)
-{
- uint32_t high = v >> 32;
- uint32_t low = uint32_t(v);
-
- return high ? 32+flog2(high) : flog2(low);
-}
-
-inline bitcount_t trailingzeros(uint64_t v)
-{
- uint32_t high = v >> 32;
- uint32_t low = uint32_t(v);
-
- return low ? trailingzeros(low) : trailingzeros(high)+32;
-}
-
-#endif
-
-inline bitcount_t flog2(uint8_t v)
-{
- return flog2(uint32_t(v));
-}
-
-inline bitcount_t flog2(uint16_t v)
-{
- return flog2(uint32_t(v));
-}
-
-#if __SIZEOF_INT128__
-inline bitcount_t flog2(__uint128_t v)
-{
- uint64_t high = uint64_t(v >> 64);
- uint64_t low = uint64_t(v);
-
- return high ? 64+flog2(high) : flog2(low);
-}
-#endif
-
-inline bitcount_t trailingzeros(uint8_t v)
-{
- return trailingzeros(uint32_t(v));
-}
-
-inline bitcount_t trailingzeros(uint16_t v)
-{
- return trailingzeros(uint32_t(v));
-}
-
-#if __SIZEOF_INT128__
-inline bitcount_t trailingzeros(__uint128_t v)
-{
- uint64_t high = uint64_t(v >> 64);
- uint64_t low = uint64_t(v);
- return low ? trailingzeros(low) : trailingzeros(high)+64;
-}
-#endif
-
-template <typename UInt>
-inline bitcount_t clog2(UInt v)
-{
- return flog2(v) + ((v & (-v)) != v);
-}
-
-template <typename UInt>
-inline UInt addwithcarry(UInt x, UInt y, bool carryin, bool* carryout)
-{
- UInt half_result = y + carryin;
- UInt result = x + half_result;
- *carryout = (half_result < y) || (result < x);
- return result;
-}
-
-template <typename UInt>
-inline UInt subwithcarry(UInt x, UInt y, bool carryin, bool* carryout)
-{
- UInt half_result = y + carryin;
- UInt result = x - half_result;
- *carryout = (half_result < y) || (result > x);
- return result;
-}
-
-
-template <typename UInt, typename UIntX2>
-class uint_x4 {
-// private:
- static constexpr unsigned int UINT_BITS = sizeof(UInt) * CHAR_BIT;
-public:
- union {
-#if PCG_LITTLE_ENDIAN
- struct {
- UInt v0, v1, v2, v3;
- } w;
- struct {
- UIntX2 v01, v23;
- } d;
-#else
- struct {
- UInt v3, v2, v1, v0;
- } w;
- struct {
- UIntX2 v23, v01;
- } d;
-#endif
- // For the array access versions, the code that uses the array
- // must handle endian itself. Yuck.
- UInt wa[4];
- UIntX2 da[2];
- };
-
-public:
- uint_x4() = default;
-
- constexpr uint_x4(UInt v3, UInt v2, UInt v1, UInt v0)
-#if PCG_LITTLE_ENDIAN
- : w{v0, v1, v2, v3}
-#else
- : w{v3, v2, v1, v0}
-#endif
- {
- // Nothing (else) to do
- }
-
- constexpr uint_x4(UIntX2 v23, UIntX2 v01)
-#if PCG_LITTLE_ENDIAN
- : d{v01,v23}
-#else
- : d{v23,v01}
-#endif
- {
- // Nothing (else) to do
- }
-
- constexpr uint_x4(UIntX2 v01)
-#if PCG_LITTLE_ENDIAN
- : d{v01, UIntX2(0)}
-#else
- : d{UIntX2(0),v01}
-#endif
- {
- // Nothing (else) to do
- }
-
- template<class Integral,
- typename std::enable_if<(std::is_integral<Integral>::value
- && sizeof(Integral) <= sizeof(UIntX2))
- >::type* = nullptr>
- constexpr uint_x4(Integral v01)
-#if PCG_LITTLE_ENDIAN
- : d{UIntX2(v01), UIntX2(0)}
-#else
- : d{UIntX2(0), UIntX2(v01)}
-#endif
- {
- // Nothing (else) to do
- }
-
- explicit constexpr operator UIntX2() const
- {
- return d.v01;
- }
-
- template<class Integral,
- typename std::enable_if<(std::is_integral<Integral>::value
- && sizeof(Integral) <= sizeof(UIntX2))
- >::type* = nullptr>
- explicit constexpr operator Integral() const
- {
- return Integral(d.v01);
- }
-
- explicit constexpr operator bool() const
- {
- return d.v01 || d.v23;
- }
-
- template<typename U, typename V>
- friend uint_x4<U,V> operator*(const uint_x4<U,V>&, const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend uint_x4<U,V> operator*(const uint_x4<U,V>&, V);
-
- template<typename U, typename V>
- friend std::pair< uint_x4<U,V>,uint_x4<U,V> >
- divmod(const uint_x4<U,V>&, const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend uint_x4<U,V> operator+(const uint_x4<U,V>&, const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend uint_x4<U,V> operator-(const uint_x4<U,V>&, const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend uint_x4<U,V> operator<<(const uint_x4<U,V>&, const bitcount_t shift);
-
- template<typename U, typename V>
- friend uint_x4<U,V> operator>>(const uint_x4<U,V>&, const bitcount_t shift);
-
- template<typename U, typename V>
- friend uint_x4<U,V> operator&(const uint_x4<U,V>&, const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend uint_x4<U,V> operator|(const uint_x4<U,V>&, const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend uint_x4<U,V> operator^(const uint_x4<U,V>&, const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend bool operator==(const uint_x4<U,V>&, const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend bool operator!=(const uint_x4<U,V>&, const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend bool operator<(const uint_x4<U,V>&, const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend bool operator<=(const uint_x4<U,V>&, const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend bool operator>(const uint_x4<U,V>&, const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend bool operator>=(const uint_x4<U,V>&, const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend uint_x4<U,V> operator~(const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend uint_x4<U,V> operator-(const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend bitcount_t flog2(const uint_x4<U,V>&);
-
- template<typename U, typename V>
- friend bitcount_t trailingzeros(const uint_x4<U,V>&);
-
- uint_x4& operator*=(const uint_x4& rhs)
- {
- uint_x4 result = *this * rhs;
- return *this = result;
- }
-
- uint_x4& operator*=(UIntX2 rhs)
- {
- uint_x4 result = *this * rhs;
- return *this = result;
- }
-
- uint_x4& operator/=(const uint_x4& rhs)
- {
- uint_x4 result = *this / rhs;
- return *this = result;
- }
-
- uint_x4& operator%=(const uint_x4& rhs)
- {
- uint_x4 result = *this % rhs;
- return *this = result;
- }
-
- uint_x4& operator+=(const uint_x4& rhs)
- {
- uint_x4 result = *this + rhs;
- return *this = result;
- }
-
- uint_x4& operator-=(const uint_x4& rhs)
- {
- uint_x4 result = *this - rhs;
- return *this = result;
- }
-
- uint_x4& operator&=(const uint_x4& rhs)
- {
- uint_x4 result = *this & rhs;
- return *this = result;
- }
-
- uint_x4& operator|=(const uint_x4& rhs)
- {
- uint_x4 result = *this | rhs;
- return *this = result;
- }
-
- uint_x4& operator^=(const uint_x4& rhs)
- {
- uint_x4 result = *this ^ rhs;
- return *this = result;
- }
-
- uint_x4& operator>>=(bitcount_t shift)
- {
- uint_x4 result = *this >> shift;
- return *this = result;
- }
-
- uint_x4& operator<<=(bitcount_t shift)
- {
- uint_x4 result = *this << shift;
- return *this = result;
- }
-
-};
-
-template<typename U, typename V>
-bitcount_t flog2(const uint_x4<U,V>& v)
-{
-#if PCG_LITTLE_ENDIAN
- for (uint8_t i = 4; i !=0; /* dec in loop */) {
- --i;
-#else
- for (uint8_t i = 0; i < 4; ++i) {
-#endif
- if (v.wa[i] == 0)
- continue;
- return flog2(v.wa[i]) + uint_x4<U,V>::UINT_BITS*i;
- }
- abort();
-}
-
-template<typename U, typename V>
-bitcount_t trailingzeros(const uint_x4<U,V>& v)
-{
-#if PCG_LITTLE_ENDIAN
- for (uint8_t i = 0; i < 4; ++i) {
-#else
- for (uint8_t i = 4; i !=0; /* dec in loop */) {
- --i;
-#endif
- if (v.wa[i] != 0)
- return trailingzeros(v.wa[i]) + uint_x4<U,V>::UINT_BITS*i;
- }
- return uint_x4<U,V>::UINT_BITS*4;
-}
-
-template <typename UInt, typename UIntX2>
-std::pair< uint_x4<UInt,UIntX2>, uint_x4<UInt,UIntX2> >
- divmod(const uint_x4<UInt,UIntX2>& orig_dividend,
- const uint_x4<UInt,UIntX2>& divisor)
-{
- // If the dividend is less than the divisor, the answer is always zero.
- // This takes care of boundary cases like 0/x (which would otherwise be
- // problematic because we can't take the log of zero. (The boundary case
- // of division by zero is undefined.)
- if (orig_dividend < divisor)
- return { uint_x4<UInt,UIntX2>(UIntX2(0)), orig_dividend };
-
- auto dividend = orig_dividend;
-
- auto log2_divisor = flog2(divisor);
- auto log2_dividend = flog2(dividend);
- // assert(log2_dividend >= log2_divisor);
- bitcount_t logdiff = log2_dividend - log2_divisor;
-
- constexpr uint_x4<UInt,UIntX2> ONE(UIntX2(1));
- if (logdiff == 0)
- return { ONE, dividend - divisor };
-
- // Now we change the log difference to
- // floor(log2(divisor)) - ceil(log2(dividend))
- // to ensure that we *underestimate* the result.
- logdiff -= 1;
-
- uint_x4<UInt,UIntX2> quotient(UIntX2(0));
-
- auto qfactor = ONE << logdiff;
- auto factor = divisor << logdiff;
-
- do {
- dividend -= factor;
- quotient += qfactor;
- while (dividend < factor) {
- factor >>= 1;
- qfactor >>= 1;
- }
- } while (dividend >= divisor);
-
- return { quotient, dividend };
-}
-
-template <typename UInt, typename UIntX2>
-uint_x4<UInt,UIntX2> operator/(const uint_x4<UInt,UIntX2>& dividend,
- const uint_x4<UInt,UIntX2>& divisor)
-{
- return divmod(dividend, divisor).first;
-}
-
-template <typename UInt, typename UIntX2>
-uint_x4<UInt,UIntX2> operator%(const uint_x4<UInt,UIntX2>& dividend,
- const uint_x4<UInt,UIntX2>& divisor)
-{
- return divmod(dividend, divisor).second;
-}
-
-
-template <typename UInt, typename UIntX2>
-uint_x4<UInt,UIntX2> operator*(const uint_x4<UInt,UIntX2>& a,
- const uint_x4<UInt,UIntX2>& b)
-{
- constexpr auto UINT_BITS = uint_x4<UInt,UIntX2>::UINT_BITS;
- uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
- bool carryin = false;
- bool carryout;
- UIntX2 a0b0 = UIntX2(a.w.v0) * UIntX2(b.w.v0);
- r.w.v0 = UInt(a0b0);
- r.w.v1 = UInt(a0b0 >> UINT_BITS);
-
- UIntX2 a1b0 = UIntX2(a.w.v1) * UIntX2(b.w.v0);
- r.w.v2 = UInt(a1b0 >> UINT_BITS);
- r.w.v1 = addwithcarry(r.w.v1, UInt(a1b0), carryin, &carryout);
- carryin = carryout;
- r.w.v2 = addwithcarry(r.w.v2, UInt(0U), carryin, &carryout);
- carryin = carryout;
- r.w.v3 = addwithcarry(r.w.v3, UInt(0U), carryin, &carryout);
-
- UIntX2 a0b1 = UIntX2(a.w.v0) * UIntX2(b.w.v1);
- carryin = false;
- r.w.v2 = addwithcarry(r.w.v2, UInt(a0b1 >> UINT_BITS), carryin, &carryout);
- carryin = carryout;
- r.w.v3 = addwithcarry(r.w.v3, UInt(0U), carryin, &carryout);
-
- carryin = false;
- r.w.v1 = addwithcarry(r.w.v1, UInt(a0b1), carryin, &carryout);
- carryin = carryout;
- r.w.v2 = addwithcarry(r.w.v2, UInt(0U), carryin, &carryout);
- carryin = carryout;
- r.w.v3 = addwithcarry(r.w.v3, UInt(0U), carryin, &carryout);
-
- UIntX2 a1b1 = UIntX2(a.w.v1) * UIntX2(b.w.v1);
- carryin = false;
- r.w.v2 = addwithcarry(r.w.v2, UInt(a1b1), carryin, &carryout);
- carryin = carryout;
- r.w.v3 = addwithcarry(r.w.v3, UInt(a1b1 >> UINT_BITS), carryin, &carryout);
-
- r.d.v23 += a.d.v01 * b.d.v23 + a.d.v23 * b.d.v01;
-
- return r;
-}
-
-
-template <typename UInt, typename UIntX2>
-uint_x4<UInt,UIntX2> operator*(const uint_x4<UInt,UIntX2>& a,
- UIntX2 b01)
-{
- constexpr auto UINT_BITS = uint_x4<UInt,UIntX2>::UINT_BITS;
- uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
- bool carryin = false;
- bool carryout;
- UIntX2 a0b0 = UIntX2(a.w.v0) * UIntX2(UInt(b01));
- r.w.v0 = UInt(a0b0);
- r.w.v1 = UInt(a0b0 >> UINT_BITS);
-
- UIntX2 a1b0 = UIntX2(a.w.v1) * UIntX2(UInt(b01));
- r.w.v2 = UInt(a1b0 >> UINT_BITS);
- r.w.v1 = addwithcarry(r.w.v1, UInt(a1b0), carryin, &carryout);
- carryin = carryout;
- r.w.v2 = addwithcarry(r.w.v2, UInt(0U), carryin, &carryout);
- carryin = carryout;
- r.w.v3 = addwithcarry(r.w.v3, UInt(0U), carryin, &carryout);
-
- UIntX2 a0b1 = UIntX2(a.w.v0) * UIntX2(b01 >> UINT_BITS);
- carryin = false;
- r.w.v2 = addwithcarry(r.w.v2, UInt(a0b1 >> UINT_BITS), carryin, &carryout);
- carryin = carryout;
- r.w.v3 = addwithcarry(r.w.v3, UInt(0U), carryin, &carryout);
-
- carryin = false;
- r.w.v1 = addwithcarry(r.w.v1, UInt(a0b1), carryin, &carryout);
- carryin = carryout;
- r.w.v2 = addwithcarry(r.w.v2, UInt(0U), carryin, &carryout);
- carryin = carryout;
- r.w.v3 = addwithcarry(r.w.v3, UInt(0U), carryin, &carryout);
-
- UIntX2 a1b1 = UIntX2(a.w.v1) * UIntX2(b01 >> UINT_BITS);
- carryin = false;
- r.w.v2 = addwithcarry(r.w.v2, UInt(a1b1), carryin, &carryout);
- carryin = carryout;
- r.w.v3 = addwithcarry(r.w.v3, UInt(a1b1 >> UINT_BITS), carryin, &carryout);
-
- r.d.v23 += a.d.v23 * b01;
-
- return r;
-}
-
-
-template <typename UInt, typename UIntX2>
-uint_x4<UInt,UIntX2> operator+(const uint_x4<UInt,UIntX2>& a,
- const uint_x4<UInt,UIntX2>& b)
-{
- uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
-
- bool carryin = false;
- bool carryout;
- r.w.v0 = addwithcarry(a.w.v0, b.w.v0, carryin, &carryout);
- carryin = carryout;
- r.w.v1 = addwithcarry(a.w.v1, b.w.v1, carryin, &carryout);
- carryin = carryout;
- r.w.v2 = addwithcarry(a.w.v2, b.w.v2, carryin, &carryout);
- carryin = carryout;
- r.w.v3 = addwithcarry(a.w.v3, b.w.v3, carryin, &carryout);
-
- return r;
-}
-
-template <typename UInt, typename UIntX2>
-uint_x4<UInt,UIntX2> operator-(const uint_x4<UInt,UIntX2>& a,
- const uint_x4<UInt,UIntX2>& b)
-{
- uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
-
- bool carryin = false;
- bool carryout;
- r.w.v0 = subwithcarry(a.w.v0, b.w.v0, carryin, &carryout);
- carryin = carryout;
- r.w.v1 = subwithcarry(a.w.v1, b.w.v1, carryin, &carryout);
- carryin = carryout;
- r.w.v2 = subwithcarry(a.w.v2, b.w.v2, carryin, &carryout);
- carryin = carryout;
- r.w.v3 = subwithcarry(a.w.v3, b.w.v3, carryin, &carryout);
-
- return r;
-}
-
-
-template <typename UInt, typename UIntX2>
-uint_x4<UInt,UIntX2> operator&(const uint_x4<UInt,UIntX2>& a,
- const uint_x4<UInt,UIntX2>& b)
-{
- return uint_x4<UInt,UIntX2>(a.d.v23 & b.d.v23, a.d.v01 & b.d.v01);
-}
-
-template <typename UInt, typename UIntX2>
-uint_x4<UInt,UIntX2> operator|(const uint_x4<UInt,UIntX2>& a,
- const uint_x4<UInt,UIntX2>& b)
-{
- return uint_x4<UInt,UIntX2>(a.d.v23 | b.d.v23, a.d.v01 | b.d.v01);
-}
-
-template <typename UInt, typename UIntX2>
-uint_x4<UInt,UIntX2> operator^(const uint_x4<UInt,UIntX2>& a,
- const uint_x4<UInt,UIntX2>& b)
-{
- return uint_x4<UInt,UIntX2>(a.d.v23 ^ b.d.v23, a.d.v01 ^ b.d.v01);
-}
-
-template <typename UInt, typename UIntX2>
-uint_x4<UInt,UIntX2> operator~(const uint_x4<UInt,UIntX2>& v)
-{
- return uint_x4<UInt,UIntX2>(~v.d.v23, ~v.d.v01);
-}
-
-template <typename UInt, typename UIntX2>
-uint_x4<UInt,UIntX2> operator-(const uint_x4<UInt,UIntX2>& v)
-{
- return uint_x4<UInt,UIntX2>(0UL,0UL) - v;
-}
-
-template <typename UInt, typename UIntX2>
-bool operator==(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
-{
- return (a.d.v01 == b.d.v01) && (a.d.v23 == b.d.v23);
-}
-
-template <typename UInt, typename UIntX2>
-bool operator!=(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
-{
- return !operator==(a,b);
-}
-
-
-template <typename UInt, typename UIntX2>
-bool operator<(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
-{
- return (a.d.v23 < b.d.v23)
- || ((a.d.v23 == b.d.v23) && (a.d.v01 < b.d.v01));
-}
-
-template <typename UInt, typename UIntX2>
-bool operator>(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
-{
- return operator<(b,a);
-}
-
-template <typename UInt, typename UIntX2>
-bool operator<=(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
-{
- return !(operator<(b,a));
-}
-
-template <typename UInt, typename UIntX2>
-bool operator>=(const uint_x4<UInt,UIntX2>& a, const uint_x4<UInt,UIntX2>& b)
-{
- return !(operator<(a,b));
-}
-
-
-
-template <typename UInt, typename UIntX2>
-uint_x4<UInt,UIntX2> operator<<(const uint_x4<UInt,UIntX2>& v,
- const bitcount_t shift)
-{
- uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
- const bitcount_t bits = uint_x4<UInt,UIntX2>::UINT_BITS;
- const bitcount_t bitmask = bits - 1;
- const bitcount_t shiftdiv = shift / bits;
- const bitcount_t shiftmod = shift & bitmask;
-
- if (shiftmod) {
- UInt carryover = 0;
-#if PCG_LITTLE_ENDIAN
- for (uint8_t out = shiftdiv, in = 0; out < 4; ++out, ++in) {
-#else
- for (uint8_t out = 4-shiftdiv, in = 4; out != 0; /* dec in loop */) {
- --out, --in;
-#endif
- r.wa[out] = (v.wa[in] << shiftmod) | carryover;
- carryover = (v.wa[in] >> (bits - shiftmod));
- }
- } else {
-#if PCG_LITTLE_ENDIAN
- for (uint8_t out = shiftdiv, in = 0; out < 4; ++out, ++in) {
-#else
- for (uint8_t out = 4-shiftdiv, in = 4; out != 0; /* dec in loop */) {
- --out, --in;
-#endif
- r.wa[out] = v.wa[in];
- }
- }
-
- return r;
-}
-
-template <typename UInt, typename UIntX2>
-uint_x4<UInt,UIntX2> operator>>(const uint_x4<UInt,UIntX2>& v,
- const bitcount_t shift)
-{
- uint_x4<UInt,UIntX2> r = {0U, 0U, 0U, 0U};
- const bitcount_t bits = uint_x4<UInt,UIntX2>::UINT_BITS;
- const bitcount_t bitmask = bits - 1;
- const bitcount_t shiftdiv = shift / bits;
- const bitcount_t shiftmod = shift & bitmask;
-
- if (shiftmod) {
- UInt carryover = 0;
-#if PCG_LITTLE_ENDIAN
- for (uint8_t out = 4-shiftdiv, in = 4; out != 0; /* dec in loop */) {
- --out, --in;
-#else
- for (uint8_t out = shiftdiv, in = 0; out < 4; ++out, ++in) {
-#endif
- r.wa[out] = (v.wa[in] >> shiftmod) | carryover;
- carryover = (v.wa[in] << (bits - shiftmod));
- }
- } else {
-#if PCG_LITTLE_ENDIAN
- for (uint8_t out = 4-shiftdiv, in = 4; out != 0; /* dec in loop */) {
- --out, --in;
-#else
- for (uint8_t out = shiftdiv, in = 0; out < 4; ++out, ++in) {
-#endif
- r.wa[out] = v.wa[in];
- }
- }
-
- return r;
-}
-
-} // namespace pcg_extras
-
-#endif // PCG_UINT128_HPP_INCLUDED
diff --git a/benchmarks/others/sstr.h b/benchmarks/others/sstr.h
index 7947870a..5301978a 100644
--- a/benchmarks/others/sstr.h
+++ b/benchmarks/others/sstr.h
@@ -163,19 +163,19 @@ STC_INLINE sstr_size_t sstr_capacity(sstr s) {
return sstr_select_(&s, cap(&s));
}
-STC_INLINE bool sstr_equals(sstr s1, const char* str) {
+STC_INLINE bool sstr_equalto(sstr s1, const char* str) {
return strcmp(sstr_str(&s1), str) == 0;
}
-STC_INLINE bool sstr_equals_s(sstr s1, sstr s2) {
+STC_INLINE bool sstr_equalto_s(sstr s1, sstr s2) {
return strcmp(sstr_str(&s1), sstr_str(&s2)) == 0;
}
-STC_INLINE int sstr_equals_ref(const sstr* s1, const sstr* s2) {
+STC_INLINE int sstr_equals(const sstr* s1, const sstr* s2) {
return strcmp(sstr_str(s1), sstr_str(s2)) == 0;
}
-STC_INLINE int sstr_compare_ref(const sstr* s1, const sstr* s2) {
+STC_INLINE int sstr_compare(const sstr* s1, const sstr* s2) {
return strcmp(sstr_str(s1), sstr_str(s2));
}
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md
index ddf3783c..0eb11f82 100644
--- a/docs/ccommon_api.md
+++ b/docs/ccommon_api.md
@@ -2,25 +2,53 @@
The following handy macros are safe to use, i.e. have no side-effects.
-### c_fordefer, c_forscope, c_forvar, c_forvar_initdel
+### c_forvar, c_forauto, c_forscope, c_fordefer
General ***defer*** mechanics for resource acquisition. These macros allows to specify the release of the
resource where the resource acquisition takes place. Makes it easier to verify that resources are released.
-**Note**: These macros are one-time executed **for-loops**. Use ***only*** `continue` in order to break out
-of a `c_for*`-block. ***Do not*** use `return`, `goto` or `break`, as they will prevent the `end`-statement to
-be executed when leaving scope. This is not particular to the `c_for*()` macros, as one must always make sure
-to unwind temporary allocated resources before a `return` in C.
+**NB**: These macros are one-time executed **for-loops**. Use ***only*** `continue` in order to break out
+of these four `c_for`-blocks. ***Do not*** use `return` or `goto` (or `break`) inside them, as they will
+prevent the `end`-statement to be executed when leaving scope. This is not particular to the `c_for*()`
+macros, as one must always make sure to unwind temporary allocated resources before a `return` in C.
| Usage | Description |
|:---------------------------------------|:--------------------------------------------------|
-| `c_fordefer (end...)` | Defer execution of `end` to end of block |
-| `c_forscope (start, end)` | Execute `start`. Defer `end` to end of block |
| `c_forvar (Type var=init, end...)` | Declare `var`. Defer `end` to end of block |
-| `c_forvar_initdel (Type, var...)` | `c_forvar (Type var=Type_init(), Type_del(&var))` |
+| `c_forauto (Type, var...)` | `c_forvar (Type var=Type_init(), Type_del(&var))` |
+| `c_forscope (start, end)` | Execute `start`. Defer `end` to end of block |
+| `c_fordefer (end...)` | Defer execution of `end` to end of block |
-For multiple variables, use either multiple **c_forvar** in sequence, or declare variable outside
-scope and use **c_fordefer** or **c_forscope**. Also, **c_forvar_initdel** support up to 3 vars.
+For multiple variables, use either multiple **c_forvar** in sequence, or declare variable outside
+scope and use **c_forscope** or **c_fordefer**. Also, **c_forauto** support up to 3 vars.
```c
+c_forvar (cstr s = cstr_lit("Hello"), cstr_del(&s))
+{
+ cstr_append(&s, " world");
+ printf("%s\n", s.str);
+}
+
+c_forauto (cstr, s1, s2)
+{
+ cstr_append(&s1, "Hello");
+ cstr_append(&s1, " world");
+
+ cstr_append(&s2, "Cool");
+ cstr_append(&s2, " stuff");
+
+ printf("%s %s\n", s1.str, s2.str);
+}
+
+using_cvec(i32, int32_t);
+...
+cvec_i32 vec;
+c_forscope (vec = cvec_i32_init(), cvec_i32_del(&vec))
+{
+ cvec_i32_push_back(&vec, 123);
+ cvec_i32_push_back(&vec, 321);
+
+ c_foreach (i, cvec_i32, vec) printf(" %d", *i.ref);
+}
+
cstr s1 = cstr_lit("Hello"), s2 = cstr_lit("world");
c_fordefer (cstr_del(&s1), cstr_del(&s2))
{
@@ -66,10 +94,12 @@ int main()
using_csset(x, int);
...
c_var (csset_x, set, {23, 3, 7, 5, 12});
-c_foreach (i, csset_x, set) printf(" %d", *i.ref);
+c_foreach (i, csset_x, set)
+ printf(" %d", *i.ref);
// 3 5 7 12 23
csset_x_iter_t it = csset_x_find(&set, 7);
-c_foreach (i, csset_x, it, csset_x_end(&set)) printf(" %d", *i.ref);
+c_foreach (i, csset_x, it, csset_x_end(&set))
+ printf(" %d", *i.ref);
// 7 12 23
```
diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md
index 42e46240..83c0f19f 100644
--- a/docs/cdeq_api.md
+++ b/docs/cdeq_api.md
@@ -23,7 +23,7 @@ be replaced by `i` in all of the following documentation.
`using_cdeq_str()` is a shorthand for:
```
-using_cdeq(str, cstr, c_rawstr_compare, cstr_del, cstr_from, cstr_toraw, const char*)
+using_cdeq(str, cstr, c_rawstr_compare, cstr_del, cstr_from, cstr_str, const char*)
```
## Methods
diff --git a/docs/clist_api.md b/docs/clist_api.md
index 2028e764..b57662ca 100644
--- a/docs/clist_api.md
+++ b/docs/clist_api.md
@@ -35,7 +35,7 @@ The macro `using_clist()` must be instantiated in the global scope. `X` is a typ
will affect the names of all clist types and methods. E.g. declaring `using_clist(i, int);`, `X` should
be replaced by `i` in all of the following documentation. `using_clist_str()` is a shorthand for
```c
-using_clist(str, cstr, c_rawstr_compare, cstr_del, cstr_from, cstr_toraw, const char*)
+using_clist(str, cstr, c_rawstr_compare, cstr_del, cstr_from, cstr_str, const char*)
```
## Methods
diff --git a/docs/cmap_api.md b/docs/cmap_api.md
index ea9efb39..59afa5ab 100644
--- a/docs/cmap_api.md
+++ b/docs/cmap_api.md
@@ -41,7 +41,7 @@ using_cmap_str() // using_cmap(str, cstr, cstr, .
```
The `using_cmap()` macro family must be instantiated in the global scope. `X` is a type tag name and
will affect the names of all cmap types and methods. E.g. declaring `using_cmap(ii, int, int);`, `X` should
-be replaced by `ii` in all of the following documentation.
+be replaced by `ii` in all of the following documentation. Argument `flag` is `c_true` by default.
## Methods
@@ -275,7 +275,7 @@ static int Viking_equals(const Viking* a, const Viking* b) {
}
static uint32_t Viking_hash(const Viking* a, int ignored) {
- return cstr_hash(a->name) ^ (cstr_hash(a->country) >> 15);
+ return cstr_hash(&a->name) ^ (cstr_hash(&a->country) >> 15);
}
static void Viking_del(Viking* v) {
diff --git a/docs/cset_api.md b/docs/cset_api.md
index 2c0f4611..dc628750 100644
--- a/docs/cset_api.md
+++ b/docs/cset_api.md
@@ -12,9 +12,9 @@ A **cset** is an associative container that contains a set of unique objects of
using_cset(X, Key);
using_cset(X, Key, keyEquals, keyHash);
using_cset(X, Key, keyEquals, keyHash, keyDel, keyClone = c_no_clone);
-using_cset(X, Key, keyEqualsRaw, keyHashRaw, keyDel, keyFromRaw, keyToRaw, RawKey);
+using_cset(X, Key, keyEqualsRaw, keyHashRaw, keyDel, keyFromRaw, keyToRaw, RawKey, flag);
-using_cset_str(); // using_cset(str, cstr, ...)
+using_cset_str(); // using_cset(str, cstr, c_rawstr_equals, c_rawstr_hash, cstr_del, ...)
```
The macro `using_cset()` must be instantiated in the global scope. `X` is a type tag name and
will affect the names of all cset types and methods. E.g. declaring `using_cset(i, int);`, `X` should
diff --git a/docs/csmap_api.md b/docs/csmap_api.md
index 8e152497..dba4fecd 100644
--- a/docs/csmap_api.md
+++ b/docs/csmap_api.md
@@ -19,27 +19,27 @@ See the c++ class [std::map](https://en.cppreference.com/w/cpp/container/map) fo
using_csmap(X, Key, Mapped);
using_csmap(X, Key, Mapped, keyCompare);
-using_csmap(X, Key, Mapped, keyCompare, mappedDel, mappedClone);
+using_csmap(X, Key, Mapped, keyCompare, mappedDel, mappedClone = c_no_clone);
using_csmap(X, Key, Mapped, keyCompare, mappedDel, mappedFromRaw, mappedToRaw, RawMapped);
using_csmap(X, Key, Mapped, keyCompareRaw, mappedDel, mappedFromRaw, mappedToRaw, RawMapped,
- keyDel, keyFromRaw, keyToRaw, RawKey);
+ keyDel, keyFromRaw, keyToRaw, RawKey, flag);
using_csmap_keydef(X, Key, Mapped, keyCompare, keyDel, keyClone);
-using_csmap_keydef(X, Key, Mapped, keyCompareRaw, keyDel, keyFromRaw, keyToRaw, RawKey);
+using_csmap_keydef(X, Key, Mapped, keyCompareRaw, keyDel, keyFromRaw, keyToRaw, RawKey, flag);
using_csmap_strkey(X, Mapped); // using_csmap(X, cstr, Mapped, ...)
using_csmap_strkey(X, Mapped, mappedDel, mappedClone);
-using_csmap_strkey(X, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped);
+using_csmap_strkey(X, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped, flag);
using_csmap_strval(X, Key); // using_csmap(X, Key, cstr, ...)
using_csmap_strval(X, Key, keyCompare);
using_csmap_strval(X, Key, keyCompare, keyDel, keyClone);
-using_csmap_strval(X, Key, keyCompareRaw, keyDel, keyFromRaw, keyToRaw, RawKey);
+using_csmap_strval(X, Key, keyCompareRaw, keyDel, keyFromRaw, keyToRaw, RawKey, flag);
using_csmap_str(); // using_csmap(str, cstr, cstr, ...)
```
The `using_csmap()` macro family must be instantiated in the global scope. `X` is a type tag name and
will affect the names of all csmap types and methods. E.g. declaring `using_csmap(ii, int, int);`, `X` should
-be replaced by `ii` in all of the following documentation.
+be replaced by `ii` in all of the following documentation. Argument `flag` is `c_true` by default.
## Methods
diff --git a/docs/csset_api.md b/docs/csset_api.md
index 38e9072e..d308fefd 100644
--- a/docs/csset_api.md
+++ b/docs/csset_api.md
@@ -13,9 +13,9 @@ See the c++ class [std::set](https://en.cppreference.com/w/cpp/container/set) fo
using_csset(X, Key);
using_csset(X, Key, keyCompare);
using_csset(X, Key, keyCompare, keyDel, keyClone = c_no_clone);
-using_csset(X, Key, keyCompareRaw, keyDel, keyFromRaw, keyToRaw, RawKey);
+using_csset(X, Key, keyCompareRaw, keyDel, keyFromRaw, keyToRaw, RawKey, flag);
-using_csset_str(); // using_csset(str, cstr, ...)
+using_csset_str(); // using_csset(str, cstr, cstr_del, cstr_from, cstr_str, const char*, c_true)
```
The macro `using_csset()` must be instantiated in the global scope. `X` is a type tag name and
will affect the names of all csset types and methods. E.g. declaring `using_csset(i, int);`, `X` should
diff --git a/docs/cstr_api.md b/docs/cstr_api.md
index adf57209..fd2b241f 100644
--- a/docs/cstr_api.md
+++ b/docs/cstr_api.md
@@ -28,6 +28,8 @@ cstr* cstr_take(cstr* self, cstr s); // take th
cstr cstr_move(cstr* self); // move string to caller, leave empty string
void cstr_del(cstr *self); // destructor
+const char* cstr_str(const cstr* self); // self->str
+char* cstr_data(cstr* self); // self->str
size_t cstr_size(cstr s);
size_t cstr_length(cstr s);
size_t cstr_capacity(cstr s);
@@ -58,15 +60,15 @@ void cstr_replace_all(cstr* self, const char* find, const char* replace)
void cstr_erase(cstr* self, size_t pos);
void cstr_erase_n(cstr* self, size_t pos, size_t n);
-bool cstr_equals(cstr s, const char* str);
-bool cstr_equals_s(cstr s, cstr s2);
+bool cstr_equalto(cstr s, const char* str);
+bool cstr_equalto_s(cstr s, cstr s2);
size_t cstr_find(cstr s, const char* needle);
size_t cstr_find_n(cstr s, const char* needle, size_t pos, size_t nmax);
bool cstr_contains(cstr s, const char* needle);
bool cstr_starts_with(cstr s, const char* str);
bool cstr_ends_with(cstr s, const char* str);
-bool cstr_iequals(cstr s, const char* str); // prefix i = case-insensitive
+bool cstr_iequalto(cstr s, const char* str); // prefix i = case-insensitive
size_t cstr_ifind_n(cstr s, const char* needle, size_t pos, size_t nmax);
bool cstr_icontains(cstr s, const char* needle);
bool cstr_istarts_with(cstr s, const char* str);
@@ -89,14 +91,13 @@ Note that all methods with arguments `(..., const char* str, size_t n)`, `n` mus
#### Helper methods:
```c
-const char* cstr_toraw(const cstr* self);
-
-int cstr_compare_ref(const cstr *s1, const cstr *s2);
-bool cstr_equals_ref(const cstr *s1, const cstr *s2);
+int cstr_compare(const cstr *s1, const cstr *s2);
+bool cstr_equals(const cstr *s1, const cstr *s2);
+bool cstr_hash(const cstr *s, ...);
int c_rawstr_compare(const char** x, const char** y);
bool c_rawstr_equals(const char** x, const char** y);
-uint64_t c_rawstr_hash(const char* const* x, size_t ignored);
+uint64_t c_rawstr_hash(const char* const* x, ...);
int c_strncasecmp(const char* str1, const char* str2, size_t n);
char* c_strnstrn(const char* str, const char* needle, size_t slen, size_t nlen);
diff --git a/docs/csview_api.md b/docs/csview_api.md
index 2906acdc..dfca37c5 100644
--- a/docs/csview_api.md
+++ b/docs/csview_api.md
@@ -45,7 +45,7 @@ csview csview_slice(csview sv, intptr_t p1, intptr_t p2); // negative p
csview csview_first_token(csview sv, csview sep); // see split example below.
csview csview_next_token(csview sv, csview sep, csview token);
-bool csview_equals(csview sv, csview sv2);
+bool csview_equalto(csview sv, csview sv2);
size_t csview_find(csview sv, csview needle);
bool csview_contains(csview sv, csview needle);
bool csview_starts_with(csview sv, csview sub);
@@ -78,9 +78,9 @@ bool cstr_ends_with_v(cstr s, csview sub);
```
#### Helper methods
```c
-int csview_compare_ref(const csview* x, const csview* y);
-bool csview_equals_ref(const csview* x, const csview* y);
-uint64_t csview_hash_ref(const csview* x, size_t ignored);
+int csview_compare(const csview* x, const csview* y);
+bool csview_equals(const csview* x, const csview* y);
+uint64_t csview_hash(const csview* x, ...);
```
## Types
@@ -220,7 +220,7 @@ int main()
csview suffix = csview_substr(text, -12, cstr_npos); // from pos -12 to end
printf("%.*s\n", csview_ARG(suffix));
- c_forvar_initdel (cmap_si, map)
+ c_forauto (cmap_si, map)
{
cmap_si_emplace(&map, c_sv("hello"), 100);
cmap_si_emplace(&map, c_sv("world"), 200);
diff --git a/docs/cvec_api.md b/docs/cvec_api.md
index 016d82e3..5a98b9ca 100644
--- a/docs/cvec_api.md
+++ b/docs/cvec_api.md
@@ -27,7 +27,7 @@ be replaced by `i` in all of the following documentation.
`using_cvec_str()` is a shorthand for:
```
-using_cvec(str, cstr, c_rawstr_compare, cstr_del, cstr_from, cstr_toraw, const char*)
+using_cvec(str, cstr, c_rawstr_compare, cstr_del, cstr_from, cstr_str, const char*)
```
## Methods
diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c
index 35afbe3a..6cebffb7 100644
--- a/examples/csmap_erase.c
+++ b/examples/csmap_erase.c
@@ -1,77 +1,77 @@
-// map_erase.c
-// https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-16
-#include <stc/csmap.h>
-#include <stc/cstr.h>
-#include <stdio.h>
-
-using_csmap_strval(my, int);
-
-void printmap(csmap_my map)
-{
- c_foreach (e, csmap_my, map)
- printf(" [%d, %s]", e.ref->first, e.ref->second.str);
- printf("\nsize() == %zu\n\n", csmap_my_size(map));
-}
-
-int main()
-{
- c_forvar (csmap_my m1 = csmap_my_init(), csmap_my_del(&m1))
- {
- // Fill in some data to test with, one at a time
- csmap_my_emplace(&m1, 1, "A");
- csmap_my_emplace(&m1, 2, "B");
- csmap_my_emplace(&m1, 3, "C");
- csmap_my_emplace(&m1, 4, "D");
- csmap_my_emplace(&m1, 5, "E");
-
- puts("Starting data of map m1 is:");
- printmap(m1);
- // The 1st member function removes an element at a given position
- csmap_my_erase_at(&m1, csmap_my_fwd(csmap_my_begin(&m1), 1));
- puts("After the 2nd element is deleted, the map m1 is:");
- printmap(m1);
- }
-
- c_forvar (csmap_my m2 = csmap_my_init(), csmap_my_del(&m2))
- {
- // Fill in some data to test with, one at a time, using emplace
- c_emplace(csmap_my, m2, {
- {10, "Bob"},
- {11, "Rob"},
- {12, "Robert"},
- {13, "Bert"},
- {14, "Bobby"}
- });
-
- puts("Starting data of map m2 is:");
- printmap(m2);
- csmap_my_iter_t it1 = csmap_my_fwd(csmap_my_begin(&m2), 1);
- csmap_my_iter_t it2 = csmap_my_find(&m2, csmap_my_back(&m2)->first);
- // The 2nd member function removes elements
- // in the range [First, Last)
- csmap_my_erase_range(&m2, it1, it2);
- puts("After the middle elements are deleted, the map m2 is:");
- printmap(m2);
- }
-
- c_forvar (csmap_my m3 = csmap_my_init(), csmap_my_del(&m3))
- {
- // Fill in some data to test with, one at a time, using emplace
- csmap_my_emplace(&m3, 1, "red");
- csmap_my_emplace(&m3, 2, "yellow");
- csmap_my_emplace(&m3, 3, "blue");
- csmap_my_emplace(&m3, 4, "green");
- csmap_my_emplace(&m3, 5, "orange");
- csmap_my_emplace(&m3, 5, "purple");
- csmap_my_emplace(&m3, 5, "pink");
-
- puts("Starting data of map m3 is:");
- printmap(m3);
- // The 3rd member function removes elements with a given Key
- size_t count = csmap_my_erase(&m3, 2);
- // The 3rd member function also returns the number of elements removed
- printf("The number of elements removed from m3 is: %zu\n", count);
- puts("After the element with a key of 2 is deleted, the map m3 is:");
- printmap(m3);
- }
-} \ No newline at end of file
+// map_erase.c
+// From C++ example: https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-16
+#include <stc/csmap.h>
+#include <stc/cstr.h>
+#include <stdio.h>
+
+using_csmap_strval(my, int);
+
+void printmap(csmap_my map)
+{
+ c_foreach (e, csmap_my, map)
+ printf(" [%d, %s]", e.ref->first, e.ref->second.str);
+ printf("\nsize() == %zu\n\n", csmap_my_size(map));
+}
+
+int main()
+{
+ c_forauto (csmap_my, m1)
+ {
+ // Fill in some data to test with, one at a time
+ csmap_my_insert(&m1, 1, cstr_lit("A"));
+ csmap_my_insert(&m1, 2, cstr_lit("B"));
+ csmap_my_insert(&m1, 3, cstr_lit("C"));
+ csmap_my_insert(&m1, 4, cstr_lit("D"));
+ csmap_my_insert(&m1, 5, cstr_lit("E"));
+
+ puts("Starting data of map m1 is:");
+ printmap(m1);
+ // The 1st member function removes an element at a given position
+ csmap_my_erase_at(&m1, csmap_my_fwd(csmap_my_begin(&m1), 1));
+ puts("After the 2nd element is deleted, the map m1 is:");
+ printmap(m1);
+ }
+
+ c_forauto (csmap_my, m2)
+ {
+ // Fill in some data to test with, one at a time, using c_emplace()
+ c_emplace(csmap_my, m2, {
+ {10, "Bob"},
+ {11, "Rob"},
+ {12, "Robert"},
+ {13, "Bert"},
+ {14, "Bobby"}
+ });
+
+ puts("Starting data of map m2 is:");
+ printmap(m2);
+ csmap_my_iter_t it1 = csmap_my_fwd(csmap_my_begin(&m2), 1);
+ csmap_my_iter_t it2 = csmap_my_find(&m2, csmap_my_back(&m2)->first);
+ // The 2nd member function removes elements
+ // in the range [First, Last)
+ csmap_my_erase_range(&m2, it1, it2);
+ puts("After the middle elements are deleted, the map m2 is:");
+ printmap(m2);
+ }
+
+ c_forauto (csmap_my, m3)
+ {
+ // Fill in some data to test with, one at a time, using emplace
+ csmap_my_emplace(&m3, 1, "red");
+ csmap_my_emplace(&m3, 2, "yellow");
+ csmap_my_emplace(&m3, 3, "blue");
+ csmap_my_emplace(&m3, 4, "green");
+ csmap_my_emplace(&m3, 5, "orange");
+ csmap_my_emplace(&m3, 6, "purple");
+ csmap_my_emplace(&m3, 7, "pink");
+
+ puts("Starting data of map m3 is:");
+ printmap(m3);
+ // The 3rd member function removes elements with a given Key
+ size_t count = csmap_my_erase(&m3, 2);
+ // The 3rd member function also returns the number of elements removed
+ printf("The number of elements removed from m3 is: %zu\n", count);
+ puts("After the element with a key of 2 is deleted, the map m3 is:");
+ printmap(m3);
+ }
+}
diff --git a/examples/csmap_find.c b/examples/csmap_find.c
index 314e702e..46ae01fa 100644
--- a/examples/csmap_find.c
+++ b/examples/csmap_find.c
@@ -1,65 +1,68 @@
-// This implements the c++ std::map::find example at:
-// https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-17
-#include <stc/csmap.h>
-#include <stc/cvec.h>
-#include <stc/cstr.h>
-#include <stdio.h>
-
-using_csmap_strval(istr, int);
-using_cvec(istr, csmap_istr_rawvalue_t, c_no_compare);
-
-void print_elem(csmap_istr_rawvalue_t p) {
- printf("(%d, %s) ", p.first, p.second);
-}
-
-#define using_print_collection(CX) \
- void print_collection_##CX(CX t) { \
- printf("%zu elements: ", CX##_size(t)); \
- \
- c_foreach (p, CX, t) { \
- print_elem(CX##_value_toraw(p.ref)); \
- } \
- puts(""); \
- }
-
-using_print_collection(csmap_istr);
-using_print_collection(cvec_istr);
-
-
-void findit(csmap_istr c, csmap_istr_key_t val) {
- printf("Trying find() on value %d\n", val);
- csmap_istr_value_t* result = csmap_istr_get(&c, val); // easier with get than find.
- if (result) {
- printf("Element found: "); print_elem(csmap_istr_value_toraw(result)); puts("");
- } else {
- puts("Element not found.");
- }
-}
-
-int main()
-{
- c_forvar (csmap_istr m1 = csmap_istr_init(), csmap_istr_del(&m1))
- c_forvar (cvec_istr v = cvec_istr_init(), cvec_istr_del(&v)) {
- c_emplace(csmap_istr, m1, { { 40, "Zr" }, { 45, "Rh" } });
- puts("The starting map m1 is (key, value):");
- print_collection_csmap_istr(m1);
-
- typedef cvec_istr_value_t pair;
- cvec_istr_emplace_back(&v, (pair){43, "Tc"});
- cvec_istr_emplace_back(&v, (pair){41, "Nb"});
- cvec_istr_emplace_back(&v, (pair){46, "Pd"});
- cvec_istr_emplace_back(&v, (pair){42, "Mo"});
- cvec_istr_emplace_back(&v, (pair){44, "Ru"});
- cvec_istr_emplace_back(&v, (pair){44, "Ru"}); // attempt a duplicate
-
- puts("Inserting the following vector data into m1:");
- print_collection_cvec_istr(v);
- c_foreach (i, cvec_istr, v) csmap_istr_emplace(&m1, i.ref->first, i.ref->second);
-
- puts("The modified map m1 is (key, value):");
- print_collection_csmap_istr(m1);
- puts("");
- findit(m1, 45);
- findit(m1, 6);
- }
-} \ No newline at end of file
+// This implements the c++ std::map::find example at:
+// https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-17
+#include <stc/csmap.h>
+#include <stc/cvec.h>
+#include <stc/cstr.h>
+#include <stdio.h>
+
+using_csmap_strval(istr, int);
+using_cvec(istr, csmap_istr_rawvalue_t, c_no_compare);
+
+void print_elem(csmap_istr_rawvalue_t p) {
+ printf("(%d, %s) ", p.first, p.second);
+}
+
+#define using_print_collection(CX) \
+ void print_collection_##CX(CX t) { \
+ printf("%zu elements: ", CX##_size(t)); \
+ \
+ c_foreach (p, CX, t) { \
+ print_elem(CX##_value_toraw(p.ref)); \
+ } \
+ puts(""); \
+ }
+
+using_print_collection(csmap_istr);
+using_print_collection(cvec_istr);
+
+
+void findit(csmap_istr c, csmap_istr_key_t val)
+{
+ printf("Trying find() on value %d\n", val);
+ csmap_istr_value_t* result = csmap_istr_get(&c, val); // easier with get than find.
+ if (result) {
+ printf("Element found: "); print_elem(csmap_istr_value_toraw(result)); puts("");
+ } else {
+ puts("Element not found.");
+ }
+}
+
+int main()
+{
+ c_forauto (csmap_istr, m1)
+ c_forauto (cvec_istr, v)
+ {
+ c_emplace(csmap_istr, m1, { { 40, "Zr" }, { 45, "Rh" } });
+ puts("The starting map m1 is (key, value):");
+ print_collection_csmap_istr(m1);
+
+ typedef cvec_istr_value_t pair;
+ cvec_istr_emplace_back(&v, (pair){43, "Tc"});
+ cvec_istr_emplace_back(&v, (pair){41, "Nb"});
+ cvec_istr_emplace_back(&v, (pair){46, "Pd"});
+ cvec_istr_emplace_back(&v, (pair){42, "Mo"});
+ cvec_istr_emplace_back(&v, (pair){44, "Ru"});
+ cvec_istr_emplace_back(&v, (pair){44, "Ru"}); // attempt a duplicate
+
+ puts("Inserting the following vector data into m1:");
+ print_collection_cvec_istr(v);
+
+ c_foreach (i, cvec_istr, v) csmap_istr_emplace(&m1, i.ref->first, i.ref->second);
+
+ puts("The modified map m1 is (key, value):");
+ print_collection_csmap_istr(m1);
+ puts("");
+ findit(m1, 45);
+ findit(m1, 6);
+ }
+}
diff --git a/examples/cstr_match.c b/examples/cstr_match.c
index 72bdd2d2..a4dbe8d8 100644
--- a/examples/cstr_match.c
+++ b/examples/cstr_match.c
@@ -6,7 +6,7 @@ int main()
c_forvar (cstr ss = cstr_lit("The quick brown fox jumps over the lazy dog.JPG"), cstr_del(&ss)) {
size_t pos = cstr_find_n(ss, "brown", 0, 5);
printf("%zu [%s]\n", pos, pos == cstr_npos ? "<NULL>" : &ss.str[pos]);
- printf("iequals: %d\n", cstr_iequals(ss, "the quick brown fox jumps over the lazy dog.jpg"));
+ printf("iequals: %d\n", cstr_iequalto(ss, "the quick brown fox jumps over the lazy dog.jpg"));
printf("icontains: %d\n", cstr_icontains(ss, "uMPS Ove"));
printf("istarts_with: %d\n", cstr_istarts_with(ss, "The Quick Brown"));
printf("iends_with: %d\n", cstr_iends_with(ss, ".Jpg"));
diff --git a/examples/ex_gauss1.c b/examples/ex_gauss1.c
index 9eb5982d..d089de89 100644
--- a/examples/ex_gauss1.c
+++ b/examples/ex_gauss1.c
@@ -6,8 +6,8 @@
#include <stc/cmap.h>
#include <stc/cvec.h>
-// Declare int -> int hashmap. Uses typetag 'i' for ints.
-using_cmap(i, int, size_t);
+// Declare int -> int hashmap. Uses typetag 'ii' for ints.
+using_cmap(ii, int32_t, size_t);
// Declare int vector with map entries that can be sorted by map keys.
typedef struct {int first; size_t second;} mapval;
@@ -15,7 +15,7 @@ static int compare(mapval *a, mapval *b) {
return c_default_compare(&a->first, &b->first);
}
-using_cvec(e, mapval, compare);
+using_cvec(pair, mapval, compare);
int main()
{
@@ -30,23 +30,23 @@ int main()
stc64_normalf_t dist = stc64_normalf_init(Mean, StdDev);
// Create and init histogram vec and map with defered destructors:
- c_forvar (cvec_e vhist = cvec_e_init(), cvec_e_del(&vhist))
- c_forvar (cmap_i mhist = cmap_i_init(), cmap_i_del(&mhist))
+ c_forauto (cvec_pair, histvec)
+ c_forauto (cmap_ii, histmap)
{
c_forrange (N) {
int index = (int) round( stc64_normalf(&rng, &dist) );
- cmap_i_emplace(&mhist, index, 0).ref->second += 1;
+ cmap_ii_emplace(&histmap, index, 0).ref->second += 1;
}
// Transfer map to vec and sort it by map keys.
- c_foreach (i, cmap_i, mhist)
- cvec_e_push_back(&vhist, c_make(mapval){i.ref->first, i.ref->second});
+ c_foreach (i, cmap_ii, histmap)
+ cvec_pair_push_back(&histvec, (mapval){i.ref->first, i.ref->second});
- cvec_e_sort(&vhist);
+ cvec_pair_sort(&histvec);
// Print the gaussian bar chart
- c_forvar (cstr bar = cstr_null, cstr_del(&bar))
- c_foreach (i, cvec_e, vhist) {
+ c_forauto (cstr, bar)
+ c_foreach (i, cvec_pair, histvec) {
size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N);
if (n > 0) {
cstr_resize(&bar, n, '*');
diff --git a/examples/mmap.c b/examples/mmap.c
index 04e501aa..9c92b19b 100644
--- a/examples/mmap.c
+++ b/examples/mmap.c
@@ -7,26 +7,26 @@
// Map of int => clist_str. Note the negation of c_default_compare
using_clist_str();
-using_csmap(m, int, clist_str, -c_default_compare, clist_str_del);
+using_csmap(mult, int, clist_str, -c_default_compare, clist_str_del);
-void print(const csmap_m mmap)
+void print(const csmap_mult mmap)
{
- c_foreach (e, csmap_m, mmap) {
+ c_foreach (e, csmap_mult, mmap) {
c_foreach (s, clist_str, e.ref->second)
printf("{%d,%s} ", e.ref->first, s.ref->str);
}
puts("");
}
-void insert(csmap_m* mmap, int key, const char* str)
+void insert(csmap_mult* mmap, int key, const char* str)
{
- clist_str *list = &csmap_m_insert(mmap, key, clist_str_init()).ref->second;
+ clist_str *list = &csmap_mult_insert(mmap, key, clist_str_init()).ref->second;
clist_str_emplace_back(list, str);
}
int main()
{
- c_forvar (csmap_m mmap = csmap_m_init(), csmap_m_del(&mmap))
+ c_forauto (csmap_mult, mmap)
{
// list-initialize
struct {int i; const char* s;} vals[] = {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}};
@@ -49,13 +49,13 @@ int main()
print(mmap);
// erase all entries with key 5
- csmap_m_erase(&mmap, 5);
+ csmap_mult_erase(&mmap, 5);
print(mmap);
// find and erase a specific entry
clist_str_iter_t pos;
- c_foreach (e, csmap_m, mmap)
- if ((pos = clist_str_find(&e.ref->second, "bar")).ref) {
+ c_foreach (e, csmap_mult, mmap)
+ if ((pos = clist_str_find(&e.ref->second, "bar")).ref != clist_str_end(&e.ref->second).ref) {
clist_str_erase(&e.ref->second, pos);
break;
}
diff --git a/examples/priority.c b/examples/priority.c
index ee64bf42..4824a294 100644
--- a/examples/priority.c
+++ b/examples/priority.c
@@ -13,7 +13,7 @@ int main() {
size_t N = 10000000;
stc64_t rng = stc64_init(time(NULL));
stc64_uniform_t dist = stc64_uniform_init(0, N * 10);
- c_forvar (cpque_i heap = cpque_i_init(), cpque_i_del(&heap))
+ c_forauto (cpque_i, heap)
{
// Push ten million random numbers to priority queue
c_forrange (N)
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index cdfb1e61..e901f499 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -53,9 +53,11 @@
/* Macro overloading feature support based on: https://rextester.com/ONP80107 */
#define c_MACRO_OVERLOAD(name, ...) \
- c_SELECT(name, c_NUM_ARGS(__VA_ARGS__))(__VA_ARGS__)
-#define c_SELECT(name, num) c_CONCAT(name ## _, num)
+ c_PASTE3(name, _, c_NUM_ARGS(__VA_ARGS__))(__VA_ARGS__)
#define c_CONCAT(a, b) a ## b
+#define c_CONCAT3(a, b, c) a ## b ## c
+#define c_PASTE(a, b) c_CONCAT(a, b)
+#define c_PASTE3(a, b, c) c_CONCAT3(a, b, c)
#define c_EXPAND(...) __VA_ARGS__
#define c_NUM_ARGS(...) _c_APPLY_ARG_N((__VA_ARGS__, _c_RSEQ_N))
@@ -99,7 +101,7 @@
#define c_rawstr_compare(x, y) strcmp(*(x), *(y))
#define c_rawstr_equals(x, y) (strcmp(*(x), *(y)) == 0)
-#define c_rawstr_hash(p, none) c_default_hash(*(p), strlen(*(p)))
+#define c_rawstr_hash(p, ...) c_default_hash(*(p), strlen(*(p)))
#define c_no_clone(x) (assert(!"c_no_clone() called"), x)
#define c_default_fromraw(x) (x)
@@ -133,13 +135,13 @@
#define c_forscope(start, end) for (int _c_ii = (start, 0); !_c_ii; ++_c_ii, end)
#define c_fordefer(...) for (int _c_ii = 0; !_c_ii; ++_c_ii, __VA_ARGS__)
-#define c_forvar_initdel(...) c_MACRO_OVERLOAD(c_forvar_initdel, __VA_ARGS__)
-#define c_forvar_initdel_2(CX, a) \
+#define c_forauto(...) c_MACRO_OVERLOAD(c_forauto, __VA_ARGS__)
+#define c_forauto_2(CX, a) \
c_forvar(CX a = CX##_init(), CX##_del(&a))
-#define c_forvar_initdel_3(CX, a, b) \
+#define c_forauto_3(CX, a, b) \
c_forvar(c_EXPAND(CX a = CX##_init(), b = CX##_init()), \
CX##_del(&b), CX##_del(&a))
-#define c_forvar_initdel_4(CX, a, b, c) \
+#define c_forauto_4(CX, a, b, c) \
c_forvar(c_EXPAND(CX a = CX##_init(), b = CX##_init(), c = CX##_init()), \
CX##_del(&c), CX##_del(&b), CX##_del(&a))
diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h
index 49de8662..d052e9eb 100644
--- a/include/stc/cdeq.h
+++ b/include/stc/cdeq.h
@@ -45,7 +45,7 @@
_c_using_cdeq(cdeq_##X, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue, defTypes)
#define using_cdeq_str() \
- using_cdeq_7(str, cstr, c_rawstr_compare, cstr_del, cstr_from, cstr_toraw, const char*)
+ using_cdeq_7(str, cstr, c_rawstr_compare, cstr_del, cstr_from, cstr_str, const char*)
struct cdeq_rep { size_t size, cap; void* base[]; };
diff --git a/include/stc/clist.h b/include/stc/clist.h
index 4166fffe..75364a4f 100644
--- a/include/stc/clist.h
+++ b/include/stc/clist.h
@@ -74,7 +74,7 @@
_c_using_clist(clist_##X, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue, defTypes)
#define using_clist_str() \
- using_clist_7(str, cstr, c_rawstr_compare, cstr_del, cstr_from, cstr_toraw, const char*)
+ using_clist_7(str, cstr, c_rawstr_compare, cstr_del, cstr_from, cstr_str, const char*)
#define _c_clist_types(CX, Value) \
diff --git a/include/stc/cmap.h b/include/stc/cmap.h
index 89975374..476558c7 100644
--- a/include/stc/cmap.h
+++ b/include/stc/cmap.h
@@ -67,14 +67,14 @@ int main(void) {
#define using_cmap_10(X, Key, Mapped, keyEquals, keyHash, \
mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes) \
using_cmap_14(X, Key, Mapped, keyEquals, keyHash, \
- mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes, \
- c_default_del, c_default_fromraw, c_default_toraw, Key)
+ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \
+ c_default_del, c_default_fromraw, c_default_toraw, Key, defTypes)
#define using_cmap_14(X, Key, Mapped, keyEqualsRaw, keyHashRaw, \
- mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes, \
- keyDel, keyFromRaw, keyToRaw, RawKey) \
+ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \
+ keyDel, keyFromRaw, keyToRaw, RawKey, defTypes) \
_c_using_chash(cmap_##X, cmap_, Key, Mapped, keyEqualsRaw, keyHashRaw, \
- mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes, \
- keyDel, keyFromRaw, keyToRaw, RawKey)
+ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \
+ keyDel, keyFromRaw, keyToRaw, RawKey, defTypes)
#define using_cmap_keydef(...) c_MACRO_OVERLOAD(using_cmap_keydef, __VA_ARGS__)
@@ -85,13 +85,13 @@ int main(void) {
#define using_cmap_keydef_10(X, Key, Mapped, keyEqualsRaw, keyHashRaw, \
keyDel, keyFromRaw, keyToRaw, RawKey, defTypes) \
_c_using_chash(cmap_##X, cmap_, Key, Mapped, keyEqualsRaw, keyHashRaw, \
- c_default_del, c_default_fromraw, c_default_toraw, Mapped, defTypes, \
- keyDel, keyFromRaw, keyToRaw, RawKey)
+ c_default_del, c_default_fromraw, c_default_toraw, Mapped, \
+ keyDel, keyFromRaw, keyToRaw, RawKey, defTypes)
#define using_cmap_str() \
_c_using_chash(cmap_str, cmap_, cstr, cstr, c_rawstr_equals, c_rawstr_hash, \
- cstr_del, cstr_from, cstr_toraw, const char*, c_true, \
- cstr_del, cstr_from, cstr_toraw, const char*)
+ cstr_del, cstr_from, cstr_str, const char*, \
+ cstr_del, cstr_from, cstr_str, const char*, c_true)
#define using_cmap_strkey(...) c_MACRO_OVERLOAD(using_cmap_strkey, __VA_ARGS__)
@@ -106,8 +106,8 @@ int main(void) {
_c_using_chash_strkey(X, cmap_, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes)
#define _c_using_chash_strkey(X, C, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes) \
_c_using_chash(C##X, C, cstr, Mapped, c_rawstr_equals, c_rawstr_hash, \
- mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes, \
- cstr_del, cstr_from, cstr_toraw, const char*)
+ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \
+ cstr_del, cstr_from, cstr_str, const char*, defTypes)
#define using_cmap_strval(...) c_MACRO_OVERLOAD(using_cmap_strval, __VA_ARGS__)
@@ -122,8 +122,8 @@ int main(void) {
using_cmap_strval_9(X, Key, keyEquals, keyHash, keyDel, keyClone, c_default_toraw, Key, c_true)
#define using_cmap_strval_9(X, Key, keyEqualsRaw, keyHashRaw, keyDel, keyFromRaw, keyToRaw, RawKey, defTypes) \
_c_using_chash(cmap_##X, cmap_, Key, cstr, keyEqualsRaw, keyHashRaw, \
- cstr_del, cstr_from, cstr_toraw, const char*, defTypes, \
- keyDel, keyFromRaw, keyToRaw, RawKey)
+ cstr_del, cstr_from, cstr_str, const char*, \
+ keyDel, keyFromRaw, keyToRaw, RawKey, defTypes)
#define SET_ONLY_cmap_(...)
#define MAP_ONLY_cmap_(...) __VA_ARGS__
@@ -169,8 +169,8 @@ STC_INLINE uint64_t c_default_hash64(const void* data, size_t ignored)
} CX
#define _c_using_chash(CX, C, Key, Mapped, keyEqualsRaw, keyHashRaw, \
- mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes, \
- keyDel, keyFromRaw, keyToRaw, RawKey) \
+ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \
+ keyDel, keyFromRaw, keyToRaw, RawKey, defTypes) \
defTypes( _c_chash_types(CX, C, Key, Mapped); ) \
\
MAP_ONLY_##C( struct CX##_value_t { \
@@ -368,7 +368,7 @@ STC_INLINE size_t fastrange_uint64_t(uint64_t x, uint64_t n) \
CX##_bucket_(const CX* self, const CX##_rawkey_t* rkeyptr) { \
const uint64_t _hash = keyHashRaw(rkeyptr, sizeof *rkeyptr); \
uint_fast8_t _hx; size_t _cap = self->bucket_count; \
- chash_bucket_t b = {c_SELECT(fastrange,CMAP_SIZE_T)(_hash, _cap), (uint_fast8_t)(_hash | 0x80)}; \
+ chash_bucket_t b = {c_PASTE(fastrange_,CMAP_SIZE_T)(_hash, _cap), (uint_fast8_t)(_hash | 0x80)}; \
const uint8_t* _hashx = self->_hashx; \
while ((_hx = _hashx[b.idx])) { \
if (_hx == b.hx) { \
@@ -444,7 +444,7 @@ STC_INLINE size_t fastrange_uint64_t(uint64_t x, uint64_t n) \
if (! _hashx[j]) \
break; \
CX##_rawkey_t _raw = keyToRaw(KEY_REF_##C(_slot+j)); \
- k = c_SELECT(fastrange,CMAP_SIZE_T)(keyHashRaw(&_raw, sizeof _raw), _cap); \
+ k = c_PASTE(fastrange_,CMAP_SIZE_T)(keyHashRaw(&_raw, sizeof _raw), _cap); \
if ((j < i) ^ (k <= i) ^ (k > j)) /* is k outside (i, j]? */ \
memcpy((void *) &_slot[i], &_slot[j], sizeof *_slot), _hashx[i] = _hashx[j], i = j; \
} \
diff --git a/include/stc/cset.h b/include/stc/cset.h
index 071a7cbc..6979311a 100644
--- a/include/stc/cset.h
+++ b/include/stc/cset.h
@@ -57,11 +57,11 @@ int main(void) {
using_cset_9(X, Key, keyEquals, keyHash, keyDel, keyClone, c_default_toraw, Key, c_true)
#define using_cset_9(X, Key, keyEqualsRaw, keyHashRaw, keyDel, keyFromRaw, keyToRaw, RawKey, defTypes) \
_c_using_chash(cset_##X, cset_, Key, Key, keyEqualsRaw, keyHashRaw, \
- @@, @@, @@, void, defTypes, keyDel, keyFromRaw, keyToRaw, RawKey)
+ @@, @@, @@, void, keyDel, keyFromRaw, keyToRaw, RawKey, defTypes)
-/* cset_str: */
#define using_cset_str() \
- _c_using_chash_strkey(str, cset_, cstr, @@, @@, @@, void, c_true)
+ using_cset_9(str, cstr, c_rawstr_equals, c_rawstr_hash, cstr_del, \
+ cstr_from, cstr_str, const char*, c_true)
#define SET_ONLY_cset_(...) __VA_ARGS__
#define MAP_ONLY_cset_(...)
diff --git a/include/stc/csmap.h b/include/stc/csmap.h
index b61af839..bf7fd6ab 100644
--- a/include/stc/csmap.h
+++ b/include/stc/csmap.h
@@ -52,6 +52,7 @@ int main(void) {
#define forward_csmap(X, Key, Mapped) _c_aatree_types(csmap_##X, csmap_, Key, Mapped)
+
#define using_csmap(...) c_MACRO_OVERLOAD(using_csmap, __VA_ARGS__)
#define using_csmap_3(X, Key, Mapped) \
@@ -64,14 +65,14 @@ int main(void) {
using_csmap_9(X, Key, Mapped, keyCompare, mappedDel, mappedClone, c_default_toraw, Mapped, c_true)
#define using_csmap_9(X, Key, Mapped, keyCompare, mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes) \
using_csmap_13(X, Key, Mapped, keyCompare, \
- mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes, \
- c_default_del, c_default_fromraw, c_default_toraw, Key)
+ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \
+ c_default_del, c_default_fromraw, c_default_toraw, Key, defTypes)
#define using_csmap_13(X, Key, Mapped, keyCompareRaw, \
- mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes, \
- keyDel, keyFromRaw, keyToRaw, RawKey) \
+ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \
+ keyDel, keyFromRaw, keyToRaw, RawKey, defTypes) \
_c_using_aatree(csmap_##X, csmap_, Key, Mapped, keyCompareRaw, \
- mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes, \
- keyDel, keyFromRaw, keyToRaw, RawKey)
+ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \
+ keyDel, keyFromRaw, keyToRaw, RawKey, defTypes)
#define using_csmap_keydef(...) c_MACRO_OVERLOAD(using_csmap_keydef, __VA_ARGS__)
@@ -82,13 +83,13 @@ int main(void) {
#define using_csmap_keydef_9(X, Key, Mapped, keyCompareRaw, \
keyDel, keyFromRaw, keyToRaw, RawKey, defTypes) \
_c_using_aatree(csmap_##X, csmap_, Key, Mapped, keyCompareRaw, \
- c_default_del, c_default_fromraw, c_default_toraw, Mapped, defTypes, \
- keyDel, keyFromRaw, keyToRaw, RawKey)
+ c_default_del, c_default_fromraw, c_default_toraw, Mapped, \
+ keyDel, keyFromRaw, keyToRaw, RawKey, defTypes)
#define using_csmap_str() \
_c_using_aatree(csmap_str, csmap_, cstr, cstr, c_rawstr_compare, \
- cstr_del, cstr_from, cstr_toraw, const char*, c_true, \
- cstr_del, cstr_from, cstr_toraw, const char*)
+ cstr_del, cstr_from, cstr_str, const char*, \
+ cstr_del, cstr_from, cstr_str, const char*, c_true)
#define using_csmap_strkey(...) c_MACRO_OVERLOAD(using_csmap_strkey, __VA_ARGS__)
@@ -103,8 +104,8 @@ int main(void) {
_c_using_aatree_strkey(X, csmap_, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes)
#define _c_using_aatree_strkey(X, C, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes) \
_c_using_aatree(C##X, C, cstr, Mapped, c_rawstr_compare, \
- mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes, \
- cstr_del, cstr_from, cstr_toraw, const char*)
+ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \
+ cstr_del, cstr_from, cstr_str, const char*, defTypes)
#define using_csmap_strval(...) c_MACRO_OVERLOAD(using_csmap_strval, __VA_ARGS__)
@@ -119,8 +120,8 @@ int main(void) {
using_csmap_strval_8(X, Key, keyCompare, keyDel, keyClone, c_default_toraw, Key, c_true)
#define using_csmap_strval_8(X, Key, keyCompareRaw, keyDel, keyFromRaw, keyToRaw, RawKey, defTypes) \
_c_using_aatree(csmap_##X, csmap_, Key, cstr, keyCompareRaw, \
- cstr_del, cstr_from, cstr_toraw, const char*, defTypes, \
- keyDel, keyFromRaw, keyToRaw, RawKey)
+ cstr_del, cstr_from, cstr_str, const char*, \
+ keyDel, keyFromRaw, keyToRaw, RawKey, defTypes)
#define SET_ONLY_csmap_(...)
#define MAP_ONLY_csmap_(...) __VA_ARGS__
@@ -159,8 +160,8 @@ struct csmap_rep { size_t root, disp, head, size, cap; void* nodes[]; };
} CX
#define _c_using_aatree(CX, C, Key, Mapped, keyCompareRaw, \
- mappedDel, mappedFromRaw, mappedToRaw, RawMapped, defTypes, \
- keyDel, keyFromRaw, keyToRaw, RawKey) \
+ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \
+ keyDel, keyFromRaw, keyToRaw, RawKey, defTypes) \
defTypes( _c_aatree_types(CX, C, Key, Mapped); ) \
\
MAP_ONLY_##C( struct CX##_value_t { \
diff --git a/include/stc/csptr.h b/include/stc/csptr.h
index c6c36fb0..4f183498 100644
--- a/include/stc/csptr.h
+++ b/include/stc/csptr.h
@@ -80,8 +80,8 @@ typedef long atomic_count_t;
using_csptr_4(X, Value, valueCompare, c_default_del)
#define using_csptr_4(X, Value, valueCompare, valueDel) \
_c_using_csptr(csptr_##X, Value, valueCompare, valueDel, c_true)
-#define using_csptr_5(X, Value, valueCompare, valueDel, defineTypes) \
- _c_using_csptr(csptr_##X, Value, valueCompare, valueDel, defineTypes)
+#define using_csptr_5(X, Value, valueCompare, valueDel, defTypes) \
+ _c_using_csptr(csptr_##X, Value, valueCompare, valueDel, defTypes)
#define _csptr_types(CX, Value) \
typedef Value CX##_value_t; \
@@ -91,8 +91,8 @@ typedef long atomic_count_t;
atomic_count_t* use_count; \
} CX
-#define _c_using_csptr(CX, Value, valueCompare, valueDel, defineTypes) \
- defineTypes( _csptr_types(CX, Value); ) \
+#define _c_using_csptr(CX, Value, valueCompare, valueDel, defTypes) \
+ defTypes( _csptr_types(CX, Value); ) \
struct CX##_rep_ {atomic_count_t cnt; CX##_value_t val;}; \
\
STC_INLINE CX \
diff --git a/include/stc/csset.h b/include/stc/csset.h
index 2056009b..747307e4 100644
--- a/include/stc/csset.h
+++ b/include/stc/csset.h
@@ -59,10 +59,11 @@ int main(void) {
using_csset_8(X, Key, keyCompare, keyDel, keyClone, c_default_toraw, Key, c_true)
#define using_csset_8(X, Key, keyCompareRaw, keyDel, keyFromRaw, keyToRaw, RawKey, defTypes) \
_c_using_aatree(csset_##X, csset_, Key, Key, keyCompareRaw, \
- @@, @@, @@, void, defTypes, keyDel, keyFromRaw, keyToRaw, RawKey)
+ @@, @@, @@, void, keyDel, keyFromRaw, keyToRaw, RawKey, defTypes)
#define using_csset_str() \
- _c_using_aatree_strkey(str, csset_, cstr, @@, @@, @@, void, c_true)
+ using_csset_8(str, cstr, c_rawstr_compare, cstr_del, \
+ cstr_from, cstr_str, const char*, c_true)
#define SET_ONLY_csset_(...) __VA_ARGS__
#define MAP_ONLY_csset_(...)
diff --git a/include/stc/cstr.h b/include/stc/cstr.h
index d5c2dd86..67f1a0a5 100644
--- a/include/stc/cstr.h
+++ b/include/stc/cstr.h
@@ -118,11 +118,11 @@ STC_INLINE cstr_iter_t cstr_begin(cstr* self)
STC_INLINE cstr_iter_t cstr_end(cstr* self)
{ return c_make(cstr_iter_t){self->str + _cstr_rep(self)->size}; }
STC_INLINE void cstr_next(cstr_iter_t* it) {++it->ref; }
-STC_INLINE bool cstr_equals(cstr s, const char* str)
+STC_INLINE bool cstr_equalto(cstr s, const char* str)
{ return strcmp(s.str, str) == 0; }
-STC_INLINE bool cstr_equals_s(cstr s1, cstr s2)
+STC_INLINE bool cstr_equalto_s(cstr s1, cstr s2)
{ return strcmp(s1.str, s2.str) == 0; }
-STC_INLINE bool cstr_iequals(cstr s, const char* str)
+STC_INLINE bool cstr_iequalto(cstr s, const char* str)
{ return c_strncasecmp(s.str, str, cstr_npos) == 0; }
STC_INLINE bool cstr_contains(cstr s, const char* needle)
{ return strstr(s.str, needle) != NULL; }
@@ -184,12 +184,11 @@ cstr_iends_with(cstr s, const char* sub) {
return n <= sz && !c_strncasecmp(s.str + sz - n, sub, n);
}
-/* cvec/cmap adaption functions: */
-#define cstr_toraw(xp) ((xp)->str)
-#define cstr_compare_ref(xp, yp) strcmp((xp)->str, (yp)->str)
-#define cstr_equals_ref(xp, yp) (strcmp((xp)->str, (yp)->str) == 0)
-#define cstr_hash_ref(xp, none) c_default_hash((xp)->str, cstr_size(*(xp)))
-#define cstr_hash(x) c_default_hash((x).str, cstr_size(x))
+/* container adaptor functions: */
+#define cstr_toraw(xp) ((xp)->str) // deprecated
+#define cstr_compare(xp, yp) strcmp((xp)->str, (yp)->str)
+#define cstr_equals(xp, yp) (strcmp((xp)->str, (yp)->str) == 0)
+#define cstr_hash(xp, ...) c_default_hash((xp)->str, cstr_size(*(xp)))
/* -------------------------- IMPLEMENTATION ------------------------- */
diff --git a/include/stc/csview.h b/include/stc/csview.h
index c631a0b7..d5d6d246 100644
--- a/include/stc/csview.h
+++ b/include/stc/csview.h
@@ -57,8 +57,7 @@ STC_INLINE char csview_back(csview sv) { return sv.str[sv.size - 1]; }
STC_INLINE void csview_clear(csview* self) { *self = csview_null; }
-#define csview_hash(sv) c_default_hash((sv).str, (sv).size)
-STC_INLINE bool csview_equals(csview sv, csview sv2)
+STC_INLINE bool csview_equalto(csview sv, csview sv2)
{ return sv.size == sv2.size && !memcmp(sv.str, sv2.str, sv.size); }
STC_INLINE size_t csview_find(csview sv, csview needle)
{ char* res = c_strnstrn(sv.str, needle.str, sv.size, needle.size);
@@ -115,10 +114,9 @@ STC_INLINE bool cstr_ends_with_v(cstr s, csview sub)
/* ---- Container helper functions ---- */
-STC_INLINE bool csview_equals_ref(const csview* a, const csview* b)
- { return a->size == b->size && !memcmp(a->str, b->str, a->size); }
-#define csview_hash_ref(xp, none) c_default_hash((xp)->str, (xp)->size)
-#define csview_compare_ref(xp, yp) strcmp((xp)->str, (yp)->str)
+#define csview_compare(xp, yp) strcmp((xp)->str, (yp)->str)
+#define csview_hash(xp, ...) c_default_hash((xp)->str, (xp)->size)
+#define csview_equals(xp, yp) (strcmp((xp)->str, (yp)->str) == 0)
/* -------------------------- IMPLEMENTATION ------------------------- */
diff --git a/include/stc/cvec.h b/include/stc/cvec.h
index 75a8008b..567c22ae 100644
--- a/include/stc/cvec.h
+++ b/include/stc/cvec.h
@@ -45,7 +45,7 @@
_c_using_cvec(cvec_##X, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue, defTypes)
#define using_cvec_str() \
- using_cvec_7(str, cstr, c_rawstr_compare, cstr_del, cstr_from, cstr_toraw, const char*)
+ using_cvec_7(str, cstr, c_rawstr_compare, cstr_del, cstr_from, cstr_str, const char*)
struct cvec_rep { size_t size, cap; void* data[]; };
#define _cvec_rep(self) c_container_of((self)->data, struct cvec_rep, data)