meritrank/
node.rs

1use crate::MeritRankError;
2
3// use uuid::Uuid;
4
5// use crate::error::MeritRankError;
6// use crate::graph::{MyGraph, MyDiGraph};
7// use crate::node::{NodeId, Weight, Node};
8// use crate::edge::EdgeId;
9// use crate::random_walk::RandomWalk;
10// use crate::counter::{Counter, CounterIterator};
11// use crate::ranking::{PosWalk};
12// use crate::walk::WalkIdGenerator;
13// use crate::walk_storage::WalkStorage;
14// use crate::constants::{ASSERT, OPTIMIZE_INVALIDATION};
15// use crate::common::{sign};
16
17#[derive(Hash, PartialOrd, Ord, Clone, Copy, PartialEq, Eq)]
18pub enum NodeId {
19    Int(i32),
20    UInt(usize),
21    None,
22}
23
24impl Default for NodeId {
25    fn default() -> Self {
26        NodeId::UInt(0)
27    }
28}
29
30impl NodeId {
31    /// Checks if the NodeId is equal to None.
32    pub fn is_none(&self) -> bool {
33        // matches!(self, NodeId::None)
34        match self {
35            NodeId::None => true,
36            _ => false,
37        }
38    }
39
40    /// Checks if the NodeId is not equal to None and not equal to the default value (0).
41    pub fn is_some(&self) -> bool {
42        // check if NodeId is not None and not default (== 0)
43        // !self.is_none() && *self != NodeId::default()
44        match self {
45            NodeId::None => false,
46            NodeId::Int(id) => *id != 0,
47            NodeId::UInt(id) => *id != 0,
48        }
49    }
50}
51
52/// Represents a node in the MeritRank graph.
53#[derive(Debug, Hash, Default, PartialOrd, Ord, Clone, PartialEq, Eq)]
54pub struct Node {
55    id: NodeId,
56}
57
58impl Node {
59    /// Creates a new Node with the specified id.
60    pub fn new(id: NodeId) -> Self {
61        Node { id }
62    }
63
64    /// Returns the id of the node.
65    pub fn get_id(&self) -> NodeId {
66        self.id
67    }
68}
69
70impl<T> From<T> for Node
71where
72    T: Into<NodeId>,
73{
74    /// Converts the value into a Node using the provided id.
75    fn from(id: T) -> Self {
76        Node { id: id.into() }
77    }
78}
79
80impl From<i32> for NodeId {
81    /// Converts the i32 value into a NodeId::Int variant.
82    fn from(id: i32) -> Self {
83        NodeId::Int(id)
84    }
85}
86
87impl From<usize> for NodeId {
88    /// Converts the usize value into a NodeId::UInt variant.
89    fn from(id: usize) -> Self {
90        NodeId::UInt(id)
91    }
92}
93
94/// The weight type used in the MeritRank graph.
95pub type Weight = f64;
96
97impl From<NodeId> for Weight {
98    /// Converts the NodeId into a Weight.
99    fn from(id: NodeId) -> Self {
100        match id {
101            NodeId::Int(id) => id as f64,
102            NodeId::UInt(id) => id as f64,
103            _ => 0.0,
104        }
105    }
106}
107
108impl From<Weight> for NodeId {
109    /// Converts the Weight into a NodeId.
110    fn from(id: Weight) -> Self {
111        match id {
112            id if id < 0.0 => NodeId::Int(id as i32),
113            id if id >= 0.0 => NodeId::UInt(id as usize),
114            _ => NodeId::default(),
115        }
116    }
117}
118
119use std::str::FromStr;
120
121impl FromStr for NodeId {
122    type Err = MeritRankError;
123
124    /// Parses a NodeId from a string.
125    fn from_str(s: &str) -> Result<Self, Self::Err> {
126        match s.parse::<i32>() {
127            Ok(id) => Ok(NodeId::Int(id)),
128            Err(_) => match s.parse::<usize>() {
129                Ok(id) => Ok(NodeId::UInt(id)),
130                Err(_) => Err(MeritRankError::NodeIdParseError),
131            },
132        }
133    }
134}