Repository navigation
Expand file tree
/
Copy pathovec.rs
More file actions
92 lines (88 loc) · 1.86 KB
/
Copy pathovec.rs
File metadata and controls
92 lines (88 loc) · 1.86 KB
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
// Copyright © 2022-2024 Rouven Spreckels <rs@qu1x.dev>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use core::mem::take;
use nalgebra::{base::allocator::Allocator, DefaultAllocator, DimName, OVector};
/// Owned vector of item `T` and capacity `D`.
#[doc(hidden)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OVec<T: Default, D: DimName>
where
OVector<T, D>: Default,
DefaultAllocator: Allocator<T, D>,
{
size: usize,
data: OVector<T, D>,
}
impl<T: Default, D: DimName> OVec<T, D>
where
OVector<T, D>: Default,
DefaultAllocator: Allocator<T, D>,
{
/// New empty vector.
#[must_use]
#[inline]
pub fn new() -> Self {
Self::default()
}
/// Number of items.
#[must_use]
#[inline]
pub const fn len(&self) -> usize {
self.size
}
/// Whether vector is empty.
#[must_use]
#[inline]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
/// Whether vector is full.
#[must_use]
#[inline]
pub fn is_full(&self) -> bool {
self.len() == self.data.len()
}
/// Immutable slice of items.
#[must_use]
#[inline]
pub fn as_slice(&self) -> &[T] {
&self.data.as_slice()[..self.len()]
}
/// Adds `item`.
///
/// # Panics
///
/// Panics if [`Self::is_full()`].
#[inline]
pub fn push(&mut self, item: T) {
self.data[self.size] = item;
self.size += 1;
}
/// Removes last item.
///
/// Returns `Some(T)` or `None` if [`Self::is_empty()`].
#[inline]
pub fn pop(&mut self) -> Option<T> {
if self.is_empty() {
None
} else {
self.size -= 1;
Some(take(&mut self.data[self.size]))
}
}
}
impl<T: Default, D: DimName> Default for OVec<T, D>
where
OVector<T, D>: Default,
DefaultAllocator: Allocator<T, D>,
{
fn default() -> Self {
Self {
size: 0,
data: OVector::default(),
}
}
}