raphtory_api/core/input/
input_node.rs

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
//! A data structure for representing input nodes in a temporal graph.
//!
//! Input nodes are used when first creating or accessing verticies by the user.
//! This trait allows you to use a variety of types as input nodes, including
//! `u64`, `&str`, and `String`.

use crate::core::utils::hashing;

const MAX_U64_BYTES: [u8; 20] = [
    49, 56, 52, 52, 54, 55, 52, 52, 48, 55, 51, 55, 48, 57, 53, 53, 49, 54, 49, 53,
];

pub fn parse_u64_strict(input: &str) -> Option<u64> {
    if input.len() > 20 {
        // a u64 string has at most 20 bytes
        return None;
    }
    let byte_0 = b'0';
    let byte_1 = b'1';
    let byte_9 = b'9';
    let mut input_iter = input.bytes();
    let first = input_iter.next()?;
    if first == byte_0 {
        return input_iter.next().is_none().then_some(0);
    }

    let mut check_max = input.len() == 20;
    if check_max {
        if !(byte_1..=MAX_U64_BYTES[0]).contains(&first) {
            return None;
        }
    } else if !(byte_1..=byte_9).contains(&first) {
        return None;
    }

    let mut result = (first - byte_0) as u64;
    for (next_byte, max_byte) in input_iter.zip(MAX_U64_BYTES[1..].iter().copied()) {
        if check_max {
            if !(byte_0..=max_byte).contains(&next_byte) {
                return None;
            }
            check_max = next_byte == max_byte;
        } else if !(byte_0..=byte_9).contains(&next_byte) {
            return None;
        }
        result = result * 10 + (next_byte - byte_0) as u64;
    }
    Some(result)
}

pub trait InputNode: Clone {
    fn id(&self) -> u64;
    fn id_str(&self) -> Option<&str>;
}

impl InputNode for u64 {
    fn id(&self) -> u64 {
        *self
    }

    fn id_str(&self) -> Option<&str> {
        None
    }
}

impl<'a> InputNode for &'a str {
    fn id(&self) -> u64 {
        parse_u64_strict(self).unwrap_or_else(|| hashing::calculate_hash(self))
    }

    fn id_str(&self) -> Option<&str> {
        Some(self)
    }
}

impl InputNode for String {
    fn id(&self) -> u64 {
        let s: &str = self;
        s.id()
    }

    fn id_str(&self) -> Option<&str> {
        Some(self)
    }
}

#[cfg(test)]
mod test {
    use crate::core::input::input_node::{parse_u64_strict, InputNode};
    use proptest::prelude::*;

    #[test]
    fn test_weird_num_edge_cases() {
        assert_ne!("+3".id(), "3".id());
        assert_eq!(3.id(), "3".id());
        assert_ne!("00".id(), "0".id());
        assert_eq!("0".id(), 0.id());
    }

    #[test]
    fn test_u64_string_works() {
        proptest!(|(n in any::<u64>())| {
            assert_eq!(n.to_string().id(), n);
        });
    }

    #[test]
    fn test_if_str_parses_it_is_a_u64() {
        proptest!(|(s in any::<String>())| {
            let res = parse_u64_strict(&s);
            if let Some(n) = res {
                assert_eq!(n.to_string(), s)
            } else {
                assert_ne!(s.id().to_string(), s)
            }
        });
    }
}