1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2024 Duskphantom Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0

use std::collections::{HashMap, HashSet};

use anyhow::{anyhow, Result};

pub trait GraphNode: std::hash::Hash + std::cmp::Eq + Sized + Clone {}

impl<T> GraphNode for T where T: std::hash::Hash + std::cmp::Eq + Sized + Clone {}

pub trait GraphNodeFromStr: GraphNode {
    /// Parse the node from a str
    fn from_str(input: &str) -> Result<Self>
    where
        Self: Sized;
}

impl<T> GraphNodeFromStr for T
where
    T: std::str::FromStr + GraphNode,
{
    fn from_str(input: &str) -> Result<Self> {
        input.parse().map_err(|_| anyhow!("parse error"))
    }
}

/// Undirected graph
pub struct UdGraph<T: GraphNode> {
    id_alloc: u64,
    n_id: std::collections::HashMap<T, u64>,
    id_n: std::collections::HashMap<u64, T>,
    edges: std::collections::HashMap<u64, std::collections::HashSet<u64>>,
}

impl<T: GraphNode> UdGraph<T> {
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        Self {
            id_alloc: 0,
            n_id: std::collections::HashMap::new(),
            id_n: std::collections::HashMap::new(),
            edges: std::collections::HashMap::new(),
        }
    }

    pub fn add_node(&mut self, node: T) -> u64 {
        if let Some(id) = self.n_id.get(&node) {
            return *id;
        }
        let id = self.id_alloc;
        assert!(id < u64::MAX);
        self.id_alloc += 1;
        self.n_id.insert(node.clone(), id);
        self.id_n.insert(id, node);

        self.edges.entry(id).or_default();

        id
    }

    /// if any node is not in the graph, it will be added to the graph.
    /// the self to self edge adding will be ignored
    pub fn add_edge(&mut self, from: T, to: T) {
        let from_id = self.add_node(from);
        let to_id = self.add_node(to);
        self._add_edge_by_id(from_id, to_id)
    }

    pub fn add_edge_ref(&mut self, from: &T, to: &T) {
        let from_id = self.add_node(from.clone());
        let to_id = self.add_node(to.clone());
        self._add_edge_by_id(from_id, to_id)
    }

    #[inline]
    fn _add_edge_by_id(&mut self, from_id: u64, to_id: u64) {
        if from_id == to_id {
            return;
        }
        self.edges.entry(from_id).or_default().insert(to_id);
        self.edges.entry(to_id).or_default().insert(from_id);
    }

    fn get_node(&self, id: u64) -> Option<&T> {
        self.id_n.get(&id)
    }

    pub fn nodes(&self) -> std::collections::hash_map::Values<u64, T> {
        self.id_n.values()
    }

    pub fn iter(&self) -> UdGraphIter<T> {
        UdGraphIter {
            graph: self,
            nodes_iter: self.id_n.keys(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.id_n.is_empty()
    }

    /// get neighbor nodes of the node `from`
    pub fn get_nbs<'a>(&'a self, from: &T) -> Option<Neighbors<'a, T>> {
        let id = self.n_id.get(from)?;
        let tos = self.edges.get(id)?;
        let tos_iter = tos.iter();
        Some(Neighbors {
            graph: self,
            tos,
            tos_iter,
        })
    }
}

impl<T: GraphNode> UdGraph<T> {
    pub fn gen_dot(&self, graph_name: &str, node_shower: impl Fn(&T) -> String) -> String {
        let mut res = String::new();
        res.push_str(&format!("graph {} {{\n", graph_name));

        let mut showed: HashSet<(u64, u64)> = HashSet::new();
        let mut sorted_edges: Vec<(&u64, &HashSet<u64>)> = self.edges.iter().collect();
        sorted_edges.sort_by_key(|(k, _)| **k);
        for (k, nbs) in sorted_edges {
            let from = self.get_node(*k).unwrap();
            let from_str = node_shower(from);
            let mut sorted_nbs: Vec<&u64> = nbs.iter().collect();
            sorted_nbs.sort_by_key(|&&x| x);
            for to in sorted_nbs {
                if showed.contains(&(*k, *to)) || showed.contains(&(*to, *k)) {
                    continue;
                }
                showed.insert((*k, *to));
                let to = self.get_node(*to).unwrap();
                let to_str = node_shower(to);
                res.push_str(&format!("{} -- {};\n", from_str, to_str));
            }
        }
        res.push_str("}\n");
        res
    }
}

pub struct UdGraphIter<'a, T: GraphNode> {
    graph: &'a UdGraph<T>,
    nodes_iter: std::collections::hash_map::Keys<'a, u64, T>,
}

impl<'a, T: GraphNode> Iterator for UdGraphIter<'a, T> {
    type Item = (&'a T, Neighbors<'a, T>);
    fn next(&mut self) -> Option<Self::Item> {
        self.nodes_iter.next().map(|id| {
            let node = self.graph.get_node(*id).unwrap();
            let nbs = self.graph.get_nbs(node).unwrap();
            (node, nbs)
        })
    }
}

