Programming Assignment 5 - Array Sequences

Advanced Programming II, Fall 2002


Due Date

This assignment is due by 2:00 p.m. on Saturday, 16 November.

See the assignment turn-in page (last modified on 4 March 2002) for instructions on turning in your assignment.

Background

An array sequence is a data structure that acts like an array and a sequence. Data in an array sequence can be accessed via an index that starts at 0 and goes to n - 1 when the array sequence has n elements.

Two array sequences can be catenated together to form a new array sequence. The new array sequence contains n1 + n2 elements, where the original sequences contain n1 and n2 elements, respectively.

Given an array sequence as that contains n elements and two indices 0 <= i <= j <= n, as can be sliced at the index range (i, j) to form a new array sequence. The new array sequence contains j - i elements and has the following relation to the original array sequence:

for (k = 0; k < j - i; k++) as'[k] == as[i + k]

If j is less than i, the index range (i, j) is invalid.

The Problem

Implement the array sequence data structure; it should have the following interface prototype:

class array_sequence {

  public: 

    // The array-sequence element type.

       typedef int etype;

    // Create an empty array sequence.

       array_sequence();

    // Create an array sequence contining the single value e.

       array_sequence(const etype & e);

    // Create the array sequence containing, in left-to-right order, the values
    // contained in the pointer range (beg, end).

       array_sequence(const etype * beg, const etype * end);

    // Return the element at index i, or die with an error message if i is out
    // of range.

       etype get(unsigned i) const;

    // Replace the element at index i with the element e; die with an error
    // message if i is out of range.

       void put(unsigned i, const etype & e);

    // Return an array sequence holding the values in the index range (i, j);
    // die with an error message if (i, j) is not a valid index range.

       array_sequence slice(unsigned i, unsigned j) const;

    // Return the array sequence that is the catenation of this array sequence
    // with the given sequence.

       array_sequence concat(const array_sequence & as) const;

    // Return the number of elements in this sequence.

       unsigned size(void) const;
  };

Your implementation should be efficient in that all operations except for the range constructor should take sub-linear time to perform.


This page last modified on 12 November 2002.