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
//! This file contains an implementation of Vec that can't be empty.

use crate::*;

use std::vec::Drain;
use std::vec::Splice;
use std::ops::Bound;


// ===================
// === NonEmptyVec ===
// ===================

/// A version of [`std::vec::Vec`] that can't be empty.
#[allow(missing_docs)]
#[derive(Clone,Debug,PartialEq)]
pub struct NonEmptyVec<T> {
    elems: Vec<T>
}

impl<T> Deref for NonEmptyVec<T> {
    type Target = Vec<T>;

    fn deref(&self) -> &Self::Target {
        &self.elems
    }
}

impl<T> NonEmptyVec<T> {
    /// Construct a new non-empty vector.
    ///
    /// The vector will not allocate more than the space required to contain `first` and `rest`.
    ///
    /// # Examples
    ///
    /// ```
    /// #![allow(unused_mut)]
    /// use enso_prelude::NonEmptyVec;
    /// let mut vec: NonEmptyVec<usize> = NonEmptyVec::new(0,vec![]);
    /// ```
    pub fn new(first:T, rest:Vec<T>) -> NonEmptyVec<T> {
        let mut elems = vec![first];
        elems.extend(rest);
        NonEmptyVec{elems}
    }

    /// Construct a `NonEmptyVec` containing a single element.
    ///
    /// # Examples
    ///
    /// ```
    /// use enso_prelude::NonEmptyVec;
    /// let vec = NonEmptyVec::singleton(0);
    /// assert_eq!(vec.get(0),Some(&0));
    /// assert_eq!(vec.len(),1);
    /// ```
    pub fn singleton(first:T) -> NonEmptyVec<T> {
        NonEmptyVec::new(first,vec![])
    }

    /// Construct a new, `NonEmptyVec<T>` containing the provided element and with the provided
    /// `capacity`.
    ///
    /// If `capacity` is 0, then the vector will be allocated with capacity for the provided `first`
    /// element. The vector will be able to hold exactly `capacity` elements without reallocating.
    ///
    /// It is important to note that although the returned vector has the *capacity* specified, the
    /// vector will have a length of 1.
    ///
    /// # Panics
    ///
    /// Panics if `capacity` is not > 0.
    ///
    /// # Examples
    ///
    /// ```
    /// use enso_prelude::NonEmptyVec;
    /// let mut vec = NonEmptyVec::with_capacity(0, 10);
    ///
    /// // The vector contains one item, even though it has capacity for more
    /// assert_eq!(vec.len(), 1);
    ///
    /// // These are all done without reallocating...
    /// for i in 1..10 {
    ///     vec.push(i);
    /// }
    ///
    /// // ...but this may make the vector reallocate
    /// vec.push(11);
    /// ```
    pub fn with_capacity(first:T, capacity:usize) -> NonEmptyVec<T> {
        if capacity == 0 {
            panic!("Capacity must be greater than zero for a NonEmptyVec.");
        }
        let mut elems = Vec::with_capacity(capacity);
        elems.push(first);
        NonEmptyVec{elems}
    }

    /// Reserve capacity for at least `additional` more elements to be inserted in the given
    /// `Vec<T>`.
    ///
    /// The collection may reserve more space to avoid frequent reallocations. After calling
    /// `reserve`, capacity will be greater than or equal to `self.len() + additional`. Does nothing
    /// if capacity is already sufficient.
    ///
    /// # Panics
    ///
    /// Panics if the new capacity overflows `usize`.
    ///
    /// # Examples
    ///
    /// ```
    /// use enso_prelude::NonEmptyVec;
    /// let mut vec = NonEmptyVec::new(0,vec![]);
    /// vec.reserve(10);
    /// assert!(vec.capacity() >= 11);
    /// ```
    pub fn reserve(&mut self, additional:usize) {
        self.elems.reserve(additional);
    }

    /// Shrinks the capacity of the `NonEmotyVec` 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 enso_prelude::NonEmptyVec;
    /// let mut vec = NonEmptyVec::with_capacity(0, 10);
    /// assert_eq!(vec.capacity(),10);
    /// vec.shrink_to_fit();
    /// assert!(vec.capacity() < 10);
    /// ```
    pub fn shrink_to_fit(&mut self) {
        self.elems.shrink_to_fit();
    }

    /// Append an element to the back of a collection.
    ///
    /// # Panics
    ///
    /// Panics if the number of elements in the vector overflows a `usize`.
    ///
    /// # Examples
    ///
    /// ```
    /// use enso_prelude::NonEmptyVec;
    /// let mut vec = NonEmptyVec::new(0,vec![1,2]);
    /// vec.push(3);
    /// assert_eq!(vec.len(),4);
    /// ```
    pub fn push(&mut self, value:T) {
        self.elems.push(value)
    }

    /// Remove an element from the back of the collection, returning it.
    ///
    /// Will not pop any item if there is only one item left in the vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use enso_prelude::NonEmptyVec;
    /// let mut vec = NonEmptyVec::new(0,vec![1]);
    /// assert!(vec.pop().is_some());
    /// assert!(vec.pop().is_none());
    /// assert_eq!(vec.len(),1);
    /// ```
    pub fn pop(&mut self) -> Option<T> {
        (self.len() > 1).and_option_from(||self.elems.pop())
    }

