ih_muse_proto/
node_elem_ranges.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
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
use std::cmp::Ordering;
use std::ops::{RangeBounds, RangeInclusive};

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use uuid::Uuid;

#[derive(Deserialize, Serialize, Debug, PartialEq)]
pub struct NodeElementRange {
    pub node_id: Uuid,
    pub range: OrdRangeInc,
}

#[derive(Deserialize, Serialize, Debug, Default)]
pub struct GetRangesRequest {
    pub ini: Option<u64>,
    pub end: Option<u64>,
}

/// Wrapper around `RangeInclusive<u64>` to implement `Ord` and `PartialOrd`.
#[derive(Debug, Clone, Eq)]
pub struct OrdRangeInc(pub RangeInclusive<u64>);

impl OrdRangeInc {
    pub const MIN_SIZE: u64 = 10;

    pub fn new(start: u64, end: u64) -> Self {
        if (end - start) + 1 < OrdRangeInc::MIN_SIZE {
            panic!("Range len cannot be smaller than {}", OrdRangeInc::MIN_SIZE)
        }
        OrdRangeInc(start..=end)
    }

    pub fn new_bound(bound: u64) -> Self {
        OrdRangeInc(bound..=bound)
    }

    pub fn len(&self) -> u64 {
        self.end() - self.start() + 1
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    pub fn start(&self) -> &u64 {
        self.0.start()
    }

    pub fn end(&self) -> &u64 {
        self.0.end()
    }

    pub fn contains(&self, item: &u64) -> bool {
        self.0.contains(item)
    }

    pub fn is_bound(&self) -> bool {
        *self.start() == *self.end()
    }
}

impl From<RangeInclusive<u64>> for OrdRangeInc {
    fn from(range: RangeInclusive<u64>) -> Self {
        OrdRangeInc(range)
    }
}

// Implement Serialize for OrdRangeInc
impl Serialize for OrdRangeInc {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let (start, end) = self.0.clone().into_inner();
        let tuple = (start, end);
        tuple.serialize(serializer)
    }
}

// Implement Deserialize for OrdRangeInc
impl<'de> Deserialize<'de> for OrdRangeInc {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let (start, end) = <(u64, u64)>::deserialize(deserializer)?;
        Ok(OrdRangeInc::new(start, end))
    }
}

fn check_bound(bound: &u64, range: &OrdRangeInc, inverted: bool) -> Ordering {
    if bound < range.start() {
        if inverted {
            return Ordering::Greater;
        }
        return Ordering::Less;
    }
    if bound > range.end() {
        if inverted {
            return Ordering::Less;
        }
        return Ordering::Greater;
    }
    Ordering::Equal
}

impl Ord for OrdRangeInc {
    fn cmp(&self, other: &Self) -> Ordering {
        if self.is_bound() && other.is_bound() {
            panic!("Trying two reserved OrdRangeInc::bound");
        }
        if self.is_bound() {
            return check_bound(self.start(), other, false);
        }
        if other.is_bound() {
            return check_bound(other.start(), self, true);
        }
        // This works because in OrdRangeInc the ranges should never collide
        // u64 in one range will never exists in another range
        let start_cmp = self.start().cmp(other.start());
        if start_cmp == Ordering::Equal {
            self.end().cmp(other.end())
        } else {
            start_cmp
        }
    }
}

impl PartialOrd for OrdRangeInc {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq for OrdRangeInc {
    fn eq(&self, other: &Self) -> bool {
        if self.is_bound() {
            return other.contains(self.start());
        } else if other.is_bound() {
            return self.contains(other.start());
        }
        self.start() == other.start() && self.end() == other.end()
    }
}

impl RangeBounds<u64> for OrdRangeInc {
    fn start_bound(&self) -> std::ops::Bound<&u64> {
        std::ops::Bound::Included(self.start())
    }

    fn end_bound(&self) -> std::ops::Bound<&u64> {
        std::ops::Bound::Included(self.end())
    }
}