-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.rs
147 lines (132 loc) · 3.74 KB
/
util.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
use std::cmp::Ordering;
use std::fmt;
use std::mem::MaybeUninit;
use std::ops::{Deref, DerefMut};
fn approx_cmp(a: &f64, b: &f64) -> Ordering {
match a.partial_cmp(b) {
Some(o) => o,
None => match (a.is_nan(), b.is_nan()) {
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
_ => Ordering::Equal,
},
}
}
/// Returns the index of the larges element in the sequence.
///
/// # Note
/// This method may not work as expected with NaNs.
pub fn argmax(iter: impl Iterator<Item = f64>) -> Option<usize> {
iter.enumerate()
.max_by(|(_, a), (_, b)| approx_cmp(a, b))
.map(|(idx, _)| idx)
}
/// Wrapper for a key-value pair that is ordable by the key.
#[derive(Debug)]
pub struct OrdPair<K: Ord, V>(pub K, pub V);
impl<K: Ord, V> Eq for OrdPair<K, V> {}
impl<K: Ord, V> PartialEq for OrdPair<K, V> {
fn eq(&self, other: &Self) -> bool {
self.0.eq(&other.0)
}
}
impl<K: Ord, V> PartialOrd for OrdPair<K, V> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<K: Ord, V> Ord for OrdPair<K, V> {
fn cmp(&self, other: &Self) -> Ordering {
self.0.cmp(&other.0)
}
}
/// A vector with a fixed maximal length that is allocated on the stack.
pub struct FixedVec<T, const N: usize> {
data: [MaybeUninit<T>; N],
len: usize,
}
impl<T, const N: usize> FixedVec<T, N> {
pub const fn new() -> Self {
debug_assert!(N > 0);
Self {
data: unsafe { MaybeUninit::<[MaybeUninit<T>; N]>::uninit().assume_init() },
len: 0,
}
}
pub fn push(&mut self, v: T) -> bool {
if self.len < N {
self.data[self.len].write(v);
self.len += 1;
true
} else {
false
}
}
pub fn pop(&mut self) -> Option<T> {
if self.len > 0 {
self.len -= 1;
Some(unsafe { self.data[self.len].assume_init_read() })
} else {
None
}
}
pub fn len(&self) -> usize {
self.len
}
pub const fn capacity(&self) -> usize {
N
}
}
impl<T, const N: usize> Drop for FixedVec<T, N> {
fn drop(&mut self) {
for d in self.data[..self.len].iter_mut() {
unsafe { d.assume_init_drop() }
}
}
}
impl<T, const N: usize> Default for FixedVec<T, N> {
fn default() -> Self {
Self::new()
}
}
impl<T, const N: usize> Deref for FixedVec<T, N> {
type Target = [T];
fn deref(&self) -> &Self::Target {
// TODO: replace with slice_assume_init_ref
let slice = &self.data[..self.len];
unsafe { &*(slice as *const [MaybeUninit<T>] as *const [T]) }
}
}
impl<T, const N: usize> DerefMut for FixedVec<T, N> {
fn deref_mut(&mut self) -> &mut Self::Target {
// TODO: replace with slice_assume_init_mut
let slice = &mut self.data[..self.len];
unsafe { &mut *(slice as *mut [MaybeUninit<T>] as *mut [T]) }
}
}
impl<T: fmt::Debug, const N: usize> fmt::Debug for FixedVec<T, N> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_list().entries(self.iter()).finish()
}
}
#[cfg(test)]
mod test {
use super::FixedVec;
#[test]
fn fixed_vec() {
let mut v = FixedVec::<usize, 4>::new();
assert_eq!(v.len(), 0);
assert!(v.push(1));
assert!(v.push(2));
assert!(v.push(3));
assert!(v.push(4));
assert!(!v.push(42));
assert_eq!(v.len(), 4);
assert_eq!(v.pop(), Some(4));
assert_eq!(v.pop(), Some(3));
assert_eq!(v.pop(), Some(2));
assert_eq!(v.pop(), Some(1));
assert_eq!(v.pop(), None);
assert_eq!(v.len(), 0);
}
}