1
0

2 Commits

Author SHA1 Message Date
David Allemang
265de59917 Only reserve output size in path::walk 2020-10-14 17:11:39 -04:00
Jacob
16c9d7d62f Add override for Path::walk with pre-initialized container 2020-01-20 17:27:31 -05:00
3 changed files with 44 additions and 34 deletions

View File

@@ -6,13 +6,6 @@
#include <string>
namespace tc {
struct Action;
struct Path;
struct Cosets;
struct Rel;
struct Group;
struct SubGroup;
struct Action {
int from_idx = -1;
int gen = -1;
@@ -25,10 +18,9 @@ namespace tc {
};
struct Path {
const tc::Group &context;
std::vector<Action> path;
explicit Path(const tc::Group &context);
Path() = default;
Path(const Path &) = default;
@@ -37,6 +29,25 @@ namespace tc {
[[nodiscard]] Action get(int to_idx) const;
void put(int from_idx, int gen, int to_idx);
template<class C, class T, class E>
void walk(
C& res,
T start,
std::vector<E> gens,
std::function<T(const T &, const E &)> op
) const {
size_t s = size();
res.reserve(s);
res.push_back(start);
for (int i = 1; i < s; ++i) {
auto &action = path[i];
auto &from = res.get(action.from_idx);
auto &val = gens[action.gen];
res.push_back(op(from,val));
}
}
template<class T, class E>
[[nodiscard]] std::vector<T> walk(
@@ -44,14 +55,15 @@ namespace tc {
std::vector<E> gens,
std::function<T(const T &, const E &)> op
) const {
std::vector<T> res(size());
res[0] = start;
std::vector<T> res;
res.reserve(size());
res.push_back(start);
for (int i = 1; i < res.size(); ++i) {
for (int i = 1; i < size(); ++i) {
auto &action = path[i];
auto &from = res[action.from_idx];
auto &val = gens[action.gen];
res[i] = op(from, val);
res.push_back(op(from, val));
}
return res;
@@ -62,10 +74,11 @@ namespace tc {
T start,
std::function<T(const T &, const int &)> op
) const {
std::vector<T> res(size());
res[0] = start;
std::vector<T> res;
res.reserve(size());
res.push_back(start);
for (int i = 1; i < res.size(); ++i) {
for (int i = 1; i < size(); ++i) {
auto &action = path[i];
auto &from = res[action.from_idx];
auto &val = action.gen;
@@ -79,14 +92,13 @@ namespace tc {
};
struct Cosets {
const tc::Group &context;
int ngens;
std::vector<int> data;
Path path;
Cosets(const Cosets &) = default;
explicit Cosets(const tc::Group &context);
explicit Cosets(int ngens);
void add_row();
@@ -114,8 +126,10 @@ namespace tc {
[[nodiscard]] Rel shift(int off) const;
};
struct SubGroup;
struct Group {
const int ngens;
int ngens;
std::vector<std::vector<int>> _mults;
std::string name;

View File

@@ -9,10 +9,6 @@ namespace tc {
: from_idx(from_idx), gen(gen) {
}
Path::Path(const tc::Group &context)
: context(context), path() {
}
void Path::add_row() {
path.resize(path.size() + 1);
}
@@ -29,18 +25,18 @@ namespace tc {
return path.size();
}
Cosets::Cosets(const tc::Group &context)
: path(context), context(context), data() {
Cosets::Cosets(int ngens)
: ngens(ngens) {
}
void Cosets::add_row() {
data.resize(data.size() + context.ngens, -1);
data.resize(data.size() + ngens, -1);
path.add_row();
}
void Cosets::put(int coset, int gen, int target) {
data[coset * context.ngens + gen] = target;
data[target * context.ngens + gen] = coset;
data[coset * ngens + gen] = target;
data[target * ngens + gen] = coset;
if (path.get(target).from_idx == -1) {
path.put(coset, gen, target);
@@ -48,10 +44,10 @@ namespace tc {
}
void Cosets::put(int idx, int target) {
int coset = idx / context.ngens;
int gen = idx % context.ngens;
int coset = idx / ngens;
int gen = idx % ngens;
data[idx] = target;
data[target * context.ngens + gen] = coset;
data[target * ngens + gen] = coset;
if (path.get(target).from_idx == -1) {
path.put(coset, gen, target);
@@ -59,7 +55,7 @@ namespace tc {
}
int Cosets::get(int coset, int gen) const {
return data[coset * context.ngens + gen];
return data[coset * ngens + gen];
}
int Cosets::get(int idx) const {

View File

@@ -65,7 +65,7 @@ namespace tc {
};
Cosets Group::solve(const std::vector<int> &sub_gens) const {
Cosets cosets(*this);
Cosets cosets(ngens);
cosets.add_row();
if (ngens == 0) {