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
//! A minimal wrapper around persistable dynamic arrays to provide fixed-capacity semantics.
use std::ops::{Index, IndexMut};
use std::sync::Arc;

use parking_lot::RwLock;

use crate::allocators::{
    bump_allocator::BumpAllocator, persistently_allocatable::PersistentlyAllocatable,
};
use crate::{
    collections::pvec::{PVec, PVecIterMut},
    Persistable,
};

pub enum OperationStatus {
    Ok,
    CapacityReached,
}

#[repr(C)]
pub struct FixedPVec<T> {
    size: usize,
    buf: PVec<T>,
}

impl<T> Persistable for FixedPVec<T> where T: Persistable {}

impl<T> FixedPVec<T>
where
    T: Persistable,
{
    pub fn new(size: usize) -> Self {
        Self {
            size,
            buf: PVec::new(),
        }
    }

    pub fn allocate(&mut self, allocator: Arc<RwLock<BumpAllocator>>) {
        self.set_allocator(allocator);
        self.buf.resize_to_capacity(self.size);
    }

    pub fn push(&mut self, e: T) -> OperationStatus {
        if self.buf.len() == self.size {
            return OperationStatus::CapacityReached;
        }

        self.buf.push(e);

        OperationStatus::Ok
    }

    pub fn pop(&mut self) -> Option<T> {
        self.buf.pop()
    }

    pub fn capacity(&self) -> usize {
        self.size
    }

    pub fn iter_mut(&mut self) -> PVecIterMut<T> {
        self.buf.iter_mut()
    }
}

impl<T> PersistentlyAllocatable for FixedPVec<T>
where
    T: Persistable,
{
    fn set_allocator(&mut self, allocator: Arc<RwLock<BumpAllocator>>) {
        self.buf.set_allocator(allocator);
    }

    fn get_allocator(&self) -> Option<Arc<RwLock<BumpAllocator>>> {
        self.buf.get_allocator()
    }
}

impl<T> Index<usize> for FixedPVec<T>
where
    T: Persistable,
{
    type Output = T;

    fn index(&self, index: usize) -> &Self::Output {
        self.buf.index(index)
    }
}

impl<T> IndexMut<usize> for FixedPVec<T>
where
    T: Persistable,
{
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        self.buf.index_mut(index)
    }
}