// Course: CS 141 // Lecture Section: Enter your lecture section number here (1, 2) // Lab Section: Enter your lab section number here (21, 22, 23) // Assignment #: 1 // First Name: Enter your FIRST name here (eg, John) // Last Name: Enter your LAST name here (eg, Doe) // ID Number: Enter your ID number here (eg, 12-345-678) // Email address: Enter your UCR email address here (eg, jdoe@cs.ucr.edu) // ======================================================================= #ifndef _ARRAY_H #define _ARRAY_H // growable array // use like this: // // #include "Array.h" // #include "Array.cc" // // ... // // Array h(-1); // -1 is the default value for uninitialized elements // Array g; // uninitialized elements of g have no default value // // h[2] = 3; // // for (int i = 0; i < h.size(); ++i) ... // // g = h; // make g a deep copy of h (including the default values) // // h.clear(); // reset h to initial state // // #include template class Array { VALUE *table; // NULL or pointer to new VALUE[table_size] int max_referenced; // largest index in table referenced so far int table_size; int use_default; // 0 or 1 - 1 iff initialize new cells VALUE default_init; // if use_default = 1, the default init value void copy(const Array& a); // copy a into this array void grow(); // replace the table with one twice as large // and copy the old contents into the new table public: ~Array(); // destructor Array(); // constructor without default, or... Array(const VALUE& def); // with default for newly allocated elements Array(const Array& a) { // copy constructor table = NULL; copy(a); } int size() const; // 1+largest index referenced so far VALUE & operator[](int i); // return a reference to the i'th elt // grow the table if necessary VALUE operator[](int i) const; // use only when i < size void clear(); // reset table to empty state // assignment operator Array operator=(const Array& a) { copy(a); return *this; } }; // print function for Array: template std::ostream & operator<<(std::ostream & out, const Array & a); #endif