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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Implements a cache-friendly but limited representation of a list of strings.
//!
//! Strings are stored contiguously in a vector of bytes, with their lengths and starting indices
//! being stored separately.
//!
//! Limitations include being unable to mutate strings stored in the vector.
//!
//! # Examples
//! ```
//! # use compact_strings::CompactStrings;
//! let mut cmpstrs = CompactStrings::with_capacity(10);
//!
//! cmpstrs.push("One".to_string());
//! cmpstrs.push("Two".to_string());
//! cmpstrs.push("Three".to_string());
//!
//! cmpstrs.remove(1);
//!
//! assert_eq!(cmpstrs.get(0), Some("One"));
//! assert_eq!(cmpstrs.get(1), Some("Three"));
//! assert_eq!(cmpstrs.get(2), None);
//! ```

use std::ops::Index;

/// A cache-friendly but limited representation of a list of strings.
///
/// Strings are stored contiguously in a vector of bytes, with their lengths and starting indices
/// being stored separately.
///
/// Limitations include being unable to mutate strings stored in the vector.
///
/// # Examples
/// ```
/// # use compact_strings::CompactStrings;
/// let mut cmpstrs = CompactStrings::with_capacity(10);
///
/// cmpstrs.push("One".to_string());
/// cmpstrs.push("Two".to_string());
/// cmpstrs.push("Three".to_string());
///
/// cmpstrs.remove(1);
///
/// assert_eq!(cmpstrs.get(0), Some("One"));
/// assert_eq!(cmpstrs.get(1), Some("Three"));
/// assert_eq!(cmpstrs.get(2), None);
/// ```
pub struct CompactStrings {
    inner: Vec<u8>,
    strings: Vec<(usize, usize)>,
}

impl CompactStrings {
    /// Constructs a new, empty [`CompactStrings`].
    ///
    /// The [`CompactStrings`] will not allocate until strings are pushed into it.
    ///
    /// # Examples
    /// ```
    /// # use compact_strings::CompactStrings;
    /// let mut cmpstrs = CompactStrings::new();
    /// ```
    pub const fn new() -> Self {
        Self {
            inner: Vec::new(),
            strings: Vec::new(),
        }
    }

