Skip to main content

dugong_graphlib/graph/
edge_key.rs

1//! Edge key types.
2//!
3//! `graphlib` models edges with `v`, `w`, and an optional `name` (for multigraph support).
4
5use std::hash::{Hash, Hasher};
6
7#[derive(Clone, Copy, Hash)]
8pub(in crate::graph) struct EdgeKeyView<'a> {
9    pub(in crate::graph) v: &'a str,
10    pub(in crate::graph) w: &'a str,
11    pub(in crate::graph) name: Option<&'a str>,
12}
13
14impl<'a> hashbrown::Equivalent<EdgeKey> for EdgeKeyView<'a> {
15    fn equivalent(&self, key: &EdgeKey) -> bool {
16        key.v == self.v && key.w == self.w && key.name.as_deref() == self.name
17    }
18}
19
20#[derive(Debug, Clone)]
21pub struct EdgeKey {
22    pub v: String,
23    pub w: String,
24    pub name: Option<String>,
25}
26
27impl EdgeKey {
28    pub fn new(
29        v: impl Into<String>,
30        w: impl Into<String>,
31        name: Option<impl Into<String>>,
32    ) -> Self {
33        Self {
34            v: v.into(),
35            w: w.into(),
36            name: name.map(Into::into),
37        }
38    }
39}
40
41impl PartialEq for EdgeKey {
42    fn eq(&self, other: &Self) -> bool {
43        self.v == other.v && self.w == other.w && self.name == other.name
44    }
45}
46
47impl Eq for EdgeKey {}
48
49impl Hash for EdgeKey {
50    fn hash<H: Hasher>(&self, state: &mut H) {
51        self.v.hash(state);
52        self.w.hash(state);
53        self.name.hash(state);
54    }
55}