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
use crate::{
    helpers::range::{range_end, range_start},
    FixedVec,
};
use orx_pinned_vec::{ConcurrentPinnedVec, PinnedVecGrowthError};
use std::cmp::Ordering;

/// Concurrent wrapper ([`orx_pinned_vec::ConcurrentPinnedVec`]) for the `FixedVec`.
pub struct ConcurrentFixedVec<T> {
    data: Vec<T>,
    ptr: *mut T,
    fixed_capacity: usize,
}

impl<T> From<FixedVec<T>> for ConcurrentFixedVec<T> {
    fn from(value: FixedVec<T>) -> Self {
        let mut data = value.data;
        let fixed_capacity = data.capacity();
        unsafe { data.set_len(fixed_capacity) };
        let ptr = data.as_mut_ptr();
        Self {
            data,
            ptr,
            fixed_capacity,
        }
    }
}

impl<T> ConcurrentPinnedVec<T> for ConcurrentFixedVec<T> {
    type P = FixedVec<T>;

    unsafe fn into_inner(mut self, len: usize) -> Self::P {
        unsafe { self.data.set_len(len) };
        self.data.into()
    }

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

    fn max_capacity(&self) -> usize {
        self.fixed_capacity
    }

    fn grow_to(&self, new_capacity: usize) -> Result<usize, orx_pinned_vec::PinnedVecGrowthError> {
        match new_capacity <= self.fixed_capacity {
            true => Ok(self.fixed_capacity),
            false => Err(PinnedVecGrowthError::FailedToGrowWhileKeepingElementsPinned),
        }
    }

    unsafe fn slices_mut<R: std::ops::RangeBounds<usize>>(
        &self,
        range: R,
    ) -> <Self::P as orx_pinned_vec::PinnedVec<T>>::SliceMutIter<'_> {
        let a = range_start(&range);
        let b = range_end(&range, self.fixed_capacity);

        match b.saturating_sub(a) {
            0 => Some(&mut []),
            _ => match (a.cmp(&self.fixed_capacity), b.cmp(&self.fixed_capacity)) {
                (Ordering::Equal | Ordering::Greater, _) => None,
                (_, Ordering::Greater) => None,
                _ => {
                    let p = self.ptr.add(a);
                    let slice = unsafe { std::slice::from_raw_parts_mut(p, b - a) };
                    Some(slice)
                }
            },
        }
    }

    unsafe fn iter<'a>(&'a self, len: usize) -> impl Iterator<Item = &'a T> + 'a
    where
        T: 'a,
    {
        let p = self.data.as_ptr();
        let slice = std::slice::from_raw_parts(p, len);
        slice.iter()
    }

    unsafe fn iter_mut<'a>(&'a mut self, len: usize) -> impl Iterator<Item = &'a mut T> + 'a
    where
        T: 'a,
    {
        let p = self.data.as_mut_ptr();
        let slice = std::slice::from_raw_parts_mut(p, len);
        slice.iter_mut()
    }

    unsafe fn set_pinned_vec_len(&mut self, len: usize) {
        self.data.set_len(len);
    }

    unsafe fn get(&self, index: usize) -> Option<&T> {
        match index < self.fixed_capacity {
            true => Some(&*self.ptr.add(index)),
            false => None,
        }
    }

    unsafe fn get_mut(&mut self, index: usize) -> Option<&mut T> {
        match index < self.capacity() {
            true => Some(&mut self.data[index]),
            false => None,
        }
    }

    unsafe fn get_ptr_mut(&self, index: usize) -> *mut T {
        assert!(index < self.fixed_capacity);
        self.ptr.add(index)
    }

    unsafe fn reserve_maximum_concurrent_capacity(
        &mut self,
        _: usize,
        new_maximum_capacity: usize,
    ) -> usize {
        let additional = new_maximum_capacity.saturating_sub(self.fixed_capacity);
        self.data.reserve(additional);
        self.fixed_capacity = self.data.capacity();
        self.data.set_len(self.fixed_capacity);
        self.fixed_capacity
    }

    unsafe fn clear(&mut self, prior_len: usize) {
        self.set_pinned_vec_len(prior_len);
        self.data.clear()
    }
}