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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//! A region that stores slices of copy types.

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{CopyIter, Push, Region, ReserveItems};

/// A container for owned types.
///
/// The container can absorb any type, and stores an owned version of the type, similarly to what
/// vectors do. We recommend using this container for copy types, but there is no restriction in
/// the implementation, and in fact it can correctly store owned values, although any data owned
/// by `T` is regular heap-allocated data, and not contained in regions.
///
/// # Examples
///
/// ```
/// use flatcontainer::{Push, OwnedRegion, Region};
/// let mut r = <OwnedRegion<_>>::default();
///
/// let panagram_en = "The quick fox jumps over the lazy dog";
/// let panagram_de = "Zwölf Boxkämpfer jagen Viktor quer über den großen Sylter Deich";
///
/// let en_index = r.push(panagram_en.as_bytes());
/// let de_index = r.push(panagram_de.as_bytes());
///
/// assert_eq!(panagram_de.as_bytes(), r.index(de_index));
/// assert_eq!(panagram_en.as_bytes(), r.index(en_index));
/// ```
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct OwnedRegion<T> {
    slices: Vec<T>,
}

impl<T: Clone> Clone for OwnedRegion<T> {
    fn clone(&self) -> Self {
        Self {
            slices: self.slices.clone(),
        }
    }

    fn clone_from(&mut self, source: &Self) {
        self.slices.clone_from(&source.slices);
    }
}

impl<T> Region for OwnedRegion<T>
where
    [T]: ToOwned,
{
    type Owned = <[T] as ToOwned>::Owned;
    type ReadItem<'a> = &'a [T] where Self: 'a;
    type Index = (usize, usize);

    #[inline]
    fn merge_regions<'a>(regions: impl Iterator<Item = &'a Self> + Clone) -> Self
    where
        Self: 'a,
    {
        Self {
            slices: Vec::with_capacity(regions.map(|r| r.slices.len()).sum()),
        }
    }

    #[inline]
    fn index(&self, (start, end): Self::Index) -> Self::ReadItem<'_> {
        &self.slices[start..end]
    }

    #[inline]
    fn reserve_regions<'a, I>(&mut self, regions: I)
    where
        Self: 'a,
        I: Iterator<Item = &'a Self> + Clone,
    {
        self.slices.reserve(regions.map(|r| r.slices.len()).sum());
    }

    #[inline]
    fn clear(&mut self) {
        self.slices.clear();
    }

    #[inline]
    fn heap_size<F: FnMut(usize, usize)>(&self, mut callback: F) {
        let size_of_t = std::mem::size_of::<T>();
        callback(
            self.slices.len() * size_of_t,
            self.slices.capacity() * size_of_t,
        );
    }

    #[inline]
    fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>
    where
        Self: 'a,
    {
        item
    }
}

impl<T> Default for OwnedRegion<T> {
    #[inline]
    fn default() -> Self {
        Self {
            slices: Vec::default(),
        }
    }
}

impl<T, const N: usize> Push<[T; N]> for OwnedRegion<T>
where
    [T]: ToOwned,
{
    #[inline]
    fn push(&mut self, item: [T; N]) -> <OwnedRegion<T> as Region>::Index {
        let start = self.slices.len();
        self.slices.extend(item);
        (start, self.slices.len())
    }
}

impl<T: Clone, const N: usize> Push<&[T; N]> for OwnedRegion<T> {
    #[inline]
    fn push(&mut self, item: &[T; N]) -> <OwnedRegion<T> as Region>::Index {
        let start = self.slices.len();
        self.slices.extend_from_slice(item);
        (start, self.slices.len())
    }
}

impl<T: Clone, const N: usize> Push<&&[T; N]> for OwnedRegion<T> {
    #[inline]
    fn push(&mut self, item: &&[T; N]) -> <OwnedRegion<T> as Region>::Index {
        self.push(*item)
    }
}

impl<'b, T: Clone, const N: usize> ReserveItems<&'b [T; N]> for OwnedRegion<T> {
    #[inline]
    fn reserve_items<I>(&mut self, items: I)
    where
        I: Iterator<Item = &'b [T; N]> + Clone,
    {
        self.slices.reserve(items.map(|i| i.len()).sum());
    }
}

