123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637 |
- #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>
- #include <utility>
- #ifdef __GNUC__
- #include <cxxabi.h>
- #endif
- #ifdef __GNUC__
- #define PCG_NOINLINE __attribute__((noinline))
- #else
- #define PCG_NOINLINE
- #endif
- #if __SIZEOF_INT128__
- namespace pcg_extras {
- typedef __uint128_t pcg128_t;
- }
- #define PCG_128BIT_CONSTANT(high,low) \
- ((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) \
- pcg128_t(high,low)
- #define PCG_EMULATED_128BIT_MATH 1
- #endif
- namespace pcg_extras {
- #ifndef PCG_BITCOUNT_T
- typedef uint8_t bitcount_t;
- #else
- typedef PCG_BITCOUNT_T bitcount_t;
- #endif
- 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;
- 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' + 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;
- }
- 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;
- }
- 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);
- }
- 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;
- }
- 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
- }
- #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
- #endif
-
- 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;
- while (dest_first != dest_last) {
- if ((count++ % SCALE) == 0)
- value = *src_first++;
- else
- value >>= DEST_BITS;
- *dest_first++ = dest_t(value);
- }
- return src_first;
- }
-
- 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;
- }
- 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>{});
- }
- 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);
-
-
- 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 = (uint32_t*) malloc(GEN_SIZE * FROM_ELEMS);
- generator.generate(buffer, buffer+FROM_ELEMS);
- uneven_copy(buffer, dest, dest+size);
- free(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>{});
- }
- 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;
- auto count = to - from;
- while (count > 1) {
- delta_t chosen(bounded_rand(rng, count));
- --count;
- --to;
- using std::swap;
- swap(*(from+chosen), *to);
- }
- }
- 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)...)
- {
-
- }
- 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());
- }
- };
- 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__);
- };
- 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;
- const char* pretty_name =
- abi::__cxa_demangle(implementation_typename, NULL, NULL, &status);
- if (status == 0)
- out << pretty_name;
- free((void*) pretty_name);
- if (status == 0)
- return out;
- #endif
- out << implementation_typename;
- return out;
- }
- }
- #endif
|