pub struct Neighbors<'a, T: GraphNode> {
    graph: &'a UdGraph<T>,
    tos: &'a std::collections::HashSet<u64>,
    tos_iter: std::collections::hash_set::Iter<'a, u64>,
}
/// impl Iterator for ToNodes
impl<'a, T: GraphNode> Iterator for Neighbors<'a, T> {
    type Item = &'a T;
    fn next(&mut self) -> Option<Self::Item> {
        self.tos_iter
            .next()
            .map(|id| self.graph.get_node(*id).unwrap())
    }
}

impl<T: GraphNode> Neighbors<'_, T> {
    pub fn contains(&self, node: &T) -> bool {
        self.graph
            .n_id
            .get(node)
            .map_or(false, |id| self.tos.contains(id))
    }
}

impl<T: GraphNode> From<HashMap<T, HashSet<T>>> for UdGraph<T> {
    fn from(value: HashMap<T, HashSet<T>>) -> Self {
        let mut g = UdGraph::new();
        for (k, v) in value {
            for to in v {
                g.add_edge(k.clone(), to);
            }
        }
        g
    }
}
impl<T: GraphNode> From<HashSet<(T, T)>> for UdGraph<T> {
    fn from(value: HashSet<(T, T)>) -> Self {
        let mut g = UdGraph::new();
        for (k, v) in value {
            g.add_edge(k, v);
        }
        g
    }
}

impl<T: GraphNode> From<UdGraph<T>> for HashMap<T, HashSet<T>> {
    fn from(g: UdGraph<T>) -> Self {
        let mut res = HashMap::new();
        for (k, v) in g.edges.iter() {
            let k = g.get_node(*k).unwrap().clone();
            let mut vs = HashSet::new();
            for to in v {
                vs.insert(g.get_node(*to).unwrap().clone());
            }
            res.insert(k, vs);
        }
        res
    }
}

#[macro_export]
/// a macro to create a graph
/// # Example
/// ```rust
/// use duskphantom_graph::*;
/// let g: UdGraph<u32> = udgraph!(
///    {1 -> 2,3},
///   {2 -> 3}
/// ).unwrap();
/// ```
/// or
/// ```rust
/// use duskphantom_graph::*;
/// let g: UdGraph<u32> = udgraph!(u32; {1 -> 2,3}, {2 -> 3}).unwrap();
/// ```
macro_rules! udgraph {
    ($({$key:tt $sep:tt $($tos:tt),*}$(,)?)*) => {{
        let parse_graph=||->anyhow::Result<$crate::UdGraph<_>>{
            let mut g=$crate::UdGraph::new();
            $(
                $(
                    let k=$crate::GraphNodeFromStr::from_str(&stringify!($key))?;
                    let v=$crate::GraphNodeFromStr::from_str(&stringify!($tos))?;
                    g.add_edge(k,v);
                )*
            )*
            Ok(g)
        };
        parse_graph()
    }};
    ($n_ty:ty;$({$key:tt $sep:tt $($tos:tt),*}$(,)?)*) => {{
        let parse_graph=||->anyhow::Result<$crate::UdGraph<$n_ty>>{
            let mut g=$crate::UdGraph::new();
            $(
                $(
                    let k:$n_ty=$crate::GraphNodeFromStr::from_str(stringify!($key))?;
                    let v:$n_ty=$crate::GraphNodeFromStr::from_str(stringify!($tos))?;
                    g.add_edge(k,v);
                )*
            )*
            Ok(g)
        };
        parse_graph()
    }};
}

#[cfg(test)]
mod tests {

    use super::*;
    #[test]
    fn basic() {
        let mut g = UdGraph::<u32>::new();
        g.add_edge(1, 2);
        g.add_edge(1, 3);
        g.add_edge(2, 3);

        let mut ns = g.nodes().collect::<Vec<&u32>>();
        ns.sort();
        assert_eq!(ns, vec![&1, &2, &3]);

        let mut nbs: Vec<&u32> = g.get_nbs(&1).unwrap().collect();
        nbs.sort();
        assert_eq!(nbs, vec![&2, &3]);

        let mut nbs: Vec<&u32> = g.get_nbs(&2).unwrap().collect();
        nbs.sort();
        assert_eq!(nbs, vec![&1, &3]);
    }

    #[test]
    fn test_macro() {
        let g: UdGraph<u32> = udgraph!(
            {1 -> 2,3},
            {2 -> 3}
        )
        .unwrap();
        assert!(!g.is_empty());
        let hm: HashMap<u32, HashSet<u32>> = g.into();
        assert_eq!(hm.len(), 3);
        assert_eq!(hm.get(&1).unwrap().len(), 2);
        assert_eq!(hm.get(&2).unwrap().len(), 2);

        let g = udgraph!(u32; {1 -> 2,3}, {2 -> 3}).unwrap();
        assert!(!g.is_empty());
        let hm: HashMap<u32, HashSet<u32>> = g.into();
        assert_eq!(hm.len(), 3);
        assert_eq!(hm.get(&1).unwrap().len(), 2);
        assert_eq!(hm.get(&2).unwrap().len(), 2);
    }

    #[test]
    // the self to self edge adding will be ignored
    fn test_self_to_self() {
        let g: UdGraph<u32> = udgraph!({1 -> 1}).unwrap();
        assert!(!g.is_empty());
        let hm: HashMap<u32, HashSet<u32>> = g.into();
        assert_eq!(hm.len(), 1);
        assert_eq!(hm.get(&1).unwrap().len(), 0);
    }
}