Due Apr.24 (Thu)22 (Tue) at start of class, on D2L and hardcopy
This project has the following parts:
Part A (75pts)
Implement a library Vec320 that implements a Vector type.
Your implementation will be similar to the built-in Vec type in Rust,
which is itself similar to Java's the ArrayList implementation of List.
I have provided an initial
src/lib.rs,
containing
the Vec320 struct,
as well as
the headers for the required functions.
Place this file in your project in src/, next to main.rs.
Within your implementation of Vec320,
you may not use any built-in library functions except for the
following exceptions:
You can use .len() to find the length of an array slice.
I have provided a function
boxed_array<T>(usize len).
This allows you to create array
slices on the heap whose size is dynamically determined at runtime.
Do not change the contents of this function.
However, do use it to
allocate the heap memory for your instances of Vec320.
The following function implementations are required for this part.
I recommend you handle the functions in the order given below:
(5pts)new() : Create a new Vec320 object, which should be empty.
(5pts)element_at(index) : Return the element at the given index.
If the index is invalid, panic.
(5pts)set(index, new_value) : Change the value at the given index to new_value.
If the index is invalid, panic.
(20pts)add(item, index) :
Add an item into the Vec320 at the given index.
All items from that index to the end of the Vec320 should be shifted to the right by one.
If there is no more room for an additional element in the heap array,
allocate a new heap array with twice as much room, copy all the elements over,
and then insert the new element.
(5pts)push(item) : Add an item to the end of the Vec320.
If there is no more room for the
element in the heap array, allocate a new heap array with twice as much room, copy all the
elements over, and then insert the new element.
(10pts)remove(index) : Remove the item at the given index, returning it.
If the index is invalid, panic.
(5pts)pop : Remove the last item from the end of Vec320. Return the removed item.
(5pts)contains(item) : Returns whether this Vec320 contains the given item.
(15pts)filter(Fn(T) -> bool) :
Takes as input a function that accepts an element of this Vec320 and
returns either true or false.
Returns a new Vec320, containing only the elements of this Vec320
that the function returns true for.
The original Vec320 is unchanged.
Include several unit tests for each of your functions.
Write the tests before your code, then stub out the function and make sure it compiles
(with tests failing), then complete the body of the code.
Do not start writing the next
function before completing the previous.
Part B (25pts)
Write a main() program that uses your Vec320 library to do the following:
Read a number from stdin: the number of test cases that follow.
For each test case, there are two lines of input, s1 and s2.
Read both lines of input, storing them in Vec320<u8> objects.
Remove all characters from both of the strings
except for lowercase letters a–z, using
the filter() function.
Afterwards, s1 and s2 contain only letters a–z.
byte literals: The byte corresponding to ASCII z can be
written as b'z'
After applying the filter, create a new merged string (Vec320<u8>) by alternating characters
between the two strings s1 and s2. Start with the first character of s1, followed by the first
character of s2, followed by the second character of s1, followed by the second character of s2,
etc..
Continue until one of the strings runs out of characters, and then, if the other string has
any characters left, append them to the end of the merged result.
Print the contents of your merged string to stdout.
Here is an example:
3
abc
def
ij
klmn
uvwx
yz
which would result in the output
adbecf
ikjlmn
uyvzwx
Note in the 2nd and 3rd inputs, how the “excess” letters of the longer
of the two strings are appended to the end.
deliverables
Submit your completed lib.rs and main.rs files to D2L by the posted deadline
no .zips, thx.
Also, bring hardcopy of these two files to the first class at/after the deadline.