aboutsummaryrefslogtreecommitdiff
path: root/aberth/polygen.cpp
blob: d846e9024ef7dafa90dafcc59f93233f61d7639d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <cassert>
#include "polygen.h"
#include "util.h"

using namespace std;


namespace PolyGen::Derbyshire {
    // Returns whether we just looped around
    bool next(Poly &poly) {
        for (int i = 1; i < (int)poly.size(); i++) {
            if (poly[i] == -1) {
                poly[i] = 1;
                return false;
            }
            poly[i] = -1;
        }
        return true;
    }

    Poly atIndex(int index) {
        Poly poly;
        poly[0] = 1;
        for (int i = 1; i <= N; i++) {
            poly[i] = index & 1 ? 1 : -1;
            index >>= 1;
        }
        assert(index == 0);
        return poly;
    }

    int numPolys() {
        return 1 << N;
    }
}

namespace PolyGen::Christensen {
    // Returns whether we just looped around
    bool next(Poly &poly) {
        for (int i = 0; i < (int)poly.size(); i++) {
            if (poly[i] < kCoeffBound) {
                poly[i]++;
                return false;
            }
            poly[i] = -kCoeffBound;
        }
        return true;
    }

    Poly atIndex(int index) {
        Poly poly;
        for (int i = 0; i < (int)poly.size(); i++) {
            poly[i] = index % (2 * kCoeffBound + 1) - kCoeffBound;
            index /= 2 * kCoeffBound + 1;
        }
        assert(index == 0);
        return poly;
    }

    int numPolys() {
        return ipow(2 * kCoeffBound + 1, N + 1);
    }
}

namespace PolyGen {
    vector<Job> genJobs(int targetJobs) {
        int njobs = min(numPolys(), ceil2(targetJobs));
        int jobsize = numPolys() / njobs;

        vector<Job> jobs(njobs);
        for (int i = 0; i < njobs; i++) {
            jobs[i].init = atIndex(i * jobsize);
            jobs[i].numItems = jobsize;
        }

        return jobs;
    }
}