    /// Obtain a mutable reference to teh element in the vector at the specified `index`.
    ///
    /// # Examples
    ///
    /// ```
    /// use enso_prelude::NonEmptyVec;
    /// let mut vec   = NonEmptyVec::new(0,vec![1,2]);
    /// let reference = vec.get_mut(0);
    /// assert!(reference.is_some());
    /// assert_eq!(*reference.unwrap(),0);
    /// ```
    pub fn get_mut(&mut self, index:usize) -> Option<&mut T> {
        self.elems.get_mut(index)
    }

    /// Obtain an immutable reference to the head of the `NonEmptyVec`.
    ///
    /// # Examples
    ///
    /// ```
    /// use enso_prelude::NonEmptyVec;
    /// let vec = NonEmptyVec::new(0,vec![1,2]);
    /// assert_eq!(*vec.first(), 0);
    /// ```
    pub fn first(&self) -> &T {
        &self.elems.first().expect("The NonEmptyVec always has an item in it.")
    }

    /// Obtain a mutable reference to the head of the `NonEmptyVec`.
    ///
    /// # Examples
    ///
    /// ```
    /// use enso_prelude::NonEmptyVec;
    /// let mut vec = NonEmptyVec::new(0,vec![1,2]);
    /// assert_eq!(*vec.first_mut(), 0);
    /// ```
    pub fn first_mut(&mut self) -> &mut T {
        self.elems.first_mut().expect("The NonEmptyVec always has an item in it.")
    }

    /// Obtain an immutable reference to the last element in the `NonEmptyVec`.
    ///
    /// # Examples
    ///
    /// ```
    /// use enso_prelude::NonEmptyVec;
    /// let vec = NonEmptyVec::new(0,vec![1,2]);
    /// assert_eq!(*vec.last(),2)
    /// ```
    pub fn last(&self) -> &T {
        self.get(self.len() - 1).expect("There is always one element in a NonEmptyVec.")
    }

    /// Obtain a mutable reference to the last element in the `NonEmptyVec`.
    ///
    /// # Examples
    ///
    /// ```
    /// use enso_prelude::NonEmptyVec;
    /// let mut vec = NonEmptyVec::new(0,vec![1,2]);
    /// assert_eq!(*vec.last_mut(),2)
    /// ```
    pub fn last_mut(&mut self) -> &mut T {
        self.get_mut(self.len() - 1).expect("There is always one element in a NonEmptyVec.")
    }

    /// Create a draining iterator that removes the specified range in the vector and yields the
    /// removed items.
    ///
    /// It will never remove the root element of the vector.
    ///
    /// # Panics
    ///
    /// Panics if the starting point is greater than the end point or if the end point is greater
    /// than the length of the vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use enso_prelude::NonEmptyVec;
    /// let mut vec = NonEmptyVec::new(0,vec![1,2,3,4,5]);
    /// let drained:Vec<i32> = vec.drain(1..=5).collect();
    /// assert_eq!(drained,[1,2,3,4,5])
    /// ```
    pub fn drain<R>(&mut self, range:R) -> Drain<T> where R:RangeBounds<usize> {
        if range.contains(&0) {
            match range.end_bound() {
                Bound::Included(n) => self.elems.drain(1..=*n),
                Bound::Excluded(n) => self.elems.drain(1..*n),
                Bound::Unbounded   => self.elems.drain(1..)
            }
        } else {
            self.elems.drain(range)
        }
    }

    /// Creates a splicing iterator that replaces the specified range in the vector with the given 4
    /// `replace_with` iterator and yields the removed items.
    ///
    /// `replace_with` does not need to be the same length as range. The element range is removed
    /// even if the iterator is not consumed until the end.
    ///
    /// It is unspecified how many elements are removed from the vector if the Splice value is leaked.
    ///
    /// The input iterator replace_with is only consumed when the Splice value is dropped.
    ///
    /// # Panics
    ///
    /// Panics if the starting point is greater than the end point or if the end point is greater
    /// than the length of the vector.
    ///
    /// # Examples
    ///
    /// ```
    /// use enso_prelude::NonEmptyVec;
    /// let mut vec        = NonEmptyVec::new(0,vec![1,2,3,4,5]);
    /// let replacements   = [10,20,30,40];
    /// let yielded:Vec<_> = vec.splice(..2,replacements.iter().cloned()).collect();
    /// assert_eq!(vec.as_slice(),&[10,20,30,40,2,3,4,5]);
    /// assert_eq!(yielded,&[0,1])
    /// ```
    pub fn splice<R,I>(&mut self, range:R, replace_with:I) -> Splice<<I as IntoIterator>::IntoIter>
        where I: IntoIterator<Item = T>,
              R: RangeBounds<usize> {
        self.elems.splice(range,replace_with)
    }
}


// === Trait Impls ===

impl<T:Default> Default for NonEmptyVec<T> {
    fn default() -> Self {
        Self::singleton(default())
    }
}