RU beehive logo promo banner for Computing & Info Sciences
CS 320
2025spring
ibarland

Vec implementation
hw03

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:

The following function implementations are required for this part. I recommend you handle the functions in the order given below:

  1. (5pts) new() : Create a new Vec320 object, which should be empty.
  2. (5pts) element_at(index) : Return the element at the given index. If the index is invalid, panic.
  3. (5pts) set(index, new_value) : Change the value at the given index to new_value. If the index is invalid, panic.
  4. (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.
  5. (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.
  6. (10pts) remove(index) : Remove the item at the given index, returning it. If the index is invalid, panic.
  7. (5pts) pop : Remove the last item from the end of Vec320. Return the removed item.
  8. (5pts) contains(item) : Returns whether this Vec320 contains the given item.
  9. (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:

  1. Read a number from stdin: the number of test cases that follow.
  2. For each test case, there are two lines of input, s1 and s2.
  3. 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.
  4. 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.


logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.