    /// Constructs a new, empty [`CompactStrings`] with at least the specified capacity in the inner
    /// vector where the bytes of the strings are stored.
    ///
    /// Note that this does not affect the indices and lengths vectors which store information
    /// about where each string is stored.
    ///
    /// The [`CompactStrings`] will be able to hold at least *capacity* bytes worth of strings
    /// without reallocating the inner vector. This method is allowed to allocate for more bytes
    /// than *capacity*. If *capacity* is 0, the inner vector will not allocate.
    ///
    /// It is important to note that although the returned vector has the
    /// minimum *capacity* specified, the inner vector will have a zero *length*.
    ///
    /// If it is important to know the exact allocated capacity of the inner vector, always use the
    /// [`capacity`] method after construction.
    ///
    /// [`capacity`]: CompactStrings::capacity
    ///
    /// # Examples
    /// ```
    /// # use compact_strings::CompactStrings;
    /// let mut cmpstrs = CompactStrings::with_capacity(10);
    ///
    /// assert_eq!(cmpstrs.len(), 0);
    /// assert!(cmpstrs.capacity() >= 10);
    /// ```
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            inner: Vec::with_capacity(capacity),
            strings: Vec::new(),
        }
    }

    /// Appends a string to the back of the [`CompactStrings`].
    ///
    /// # Examples
    /// ```
    /// # use compact_strings::CompactStrings;
    /// let mut cmpstrs = CompactStrings::new();
    /// cmpstrs.push("One".to_string());
    /// cmpstrs.push("Two".to_string());
    /// cmpstrs.push("Three".to_string());
    ///
    /// assert_eq!(cmpstrs.get(0), Some("One"));
    /// assert_eq!(cmpstrs.get(1), Some("Two"));
    /// assert_eq!(cmpstrs.get(2), Some("Three"));
    /// assert_eq!(cmpstrs.get(3), None);
    /// ```
    pub fn push(&mut self, string: String) {
        let bytes = string.into_bytes();
        self.strings.push((self.inner.len(), bytes.len()));
        self.inner.extend_from_slice(&bytes);
    }

    /// Returns a reference to the string stored in the [`CompactStrings`] at that position.
    ///
    /// # Examples
    /// ```
    /// # use compact_strings::CompactStrings;
    /// let mut cmpstrs = CompactStrings::new();
    /// cmpstrs.push("One".to_string());
    /// cmpstrs.push("Two".to_string());
    /// cmpstrs.push("Three".to_string());
    ///
    /// assert_eq!(cmpstrs.get(0), Some("One"));
    /// assert_eq!(cmpstrs.get(1), Some("Two"));
    /// assert_eq!(cmpstrs.get(2), Some("Three"));
    /// assert_eq!(cmpstrs.get(3), None);
    /// ```
    pub fn get(&self, index: usize) -> Option<&str> {
        let (start, len) = *self.strings.get(index)?;
        let bytes = self.inner.get(start..start + len)?;
        unsafe { Some(std::str::from_utf8_unchecked(bytes)) }
    }

    /// Returns a reference to the string stored in the [`CompactStrings`] at that position, without
    /// doing bounds checking.
    ///
    /// # Safety
    /// Calling this method with an out-of-bounds index is undefined behavior even if the resulting reference is not used.
    ///
    /// # Examples
    /// ```
    /// # use compact_strings::CompactStrings;
    /// let mut cmpstrs = CompactStrings::new();
    /// cmpstrs.push("One".to_string());
    /// cmpstrs.push("Two".to_string());
    /// cmpstrs.push("Three".to_string());
    ///
    /// unsafe {
    ///     assert_eq!(cmpstrs.get_unchecked(0), "One");
    ///     assert_eq!(cmpstrs.get_unchecked(1), "Two");
    ///     assert_eq!(cmpstrs.get_unchecked(2), "Three");
    /// }
    /// ```
    pub unsafe fn get_unchecked(&self, index: usize) -> &str {
        let (start, len) = *self.strings.get_unchecked(index);
        let bytes = self.inner.get_unchecked(start..start + len);
        std::str::from_utf8_unchecked(bytes)
    }

    /// Returns the number of strings in the [`CompactStrings`], also referred to as its 'length'.
    ///
    /// # Examples
    /// ```
    /// # use compact_strings::CompactStrings;
    /// let mut cmpstrs = CompactStrings::new();
    ///
    /// cmpstrs.push("One".to_string());
    /// cmpstrs.push("Two".to_string());
    /// cmpstrs.push("Three".to_string());
    ///
    /// assert_eq!(cmpstrs.len(), 3);
    /// ```
    #[inline]
    pub fn len(&self) -> usize {
        self.strings.len()
    }

    /// Returns true if the [`CompactStrings`] contains no strings.
    ///
    /// # Examples
    /// ```
    /// # use compact_strings::CompactStrings;
    /// let mut cmpstrs = CompactStrings::new();
    /// assert!(cmpstrs.is_empty());
    ///
    /// cmpstrs.push("One".to_string());
    ///
    /// assert!(!cmpstrs.is_empty());
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the number of bytes the inner vector can store without reallocating.
    ///
    /// # Examples
    /// ```
    /// # use compact_strings::CompactStrings;
    /// let mut cmpstrs = CompactStrings::with_capacity(10);
    ///
    /// cmpstrs.push("One".to_string());
    ///
    /// assert!(cmpstrs.capacity() >= 10);
    /// ```
    #[inline]
    pub fn capacity(&self) -> usize {
        self.inner.capacity()
    }

    /// Clears the [`CompactStrings`], removing all strings.
    ///
    /// Note that this method has no effect on the allocated capacity of the vector.
    ///
    /// # Examples
    /// ```
    /// # use compact_strings::CompactStrings;
    /// let mut cmpstrs = CompactStrings::new();
    ///
    /// cmpstrs.push("One".to_string());
    /// cmpstrs.push("Two".to_string());
    /// cmpstrs.push("Three".to_string());
    /// cmpstrs.clear();
    ///
    /// assert!(cmpstrs.is_empty());
    /// ```
    pub fn clear(&mut self) {
        self.inner.clear();
        self.strings.clear();
    }

    /// Shrinks the capacity of the inner vector, which stores the bytes of the held strings, as much as possible.
    ///
    /// It will drop down as close as possible to the length but the allocator
    /// may still inform the vector that there is space for a few more elements.
    ///
    /// # Examples
    /// ```
    /// # use compact_strings::CompactStrings;
    /// let mut cmpstrs = CompactStrings::with_capacity(10);
    ///
    /// cmpstrs.push("One".to_string());
    /// cmpstrs.push("Two".to_string());
    /// cmpstrs.push("Three".to_string());
    ///
    /// assert!(cmpstrs.capacity() >= 10);
    /// cmpstrs.shrink_to_fit();
    /// assert!(cmpstrs.capacity() >= 3);
    /// ```
    #[inline]
    pub fn shrink_to_fit(&mut self) {
        self.inner.shrink_to_fit();
    }

    /// Shrinks the capacity of the inner vector, which stores the bytes of the held strings, with a lower bound.
    ///
    /// The capacity will remain at least as large as both the length and the supplied value.
    ///
    /// If the current capacity is less than the lower limit, this is a no-op.
    ///
    /// # Examples
    /// ```
    /// # use compact_strings::CompactStrings;
    /// let mut cmpstrs = CompactStrings::with_capacity(10);
    ///
    /// cmpstrs.push("One".to_string());
    /// cmpstrs.push("Two".to_string());
    /// cmpstrs.push("Three".to_string());
    ///
    /// assert!(cmpstrs.capacity() >= 10);
    /// cmpstrs.shrink_to(4);
    /// assert!(cmpstrs.capacity() >= 4);
    /// ```
    #[inline]
    pub fn shrink_to(&mut self, min_capacity: usize) {
        self.inner.shrink_to(min_capacity);
    }

    /// Removes the data pointing to where the string at the specified index is stored.
    ///
    /// Note that this does not remove the bytes of the string from memory, you may want to use
    /// [`remove_full`] if you desire that behavior.
    ///
    /// [`remove_full`]: CompactStrings::remove_full
    ///
    /// # Examples
    /// ```
    /// # use compact_strings::CompactStrings;
    /// let mut cmpstrs = CompactStrings::with_capacity(10);
    ///
    /// cmpstrs.push("One".to_string());
    /// cmpstrs.push("Two".to_string());
    /// cmpstrs.push("Three".to_string());
    ///
    /// cmpstrs.remove(1);
    ///
    /// assert_eq!(cmpstrs.get(0), Some("One"));
    /// assert_eq!(cmpstrs.get(1), Some("Three"));
    /// assert_eq!(cmpstrs.get(2), None);
    /// ```
    pub fn remove(&mut self, index: usize) {
        assert!(self.len() > index);
        self.strings.remove(index);
    }

    /// Removes the data pointing to where the string at the specified index is stored.
    ///
    /// Note that this also removes the bytes of the string from memory, which requires all bytes
    /// after the string to be shifted into the empty space, you may want to use [`remove`] if you
    /// do not desire that behavior.
    ///
    /// [`remove`]: CompactStrings::remove
    ///
    /// # Examples
    /// ```
    /// # use compact_strings::CompactStrings;
    /// let mut cmpstrs = CompactStrings::with_capacity(10);
    ///
    /// cmpstrs.push("One".to_string());
    /// cmpstrs.push("Two".to_string());
    /// cmpstrs.push("Three".to_string());
    ///
    /// cmpstrs.remove_full(1);
    ///
    /// assert_eq!(cmpstrs.get(0), Some("One"));
    /// assert_eq!(cmpstrs.get(1), Some("Three"));
    /// assert_eq!(cmpstrs.get(2), None);
    /// ```
    pub fn remove_full(&mut self, index: usize) {
        assert!(self.len() > index);
        let (start, len) = self.strings.remove(index);
        let inner_len = self.inner.len();
        for (idx, _) in self.strings.iter_mut().skip(index) {
            *idx -= len;
        }
        self.inner.copy_within(start + len.., start);
        self.inner.truncate(inner_len - len);
    }
}