impl<T: Clone> Push<&[T]> for OwnedRegion<T> {
    #[inline]
    fn push(&mut self, item: &[T]) -> <OwnedRegion<T> as Region>::Index {
        let start = self.slices.len();
        self.slices.extend_from_slice(item);
        (start, self.slices.len())
    }
}

impl<T: Clone> Push<&&[T]> for OwnedRegion<T>
where
    for<'a> Self: Push<&'a [T]>,
{
    #[inline]
    fn push(&mut self, item: &&[T]) -> <OwnedRegion<T> as Region>::Index {
        self.push(*item)
    }
}

impl<'b, T> ReserveItems<&'b [T]> for OwnedRegion<T>
where
    [T]: ToOwned,
{
    #[inline]
    fn reserve_items<I>(&mut self, items: I)
    where
        I: Iterator<Item = &'b [T]> + Clone,
    {
        self.slices.reserve(items.map(<[T]>::len).sum());
    }
}

impl<T> Push<Vec<T>> for OwnedRegion<T>
where
    [T]: ToOwned,
{
    #[inline]
    fn push(&mut self, mut item: Vec<T>) -> <OwnedRegion<T> as Region>::Index {
        let start = self.slices.len();
        self.slices.append(&mut item);
        (start, self.slices.len())
    }
}

impl<T: Clone> Push<&Vec<T>> for OwnedRegion<T> {
    #[inline]
    fn push(&mut self, item: &Vec<T>) -> <OwnedRegion<T> as Region>::Index {
        self.push(item.as_slice())
    }
}

impl<'a, T> ReserveItems<&'a Vec<T>> for OwnedRegion<T>
where
    [T]: ToOwned,
{
    #[inline]
    fn reserve_items<I>(&mut self, items: I)
    where
        I: Iterator<Item = &'a Vec<T>> + Clone,
    {
        self.reserve_items(items.map(Vec::as_slice));
    }
}

impl<T: Clone, I: IntoIterator<Item = T>> Push<CopyIter<I>> for OwnedRegion<T> {
    #[inline]
    fn push(&mut self, item: CopyIter<I>) -> <OwnedRegion<T> as Region>::Index {
        let start = self.slices.len();
        self.slices.extend(item.0);
        (start, self.slices.len())
    }
}

impl<T, J: IntoIterator<Item = T>> ReserveItems<CopyIter<J>> for OwnedRegion<T>
where
    [T]: ToOwned,
{
    #[inline]
    fn reserve_items<I>(&mut self, items: I)
    where
        I: Iterator<Item = CopyIter<J>> + Clone,
    {
        self.slices
            .reserve(items.flat_map(|i| i.0.into_iter()).count());
    }
}

#[cfg(test)]
mod tests {
    use crate::{CopyIter, Push, Region, ReserveItems};

    use super::*;

    #[test]
    fn test_copy_array() {
        let mut r = <OwnedRegion<u8>>::default();
        r.reserve_items(std::iter::once(&[1; 4]));
        let index = r.push([1; 4]);
        assert_eq!([1, 1, 1, 1], r.index(index));
    }

    #[test]
    fn test_copy_ref_ref_array() {
        let mut r = <OwnedRegion<u8>>::default();
        ReserveItems::reserve_items(&mut r, std::iter::once(&[1; 4]));
        let index = r.push(&&[1; 4]);
        assert_eq!([1, 1, 1, 1], r.index(index));
    }

    #[test]
    fn test_copy_vec() {
        let mut r = <OwnedRegion<u8>>::default();
        ReserveItems::reserve_items(&mut r, std::iter::once(&vec![1; 4]));
        let index = r.push(&vec![1; 4]);
        assert_eq!([1, 1, 1, 1], r.index(index));
        let index = r.push(vec![2; 4]);
        assert_eq!([2, 2, 2, 2], r.index(index));
    }

    #[test]
    fn test_copy_iter() {
        let mut r = <OwnedRegion<u8>>::default();
        r.reserve_items(std::iter::once(CopyIter(std::iter::repeat(1).take(4))));
        let index = r.push(CopyIter(std::iter::repeat(1).take(4)));
        assert_eq!([1, 1, 1, 1], r.index(index));
    }
}