row learns from table

This commit is contained in:
2019-09-12 21:37:56 -04:00
parent c9a50d540d
commit f494025f6a
5 changed files with 89 additions and 40 deletions

2
.idea/cosets.iml generated
View File

@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

15
.idea/misc.xml generated
View File

@@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
<component name="CidrRootsConfiguration">
<sourceRoots>
<file path="$PROJECT_DIR$/cosets/src" />
</sourceRoots>
<libraryRoots>
<file path="$PROJECT_DIR$/vendor" />
</libraryRoots>
</component>
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

8
.idea/modules.xml generated
View File

@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/cosets.iml" filepath="$PROJECT_DIR$/.idea/cosets.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated
View File

@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@@ -1,11 +1,13 @@
#include <iostream>
#include <vector>
template<int N>
struct Row;
template<int N>
struct Table;
template<int N>
struct Row {
std::vector<int>::const_iterator l;
std::vector<int>::const_iterator r;
@@ -15,10 +17,37 @@ struct Row {
Row(const std::vector<int> &vec, int from, int to) {
this->l = vec.begin();
this->r = vec.end();
this->r = vec.end() - 1;
this->from = from;
this->to = to;
}
bool learn(Table<N> &table) {
if (r - l == 0) {
return false;
}
while (r - l > 0) {
int next = table.get(from, *l);
if (next < 0) break;
l++;
from = next;
}
while (r - l > 0) {
int next = table.rget(*r, to);
if (next < 0) break;
r--;
to = next;
}
if (r - l == 0) {
table.set(from, *l, to);
return true;
}
return false;
}
};
template<int N>
@@ -26,15 +55,52 @@ struct Table {
std::vector<std::vector<int>> fwd;
std::vector<std::vector<int>> rev;
Table() {
add_row();
}
void add_row() {
fwd.push_back(std::vector<int>(N));
fwd.push_back(std::vector<int>(N, -1));
rev.push_back(std::vector<int>(N, -1));
}
int add_coset() {
for (int from = 0; from < size(); ++from) {
for (int gen = 0; gen < N; ++gen) {
if (get(from, gen) < 0) {
int to = size();
add_row();
set(from, gen, to);
return to;
}
}
}
return 0;
}
[[nodiscard]] unsigned size() const {
return fwd.size();
}
void set(int from, int gen, int to) {
fwd[from][gen] = to;
rev[to][gen] = from;
}
[[nodiscard]] int get(int from, int gen) const {
return fwd[from][gen];
}
[[nodiscard]] int rget(int gen, int to) const {
return rev[to][gen];
}
};
std::ostream &operator<<(std::ostream &out, const Row &row) {
template<int N>
std::ostream &operator<<(std::ostream &out, const Row<N> &row) {
out << "[ " << row.from << " | ";
auto it = row.l;
while (it != row.r) {
while (row.r - it > 0) {
out << *it << " ";
it++;
}
@@ -45,7 +111,7 @@ std::ostream &operator<<(std::ostream &out, const Row &row) {
template<int N>
std::ostream &operator<<(std::ostream &out, const Table<N> &table) {
out << "[";
for (int j = 0; j < table.fwd.size(); ++j) {
for (unsigned j = 0; j < table.size(); ++j) {
auto arr = table.fwd[j];
out << "[";
for (int i = 0; i < N; ++i) {
@@ -65,19 +131,33 @@ std::ostream &operator<<(std::ostream &out, const Table<N> &table) {
}
int main(int argc, char *argv[]) {
int gens = 2;
std::vector<std::vector<int>> ids{
{0, 0},
{1, 1},
{0, 1, 0, 1, 0, 1}
};
Row<3> row(ids[2], 0, 0);
Table<3> table;
table.add_row();
table.add_row();
table.add_row();
table.add_row();
std::cout << table;
table.set(0, 0, 1);
table.set(1, 1, 2);
std::cout << table << std::endl;
std::cout << row << std::endl;
row.learn(table);
std::cout << row << std::endl;
// std::cout << table << std::endl;
// table.add_coset();
// std::cout << table << std::endl;
//
// row.learn(table);
return 0;
}
}