// The struct is public, but its fields are not! pub struct Vec320 { /// A pointer to an array slice. Dereference it using *buffer. /// The result can be used as an array slice. (So you can use [] or len() on it.) buffer : Box<[T]>, /// Number of elements actually added to our collection. /// If this exceeds the length of data, then data must be expanded by a factor of 2 length : usize } /// The initial value of capacity for a new buffer. const INITIAL_CAPACITY : usize = 1; /// /// Create a new block of memory for this vector. /// The buffer consists of INITIAL_CAPACITY many instances of T, plus a "length" of /// the buffer (i.e., a "capacity"). /// Initially, the length of the buffer is INITIAL_CAPACITY. /// The buffer length can be retrieved by first dereferencing the buffer, /// then /// using the len() method. /// i.e., /// (*(myvec320.buffer)).len() /// /// Separately, there is the `length` WE maintain. This is the number of items /// in the buffer that are real data, NOT the size of the buffer itself. /// Don't confuse the two. /// pub fn new () -> Vec320 { // TODO } // Dr. Lahn has provided this. Other than the use of the vec! macro here, you may // not use any other built-in functions. fn boxed_array (length : usize) -> Box<[T]> { return vec![T::default(); length].into_boxed_slice(); } /// Return the element of vec at the given index. /// If index >= length or index < 0, this should panic. /// pub fn element_at(vec : &Vec320, index : usize) -> T { } #[test] fn test_element_at() { let mut holds3: Vec320 = Vec320 { buffer: boxed_array::( 5 ), length: 3 }; (*(holds3.buffer))[0] = 10; (*(holds3.buffer))[1] = 11; (*(holds3.buffer))[2] = 12; assert_eq!( element_at(&holds3, 0), 10 ); assert_eq!( element_at(&holds3, 1), 11 ); assert_eq!( element_at(&holds3, 2), 12 ); // How to assert that a function panics (not strictly required, for this hw): // call `catch_unwind`, and pass it an anonymous function of 0 arguments (a "thunk") // -- that's what the `||` creates: // @see https://www.radford.edu/~cs320/2025spring-ibarland/Lectures/unit-testing-panics_rs.txt // assert!(std::panic::catch_unwind(|| element_at(&holds3, 99)).is_err()); // If you would prefer to write //assert_panic( || element_at(&holds3, 99)); // @see https://www.radford.edu/~cs320/2025spring-ibarland/Lectures/macro-example_rs.txt } /// Changes the value at the given "index" in "vec" to have the "new_value" specified. /// Panics if index is out of bounds of the vec. /// pub fn set(vec : &mut Vec320, index : usize, new_value : T) { } #[test] fn test_set() { } /// Add a new item "element" to the end of the vector "vec". /// If there is not enough room in the vec's buffer, then create a new buffer with twice as /// much room as the previous one, and copy the old data over. /// Don't forget to update the length of vec. /// /// Question for yourself: Why must T implement Default? /// pub fn push (vec : &mut Vec320, element : T) { } #[test] fn test_push() { } /// Removes the last item in this vector. /// If the vector is empty, return None. /// Otherwise, return Some(t) where t is the element removed. /// pub fn pop(vec : &mut Vec320) -> Option { } #[test] fn test_pop() { } /// Add a new item "element" to the vector "vec", inserting the new item /// at position "index", and shifting all existing items from index onwards /// to the right by position. /// Precondition : index <= vec.length /// pub fn add(vec : &mut Vec320, element : T, index : usize) { } #[test] fn test_add() { } /// Removes the element at position "index" from the vector "vec". /// All items to the right of the removed item are then shifted left by 1 slot. /// Returns the removed item. /// Panics if index >= vec.length /// pub fn remove(vec : &mut Vec320, index : usize) -> T { } #[test] fn test_remove() { } /// Returns true if "vec" contains "item". False otherwise. /// pub fn contains (vec : &Vec320, item : T) -> bool { } #[test] fn test_contains() { } /// Returns a new Vec320, that is a deep copy of input, except for only keeping /// the values inside of input for which the filter function evaluates to true /// pub fn filter bool> (input : &Vec320, filter : F) -> Vec320 { } #[test] fn test_filter() { }