Refactor Graph class into a template to remove LabelGraph and WeightGraph
This commit is contained in:
@@ -15,7 +15,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <aoc/common/LabelGraph.hpp>
|
||||
#include <map>
|
||||
|
||||
#include <aoc/common/Graph.hpp>
|
||||
#include <aoc/framework/Solver-types.hpp>
|
||||
|
||||
class LanParty
|
||||
@@ -29,7 +31,8 @@ class LanParty
|
||||
private:
|
||||
static constexpr std::string getFirstTxComputerName();
|
||||
static constexpr std::string getLastTxComputerName();
|
||||
LabelGraph lan_;
|
||||
Graph<std::string, void> lan_{};
|
||||
std::map<std::string, int> labelMap_{};
|
||||
int findOrAddVertex(const std::string& vertexId);
|
||||
void computeInterconnectedThreeSetCount(const int vertexTx);
|
||||
bool canProcessVertex(const int vertexToCheck, const int vertexTx) const;
|
||||
|
||||
@@ -15,9 +15,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <aoc/common/GraphPathsResult.hpp>
|
||||
#include <functional>
|
||||
|
||||
#include <aoc/common/Dijkstra.hpp>
|
||||
#include <aoc/common/Graph.hpp>
|
||||
#include <aoc/common/Grid.hpp>
|
||||
#include <aoc/common/WeightGraph.hpp>
|
||||
#include <aoc/framework/Solver-types.hpp>
|
||||
|
||||
class RamRun
|
||||
@@ -34,11 +36,12 @@ class RamRun
|
||||
int maxBytes_;
|
||||
int nBytes_;
|
||||
Grid<int> vertexReferences_;
|
||||
GraphPathsResult dijkstraResult_;
|
||||
std::function<int(int)> edgeWeightFunctor_;
|
||||
Dijkstra::PathsResult dijkstraResult_;
|
||||
static constexpr char getUnknownVertexReference();
|
||||
static constexpr char getNoVertexReference();
|
||||
bool tryMarkCorrupted(const std::string& line, int& corruptedVertex);
|
||||
WeightGraph buildGraph();
|
||||
int getVertex(WeightGraph& graph, const size_t x, const size_t y);
|
||||
Graph<void, void> buildGraph();
|
||||
int getVertex(Graph<void, void>& graph, const size_t x, const size_t y);
|
||||
void resetVertexReferences();
|
||||
};
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
#include <set>
|
||||
#include <vector>
|
||||
|
||||
#include <aoc/common/WeightGraph.hpp>
|
||||
#include <aoc/common/Graph.hpp>
|
||||
#include <aoc/extra/ReindeerMazeCrossing.hpp>
|
||||
#include <aoc/extra/ReindeerMazePathIncidence.hpp>
|
||||
#include <aoc/framework/LinesSolver.hpp>
|
||||
@@ -39,15 +39,15 @@ class ReindeerMaze
|
||||
static constexpr char getEndChar();
|
||||
static constexpr char getWallChar();
|
||||
static constexpr int getTurnCost();
|
||||
void buildPathSegmentGraph(WeightGraph& graph, VertexAttachedPositions& vertexAttachedPositions,
|
||||
void buildPathSegmentGraph(Graph<int, int>& graph, VertexAttachedPositions& vertexAttachedPositions,
|
||||
const int entry, const int exit);
|
||||
void initializeWorkList(std::list<ReindeerMazeCrossing>& crossings, const int entryVertex);
|
||||
void addCheckedIncidence(std::vector<ReindeerMazePathIncidence>& incidences, const Point2 start,
|
||||
const Point2 direction);
|
||||
void addPathSegmentEdges(WeightGraph& graph, const ReindeerMazePathIncidence& pathIncidence,
|
||||
void addPathSegmentEdges(Graph<int, int>& graph, const ReindeerMazePathIncidence& pathIncidence,
|
||||
const std::vector<ReindeerMazePathIncidence>& otherPathIncidences);
|
||||
std::pair<int, int> makePositionsIdPair(const Point2& position1, const Point2& position2);
|
||||
int calcShortestPaths(const WeightGraph& graph, const VertexAttachedPositions& vertexAttachedPositions,
|
||||
int calcShortestPaths(const Graph<int, int>& graph, const VertexAttachedPositions& vertexAttachedPositions,
|
||||
const int entry, const int exit, const std::vector<int>& shortestDistances);
|
||||
int getNUniqueAttachedPositions(const VertexAttachedPositions& vertexAttachedPositions,
|
||||
const std::set<int>& vertices);
|
||||
|
||||
@@ -15,24 +15,29 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
#include <aoc/common/Graph.hpp>
|
||||
#include <aoc/common/GraphPathsResult.hpp>
|
||||
#include <aoc/common/GraphBase.hpp>
|
||||
|
||||
class WeightGraph
|
||||
class Dijkstra
|
||||
{
|
||||
public:
|
||||
static constexpr int getInfiniteDistance();
|
||||
int addVertex(const int weight);
|
||||
void addEdge(const int vertex1, const int vertex2, const int weight);
|
||||
int getVertexWeight(const int vertex) const;
|
||||
int getEdgeWeight(const int edge) const;
|
||||
GraphPathsResult dijkstra(const int source) const;
|
||||
Graph::NeighborIterator begin(const int vertex) const;
|
||||
Graph::NeighborIterator end() const;
|
||||
private:
|
||||
Graph graph_{};
|
||||
std::vector<int> edgeWeights_{};
|
||||
std::vector<int> vertexWeights_{};
|
||||
|
||||
class PathsResult
|
||||
{
|
||||
public:
|
||||
PathsResult() : distances{}, predecessors{}
|
||||
{
|
||||
}
|
||||
PathsResult(const size_t size, const int initialDistance, const int initialPredecessor)
|
||||
: distances(size, initialDistance), predecessors(size, initialPredecessor)
|
||||
{
|
||||
}
|
||||
std::vector<int> distances;
|
||||
std::vector<int> predecessors;
|
||||
};
|
||||
|
||||
static PathsResult run(const GraphBase& graph, const std::function<int(int)>& edgeWeightFunctor, const int source);
|
||||
};
|
||||
@@ -17,93 +17,58 @@
|
||||
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
#include <type_traits>
|
||||
|
||||
#include <aoc/common/GraphBase.hpp>
|
||||
|
||||
template <typename TVertexData = void, typename TEdgeData = void>
|
||||
class Graph
|
||||
: public GraphBase
|
||||
{
|
||||
public:
|
||||
int addVertex();
|
||||
void addEdge(const int vertex1, const int vertex2);
|
||||
size_t getNVertices() const;
|
||||
size_t getNEdges() const;
|
||||
bool areAdjacent(const int vertex1, const int vertex2) const;
|
||||
|
||||
struct Incidence
|
||||
int addVertex()
|
||||
requires(std::is_void_v<TVertexData>)
|
||||
{
|
||||
public:
|
||||
Incidence(const int vertex, const int edge)
|
||||
: vertex{ vertex }, edge{ edge }
|
||||
{
|
||||
}
|
||||
int vertex;
|
||||
int edge;
|
||||
};
|
||||
return addVertexInternal();
|
||||
}
|
||||
|
||||
struct NeighborIterator
|
||||
template <typename T = TVertexData>
|
||||
requires(!std::is_void_v<T>)
|
||||
int addVertex(const T& data)
|
||||
{
|
||||
public:
|
||||
// Iterator traits.
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using value_type = Incidence;
|
||||
using pointer = const value_type*;
|
||||
using reference = const value_type&;
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
vertexData_.push_back(data);
|
||||
return addVertexInternal();
|
||||
}
|
||||
|
||||
NeighborIterator(const Graph& graph, const int vertex)
|
||||
: graph_{ graph }, incidence_{ -1 }, value_{ -1, -1 }
|
||||
{
|
||||
if (vertex >= 0 && vertex < graph_.firstVertexIncidences_.size())
|
||||
{
|
||||
setIncidence(graph_.firstVertexIncidences_[vertex]);
|
||||
}
|
||||
};
|
||||
NeighborIterator& operator++()
|
||||
{
|
||||
if (incidence_ >= 0)
|
||||
{
|
||||
setIncidence(graph_.nextIncidences_[incidence_]);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
NeighborIterator operator++(int)
|
||||
{
|
||||
NeighborIterator it = *this;
|
||||
++(*this);
|
||||
return it;
|
||||
}
|
||||
bool operator==(NeighborIterator other) const
|
||||
{
|
||||
return incidence_ == other.incidence_;
|
||||
}
|
||||
bool operator!=(NeighborIterator other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
const reference operator*() const
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
const pointer operator->() const
|
||||
{
|
||||
return &value_;
|
||||
}
|
||||
private:
|
||||
const Graph& graph_;
|
||||
int incidence_;
|
||||
value_type value_;
|
||||
void setIncidence(const int incidence)
|
||||
{
|
||||
incidence_ = incidence;
|
||||
if (incidence_ >= 0)
|
||||
{
|
||||
value_ = { graph_.incidenceVertices_[incidence_], incidence_ >> 1 };
|
||||
}
|
||||
}
|
||||
};
|
||||
void addEdge(const int vertex1, const int vertex2)
|
||||
requires(std::is_void_v<TEdgeData>)
|
||||
{
|
||||
addEdgeInternal(vertex1, vertex2);
|
||||
}
|
||||
|
||||
template <typename T = TEdgeData>
|
||||
requires(!std::is_void_v<T>)
|
||||
void addEdge(const int vertex1, const int vertex2, const T& data)
|
||||
{
|
||||
edgeData_.push_back(data);
|
||||
addEdgeInternal(vertex1, vertex2);
|
||||
}
|
||||
|
||||
template <typename T = TVertexData>
|
||||
requires(!std::is_void_v<T>)
|
||||
const T& getVertexData(const int vertex) const
|
||||
{
|
||||
return vertexData_[vertex];
|
||||
}
|
||||
|
||||
template <typename T = TEdgeData>
|
||||
requires(!std::is_void_v<T>)
|
||||
const T& getEdgeData(int edge) const
|
||||
{
|
||||
return edgeData_[edge];
|
||||
}
|
||||
|
||||
NeighborIterator begin(const int vertex) const;
|
||||
NeighborIterator end() const;
|
||||
private:
|
||||
std::vector<int> firstVertexIncidences_{};
|
||||
std::vector<int> nextIncidences_{};
|
||||
std::vector<int> incidenceVertices_{};
|
||||
std::conditional_t<std::is_void_v<TVertexData>, char, std::vector<TVertexData>> vertexData_{};
|
||||
std::conditional_t<std::is_void_v<TEdgeData>, char, std::vector<TEdgeData>> edgeData_{};
|
||||
};
|
||||
|
||||
111
include/aoc/common/GraphBase.hpp
Normal file
111
include/aoc/common/GraphBase.hpp
Normal file
@@ -0,0 +1,111 @@
|
||||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2025 Stefan Müller
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it under
|
||||
// the terms of the GNU General Public License as published by the Free Software
|
||||
// Foundation, either version 3 of the License, or (at your option) any later
|
||||
// version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
|
||||
class GraphBase
|
||||
{
|
||||
public:
|
||||
size_t getNVertices() const;
|
||||
size_t getNEdges() const;
|
||||
bool areAdjacent(const int vertex1, const int vertex2) const;
|
||||
|
||||
struct Incidence
|
||||
{
|
||||
public:
|
||||
Incidence(const int vertex, const int edge)
|
||||
: vertex{ vertex }, edge{ edge }
|
||||
{
|
||||
}
|
||||
int vertex;
|
||||
int edge;
|
||||
};
|
||||
|
||||
struct NeighborIterator
|
||||
{
|
||||
public:
|
||||
// Iterator traits.
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using value_type = Incidence;
|
||||
using pointer = const value_type*;
|
||||
using reference = const value_type&;
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
|
||||
NeighborIterator(const GraphBase& graph, const int vertex)
|
||||
: graph_{ graph }, incidence_{ -1 }, value_{ -1, -1 }
|
||||
{
|
||||
if (vertex >= 0 && vertex < graph_.firstVertexIncidences.size())
|
||||
{
|
||||
setIncidence(graph_.firstVertexIncidences[vertex]);
|
||||
}
|
||||
};
|
||||
NeighborIterator& operator++()
|
||||
{
|
||||
if (incidence_ >= 0)
|
||||
{
|
||||
setIncidence(graph_.nextIncidences[incidence_]);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
NeighborIterator operator++(int)
|
||||
{
|
||||
NeighborIterator it = *this;
|
||||
++(*this);
|
||||
return it;
|
||||
}
|
||||
bool operator==(NeighborIterator other) const
|
||||
{
|
||||
return incidence_ == other.incidence_;
|
||||
}
|
||||
bool operator!=(NeighborIterator other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
const reference operator*() const
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
const pointer operator->() const
|
||||
{
|
||||
return &value_;
|
||||
}
|
||||
|
||||
private:
|
||||
const GraphBase& graph_;
|
||||
int incidence_;
|
||||
value_type value_;
|
||||
void setIncidence(const int incidence)
|
||||
{
|
||||
incidence_ = incidence;
|
||||
if (incidence_ >= 0)
|
||||
{
|
||||
value_ = { graph_.incidenceVertices[incidence_], incidence_ >> 1 };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
NeighborIterator begin(const int vertex) const;
|
||||
NeighborIterator end() const;
|
||||
|
||||
protected:
|
||||
std::vector<int> firstVertexIncidences{};
|
||||
std::vector<int> nextIncidences{};
|
||||
std::vector<int> incidenceVertices{};
|
||||
int addVertexInternal();
|
||||
void addEdgeInternal(const int vertex1, const int vertex2);
|
||||
};
|
||||
@@ -1,33 +0,0 @@
|
||||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2025 Stefan Müller
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it under
|
||||
// the terms of the GNU General Public License as published by the Free Software
|
||||
// Foundation, either version 3 of the License, or (at your option) any later
|
||||
// version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
class GraphPathsResult
|
||||
{
|
||||
public:
|
||||
GraphPathsResult()
|
||||
: distances{}, predecessors{}
|
||||
{
|
||||
}
|
||||
GraphPathsResult(const size_t size, const int initialDistance, const int initialPredecessor)
|
||||
: distances(size, initialDistance), predecessors(size, initialPredecessor)
|
||||
{
|
||||
}
|
||||
std::vector<int> distances;
|
||||
std::vector<int> predecessors;
|
||||
};
|
||||
@@ -1,38 +0,0 @@
|
||||
// Solutions to the Advent Of Code 2024.
|
||||
// Copyright (C) 2025 Stefan Müller
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it under
|
||||
// the terms of the GNU General Public License as published by the Free Software
|
||||
// Foundation, either version 3 of the License, or (at your option) any later
|
||||
// version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include <aoc/common/Graph.hpp>
|
||||
|
||||
class LabelGraph
|
||||
{
|
||||
public:
|
||||
int addVertex(const std::string& label);
|
||||
void addEdge(const int vertex1, const int vertex2);
|
||||
bool areAdjacent(const int vertex1, const int vertex2) const;
|
||||
const std::string& getVertexLabel(const int vertex) const;
|
||||
const std::map<std::string, int>& getLabelVertices() const;
|
||||
Graph::NeighborIterator begin(const int vertex) const;
|
||||
Graph::NeighborIterator end() const;
|
||||
private:
|
||||
Graph graph_{};
|
||||
std::vector<std::string> vertexLabels_{};
|
||||
std::map<std::string, int> labelVertices_{};
|
||||
};
|
||||
Reference in New Issue
Block a user