Rename EntityMap to ElementMap (tests not yet updated)

This commit is contained in:
2026-08-06 23:01:10 +02:00
parent 7d8c9aaa10
commit a7346bd782
9 changed files with 58 additions and 58 deletions
+14 -14
View File
@@ -33,15 +33,15 @@ use std::cmp::Reverse;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::hash::Hash; use std::hash::Hash;
use crate::maps::EntityMap; use crate::maps::ElementMap;
use crate::traits::{GraphTopology, Incidence, IncidenceCursor}; use crate::traits::{GraphTopology, Incidence, IncidenceCursor};
/// Return data type for [`dijkstra`] and [`dijkstra_unweighted`]. /// Return data type for [`dijkstra`] and [`dijkstra_unweighted`].
pub struct DijkstraResult<V: Copy> { pub struct DijkstraResult<V: Copy> {
/// Vertex map of minimum distances from a given `source` vertex. /// 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. /// 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. // TODO: Generalize the return type of the weight function.
@@ -128,7 +128,7 @@ pub fn dijkstra_distances<G, W>(
graph: &G, graph: &G,
source: G::Vertex, source: G::Vertex,
weights: W, weights: W,
) -> EntityMap<G::Vertex, Option<u32>> ) -> ElementMap<G::Vertex, Option<u32>>
where where
G: GraphTopology, G: GraphTopology,
G::Vertex: Hash, G::Vertex: Hash,
@@ -209,7 +209,7 @@ where
pub fn dijkstra_distances_unweighted<G>( pub fn dijkstra_distances_unweighted<G>(
graph: &G, graph: &G,
source: G::Vertex, source: G::Vertex,
) -> EntityMap<G::Vertex, Option<u32>> ) -> ElementMap<G::Vertex, Option<u32>>
where where
G: GraphTopology, G: GraphTopology,
G::Vertex: Hash, G::Vertex: Hash,
@@ -222,7 +222,7 @@ fn dijkstra_impl<G, W, F>(
source: G::Vertex, source: G::Vertex,
weights: W, weights: W,
mut on_relax: F, mut on_relax: F,
) -> EntityMap<G::Vertex, Option<u32>> ) -> ElementMap<G::Vertex, Option<u32>>
where where
G: GraphTopology, G: GraphTopology,
G::Vertex: Hash, G::Vertex: Hash,
@@ -255,9 +255,9 @@ where
/// Return data type for [`bfs`]. /// Return data type for [`bfs`].
pub struct BfsResult<V: Copy> { pub struct BfsResult<V: Copy> {
/// Vertex map of minimum distances from a given `source` vertex. /// 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. /// 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. /// [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 /// [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 where
G: GraphTopology, G: GraphTopology,
{ {
@@ -415,7 +415,7 @@ where
} }
struct BfsImplResult<V: Copy> { struct BfsImplResult<V: Copy> {
distances: EntityMap<V, Option<u32>>, distances: ElementMap<V, Option<u32>>,
found: Option<(V, u32)>, found: Option<(V, u32)>,
} }
@@ -455,9 +455,9 @@ where
/// Return data type for [`dfs`]. /// Return data type for [`dfs`].
pub struct DfsResult<V: Copy> { pub struct DfsResult<V: Copy> {
/// Vertex map indicating which vertices were visited during the search. /// 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. /// 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. /// [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 /// [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 where
G: GraphTopology, G: GraphTopology,
{ {
@@ -613,7 +613,7 @@ where
} }
struct DfsImplResult<V: Copy> { struct DfsImplResult<V: Copy> {
visited: EntityMap<V, bool>, visited: ElementMap<V, bool>,
found: Option<V>, found: Option<V>,
} }
+2 -2
View File
@@ -7,7 +7,7 @@
//! //!
//! * Undirected graph types [`Graph`] and [`AppendGraph`] built on a flat, index-based adjacency //! * Undirected graph types [`Graph`] and [`AppendGraph`] built on a flat, index-based adjacency
//! list, //! 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 //! * Connectivity and pathing algorithms: [Dijkstra's algorithm], [DFS], and [BFS] in different
//! variants, //! variants,
//! * Exposed [`traits`] to implement custom graph data structures or algorithms. //! * Exposed [`traits`] to implement custom graph data structures or algorithms.
@@ -118,7 +118,7 @@
//! [BFS]: crate::algorithms::bfs //! [BFS]: crate::algorithms::bfs
//! [Dijkstra's algorithm]: crate::algorithms::dijkstra //! [Dijkstra's algorithm]: crate::algorithms::dijkstra
//! [DFS]: crate::algorithms::dfs //! [DFS]: crate::algorithms::dfs
//! [`EntityMap`]: crate::maps::EntityMap //! [`ElementMap`]: crate::maps::ElementMap
//! [`AppendGraph`]: crate::models::AppendGraph //! [`AppendGraph`]: crate::models::AppendGraph
//! [`Graph`]: crate::models::Graph //! [`Graph`]: crate::models::Graph
//! [`GraphTopology`]: crate::traits::GraphTopology //! [`GraphTopology`]: crate::traits::GraphTopology
+9 -9
View File
@@ -1,30 +1,30 @@
//! Provides maps to associate custom data to graph vertices and edges. //! 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. //! vertices or all edges in a graph.
use std::ops::{Index, IndexMut}; use std::ops::{Index, IndexMut};
/// A map to associate custom data to graph vertices or edges. /// 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, /// 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. /// 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 /// 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 /// 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. /// vertices or edges, respectively.
pub struct EntityMap<E: Copy, T: Clone> { pub struct ElementMap<E: Copy, T: Clone> {
data: Vec<T>, data: Vec<T>,
default: T, default: T,
to_index: fn(E) -> usize, 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`, /// Creates a new map with the given `default` value, an index conversion function `to_index`,
/// and an initial `capacity`. /// and an initial `capacity`.
pub fn new(default: T, to_index: fn(E) -> usize, capacity: usize) -> Self { 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; type Output = T;
fn index(&self, e: E) -> &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 { fn index_mut(&mut self, e: E) -> &mut T {
let i = (self.to_index)(e); let i = (self.to_index)(e);
if i >= self.data.len() { if i >= self.data.len() {
+9 -9
View File
@@ -1,6 +1,6 @@
//! [`AppendGraph`], an undirected graph topology supporting addition only. //! [`AppendGraph`], an undirected graph topology supporting addition only.
use crate::maps::EntityMap; use crate::maps::ElementMap;
use crate::traits::{GraphTopology, GraphTopologyAddition, Incidence, IncidenceCursor}; use crate::traits::{GraphTopology, GraphTopologyAddition, Incidence, IncidenceCursor};
/// An opaque handle identifying a vertex in an [`AppendGraph`]. /// An opaque handle identifying a vertex in an [`AppendGraph`].
@@ -17,11 +17,11 @@ pub struct Vertex(usize);
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub struct Edge(usize); pub struct Edge(usize);
/// An [`EntityMap`] for [`AppendGraph`] vertices. /// An [`ElementMap`] for [`AppendGraph`] vertices.
pub type AppendGraphVertexMap<T> = EntityMap<Vertex, T>; pub type AppendGraphVertexMap<T> = ElementMap<Vertex, T>;
/// An [`EntityMap`] for [`AppendGraph`] edges. /// An [`ElementMap`] for [`AppendGraph`] edges.
pub type AppendGraphEdgeMap<T> = EntityMap<Edge, T>; pub type AppendGraphEdgeMap<T> = ElementMap<Edge, T>;
/// An [`Incidence`] for [`AppendGraph`]. /// An [`Incidence`] for [`AppendGraph`].
pub type AppendGraphIncidence = Incidence<Vertex, Edge>; pub type AppendGraphIncidence = Incidence<Vertex, Edge>;
@@ -147,16 +147,16 @@ impl GraphTopology for AppendGraph {
self.vertices.len() self.vertices.len()
} }
fn vertex_map<T: Clone>(&self, default: T) -> EntityMap<Self::Vertex, T> { fn vertex_map<T: Clone>(&self, default: T) -> ElementMap<Self::Vertex, T> {
EntityMap::new(default, |v| v.0, self.vertex_capacity()) ElementMap::new(default, |v| v.0, self.vertex_capacity())
} }
fn edge_count(&self) -> usize { fn edge_count(&self) -> usize {
self.incidences.len() / 2 self.incidences.len() / 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> {
EntityMap::new(default, |e| e.0 / 2, self.edge_capacity()) ElementMap::new(default, |e| e.0 / 2, self.edge_capacity())
} }
fn degree(&self, v: Self::Vertex) -> usize { fn degree(&self, v: Self::Vertex) -> usize {
+9 -9
View File
@@ -2,7 +2,7 @@
use typed_generational_arena::{Arena, Index}; use typed_generational_arena::{Arena, Index};
use crate::maps::EntityMap; use crate::maps::ElementMap;
use crate::traits::{ use crate::traits::{
GraphTopology, GraphTopologyAddition, GraphTopologyDeletion, Incidence, IncidenceCursor, 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`]. /// deleted. Obtain via graph methods like [`Graph::add_edge`] and [`Graph::edges`].
pub type Edge = Index<IncidenceEntry, usize, usize>; pub type Edge = Index<IncidenceEntry, usize, usize>;
/// An [`EntityMap`] for [`Graph`] vertices. /// An [`ElementMap`] for [`Graph`] vertices.
pub type GraphVertexMap<T> = EntityMap<Vertex, T>; pub type GraphVertexMap<T> = ElementMap<Vertex, T>;
/// An [`EntityMap`] for [`Graph`] edges. /// An [`ElementMap`] for [`Graph`] edges.
pub type GraphEdgeMap<T> = EntityMap<Edge, T>; pub type GraphEdgeMap<T> = ElementMap<Edge, T>;
/// An [`Incidence`] for [`Graph`]. /// An [`Incidence`] for [`Graph`].
pub type GraphIncidence = Incidence<Vertex, Edge>; pub type GraphIncidence = Incidence<Vertex, Edge>;
@@ -241,16 +241,16 @@ impl GraphTopology for Graph {
self.vertices.len() self.vertices.len()
} }
fn vertex_map<T: Clone>(&self, default: T) -> EntityMap<Self::Vertex, T> { fn vertex_map<T: Clone>(&self, default: T) -> ElementMap<Self::Vertex, T> {
EntityMap::new(default, |v| v.arr_idx(), self.vertex_capacity()) ElementMap::new(default, |v| v.arr_idx(), self.vertex_capacity())
} }
fn edge_count(&self) -> usize { fn edge_count(&self) -> usize {
self.incidences.len() / 2 self.incidences.len() / 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> {
EntityMap::new(default, |e| e.arr_idx() / 2, self.edge_capacity()) ElementMap::new(default, |e| e.arr_idx() / 2, self.edge_capacity())
} }
fn degree(&self, v: Self::Vertex) -> usize { fn degree(&self, v: Self::Vertex) -> usize {
+2 -2
View File
@@ -194,7 +194,7 @@ macro_rules! bfs_tests {
} }
fn assert_bfs_distances( fn assert_bfs_distances(
distances: &$crate::maps::EntityMap< distances: &$crate::maps::ElementMap<
<$T as $crate::traits::GraphTopology>::Vertex, <$T as $crate::traits::GraphTopology>::Vertex,
Option<u32>, Option<u32>,
>, >,
@@ -222,7 +222,7 @@ macro_rules! bfs_tests {
} }
fn assert_bfs_predecessors( fn assert_bfs_predecessors(
predecessors: &$crate::maps::EntityMap< predecessors: &$crate::maps::ElementMap<
<$T as $crate::traits::GraphTopology>::Vertex, <$T as $crate::traits::GraphTopology>::Vertex,
Option<<$T as $crate::traits::GraphTopology>::Vertex>, Option<<$T as $crate::traits::GraphTopology>::Vertex>,
>, >,
+3 -3
View File
@@ -163,7 +163,7 @@ macro_rules! dfs_tests {
} }
fn assert_dfs_visited( 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], vertices: &[<$T as $crate::traits::GraphTopology>::Vertex],
) { ) {
for i in 0..10 { for i in 0..10 {
@@ -177,8 +177,8 @@ macro_rules! dfs_tests {
fn assert_dfs_predecessors( fn assert_dfs_predecessors(
graph: &$T, graph: &$T,
visited: &$crate::maps::EntityMap<<$T as $crate::traits::GraphTopology>::Vertex, bool>, visited: &$crate::maps::ElementMap<<$T as $crate::traits::GraphTopology>::Vertex, bool>,
predecessors: &$crate::maps::EntityMap< predecessors: &$crate::maps::ElementMap<
<$T as $crate::traits::GraphTopology>::Vertex, <$T as $crate::traits::GraphTopology>::Vertex,
Option<<$T as $crate::traits::GraphTopology>::Vertex>, Option<<$T as $crate::traits::GraphTopology>::Vertex>,
>, >,
+5 -5
View File
@@ -131,7 +131,7 @@ macro_rules! dijkstra_tests {
} }
fn assert_distances_single_vertex( fn assert_distances_single_vertex(
distances: &$crate::maps::EntityMap< distances: &$crate::maps::ElementMap<
<$T as $crate::traits::GraphTopology>::Vertex, <$T as $crate::traits::GraphTopology>::Vertex,
Option<u32>, Option<u32>,
>, >,
@@ -160,7 +160,7 @@ macro_rules! dijkstra_tests {
} }
fn assert_distances_disconnected( fn assert_distances_disconnected(
distances: &$crate::maps::EntityMap< distances: &$crate::maps::ElementMap<
<$T as $crate::traits::GraphTopology>::Vertex, <$T as $crate::traits::GraphTopology>::Vertex,
Option<u32>, Option<u32>,
>, >,
@@ -204,7 +204,7 @@ macro_rules! dijkstra_tests {
} }
fn assert_distances_test_graph( fn assert_distances_test_graph(
distances: &$crate::maps::EntityMap< distances: &$crate::maps::ElementMap<
<$T as $crate::traits::GraphTopology>::Vertex, <$T as $crate::traits::GraphTopology>::Vertex,
Option<u32>, Option<u32>,
>, >,
@@ -261,7 +261,7 @@ macro_rules! dijkstra_tests {
} }
fn assert_distances_unweighted_test_graph( fn assert_distances_unweighted_test_graph(
distances: &$crate::maps::EntityMap< distances: &$crate::maps::ElementMap<
<$T as $crate::traits::GraphTopology>::Vertex, <$T as $crate::traits::GraphTopology>::Vertex,
Option<u32>, Option<u32>,
>, >,
@@ -302,7 +302,7 @@ macro_rules! dijkstra_tests {
<$T as $crate::traits::GraphTopology>::Edge, <$T as $crate::traits::GraphTopology>::Edge,
>, >,
>; 10], >; 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; use $crate::traits::GraphTopology;
let (graph, vertices, edges, incidences) = make_test_graph(); let (graph, vertices, edges, incidences) = make_test_graph();
+5 -5
View File
@@ -1,6 +1,6 @@
//! Core traits for undirected graph topologies. //! Core traits for undirected graph topologies.
use crate::maps::EntityMap; use crate::maps::ElementMap;
/// A trait representing an undirected graph topology. /// A trait representing an undirected graph topology.
/// ///
@@ -44,7 +44,7 @@ pub trait GraphTopology {
/// Returns the number of vertices in the graph. /// Returns the number of vertices in the graph.
fn vertex_count(&self) -> usize; 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`. /// `default`.
/// ///
/// # Examples /// # Examples
@@ -65,12 +65,12 @@ pub trait GraphTopology {
/// labels[v2] = "b"; /// labels[v2] = "b";
/// assert_eq!(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. /// Returns the number of edges in the graph.
fn edge_count(&self) -> usize; 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 /// # Examples
/// ///
@@ -92,7 +92,7 @@ pub trait GraphTopology {
/// weights[e2] = 2; /// weights[e2] = 2;
/// assert_eq!(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. /// Returns the degree of `v`, i.e. the number of incident edges, where each loop contributes 2.
/// ///