turbonss/deps/cmph/cxxmph/trigraph.h

50 lines
1.5 KiB
C
Raw Normal View History

#ifndef __CXXMPH_TRIGRAPH_H__
#define __CXXMPH_TRIGRAPH_H__
// Build a trigraph using a memory efficient representation.
//
// Prior knowledge of the number of edges and vertices for the graph is
// required. For each vertex, we store how many edges touch it (degree) and the
// index of the first edge in the vector of triples representing the edges.
2011-02-14 00:40:26 +02:00
#include <stdint.h> // for uint32_t and friends
2010-10-05 17:51:17 +03:00
#include <vector>
namespace cxxmph {
2010-09-10 10:07:06 +03:00
class TriGraph {
2010-10-28 03:17:09 +03:00
public:
2010-09-10 10:07:06 +03:00
struct Edge {
2010-10-05 17:51:17 +03:00
Edge() { }
2011-02-14 00:40:26 +02:00
Edge(uint32_t v0, uint32_t v1, uint32_t v2) {
2010-10-28 03:17:09 +03:00
vertices[0] = v0;
vertices[1] = v1;
vertices[2] = v2;
}
2011-02-14 00:40:26 +02:00
uint32_t& operator[](uint8_t v) { return vertices[v]; }
const uint32_t& operator[](uint8_t v) const { return vertices[v]; }
uint32_t vertices[3];
2010-09-10 10:07:06 +03:00
};
2011-02-14 00:40:26 +02:00
TriGraph(uint32_t nedges, uint32_t nvertices);
2012-04-15 06:03:00 +03:00
~TriGraph();
2010-10-05 17:51:17 +03:00
void AddEdge(const Edge& edge);
2011-02-14 00:40:26 +02:00
void RemoveEdge(uint32_t edge_id);
void ExtractEdgesAndClear(std::vector<Edge>* edges);
2010-11-09 02:02:18 +02:00
void DebugGraph() const;
const std::vector<Edge>& edges() const { return edges_; }
2011-02-14 00:40:26 +02:00
const std::vector<uint8_t>& vertex_degree() const { return vertex_degree_; }
const std::vector<uint32_t>& first_edge() const { return first_edge_; }
2010-09-10 10:07:06 +03:00
private:
2011-02-14 00:40:26 +02:00
uint32_t nedges_; // total number of edges
std::vector<Edge> edges_;
std::vector<Edge> next_edge_; // for implementing removal
2011-02-14 00:40:26 +02:00
std::vector<uint32_t> first_edge_; // the first edge for this vertex
std::vector<uint8_t> vertex_degree_; // number of edges for this vertex
2010-09-10 10:07:06 +03:00
};
} // namespace cxxmph
#endif // __CXXMPH_TRIGRAPH_H__