graph/graph.hpp
-
struct ClusterItem
-
template<typename Registry, GraphDir DirV = GraphDir::undirected>
class gviz::graph::Graph - #include <graph.hpp>
an adjancency-matrix implementation of graph using registry for id generation and attribute management, with support of clustering nodes.
node, edge, and cluster are called entity.
creating any kind of entity returns a unqiue id of type NodeId, EdgeId, ClusterId respectively for node, edge, cluster. using a removed id results in undefined behavior.
NOTE: use proxy methods to get/set/emplace/remove an entity’s attribute instead of get_raw_registry.
Public Types
-
using NodeId = entity_type
-
using EdgeId = entity_type
always convertible to entity_type
-
using ClusterId = entity_type
always convertible to entity_type
Public Functions
-
inline auto &get_raw_registry()
-
inline const auto &get_raw_registry() const
-
inline constexpr bool is_directed_graph() const
-
inline constexpr bool is_undirected_graph() const
-
template<typename Attr>
inline auto get_entity_attr(entity_type entity_id) -> utils::OptionalRef<Attr> get an
entity_identity’s attributeAttr- Returns
an optional reference to
Attrofentity_idifentity_idexists andAttris set or otherwiseutils::nulloptref.
-
template<typename Attr>
inline auto get_entity_attr(entity_type entity_id) const -> utils::OptionalRef<const Attr> get an
entity_identity’s attributeAttr- Returns
a const optional reference to
Attrofentity_idifentity_idexists andAttris set or otherwiseutils::nulloptref.
-
template<typename Attr>
inline bool has_entity_attr(entity_type entity_id) const checks if
entity_idis valid and has its attributeAttrset.- Parameters
entity_id – entity’s id.
- Returns
true if
entity_idexists andAttris set, otherwise false.
-
template<typename Attr, typename ValT>
inline auto set_entity_attr(entity_type entity_id, ValT &&value) -> utils::OptionalRef<Attr> sets an entity’s attribute if
entity_idis valid.- Parameters
entity_id – entity’s id.
value – the value to be set, it’ll be converted to
Attrexplicitly.
- Returns
utils::nulloptref if
entity_idis invalid, otherwise an optional reference to the residingAttrwhich was set in registry.
-
template<typename Attr, typename ...Args>
inline auto emplace_entity_attr(entity_type entity_id, Args&&... args) -> utils::OptionalRef<Attr> constructs an entity’s attribute in-place by given
argsifentity_idis valid.- Parameters
entity_id – entity’s id.
args – arguments to be passed to
Attrconstructor.
- Returns
utils::nulloptref if
entity_idis invalid, otherwise an optional reference to the constructedAttrwhich was placed in registry.
-
template<typename Attr>
inline bool remove_entity_attr(entity_type entity_id) unsets/removes
Attrattribute of entity ifentity_idis valid and hasAttr.- Parameters
entity_id – entity’s id.
args – arguments to be passed to
Attrconstructor.
- Returns
true if
entity_idis valid or hasAttr, otherwise false.
-
inline bool add_to_cluster(ClusterId cluster_id, NodeId node_id)
adds a node to a cluster.
if node was already in a cluster, it’ll be detached and added to given cluster.
- Parameters
cluster_id – target cluster’s id to add node to.
node_id – node’s id to add to cluster.
- Returns
true if
cluster_idandnode_idare both valid, otherwise false.
-
inline auto create_node_in(ClusterId cluster_id) -> std::optional<NodeId>
creates a node in the given cluster.
- Parameters
cluster_id – cluster’s id to create node in.
- Returns
an optional containing NodeId if
cluster_idis valid, otherwise std::nullopt.
-
inline auto create_edge(NodeId node_a_id, NodeId node_b_id) -> std::optional<EdgeId>
creates an edge between two given nodes.
if graph is directed, first node is source and second is dest.
if the same edge existed between these two nodes, the id of existing edge will be returned.
- Parameters
node_a_id – first (or source) node’s id.
node_b_id – second (or dest) node’s id.
- Returns
an optional containing EdgeId if both nodes’ id are valid, otherwise std::nullopt.
-
inline auto get_cluster_nodes(ClusterId cluster_id) const
returns an iterable view to node ids of given cluster.
- Parameters
cluster_id – target cluster’s id.
- Returns
an iterable view to cluster’s nodes. if
cluster_idis invalid, the view will be empty.
-
inline auto get_edge_id(NodeId node_a_id, NodeId node_b_id) const -> std::optional<EdgeId>
retrieves given two nodes’ edge. direction is determined by order of given arguments.
- Parameters
node_a_id – first node’s id.
node_b_id – second node’s id.
- Returns
an optional containing EdgeId if both nodes’ id are valid and an edge (desired direction) is between them, otherwise std::nullopt.
-
inline auto get_edge_nodes(EdgeId edge_id) const -> std::optional<std::pair<NodeId, NodeId>>
retrieves given edge’s pair of nodes.
- Parameters
edge_id – target edge’s id.
- Returns
an optional containing a pair of NodeId if
edge_idis valid, otherwise std::nullopt.
-
inline bool remove_cluster(ClusterId cluster_id)
detaches given cluster’s nodes from it, and then removes the cluster.
- Parameters
cluster_id – target cluster’s id.
- Returns
true if
cluster_idis valid, otherwise false.
-
inline bool remove_node(NodeId node_id)
removes a node.
- Parameters
node_id – target node’s id.
- Returns
true if
node_idis valid, otherwise false.
-
inline bool detach_clustered_node(NodeId node_id)
detaches a node from its cluster if it is attached to one.
- Parameters
node_id – target node’s id.
- Returns
true if
node_idis valid, otherwise false.
-
inline bool remove_edge(NodeId node_a_id, NodeId node_b_id)
removes an edge between given two nodes.
if directed graph, first node is source and second is dest of the edge.
- Parameters
node_a_id – first (source) node’s id.
node_b_id – second (dest) node’s id.
- Returns
true if both nodes’ id are valid and an edge exists between, otherwise false.
-
inline bool remove_edge(EdgeId edge_id)
removes an edge by given id.
- Parameters
edge_id – target edge’s id.
- Returns
true if
edge_idis valid, otherwise false.
-
inline auto get_degree(NodeId node_id, EdgeDir dir = EdgeDir::inout) const -> std::optional<std::size_t>
count of edges of given node’s
node_idin given directiondir.direction
diris irrelevant when graph is undirected.- Parameters
node_id – target node’s id.
- Returns
an optional containing size_t count number if
node_idis valid, otherwise std::nullopt.
-
inline auto get_edges_of(NodeId node_id, EdgeDir dir = EdgeDir::inout) const
returns view of edges of a given node’s
node_idin directiondir.to get 1st order neighbours of a node, combine this with get_edge_nodes.
- Parameters
node_id – target node’s id.
- Returns
a CallbackView to edges of target node, if
node_idis valid, otherwise an empty CallbackView.
-
inline auto nodes_view() const
view of all nodes in graph.
- Returns
a CallbackView to all nodes in graph.
-
inline auto edges_view() const
view of all edges in graph.
- Returns
a CallbackView to all edges in graph.
-
inline auto clusters_view() const
view of all clusters in graph.
- Returns
a CallbackView to all clusters in graph.
Private Types
-
using optional_entity_type = std::optional<entity_type>
-
using matrix_type = std::conditional_t<is_undirected, detail::DynamicSquareMatrix<optional_entity_type>, detail::DynamicHalfSquareMatrix<optional_entity_type>>
-
using map_type = std::map<entity_type, Item>
Private Static Attributes
-
static constexpr bool is_undirected = DirV == GraphDir::undirected
-
using NodeId = entity_type
-
class gviz::graph::Graph::Item
Public Functions
-
inline Item()
-
inline constexpr Item(ClusterItem cluster) noexcept
-
inline constexpr EntityTypeEnum type() const noexcept
-
inline constexpr bool is_node() const noexcept
-
inline constexpr bool is_edge() const noexcept
-
inline constexpr bool is_cluster() const noexcept
-
inline constexpr ClusterItem &as_cluster()
-
inline constexpr const ClusterItem &as_cluster() const
-
inline Item()
-
struct no_view_t
-
namespace gviz
- file graph.hpp
- #include <>#include <>#include <>#include <>#include <>#include <>#include <>#include <>#include <>#include <>#include “”#include “”#include “”#include “”
- dir /home/cthulhu/projects/repos/libgvizard/include/gvizard/graph
- dir /home/cthulhu/projects/repos/libgvizard/include/gvizard
- dir /home/cthulhu/projects/repos/libgvizard/include