mirror of
https://github.com/allemangD/toddcox-visualize.git
synced 2025-11-10 03:52:48 -05:00
tc::pair_map
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
add_library(tc
|
||||
include/tc/pair_map.hpp
|
||||
include/tc/core.hpp
|
||||
include/tc/cosets.hpp
|
||||
include/tc/group.hpp
|
||||
@@ -9,3 +10,5 @@ add_library(tc
|
||||
src/solve.cpp)
|
||||
|
||||
target_include_directories(tc PUBLIC include)
|
||||
|
||||
add_subdirectory(test)
|
||||
|
||||
453
tc/include/tc/pair_map.hpp
Normal file
453
tc/include/tc/pair_map.hpp
Normal file
@@ -0,0 +1,453 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <iterator>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
namespace tc {
|
||||
template<typename T>
|
||||
struct pair_map {
|
||||
struct iterator;
|
||||
struct const_iterator;
|
||||
struct view;
|
||||
struct const_view;
|
||||
|
||||
private:
|
||||
size_t _size;
|
||||
std::vector<T> _data{};
|
||||
|
||||
static size_t idx(size_t i, size_t j);
|
||||
|
||||
public:
|
||||
explicit pair_map(size_t size);
|
||||
|
||||
explicit pair_map(size_t size, const T &value);
|
||||
|
||||
[[nodiscard]] size_t size() const;
|
||||
|
||||
T &operator()(size_t i, size_t j);
|
||||
|
||||
T operator()(size_t i, size_t j) const;
|
||||
|
||||
view of(size_t f);
|
||||
|
||||
const_view of(size_t f) const;
|
||||
|
||||
const_view cof(size_t f);
|
||||
|
||||
iterator begin();
|
||||
|
||||
iterator end();
|
||||
|
||||
const_iterator begin() const;
|
||||
|
||||
const_iterator end() const;
|
||||
|
||||
const_iterator cbegin();
|
||||
|
||||
const_iterator cend();
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct pair_map<T>::iterator {
|
||||
using reference = std::tuple<size_t, size_t, T &>;
|
||||
|
||||
private:
|
||||
pair_map<T> &_map;
|
||||
size_t _i, _j;
|
||||
|
||||
public:
|
||||
iterator(pair_map<T> &map, size_t i, size_t j);
|
||||
|
||||
iterator operator++();
|
||||
|
||||
iterator operator++(int) &;
|
||||
|
||||
reference operator*();
|
||||
|
||||
bool operator!=(const iterator &other);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct pair_map<T>::const_iterator {
|
||||
using value_type = std::tuple<size_t, size_t, T>;
|
||||
|
||||
private:
|
||||
const pair_map<T> &_map;
|
||||
size_t _i, _j;
|
||||
|
||||
public:
|
||||
const_iterator(const pair_map<T> &map, size_t i, size_t j);
|
||||
|
||||
const_iterator operator++();
|
||||
|
||||
const_iterator operator++(int) &;
|
||||
|
||||
value_type operator*();
|
||||
|
||||
bool operator!=(const const_iterator &other);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct pair_map<T>::view {
|
||||
struct iterator;
|
||||
struct const_iterator;
|
||||
|
||||
private:
|
||||
pair_map<T> &_map;
|
||||
size_t _f;
|
||||
|
||||
public:
|
||||
view(pair_map<T> &map, size_t f);
|
||||
|
||||
iterator begin();
|
||||
|
||||
iterator end();
|
||||
|
||||
const_iterator begin() const;
|
||||
|
||||
const_iterator end() const;
|
||||
|
||||
const_iterator cbegin();
|
||||
|
||||
const_iterator cend();
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct pair_map<T>::view::iterator {
|
||||
using reference = std::tuple<size_t, size_t, T &>;
|
||||
|
||||
private:
|
||||
pair_map<T> &_map;
|
||||
size_t _f, _v;
|
||||
|
||||
public:
|
||||
iterator(pair_map<T> &map, size_t f, size_t v);
|
||||
|
||||
iterator operator++();
|
||||
|
||||
iterator operator++(int);
|
||||
|
||||
reference operator*();
|
||||
|
||||
bool operator!=(const iterator &other);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct pair_map<T>::view::const_iterator {
|
||||
using value_type = std::tuple<size_t, size_t, T>;
|
||||
|
||||
private:
|
||||
const pair_map<T> &_map;
|
||||
size_t _f, _v;
|
||||
|
||||
public:
|
||||
const_iterator(const pair_map<T> &map, size_t f, size_t v);
|
||||
|
||||
const_iterator operator++();
|
||||
|
||||
const_iterator operator++(int);
|
||||
|
||||
value_type operator*();
|
||||
|
||||
bool operator!=(const const_iterator &other);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct pair_map<T>::const_view {
|
||||
using const_iterator = typename pair_map<T>::view::const_iterator;
|
||||
|
||||
private:
|
||||
const pair_map<T> &_map;
|
||||
size_t _f;
|
||||
|
||||
public:
|
||||
const_view(const pair_map<T> &map, size_t f);
|
||||
|
||||
const_iterator begin() const;
|
||||
|
||||
const_iterator end() const;
|
||||
|
||||
const_iterator cbegin();
|
||||
|
||||
const_iterator cend();
|
||||
};
|
||||
|
||||
// region pair_map
|
||||
|
||||
template<typename T>
|
||||
size_t pair_map<T>::idx(size_t i, size_t j) {
|
||||
if (i < j) std::swap(i, j);
|
||||
return j * (j + 1) / 2 + i;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
pair_map<T>::pair_map(size_t size)
|
||||
: _size(size), _data(size * (size + 1) / 2) {}
|
||||
|
||||
template<typename T>
|
||||
pair_map<T>::pair_map(size_t size, const T &value)
|
||||
: _size(size), _data(size * (size + 1) / 2, value) {}
|
||||
|
||||
template<typename T>
|
||||
size_t pair_map<T>::size() const {
|
||||
return _size;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T &pair_map<T>::operator()(size_t i, size_t j) {
|
||||
return _data[idx(i, j)];
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T pair_map<T>::operator()(size_t i, size_t j) const {
|
||||
return _data[idx(i, j)];
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::view pair_map<T>::of(size_t f) {
|
||||
return view(*this, f);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::const_view pair_map<T>::of(size_t f) const {
|
||||
return const_view(*this, f);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::const_view pair_map<T>::cof(size_t f) {
|
||||
return const_view(*this, f);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::iterator pair_map<T>::begin() {
|
||||
return iterator(*this, 0, 0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::iterator pair_map<T>::end() {
|
||||
return iterator(*this, 0, _size);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::const_iterator pair_map<T>::begin() const {
|
||||
return const_iterator(*this, 0, 0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::const_iterator pair_map<T>::end() const {
|
||||
return const_iterator(*this, 0, _size);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::const_iterator pair_map<T>::cbegin() {
|
||||
return const_iterator(*this, 0, 0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::const_iterator pair_map<T>::cend() {
|
||||
return const_iterator(*this, 0, _size);
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region pair_map::iterator
|
||||
|
||||
template<typename T>
|
||||
pair_map<T>::iterator::iterator(pair_map<T> &map, size_t i, size_t j)
|
||||
:_map(map), _i(i), _j(j) {}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::iterator pair_map<T>::iterator::operator++() {
|
||||
if (++_i > _j) {
|
||||
_i = 0;
|
||||
++_j;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::iterator pair_map<T>::iterator::operator++(int) &{
|
||||
iterator it = *this;
|
||||
++this;
|
||||
return it;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::iterator::reference pair_map<T>::iterator::operator*() {
|
||||
return std::tie(_i, _j, _map(_i, _j));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool pair_map<T>::iterator::operator!=(const pair_map::iterator &other) {
|
||||
return &_map != &other._map || _i != other._i || _j != other._j;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region pair_map::const_iterator
|
||||
|
||||
template<typename T>
|
||||
pair_map<T>::const_iterator::const_iterator(const pair_map<T> &map, size_t i, size_t j)
|
||||
:_map(map), _i(i), _j(j) {}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::const_iterator pair_map<T>::const_iterator::operator++() {
|
||||
if (++_i > _j) {
|
||||
_i = 0;
|
||||
++_j;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::const_iterator pair_map<T>::const_iterator::operator++(int) &{
|
||||
const_iterator it = *this;
|
||||
++this;
|
||||
return it;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::const_iterator::value_type pair_map<T>::const_iterator::operator*() {
|
||||
return std::tuple(_i, _j, _map(_i, _j));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool pair_map<T>::const_iterator::operator!=(const pair_map::const_iterator &other) {
|
||||
return &_map != &other._map || _i != other._i || _j != other._j;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region pair_map::view
|
||||
|
||||
template<typename T>
|
||||
pair_map<T>::view::view(pair_map<T> &map, size_t f)
|
||||
: _map(map), _f(f) {}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::view::iterator pair_map<T>::view::begin() {
|
||||
return iterator(_map, _f, 0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::view::iterator pair_map<T>::view::end() {
|
||||
return iterator(_map, _f, _map._size);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::view::const_iterator pair_map<T>::view::begin() const {
|
||||
return const_iterator(_map, _f, 0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::view::const_iterator pair_map<T>::view::end() const {
|
||||
return const_iterator(_map, _f, _map._size);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::view::const_iterator pair_map<T>::view::cbegin() {
|
||||
return const_iterator(_map, _f, 0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::view::const_iterator pair_map<T>::view::cend() {
|
||||
return const_iterator(_map, _f, _map._size);
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region pair_map::view::iterator
|
||||
|
||||
template<typename T>
|
||||
pair_map<T>::view::iterator::iterator(pair_map<T> &map, size_t f, size_t v)
|
||||
: _map(map), _f(f), _v(v) {}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::view::iterator pair_map<T>::view::iterator::operator++() {
|
||||
++_v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::view::iterator pair_map<T>::view::iterator::operator++(int) {
|
||||
iterator it = *this;
|
||||
++this;
|
||||
return it;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::view::iterator::reference pair_map<T>::view::iterator::operator*() {
|
||||
auto [i, j] = std::minmax(_f, _v);
|
||||
return std::tie(i, j, _map(i, j));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool pair_map<T>::view::iterator::operator!=(const pair_map::view::iterator &other) {
|
||||
return &_map != &other._map || _f != other._f || _v != other._v;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region pair_map::view::const_iterator
|
||||
|
||||
template<typename T>
|
||||
pair_map<T>::view::const_iterator::const_iterator(const pair_map<T> &map, size_t f, size_t v)
|
||||
: _map(map), _f(f), _v(v) {}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::view::const_iterator pair_map<T>::view::const_iterator::operator++() {
|
||||
++_v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::view::const_iterator pair_map<T>::view::const_iterator::operator++(int) {
|
||||
const_iterator it = *this;
|
||||
++this;
|
||||
return it;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::view::const_iterator::value_type pair_map<T>::view::const_iterator::operator*() {
|
||||
auto [i, j] = std::minmax(_f, _v);
|
||||
return std::tuple(i, j, _map(i, j));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool pair_map<T>::view::const_iterator::operator!=(const pair_map::view::const_iterator &other) {
|
||||
return &_map != &other._map || _f != other._f || _v != other._v;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region pair_map::const_view
|
||||
|
||||
template<typename T>
|
||||
pair_map<T>::const_view::const_view(const pair_map<T> &map, size_t f)
|
||||
: _map(map), _f(f) {}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::const_view::const_iterator pair_map<T>::const_view::begin() const {
|
||||
return const_iterator(_map, _f, 0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::const_view::const_iterator pair_map<T>::const_view::end() const {
|
||||
return const_iterator(_map, _f, _map._size);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::const_view::const_iterator pair_map<T>::const_view::cbegin() {
|
||||
return const_iterator(_map, _f, 0);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename pair_map<T>::const_view::const_iterator pair_map<T>::const_view::cend() {
|
||||
return const_iterator(_map, _f, _map._size);
|
||||
}
|
||||
|
||||
// endregion
|
||||
}
|
||||
1
tc/src/keep.cpp
Normal file
1
tc/src/keep.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include <tc/pair_map.hpp>
|
||||
13
tc/test/CMakeLists.txt
Normal file
13
tc/test/CMakeLists.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
add_executable(grouptests grouptest.cpp)
|
||||
target_link_libraries(grouptests PUBLIC tc)
|
||||
|
||||
add_test(NAME GroupIterateRef COMMAND grouptests iterate_ref)
|
||||
set_tests_properties(GroupIterateRef PROPERTIES TIMEOUT 30)
|
||||
add_test(NAME GroupIterateConst COMMAND grouptests iterate_const)
|
||||
set_tests_properties(GroupIterateConst PROPERTIES TIMEOUT 30)
|
||||
add_test(NAME GroupIterate COMMAND grouptests iterate)
|
||||
set_tests_properties(GroupIterate PROPERTIES TIMEOUT 30)
|
||||
add_test(NAME GroupView COMMAND grouptests view)
|
||||
set_tests_properties(GroupView PROPERTIES TIMEOUT 30)
|
||||
|
||||
add_custom_target(alltests DEPENDS grouptests)
|
||||
94
tc/test/grouptest.cpp
Normal file
94
tc/test/grouptest.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <tc/pair_map.hpp>
|
||||
|
||||
int iterate_ref() {
|
||||
tc::pair_map<size_t> pm(4, 2);
|
||||
|
||||
for (auto [i, j, m]: pm) {
|
||||
m = i + j;
|
||||
}
|
||||
|
||||
for (auto [i, j, m]: pm) {
|
||||
std::cout << "(" << i << "," << j << ") = " << m << std::endl;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int iterate_const() {
|
||||
tc::pair_map<size_t> pm(4, 2);
|
||||
for (const auto &[i, j, m]: pm) {
|
||||
m = i + j;
|
||||
}
|
||||
|
||||
const tc::pair_map<size_t> pmc = pm;
|
||||
for (auto [i, j, m]: pmc) {
|
||||
std::cout << "(" << i << "," << j << ") = " << m << std::endl;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int iterate() {
|
||||
tc::pair_map<size_t> pm(4, 2);
|
||||
|
||||
std::cout << pm(0, 0) << std::endl;
|
||||
pm(0, 0) = 3;
|
||||
std::cout << pm(0, 0) << std::endl;
|
||||
|
||||
for (auto [i, j, m]: pm) {
|
||||
std::cout << i << " " << j << " = " << m << std::endl;
|
||||
}
|
||||
|
||||
std::cout << pm(1, 0) << " " << pm(0, 1) << std::endl;
|
||||
pm(1, 0) = 7;
|
||||
std::cout << pm(1, 0) << " " << pm(0, 1) << std::endl;
|
||||
pm(0, 1) = 9;
|
||||
std::cout << pm(1, 0) << " " << pm(0, 1) << std::endl;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int view() {
|
||||
tc::pair_map<size_t> pm(4, 2);
|
||||
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
for (int j = i; j < 4; ++j) {
|
||||
pm(i, j) = i + j;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "ALL:" << std::endl;
|
||||
for (auto [i, j, m]: pm) {
|
||||
std::cout << " (" << i << "," << j << ") = " << m << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "VIEW:" << std::endl;
|
||||
for (auto [i, j, m]: pm.of(2)) {
|
||||
std::cout << " (" << i << "," << j << ") = " << m << std::endl;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
static std::unordered_map<std::string, std::function<int()>> tests = {
|
||||
{"iterate_ref", iterate_ref},
|
||||
{"iterate_const", iterate_const},
|
||||
{"iterate", iterate},
|
||||
{"view", view},
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
std::vector<std::string> args(argv + 1, argv + argc);
|
||||
|
||||
auto it = tests.find(args[0]);
|
||||
if (it == tests.end()) {
|
||||
std::cout << "Test not found" << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
auto test = it->second;
|
||||
return test();
|
||||
}
|
||||
Reference in New Issue
Block a user