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
use crate::prelude::*;

pub struct ThinPtrArray<'a, L, E>
where
    Self: 'a,
{
    data: ManuallyDrop<&'a mut TPArrayBlock<L, E>>,
}

impl<'a, L, E> ThinPtrArray<'a, L, E> {
    /// Create a new array, with values initialized using a provided function.
    #[inline]
    pub fn new<F>(label: L, len: usize, func: F) -> Self
    where
        F: FnMut(&mut L, usize) -> E,
    {
        Self {
            data: ManuallyDrop::new(TPArrayBlock::<L, E>::new_ptr(label, len, func)),
        }
    }

    /// Create a new array, without initializing the values in it.
    #[inline]
    pub unsafe fn new_unsafe(label: L, len: usize) -> Self {
        let new_ptr = TPArrayBlock::<L, E>::new_ptr_unsafe(label, len);
        Self {
            data: ManuallyDrop::new(new_ptr),
        }
    }

    /// Unsafe access to an element at an index in the array.
    #[inline]
    pub unsafe fn unchecked_access(&'a self, idx: usize) -> &'a mut E {
        self.data.unchecked_access(idx)
    }
}

impl<'a, L, E> ThinPtrArray<'a, L, E>
where
    E: Default,
{
    /// Get a new array, initialized to default values.
    #[inline]
    pub fn new_default(label: L, len: usize) -> Self {
        Self {
            data: ManuallyDrop::new(TPArrayBlock::new_ptr_default(label, len)),
        }
    }
}

impl<'a, L, E> LabelledArray<L, E> for ThinPtrArray<'a, L, E> {
    /// Get a reference to the label of the array.
    #[inline]
    fn get_label(&self) -> &L {
        &self.data.label
    }

    /// Get a mutable reference to the label of the array.
    #[inline]
    fn get_label_mut(&mut self) -> &mut L {
        &mut self.data.label
    }
}

impl<'a, L, E> Index<usize> for ThinPtrArray<'a, L, E> {
    type Output = E;
    #[inline]
    fn index(&self, idx: usize) -> &E {
        &self.data[idx]
    }
}

impl<'a, L, E> IndexMut<usize> for ThinPtrArray<'a, L, E> {
    #[inline]
    fn index_mut(&mut self, idx: usize) -> &mut E {
        &mut self.data[idx]
    }
}

impl<'a, L, E> Clone for ThinPtrArray<'a, L, E>
where
    L: Clone,
    E: Clone,
{
    #[inline]
    fn clone(&self) -> Self {
        Self {
            data: ManuallyDrop::new((*self.data).clone()),
        }
    }
}

impl<'a, L, E> Drop for ThinPtrArray<'a, L, E> {
    #[inline]
    fn drop(&mut self) {
        let mut_ref = &mut self.data;
        unsafe { mut_ref.dealloc() };
        core::mem::forget(mut_ref);
    }
}

impl<'a, L, E> Container<(usize, E)> for ThinPtrArray<'a, L, E> {
    #[inline]
    fn add(&mut self, elem: (usize, E)) {
        self[elem.0] = elem.1;
    }
    #[inline]
    fn len(&self) -> usize {
        self.data.len()
    }
}

impl<'a, L, E> CopyMap<usize, E> for ThinPtrArray<'a, L, E> {
    #[inline]
    fn get(&self, key: usize) -> Option<&E> {
        if key > self.len() {
            None
        } else {
            Some(&self[key])
        }
    }
    #[inline]
    fn get_mut(&mut self, key: usize) -> Option<&mut E> {
        if key > self.len() {
            None
        } else {
            Some(&mut self[key])
        }
    }
    #[inline]
    fn insert(&mut self, key: usize, value: E) -> Option<E> {
        if key > self.len() {
            None
        } else {
            let ret = unsafe { std::mem::transmute_copy::<E, E>(&self[key]) };
            let value_ref = (&mut self[key]) as *mut E as *mut ManuallyDrop<E>;
            unsafe {
                *value_ref = ManuallyDrop::new(value);
            }
            Some(ret)
        }
    }
}

impl<'a, L, E> Array<E> for ThinPtrArray<'a, L, E> {}