C M S C     2 1 4
C o m p u t e r   S c i e n c e   I I
F a l l   2 0 0 2


ItemArrayList Specifications, Part 2

Updates

None so far.

Main Part

Several modifications are made to this class:

ItemArrayList

Add the following public method

Public Methods

bool erase( const ConstIterator &iter ) ;
This function will return true if the iterator lies between the endpoints of the ItemArrayList object. Recall that an iterator is basically a pointer. If it points to some valid element of the ItemArray, then it can erase that element.

A valid iterator points to a valid element in the ItemArrayList. Assume it points to element i. If you erase this element and the array has n elements, then elements from i + 1 to n - 1 are "shifted" left by 1, to index i up to n - 2.

Since erasing it causes the array to decrease in size, you must dynamically allocate a new array with a size that's one smaller than the current size, and copy the elements to the new array appropriately, as well as delete the old array (assuming it was allocated, and not NULL).

You must check that the iterator points to a valid element in the array. Hint: use the fact that begin() and end() determine the boundaries of the array, and that you can compare iterators in ItemArrayList (you may need to save them to ConstIterator variables prior to comparing them to iter). If it does not point to valid element, return false, and do not modify the ArrayList. Otherwise, if you successfully erase the element, return true.

If there is only one element in the list, and it is erased, you can set the array pointer to NULL.

Make sure to adjust the list size appropriately.

ItemArrayList operator+( const ItemArrayList & other ) const ;
Create a copy of the current ItemArrayList object by declaring a temp variable and copying *this to temp. Then, add the elements of other at the end of the list in temp. Return temp. temp's size should be the sum of the "this" array size and the "other" array size.

Note: this function does not alter either the array in * this nor the array in ItemArrayList.

ItemArrayList & operator+=( const ItemArrayList & other ) ;
Similar to operator+, but modifies *this. Then returns *this as result.

(10/7) This is operator+=, which should be a non-const method.

ItemArrayList::ConstIterator

This is a nested class inside the private section of ItemArrayList.

Add the following public data methods to give iterators "pointer arithmetic" capabilities.

Public Methods

int operator-( const ConstIterator & other ) const ;
Does the following subtraction: curr - other.curr where curr is the data member in iterator. This does pointer subtraction, and figures out the distance between two iterators. For example, if curr points to element 5 and other.curr points to element 2, the result is 3.

Note: pointer subtraction can result in negative values (for example curr points to element 2, and other.curr to element 5, results in -3).

Precondition: the two iterators point to elements in the same ItemArrayList.

ConstIterator operator+( int offset ) const ;
Returns a ConstIterator where points to the element that is offset elements forward from curr of the current object. You can either make it "safe" (i.e., it never goes past cend()) or just let it advance off the list.

To implement, make a copy of the current iterator, and set curr in the copy to be curr + offset. This uses pointer arithmetic to move curr forward by offset in the copy. Returns the copy. The current iterator's curr should not be changed.

ConstIterator operator-( int offset ) const ;
Returns a Iterator where points to the element that is offset elements backward from curr of the current object. You can either make it "safe" (i.e., it never goes past cbegin()) or just let it go out of bounds from the array (which isn't such a bad thing, really).

To implement, make a copy of the current iterator, and set curr in the copy to be curr - offset. This uses pointer arithmetic to move curr backwards by offset in the copy. Returns the copy. The current iterator's curr should not be changed.

ConstIterator & operator+=( int offset ) ;
Set curr to curr + offset (which moves curr forward by offset), and returns * this.
ConstIterator & operator-=( int offset ) ;
Set curr to curr - offset (which moves curr backward by offset), and returns * this.

ItemArrayList::Iterator

This is a nested class inside the private section of ItemArrayList.

Add the following public data methods to give iterators "pointer arithmetic" capabilities.

Public Methods

int operator-( const Iterator & iter ) const ;
Does the following subtraction: curr - other.curr where curr is the data member in iterator. This does pointer subtraction, and figures out the distance between two iterators. For example, if curr points to element 5 and other.curr points to element 2, the result is 3.

Note: pointer subtraction can result in negative values (for example curr points to element 2, and other.curr to element 5, results in -3).

Precondition: the two iterators point to elements in the same ArrayList.

Iterator operator+( int offset ) const ;
Returns a Iterator where points to the element that is offset elements forward from curr of the current object. You can either make it "safe" (i.e., it never goes past cend()) or just let it advance off the list. It's easier to do the second.

To implement, make a copy of the current iterator, and set curr in the copy to be curr + offset. This uses pointer arithmetic to move curr forward by offset in the copy. Returns the copy. The current iterator's curr should not be changed.

Iterator operator-( int offset ) const ;
Returns a Iterator where points to the element that is offset elements backward from curr of the current object. You can either make it "safe" (i.e., it never goes past cbegin()) or just let it go out of bounds from the array (which isn't such a bad thing, really).

To implement, make a copy of the current iterator, and set curr in the copy to be curr - offset. This uses pointer arithmetic to move curr backwards by offset in the copy. Returns the copy. The current iterator's curr should not be changed.

Iterator & operator+=( int offset ) ;
Set curr to curr + offset (which moves curr forward by offset), and returns * this.
Iterator & operator-=( int offset ) ;
Set curr to curr - offset (which moves curr backward by offset), and returns * this.


See the class syllabus for policies concerning email
Last Modified: Tue Sep 17 19:01:23 EDT 2002
left up down right home

Web Accessibility