From de4b5383935cb2fa5a658ac01ef1e397123fa15b Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Sat, 1 Jul 2017 17:39:56 +0300 Subject: [PATCH] Do not rely on GPL code for primes list --- gcc_primes_list.h | 65 +++++----------- jngen.h | 104 +++++++++++++------------- rnda.h | 8 +- util/generate_prime_rehash_policy.cpp | 53 +++++++++++++ 4 files changed, 132 insertions(+), 98 deletions(-) create mode 100644 util/generate_prime_rehash_policy.cpp diff --git a/gcc_primes_list.h b/gcc_primes_list.h index eaff7eb..77681ac 100644 --- a/gcc_primes_list.h +++ b/gcc_primes_list.h @@ -5,53 +5,28 @@ namespace jngen { namespace impl { -// taken from https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/ -// src/shared/hashtable-aux.cc. +// Generated with util/generate_prime_rehash_policy.cpp // Necessary for anti-unordered_set test. -extern const unsigned int primeList[] // 256 + 1 +extern const unsigned int primeList[] #ifndef JNGEN_DECLARE_ONLY -{ - 2ul, 3ul, 5ul, 7ul, 11ul, 13ul, 17ul, 19ul, 23ul, 29ul, 31ul, - 37ul, 41ul, 43ul, 47ul, 53ul, 59ul, 61ul, 67ul, 71ul, 73ul, 79ul, - 83ul, 89ul, 97ul, 103ul, 109ul, 113ul, 127ul, 137ul, 139ul, 149ul, - 157ul, 167ul, 179ul, 193ul, 199ul, 211ul, 227ul, 241ul, 257ul, - 277ul, 293ul, 313ul, 337ul, 359ul, 383ul, 409ul, 439ul, 467ul, - 503ul, 541ul, 577ul, 619ul, 661ul, 709ul, 761ul, 823ul, 887ul, - 953ul, 1031ul, 1109ul, 1193ul, 1289ul, 1381ul, 1493ul, 1613ul, - 1741ul, 1879ul, 2029ul, 2179ul, 2357ul, 2549ul, 2753ul, 2971ul, - 3209ul, 3469ul, 3739ul, 4027ul, 4349ul, 4703ul, 5087ul, 5503ul, - 5953ul, 6427ul, 6949ul, 7517ul, 8123ul, 8783ul, 9497ul, 10273ul, - 11113ul, 12011ul, 12983ul, 14033ul, 15173ul, 16411ul, 17749ul, - 19183ul, 20753ul, 22447ul, 24281ul, 26267ul, 28411ul, 30727ul, - 33223ul, 35933ul, 38873ul, 42043ul, 45481ul, 49201ul, 53201ul, - 57557ul, 62233ul, 67307ul, 72817ul, 78779ul, 85229ul, 92203ul, - 99733ul, 107897ul, 116731ul, 126271ul, 136607ul, 147793ul, - 159871ul, 172933ul, 187091ul, 202409ul, 218971ul, 236897ul, - 256279ul, 277261ul, 299951ul, 324503ul, 351061ul, 379787ul, - 410857ul, 444487ul, 480881ul, 520241ul, 562841ul, 608903ul, - 658753ul, 712697ul, 771049ul, 834181ul, 902483ul, 976369ul, - 1056323ul, 1142821ul, 1236397ul, 1337629ul, 1447153ul, 1565659ul, - 1693859ul, 1832561ul, 1982627ul, 2144977ul, 2320627ul, 2510653ul, - 2716249ul, 2938679ul, 3179303ul, 3439651ul, 3721303ul, 4026031ul, - 4355707ul, 4712381ul, 5098259ul, 5515729ul, 5967347ul, 6456007ul, - 6984629ul, 7556579ul, 8175383ul, 8844859ul, 9569143ul, 10352717ul, - 11200489ul, 12117689ul, 13109983ul, 14183539ul, 15345007ul, - 16601593ul, 17961079ul, 19431899ul, 21023161ul, 22744717ul, - 24607243ul, 26622317ul, 28802401ul, 31160981ul, 33712729ul, - 36473443ul, 39460231ul, 42691603ul, 46187573ul, 49969847ul, - 54061849ul, 58488943ul, 63278561ul, 68460391ul, 74066549ul, - 80131819ul, 86693767ul, 93793069ul, 101473717ul, 109783337ul, - 118773397ul, 128499677ul, 139022417ul, 150406843ul, 162723577ul, - 176048909ul, 190465427ul, 206062531ul, 222936881ul, 241193053ul, - 260944219ul, 282312799ul, 305431229ul, 330442829ul, 357502601ul, - 386778277ul, 418451333ul, 452718089ul, 489790921ul, 529899637ul, - 573292817ul, 620239453ul, 671030513ul, 725980837ul, 785430967ul, - 849749479ul, 919334987ul, 994618837ul, 1076067617ul, 1164186217ul, - 1259520799ul, 1362662261ul, 1474249943ul, 1594975441ul, 1725587117ul, - 1866894511ul, 2019773507ul, 2185171673ul, 2364114217ul, 2557710269ul, - 2767159799ul, 2993761039ul, 3238918481ul, 3504151727ul, 3791104843ul, - 4101556399ul, 4294967291ul, - 4294967291ul += { +2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, +71, 73, 79, 83, 89, 97, 103, 109, 113, 127, 137, 139, 149, 157, 167, 179, +193, 199, 211, 227, 241, 257, 277, 293, 313, 337, 359, 383, 409, 439, +467, 503, 541, 577, 619, 661, 709, 761, 823, 887, 953, 1031, 1109, 1193, +1289, 1381, 1493, 1613, 1741, 1879, 2029, 2179, 2357, 2549, 2753, 2971, +3209, 3469, 3739, 4027, 4349, 4703, 5087, 5503, 5953, 6427, 6949, 7517, +8123, 8783, 9497, 10273, 11113, 12011, 12983, 14033, 15173, 16411, 17749, +19183, 20753, 22447, 24281, 26267, 28411, 30727, 33223, 35933, 38873, +42043, 45481, 49201, 53201, 57557, 62233, 67307, 72817, 78779, 85229, +92203, 99733, 107897, 116731, 126271, 136607, 147793, 159871, 172933, +187091, 202409, 218971, 236897, 256279, 277261, 299951, 324503, 351061, +379787, 410857, 444487, 480881, 520241, 562841, 608903, 658753, 712697, +771049, 834181, 902483, 976369, 1056323, 1142821, 1236397, 1337629, 1447153, +1565659, 1693859, 1832561, 1982627, 2144977, 2320627, 2510653, 2716249, +2938679, 3179303, 3439651, 3721303, 4026031, 4355707, 4712381, 5098259, +5515729, 5967347, 6456007, 6984629, 7556579, 8175383, 8844859, 9569143, +10352717, 11200489 } #endif // JNGEN_DECLARE_ONLY ; diff --git a/jngen.h b/jngen.h index 54cd0d5..9b6eeff 100644 --- a/jngen.h +++ b/jngen.h @@ -552,6 +552,25 @@ class Drawer { Point(double x, double y) : x(x), y(y) {} }; + template + Point extractCoords(const T& pt) { + return Point(pt.x, pt.y); + } + + template + Point extractCoords(const std::pair& pt) { + return Point(pt.first, pt.second); + } + + template + Point extractCoords(const std::initializer_list& pt) { + static_assert( + pt.size() == 2, + "Only initializer list of size 2 can be a point"); + return Point(pt[0], pt[1]); + } + + typedef std::function DrawRequest; typedef std::pair Bbox; @@ -580,7 +599,8 @@ class Drawer { }; template -void Drawer::point(const P& p) { +void Drawer::point(const P& p_) { + Point p = extractCoords(p_); bbox_ = unite(bbox_ , bbox(Point(p.x, p.y))); requests_.push_back([p](DrawingEngine* engine) { engine->drawPoint(p.x, p.y); @@ -588,7 +608,8 @@ void Drawer::point(const P& p) { } template -void Drawer::circle(const P& p, double radius) { +void Drawer::circle(const P& p_, double radius) { + Point p = extractCoords(p_); bbox_ = unite(bbox_ , bbox({Point(p.x, p.y), radius})); requests_.push_back([p, radius](DrawingEngine* engine) { engine->drawCircle(p.x, p.y, radius); @@ -596,7 +617,9 @@ void Drawer::circle(const P& p, double radius) { } template -void Drawer::segment(const P& p1, const P& p2) { +void Drawer::segment(const P& p1_, const P& p2_) { + Point p1 = extractCoords(p1_); + Point p2 = extractCoords(p2_); bbox_ = unite(bbox_ , bbox(Point(p1.x, p1.y))); bbox_ = unite(bbox_ , bbox(Point(p2.x, p2.y))); requests_.push_back([p1, p2](DrawingEngine* engine) { @@ -607,7 +630,7 @@ void Drawer::segment(const P& p1, const P& p2) { template void Drawer::polygon(const std::vector

& points) { for (const auto& p: points) { - bbox_ = unite(bbox_, bbox(Point(p.x, p.y))); + bbox_ = unite(bbox_, bbox(extractCoords(p))); } requests_.push_back([points](DrawingEngine* engine) { @@ -995,53 +1018,28 @@ void Dsu::extend(size_t x) { namespace jngen { namespace impl { -// taken from https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/ -// src/shared/hashtable-aux.cc. +// Generated with util/generate_prime_rehash_policy.cpp // Necessary for anti-unordered_set test. -extern const unsigned int primeList[] // 256 + 1 +extern const unsigned int primeList[] #ifndef JNGEN_DECLARE_ONLY -{ - 2ul, 3ul, 5ul, 7ul, 11ul, 13ul, 17ul, 19ul, 23ul, 29ul, 31ul, - 37ul, 41ul, 43ul, 47ul, 53ul, 59ul, 61ul, 67ul, 71ul, 73ul, 79ul, - 83ul, 89ul, 97ul, 103ul, 109ul, 113ul, 127ul, 137ul, 139ul, 149ul, - 157ul, 167ul, 179ul, 193ul, 199ul, 211ul, 227ul, 241ul, 257ul, - 277ul, 293ul, 313ul, 337ul, 359ul, 383ul, 409ul, 439ul, 467ul, - 503ul, 541ul, 577ul, 619ul, 661ul, 709ul, 761ul, 823ul, 887ul, - 953ul, 1031ul, 1109ul, 1193ul, 1289ul, 1381ul, 1493ul, 1613ul, - 1741ul, 1879ul, 2029ul, 2179ul, 2357ul, 2549ul, 2753ul, 2971ul, - 3209ul, 3469ul, 3739ul, 4027ul, 4349ul, 4703ul, 5087ul, 5503ul, - 5953ul, 6427ul, 6949ul, 7517ul, 8123ul, 8783ul, 9497ul, 10273ul, - 11113ul, 12011ul, 12983ul, 14033ul, 15173ul, 16411ul, 17749ul, - 19183ul, 20753ul, 22447ul, 24281ul, 26267ul, 28411ul, 30727ul, - 33223ul, 35933ul, 38873ul, 42043ul, 45481ul, 49201ul, 53201ul, - 57557ul, 62233ul, 67307ul, 72817ul, 78779ul, 85229ul, 92203ul, - 99733ul, 107897ul, 116731ul, 126271ul, 136607ul, 147793ul, - 159871ul, 172933ul, 187091ul, 202409ul, 218971ul, 236897ul, - 256279ul, 277261ul, 299951ul, 324503ul, 351061ul, 379787ul, - 410857ul, 444487ul, 480881ul, 520241ul, 562841ul, 608903ul, - 658753ul, 712697ul, 771049ul, 834181ul, 902483ul, 976369ul, - 1056323ul, 1142821ul, 1236397ul, 1337629ul, 1447153ul, 1565659ul, - 1693859ul, 1832561ul, 1982627ul, 2144977ul, 2320627ul, 2510653ul, - 2716249ul, 2938679ul, 3179303ul, 3439651ul, 3721303ul, 4026031ul, - 4355707ul, 4712381ul, 5098259ul, 5515729ul, 5967347ul, 6456007ul, - 6984629ul, 7556579ul, 8175383ul, 8844859ul, 9569143ul, 10352717ul, - 11200489ul, 12117689ul, 13109983ul, 14183539ul, 15345007ul, - 16601593ul, 17961079ul, 19431899ul, 21023161ul, 22744717ul, - 24607243ul, 26622317ul, 28802401ul, 31160981ul, 33712729ul, - 36473443ul, 39460231ul, 42691603ul, 46187573ul, 49969847ul, - 54061849ul, 58488943ul, 63278561ul, 68460391ul, 74066549ul, - 80131819ul, 86693767ul, 93793069ul, 101473717ul, 109783337ul, - 118773397ul, 128499677ul, 139022417ul, 150406843ul, 162723577ul, - 176048909ul, 190465427ul, 206062531ul, 222936881ul, 241193053ul, - 260944219ul, 282312799ul, 305431229ul, 330442829ul, 357502601ul, - 386778277ul, 418451333ul, 452718089ul, 489790921ul, 529899637ul, - 573292817ul, 620239453ul, 671030513ul, 725980837ul, 785430967ul, - 849749479ul, 919334987ul, 994618837ul, 1076067617ul, 1164186217ul, - 1259520799ul, 1362662261ul, 1474249943ul, 1594975441ul, 1725587117ul, - 1866894511ul, 2019773507ul, 2185171673ul, 2364114217ul, 2557710269ul, - 2767159799ul, 2993761039ul, 3238918481ul, 3504151727ul, 3791104843ul, - 4101556399ul, 4294967291ul, - 4294967291ul += { +2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, +71, 73, 79, 83, 89, 97, 103, 109, 113, 127, 137, 139, 149, 157, 167, 179, +193, 199, 211, 227, 241, 257, 277, 293, 313, 337, 359, 383, 409, 439, +467, 503, 541, 577, 619, 661, 709, 761, 823, 887, 953, 1031, 1109, 1193, +1289, 1381, 1493, 1613, 1741, 1879, 2029, 2179, 2357, 2549, 2753, 2971, +3209, 3469, 3739, 4027, 4349, 4703, 5087, 5503, 5953, 6427, 6949, 7517, +8123, 8783, 9497, 10273, 11113, 12011, 12983, 14033, 15173, 16411, 17749, +19183, 20753, 22447, 24281, 26267, 28411, 30727, 33223, 35933, 38873, +42043, 45481, 49201, 53201, 57557, 62233, 67307, 72817, 78779, 85229, +92203, 99733, 107897, 116731, 126271, 136607, 147793, 159871, 172933, +187091, 202409, 218971, 236897, 256279, 277261, 299951, 324503, 351061, +379787, 410857, 444487, 480881, 520241, 562841, 608903, 658753, 712697, +771049, 834181, 902483, 976369, 1056323, 1142821, 1236397, 1337629, 1447153, +1565659, 1693859, 1832561, 1982627, 2144977, 2320627, 2510653, 2716249, +2938679, 3179303, 3439651, 3721303, 4026031, 4355707, 4712381, 5098259, +5515729, 5967347, 6456007, 6984629, 7556579, 8175383, 8844859, 9569143, +10352717, 11200489 } #endif // JNGEN_DECLARE_ONLY ; @@ -3323,12 +3321,16 @@ Array64 ArrayRandom::antiUnorderedSet( bool reserve, UnorderedSetCompiler compiler) { - int buckets; - ensure( compiler == UnorderedSetCompiler::Gcc4, "unordered set antitest supported only for gcc-4.x yet"); + ensure( + n <= 1000000, + "unordered set antitest supported only for n <= 1e7"); + + int buckets; + if (reserve) { buckets = nextPrime(std::ceil(n / maxLoadFactor), compiler); } else { diff --git a/rnda.h b/rnda.h index ac800bb..fbad369 100644 --- a/rnda.h +++ b/rnda.h @@ -77,12 +77,16 @@ Array64 ArrayRandom::antiUnorderedSet( bool reserve, UnorderedSetCompiler compiler) { - int buckets; - ensure( compiler == UnorderedSetCompiler::Gcc4, "unordered set antitest supported only for gcc-4.x yet"); + ensure( + n <= 1000000, + "unordered set antitest supported only for n <= 1e7"); + + int buckets; + if (reserve) { buckets = nextPrime(std::ceil(n / maxLoadFactor), compiler); } else { diff --git a/util/generate_prime_rehash_policy.cpp b/util/generate_prime_rehash_policy.cpp new file mode 100644 index 0000000..38612a9 --- /dev/null +++ b/util/generate_prime_rehash_policy.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +#include +using namespace std; + +int f(int n) { + unordered_set a; + a.reserve(n); + return a.bucket_count(); +} + +int main() { + const int UPPER_LIMIT = 1e7; + vector buckets; + + int n = 1; + int b = f(n); + buckets.push_back(b); + + while (n < UPPER_LIMIT) { + int step = 1; + while (f(n + step) == b) { + step *= 2; + } + step /= 2; + while (step > 0) { + if (f(n + step) == b) { + n += step; + } + step /= 2; + } + ++n; + b = f(n); + buckets.push_back(b); + cerr << n << endl; + } + string res = "{\n"; + ostringstream ss; + for (int i = 0; i < (int)buckets.size(); ++i) { + if (ss.str().size() >= 70) { + res += ss.str() + "\n"; + ss.str(""); + ss.clear(); + } + ss << buckets[i]; + if (i + 1 != (int)buckets.size()) { + ss << ", "; + } + } + res += ss.str() + "\n};\n"; + cout << res << endl; +}