ClassS04CS141/Array.H

ClassS04CS141 | ClassS04CS141 | recent changes | Preferences

No diff available--this is the first major revision. (no other diffs)
#ifndef _ARRAY_H
#define _ARRAY_H

// growable array

// use like this:
//
//  #include "Array.h"
//  #include "Array.cc"
// 
//  ...
//
//  Array<int> h(-1);   // -1 is the default value for uninitialized elements
//  Array<int> g;         // uninitialized elements of g have no default value
//  Array<int> f(h);      // copy constructor
//
//  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 <iostream.h>

template <class VALUE>
class Array {
	
	VALUE *table;
	int max_referenced;
	int table_size;
	
	int use_default;
	VALUE default_init;
	void copy(const Array& a);	// copy a into this array
	
	void grow();	  // replace the table with one twice as large
				// copy the old contents into the new table
public:
	Array();			// constructor without default, or...
	Array(const VALUE& def);	// with default for newly allocated elements
	VALUE & operator[](int i);	// return a reference to the i'th elt
                                        // grow the table if necessary
	~Array();			// destructor
	int size() const;		// 1+largest index referenced so far
	
public:

	VALUE operator[](int i) const;  // use only when i < size
		
	void clear();

	// copy constructor and assignment op		

	Array operator=(const Array& a) { copy(a); return *this; }
	Array(const Array& a) { table = NULL; copy(a); }
};

#endif

ClassS04CS141 | ClassS04CS141 | recent changes | Preferences
This page is read-only | View other revisions
Last edited April 5, 2004 2:54 pm by Neal (diff)
Search: