Rename EntityMap to ElementMap (tests not yet updated)
This commit is contained in:
+14
-14
@@ -33,15 +33,15 @@ use std::cmp::Reverse;
|
||||
use std::collections::VecDeque;
|
||||
use std::hash::Hash;
|
||||
|
||||
use crate::maps::EntityMap;
|
||||
use crate::maps::ElementMap;
|
||||
use crate::traits::{GraphTopology, Incidence, IncidenceCursor};
|
||||
|
||||
/// Return data type for [`dijkstra`] and [`dijkstra_unweighted`].
|
||||
pub struct DijkstraResult<V: Copy> {
|
||||
/// Vertex map of minimum distances from a given `source` vertex.
|
||||
pub distances: EntityMap<V, Option<u32>>,
|
||||
pub distances: ElementMap<V, Option<u32>>,
|
||||
/// Vertex map of predecessors on some shortest path from a given `source` vertex.
|
||||
pub predecessors: EntityMap<V, Option<V>>,
|
||||
pub predecessors: ElementMap<V, Option<V>>,
|
||||
}
|
||||
|
||||
// TODO: Generalize the return type of the weight function.
|
||||
@@ -128,7 +128,7 @@ pub fn dijkstra_distances<G, W>(
|
||||
graph: &G,
|
||||
source: G::Vertex,
|
||||
weights: W,
|
||||
) -> EntityMap<G::Vertex, Option<u32>>
|
||||
) -> ElementMap<G::Vertex, Option<u32>>
|
||||
where
|
||||
G: GraphTopology,
|
||||
G::Vertex: Hash,
|
||||
@@ -209,7 +209,7 @@ where
|
||||
pub fn dijkstra_distances_unweighted<G>(
|
||||
graph: &G,
|
||||
source: G::Vertex,
|
||||
) -> EntityMap<G::Vertex, Option<u32>>
|
||||
) -> ElementMap<G::Vertex, Option<u32>>
|
||||
where
|
||||
G: GraphTopology,
|
||||
G::Vertex: Hash,
|
||||
@@ -222,7 +222,7 @@ fn dijkstra_impl<G, W, F>(
|
||||
source: G::Vertex,
|
||||
weights: W,
|
||||
mut on_relax: F,
|
||||
) -> EntityMap<G::Vertex, Option<u32>>
|
||||
) -> ElementMap<G::Vertex, Option<u32>>
|
||||
where
|
||||
G: GraphTopology,
|
||||
G::Vertex: Hash,
|
||||
@@ -255,9 +255,9 @@ where
|
||||
/// Return data type for [`bfs`].
|
||||
pub struct BfsResult<V: Copy> {
|
||||
/// Vertex map of minimum distances from a given `source` vertex.
|
||||
pub distances: EntityMap<V, Option<u32>>,
|
||||
pub distances: ElementMap<V, Option<u32>>,
|
||||
/// Vertex map of predecessors on some shortest path from a given `source` vertex.
|
||||
pub predecessors: EntityMap<V, Option<V>>,
|
||||
pub predecessors: ElementMap<V, Option<V>>,
|
||||
}
|
||||
|
||||
/// [Breadth-first search] traversal from `source`, returns distances and predecessors.
|
||||
@@ -333,7 +333,7 @@ where
|
||||
/// ```
|
||||
///
|
||||
/// [Breadth-first search]: https://en.wikipedia.org/wiki/Breadth-first_search
|
||||
pub fn bfs_distances<G>(graph: &G, source: G::Vertex) -> EntityMap<G::Vertex, Option<u32>>
|
||||
pub fn bfs_distances<G>(graph: &G, source: G::Vertex) -> ElementMap<G::Vertex, Option<u32>>
|
||||
where
|
||||
G: GraphTopology,
|
||||
{
|
||||
@@ -415,7 +415,7 @@ where
|
||||
}
|
||||
|
||||
struct BfsImplResult<V: Copy> {
|
||||
distances: EntityMap<V, Option<u32>>,
|
||||
distances: ElementMap<V, Option<u32>>,
|
||||
found: Option<(V, u32)>,
|
||||
}
|
||||
|
||||
@@ -455,9 +455,9 @@ where
|
||||
/// Return data type for [`dfs`].
|
||||
pub struct DfsResult<V: Copy> {
|
||||
/// Vertex map indicating which vertices were visited during the search.
|
||||
pub visited: EntityMap<V, bool>,
|
||||
pub visited: ElementMap<V, bool>,
|
||||
/// Vertex map of predecessors on the DFS tree path from a given `source` vertex.
|
||||
pub predecessors: EntityMap<V, Option<V>>,
|
||||
pub predecessors: ElementMap<V, Option<V>>,
|
||||
}
|
||||
|
||||
/// [Depth-first search] traversal from `source`, returns visited vertices and predecessors.
|
||||
@@ -533,7 +533,7 @@ where
|
||||
/// ```
|
||||
///
|
||||
/// [Depth-first search]: https://en.wikipedia.org/wiki/Depth-first_search
|
||||
pub fn dfs_visited<G>(graph: &G, source: G::Vertex) -> EntityMap<G::Vertex, bool>
|
||||
pub fn dfs_visited<G>(graph: &G, source: G::Vertex) -> ElementMap<G::Vertex, bool>
|
||||
where
|
||||
G: GraphTopology,
|
||||
{
|
||||
@@ -613,7 +613,7 @@ where
|
||||
}
|
||||
|
||||
struct DfsImplResult<V: Copy> {
|
||||
visited: EntityMap<V, bool>,
|
||||
visited: ElementMap<V, bool>,
|
||||
found: Option<V>,
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -7,7 +7,7 @@
|
||||
//!
|
||||
//! * Undirected graph types [`Graph`] and [`AppendGraph`] built on a flat, index-based adjacency
|
||||
//! list,
|
||||
//! * [`EntityMap`] to freely associate custom data with vertices and edges,
|
||||
//! * [`ElementMap`] to freely associate custom data with vertices and edges,
|
||||
//! * Connectivity and pathing algorithms: [Dijkstra's algorithm], [DFS], and [BFS] in different
|
||||
//! variants,
|
||||
//! * Exposed [`traits`] to implement custom graph data structures or algorithms.
|
||||
@@ -118,7 +118,7 @@
|
||||
//! [BFS]: crate::algorithms::bfs
|
||||
//! [Dijkstra's algorithm]: crate::algorithms::dijkstra
|
||||
//! [DFS]: crate::algorithms::dfs
|
||||
//! [`EntityMap`]: crate::maps::EntityMap
|
||||
//! [`ElementMap`]: crate::maps::ElementMap
|
||||
//! [`AppendGraph`]: crate::models::AppendGraph
|
||||
//! [`Graph`]: crate::models::Graph
|
||||
//! [`GraphTopology`]: crate::traits::GraphTopology
|
||||
|
||||
+9
-9
@@ -1,30 +1,30 @@
|
||||
//! Provides maps to associate custom data to graph vertices and edges.
|
||||
//!
|
||||
//! [`EntityMap`] provides a copy-on-write, [`Vec`]-backed map to associate data to either all
|
||||
//! [`ElementMap`] provides a copy-on-write, [`Vec`]-backed map to associate data to either all
|
||||
//! vertices or all edges in a graph.
|
||||
|
||||
use std::ops::{Index, IndexMut};
|
||||
|
||||
/// A map to associate custom data to graph vertices or edges.
|
||||
///
|
||||
/// This map uses raw entity indices to associate homogenous custom data of type `T` to graph
|
||||
/// This map uses raw element indices to associate homogenous custom data of type `T` to graph
|
||||
/// vertices or edges. The implementation uses a [`Vec`], allocating contiguous slots for the data,
|
||||
/// which means that the provided index conversion function should map entities to contiguous
|
||||
/// which means that the provided index conversion function should map elements to contiguous
|
||||
/// indices, or indices with relatively few gaps.
|
||||
///
|
||||
/// Data allocation happens as copy-on-write, i.e. the backing [`Vec`] is only resized if a value is
|
||||
/// written beyond current capacity, but the `default` value can transparently be read. No bound or
|
||||
/// validity checks are performed by the map on the provided entity handles.
|
||||
/// validity checks are performed by the map on the provided element handles.
|
||||
///
|
||||
/// Use [`GraphTopology::vertex_map`] or [`GraphTopology::edge_map`] to obtain an `EntityMap` for
|
||||
/// Use [`GraphTopology::vertex_map`] or [`GraphTopology::edge_map`] to obtain an `ElementMap` for
|
||||
/// vertices or edges, respectively.
|
||||
pub struct EntityMap<E: Copy, T: Clone> {
|
||||
pub struct ElementMap<E: Copy, T: Clone> {
|
||||
data: Vec<T>,
|
||||
default: T,
|
||||
to_index: fn(E) -> usize,
|
||||
}
|
||||
|
||||
impl<E: Copy, T: Clone> EntityMap<E, T> {
|
||||
impl<E: Copy, T: Clone> ElementMap<E, T> {
|
||||
/// Creates a new map with the given `default` value, an index conversion function `to_index`,
|
||||
/// and an initial `capacity`.
|
||||
pub fn new(default: T, to_index: fn(E) -> usize, capacity: usize) -> Self {
|
||||
@@ -53,7 +53,7 @@ impl<E: Copy, T: Clone> EntityMap<E, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Copy, T: Clone> Index<E> for EntityMap<E, T> {
|
||||
impl<E: Copy, T: Clone> Index<E> for ElementMap<E, T> {
|
||||
type Output = T;
|
||||
|
||||
fn index(&self, e: E) -> &T {
|
||||
@@ -66,7 +66,7 @@ impl<E: Copy, T: Clone> Index<E> for EntityMap<E, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Copy, T: Clone> IndexMut<E> for EntityMap<E, T> {
|
||||
impl<E: Copy, T: Clone> IndexMut<E> for ElementMap<E, T> {
|
||||
fn index_mut(&mut self, e: E) -> &mut T {
|
||||
let i = (self.to_index)(e);
|
||||
if i >= self.data.len() {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! [`AppendGraph`], an undirected graph topology supporting addition only.
|
||||
|
||||
use crate::maps::EntityMap;
|
||||
use crate::maps::ElementMap;
|
||||
use crate::traits::{GraphTopology, GraphTopologyAddition, Incidence, IncidenceCursor};
|
||||
|
||||
/// An opaque handle identifying a vertex in an [`AppendGraph`].
|
||||
@@ -17,11 +17,11 @@ pub struct Vertex(usize);
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
|
||||
pub struct Edge(usize);
|
||||
|
||||
/// An [`EntityMap`] for [`AppendGraph`] vertices.
|
||||
pub type AppendGraphVertexMap<T> = EntityMap<Vertex, T>;
|
||||
/// An [`ElementMap`] for [`AppendGraph`] vertices.
|
||||
pub type AppendGraphVertexMap<T> = ElementMap<Vertex, T>;
|
||||
|
||||
/// An [`EntityMap`] for [`AppendGraph`] edges.
|
||||
pub type AppendGraphEdgeMap<T> = EntityMap<Edge, T>;
|
||||
/// An [`ElementMap`] for [`AppendGraph`] edges.
|
||||
pub type AppendGraphEdgeMap<T> = ElementMap<Edge, T>;
|
||||
|
||||
/// An [`Incidence`] for [`AppendGraph`].
|
||||
pub type AppendGraphIncidence = Incidence<Vertex, Edge>;
|
||||
@@ -147,16 +147,16 @@ impl GraphTopology for AppendGraph {
|
||||
self.vertices.len()
|
||||
}
|
||||
|
||||
fn vertex_map<T: Clone>(&self, default: T) -> EntityMap<Self::Vertex, T> {
|
||||
EntityMap::new(default, |v| v.0, self.vertex_capacity())
|
||||
fn vertex_map<T: Clone>(&self, default: T) -> ElementMap<Self::Vertex, T> {
|
||||
ElementMap::new(default, |v| v.0, self.vertex_capacity())
|
||||
}
|
||||
|
||||
fn edge_count(&self) -> usize {
|
||||
self.incidences.len() / 2
|
||||
}
|
||||
|
||||
fn edge_map<T: Clone>(&self, default: T) -> EntityMap<Self::Edge, T> {
|
||||
EntityMap::new(default, |e| e.0 / 2, self.edge_capacity())
|
||||
fn edge_map<T: Clone>(&self, default: T) -> ElementMap<Self::Edge, T> {
|
||||
ElementMap::new(default, |e| e.0 / 2, self.edge_capacity())
|
||||
}
|
||||
|
||||
fn degree(&self, v: Self::Vertex) -> usize {
|
||||
|
||||
+9
-9
@@ -2,7 +2,7 @@
|
||||
|
||||
use typed_generational_arena::{Arena, Index};
|
||||
|
||||
use crate::maps::EntityMap;
|
||||
use crate::maps::ElementMap;
|
||||
use crate::traits::{
|
||||
GraphTopology, GraphTopologyAddition, GraphTopologyDeletion, Incidence, IncidenceCursor,
|
||||
};
|
||||
@@ -19,11 +19,11 @@ pub type Vertex = Index<VertexIncidenceHeader, usize, usize>;
|
||||
/// deleted. Obtain via graph methods like [`Graph::add_edge`] and [`Graph::edges`].
|
||||
pub type Edge = Index<IncidenceEntry, usize, usize>;
|
||||
|
||||
/// An [`EntityMap`] for [`Graph`] vertices.
|
||||
pub type GraphVertexMap<T> = EntityMap<Vertex, T>;
|
||||
/// An [`ElementMap`] for [`Graph`] vertices.
|
||||
pub type GraphVertexMap<T> = ElementMap<Vertex, T>;
|
||||
|
||||
/// An [`EntityMap`] for [`Graph`] edges.
|
||||
pub type GraphEdgeMap<T> = EntityMap<Edge, T>;
|
||||
/// An [`ElementMap`] for [`Graph`] edges.
|
||||
pub type GraphEdgeMap<T> = ElementMap<Edge, T>;
|
||||
|
||||
/// An [`Incidence`] for [`Graph`].
|
||||
pub type GraphIncidence = Incidence<Vertex, Edge>;
|
||||
@@ -241,16 +241,16 @@ impl GraphTopology for Graph {
|
||||
self.vertices.len()
|
||||
}
|
||||
|
||||
fn vertex_map<T: Clone>(&self, default: T) -> EntityMap<Self::Vertex, T> {
|
||||
EntityMap::new(default, |v| v.arr_idx(), self.vertex_capacity())
|
||||
fn vertex_map<T: Clone>(&self, default: T) -> ElementMap<Self::Vertex, T> {
|
||||
ElementMap::new(default, |v| v.arr_idx(), self.vertex_capacity())
|
||||
}
|
||||
|
||||
fn edge_count(&self) -> usize {
|
||||
self.incidences.len() / 2
|
||||
}
|
||||
|
||||
fn edge_map<T: Clone>(&self, default: T) -> EntityMap<Self::Edge, T> {
|
||||
EntityMap::new(default, |e| e.arr_idx() / 2, self.edge_capacity())
|
||||
fn edge_map<T: Clone>(&self, default: T) -> ElementMap<Self::Edge, T> {
|
||||
ElementMap::new(default, |e| e.arr_idx() / 2, self.edge_capacity())
|
||||
}
|
||||
|
||||
fn degree(&self, v: Self::Vertex) -> usize {
|
||||
|
||||
@@ -194,7 +194,7 @@ macro_rules! bfs_tests {
|
||||
}
|
||||
|
||||
fn assert_bfs_distances(
|
||||
distances: &$crate::maps::EntityMap<
|
||||
distances: &$crate::maps::ElementMap<
|
||||
<$T as $crate::traits::GraphTopology>::Vertex,
|
||||
Option<u32>,
|
||||
>,
|
||||
@@ -222,7 +222,7 @@ macro_rules! bfs_tests {
|
||||
}
|
||||
|
||||
fn assert_bfs_predecessors(
|
||||
predecessors: &$crate::maps::EntityMap<
|
||||
predecessors: &$crate::maps::ElementMap<
|
||||
<$T as $crate::traits::GraphTopology>::Vertex,
|
||||
Option<<$T as $crate::traits::GraphTopology>::Vertex>,
|
||||
>,
|
||||
|
||||
@@ -163,7 +163,7 @@ macro_rules! dfs_tests {
|
||||
}
|
||||
|
||||
fn assert_dfs_visited(
|
||||
visited: &$crate::maps::EntityMap<<$T as $crate::traits::GraphTopology>::Vertex, bool>,
|
||||
visited: &$crate::maps::ElementMap<<$T as $crate::traits::GraphTopology>::Vertex, bool>,
|
||||
vertices: &[<$T as $crate::traits::GraphTopology>::Vertex],
|
||||
) {
|
||||
for i in 0..10 {
|
||||
@@ -177,8 +177,8 @@ macro_rules! dfs_tests {
|
||||
|
||||
fn assert_dfs_predecessors(
|
||||
graph: &$T,
|
||||
visited: &$crate::maps::EntityMap<<$T as $crate::traits::GraphTopology>::Vertex, bool>,
|
||||
predecessors: &$crate::maps::EntityMap<
|
||||
visited: &$crate::maps::ElementMap<<$T as $crate::traits::GraphTopology>::Vertex, bool>,
|
||||
predecessors: &$crate::maps::ElementMap<
|
||||
<$T as $crate::traits::GraphTopology>::Vertex,
|
||||
Option<<$T as $crate::traits::GraphTopology>::Vertex>,
|
||||
>,
|
||||
|
||||
@@ -131,7 +131,7 @@ macro_rules! dijkstra_tests {
|
||||
}
|
||||
|
||||
fn assert_distances_single_vertex(
|
||||
distances: &$crate::maps::EntityMap<
|
||||
distances: &$crate::maps::ElementMap<
|
||||
<$T as $crate::traits::GraphTopology>::Vertex,
|
||||
Option<u32>,
|
||||
>,
|
||||
@@ -160,7 +160,7 @@ macro_rules! dijkstra_tests {
|
||||
}
|
||||
|
||||
fn assert_distances_disconnected(
|
||||
distances: &$crate::maps::EntityMap<
|
||||
distances: &$crate::maps::ElementMap<
|
||||
<$T as $crate::traits::GraphTopology>::Vertex,
|
||||
Option<u32>,
|
||||
>,
|
||||
@@ -204,7 +204,7 @@ macro_rules! dijkstra_tests {
|
||||
}
|
||||
|
||||
fn assert_distances_test_graph(
|
||||
distances: &$crate::maps::EntityMap<
|
||||
distances: &$crate::maps::ElementMap<
|
||||
<$T as $crate::traits::GraphTopology>::Vertex,
|
||||
Option<u32>,
|
||||
>,
|
||||
@@ -261,7 +261,7 @@ macro_rules! dijkstra_tests {
|
||||
}
|
||||
|
||||
fn assert_distances_unweighted_test_graph(
|
||||
distances: &$crate::maps::EntityMap<
|
||||
distances: &$crate::maps::ElementMap<
|
||||
<$T as $crate::traits::GraphTopology>::Vertex,
|
||||
Option<u32>,
|
||||
>,
|
||||
@@ -302,7 +302,7 @@ macro_rules! dijkstra_tests {
|
||||
<$T as $crate::traits::GraphTopology>::Edge,
|
||||
>,
|
||||
>; 10],
|
||||
$crate::maps::EntityMap<<$T as $crate::traits::GraphTopology>::Edge, u32>,
|
||||
$crate::maps::ElementMap<<$T as $crate::traits::GraphTopology>::Edge, u32>,
|
||||
) {
|
||||
use $crate::traits::GraphTopology;
|
||||
let (graph, vertices, edges, incidences) = make_test_graph();
|
||||
|
||||
+5
-5
@@ -1,6 +1,6 @@
|
||||
//! Core traits for undirected graph topologies.
|
||||
|
||||
use crate::maps::EntityMap;
|
||||
use crate::maps::ElementMap;
|
||||
|
||||
/// A trait representing an undirected graph topology.
|
||||
///
|
||||
@@ -44,7 +44,7 @@ pub trait GraphTopology {
|
||||
/// Returns the number of vertices in the graph.
|
||||
fn vertex_count(&self) -> usize;
|
||||
|
||||
/// Creates and returns an [`EntityMap`] for the vertices with every slot initialised to
|
||||
/// Creates and returns an [`ElementMap`] for the vertices with every slot initialised to
|
||||
/// `default`.
|
||||
///
|
||||
/// # Examples
|
||||
@@ -65,12 +65,12 @@ pub trait GraphTopology {
|
||||
/// labels[v2] = "b";
|
||||
/// assert_eq!(labels[v2], "b");
|
||||
/// ```
|
||||
fn vertex_map<T: Clone>(&self, default: T) -> EntityMap<Self::Vertex, T>;
|
||||
fn vertex_map<T: Clone>(&self, default: T) -> ElementMap<Self::Vertex, T>;
|
||||
|
||||
/// Returns the number of edges in the graph.
|
||||
fn edge_count(&self) -> usize;
|
||||
|
||||
/// Creates and returns an [`EntityMap`] for the edges with every slot initialised to `default`.
|
||||
/// Creates and returns an [`ElementMap`] for the edges with every slot initialised to `default`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
@@ -92,7 +92,7 @@ pub trait GraphTopology {
|
||||
/// weights[e2] = 2;
|
||||
/// assert_eq!(weights[e2], 2);
|
||||
/// ```
|
||||
fn edge_map<T: Clone>(&self, default: T) -> EntityMap<Self::Edge, T>;
|
||||
fn edge_map<T: Clone>(&self, default: T) -> ElementMap<Self::Edge, T>;
|
||||
|
||||
/// Returns the degree of `v`, i.e. the number of incident edges, where each loop contributes 2.
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user