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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! A slab-allocated array.
use core::marker::PhantomData;

use crate::{id::Id, slab::SlabItem};

/// Iterator over [`Id`]s in an [`Array`].
#[derive(Clone, Copy)]
pub struct ArrayIter<T> {
    array: Array<T>,
    index: usize,
}

impl<T: SlabItem> Iterator for ArrayIter<T> {
    type Item = Id<T>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index >= self.array.len() {
            None
        } else {
            let id = self.array.at(self.index);
            self.index += 1;
            Some(id)
        }
    }
}

/// A pointer to contiguous `T` elements in a slab.
///
/// ```rust
/// use crabslab::{Array, CpuSlab, GrowableSlab, Id, Slab, SlabItem};
///
/// let ints = [1, 2, 3, 4, 5u32];
/// let mut slab = CpuSlab::new(vec![]);
/// let array = slab.append_array(&ints);
/// for (i, id) in array.iter().enumerate() {
///     let value = slab.read(id);
///     assert_eq!(ints[i], value);
/// }
/// ```
#[repr(C)]
pub struct Array<T> {
    // u32 offset in the slab
    pub index: u32,
    // number of `T` elements in the array
    pub len: u32,
    _phantom: PhantomData<T>,
}

impl<T> Clone for Array<T> {
    fn clone(&self) -> Self {
        Self {
            index: self.index,
            len: self.len,
            _phantom: PhantomData,
        }
    }
}

impl<T> Copy for Array<T> {}

/// An `Id<T>` is an `Array<T>` with a length of 1.
impl<T> From<Id<T>> for Array<T> {
    fn from(id: Id<T>) -> Self {
        Self {
            index: id.inner(),
            len: 1,
            _phantom: PhantomData,
        }
    }
}

impl<T> core::fmt::Debug for Array<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        if self.is_null() {
            f.write_fmt(core::format_args!(
                "Array<{}>(null)",
                core::any::type_name::<T>()
            ))
        } else {
            f.write_fmt(core::format_args!(
                "Array<{}>({}, {})",
                core::any::type_name::<T>(),
                self.index,
                self.len
            ))
        }
    }
}

impl<T> PartialEq for Array<T> {
    fn eq(&self, other: &Self) -> bool {
        self.index == other.index && self.len == other.len
    }
}

impl<T: SlabItem> SlabItem for Array<T> {
    const SLAB_SIZE: usize = 2;

    fn write_slab(&self, index: usize, slab: &mut [u32]) -> usize {
        let index = self.index.write_slab(index, slab);
        let index = self.len.write_slab(index, slab);
        index
    }

    fn read_slab(index: usize, slab: &[u32]) -> Self {
        let start = u32::read_slab(index, slab);
        let len = u32::read_slab(index + 1, slab);
        Array::new(start, len)
    }
}

impl<T: SlabItem> Default for Array<T> {
    fn default() -> Self {
        Array::NONE
    }
}

impl<T> Array<T> {
    pub const NONE: Self = Array::new(u32::MAX, 0);

    pub const fn new(index: u32, len: u32) -> Self {
        Self {
            index,
            len,
            _phantom: PhantomData,
        }
    }

    pub fn len(&self) -> usize {
        self.len as usize
    }

    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    pub fn is_null(&self) -> bool {
        self.index == u32::MAX
    }

    pub fn contains_index(&self, index: usize) -> bool {
        index >= self.index as usize && index < (self.index + self.len) as usize
    }

    pub fn at(&self, index: usize) -> Id<T>
    where
        T: SlabItem,
    {
        if index >= self.len() {
            Id::NONE
        } else {
            Id::new(self.index + (T::SLAB_SIZE * index) as u32)
        }
    }

    pub fn starting_index(&self) -> usize {
        self.index as usize
    }

    pub fn iter(&self) -> ArrayIter<T> {
        ArrayIter {
            array: *self,
            index: 0,
        }
    }

    /// Convert this array into a `u32` array.
    pub fn into_u32_array(self) -> Array<u32>
    where
        T: SlabItem,
    {
        Array {
            index: self.index,
            len: self.len * T::SLAB_SIZE as u32,
            _phantom: PhantomData,
        }
    }

    #[cfg(not(target_arch = "spirv"))]
    /// Return the slice of the slab that this array represents.
    pub fn sub_slab<'a>(&'a self, slab: &'a [u32]) -> &[u32]
    where
        T: SlabItem,
    {
        let arr = self.into_u32_array();
        &slab[arr.index as usize..(arr.index + arr.len) as usize]
    }
}

impl Array<u32> {
    pub fn union(&mut self, other: &Array<u32>) {
        let start = self.index.min(other.index);
        let end = (self.index + self.len).max(other.index + other.len);
        self.index = start;
        self.len = end - start;
    }
}