impl Index<usize> for CompactStrings {
    type Output = str;

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

/// Iterator over strings in a [`CompactStrings`]
///
/// # Examples
/// ```
/// # use compact_strings::CompactStrings;
/// let mut cmpstrs = CompactStrings::new();
/// cmpstrs.push("One".to_string());
/// cmpstrs.push("Two".to_string());
/// cmpstrs.push("Three".to_string());
///
/// let mut iter = cmpstrs.into_iter();
/// assert_eq!(iter.next(), Some("One"));
/// assert_eq!(iter.next(), Some("Two"));
/// assert_eq!(iter.next(), Some("Three"));
/// assert_eq!(iter.next(), None);
/// ```
pub struct CompactStringIterator<'a> {
    inner: &'a CompactStrings,
    index: usize,
}

impl<'a> Iterator for CompactStringIterator<'a> {
    type Item = &'a str;

    fn next(&mut self) -> Option<Self::Item> {
        let out = self.inner.get(self.index);
        self.index += 1;

        out
    }
}

impl<'a> IntoIterator for &'a CompactStrings {
    type Item = &'a str;

    type IntoIter = CompactStringIterator<'a>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        Self::IntoIter {
            inner: self,
            index: 0,
        }
    }
}

impl ExactSizeIterator for CompactStringIterator<'_> {
    #[inline]
    fn len(&self) -> usize {
        self.inner.strings.len()
    }
}