Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef EXPANDARRAY_H_INCLUDED
- #define EXPANDARRAY_H_INCLUDED
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- using namespace std;
- const unsigned short MIN = 10;
- class IOOR
- {
- const char* _message;
- const int _i;
- const int _size;
- public:
- IOOR( const char* m, const int i, const int s )
- : _message(m), _i(i), _size(s) { };
- friend ostream& operator<< ( ostream& os, const IOOR& p )
- {
- os << endl << endl << "Exception thrown in: ";
- os << p._message <<endl;
- os << "index: " << p._i << endl;
- os << "range 0 - " << (p._size-1) << endl << endl;
- return os;
- }
- };
- template <class T>
- class ExpandArray
- {
- T* _array; //dynamically allocated array of T
- unsigned _alloc; //size allocated to store
- unsigned _size;
- int _Vindex; //virtual index
- float _inc; // % we increase
- void _initial_alloc();
- public:
- // Constructors and Destructor
- ExpandArray( int i = 0, float p = .5 );
- // i is the index of the first element in your ExpandArray
- // p is the percentage that this ExpandArray will grow or shrink
- ExpandArray( const T*, unsigned s, int i = 0, float p = .5 );
- // i is the index of the first element in your ExpandArray
- // p is the percentage that this ExpandArray will grow or shrink
- // s is the number of items pointed to by T*
- ExpandArray( const ExpandArray& );
- ~ExpandArray( );//1
- ExpandArray& operator=( const ExpandArray& );
- // Insert the array or a pointer to an array into an out stream
- template<class OT>
- friend ostream& operator<<( ostream&, const ExpandArray<OT>& );
- template<class OT>
- friend ostream& operator<<( ostream&, const ExpandArray<OT>* );
- // access the item at index 0 <= i < size (assuming standard indexing)
- T& operator[]( int i );
- const T& operator[]( int i ) const; //same except const
- // Returns a sub array beginning at index first and going to index last-1
- ExpandArray operator( )( int first, int last )const;
- // Append either a new item or another ExpandArray beginning at index size
- void append ( const T& ); //2
- void append ( const ExpandArray& );
- // return the number of items in the array
- unsigned size () const;
- // Remove the item at index 0 <= i < size (assuming standard indexing)
- void remove( int i );
- // Remove the items from index first through index last-1
- void remove ( int first, int last );
- // Remove the item at index size-1 (assuming standard indexing)
- void remove( );
- // Insert at index 0 <= i < size (assuming standard indexing)
- void insert ( int i, const T& );
- // Insert at index 0 (assuming standard indexing)
- void insert ( const T& );
- //project 7
- void write(const char* filename) const;
- void read(const char* filename);
- };
- #endif // EXPANDARRAY_H_INCLUDED
- template <class T>
- void ExpandArray<T>::_initial_alloc()
- {
- if(_array == 0)
- {
- _array = new T[MIN];
- _alloc = MIN;
- }
- }
- template <class T>
- ExpandArray<T>::ExpandArray(int i, float p)
- {
- _array = NULL;
- _alloc = 0;
- _size = 0;
- _Vindex = i;
- _inc = p;
- _initial_alloc();
- }
- template <class T>
- ExpandArray<T>::ExpandArray(const T* a, unsigned s, int i, float p)
- {
- _array = NULL;
- _alloc = 0;
- _size = 0;
- _Vindex = i;
- _inc = p;
- _initial_alloc();
- for(unsigned j = 0; j < s; ++j)
- append (a[j]);
- }
- template <class T>
- ExpandArray<T>::ExpandArray(const ExpandArray& ea)
- {
- _array = 0;
- _alloc = 0;
- _size = 0;
- _Vindex = ea._Vindex;
- cout << _Vindex << endl;
- _inc = ea._inc;
- for(unsigned j = 0; j < ea._size; ++j)
- append(ea._array[j]);
- }
- template <class T>
- ExpandArray<T>::~ExpandArray()
- {
- delete[] _array;
- }
- template <class T>
- ExpandArray<T>& ExpandArray<T>::operator=( const ExpandArray<T>& ea )
- {
- if(this==&ea)
- return *this;
- if(_array!=NULL)
- {
- delete [] _array;
- _size=0;
- _alloc=0;
- }
- _array=NULL;
- _Vindex=ea._Vindex;
- _inc=ea._inc;
- append(ea);
- return *this;
- }
- template<class OT>
- ostream& operator<<(ostream& os, const ExpandArray<OT>& ea)
- {
- cout << "alloc: " << ea._alloc << endl;
- cout << "size: " << ea._size<< endl;
- for(unsigned i = 0; i < ea._size; ++i)
- os << "array: " << ea._array[i] << endl;
- return os;
- }
- template<class OT>
- ostream& operator<<(ostream& os, const ExpandArray<OT>* ea) //figure out how this should even be tested
- {
- cout << "inside reference ostream function" << endl;
- return operator<<(os, *ea);
- }
- template<class T>
- T& ExpandArray<T>::operator[](int i)
- {
- cout << "regular[] function" << endl;
- int Aindex = i - _Vindex;
- if(Aindex < 0 || Aindex >= (signed)_size)
- {
- /*cout << "Index Out of Range" << endl;
- cin.get();
- exit(200);*/ // old method
- throw IOOR("operator[]", Aindex, _size);
- }
- return _array[Aindex];
- }
- template<class T>
- const T& ExpandArray<T>::operator[](int i) const
- {
- cout << "yo, you're in the const [] function." << endl;
- int Aindex = i - _Vindex;
- if(Aindex < 0 || Aindex >= (signed)_size)
- throw IOOR("operator[]", Aindex, _size);
- return _array[Aindex];
- //return ExpandArray<T>::operator[](i); //see if this is possible later...
- }
- template<class T>
- ExpandArray<T> ExpandArray<T>::operator( )( int first, int last )const
- {
- int Aindex = first - _Vindex;
- int Zindex = last - _Vindex;
- if(Aindex < 0 || Aindex >= (signed)_size)
- throw IOOR("operator()", Aindex, _size);
- else if(Zindex < Aindex || Zindex >= (signed)_size)
- throw IOOR("operator()", Zindex, _size);
- ExpandArray<T> temp(_Vindex, _inc);
- for(int i = Aindex; i <= Zindex; ++i)
- temp.append(_array[i]);
- return temp;
- }
- template <class T>
- void ExpandArray<T>::append(const T& t)
- {
- cout << "Appending..." << endl;
- if(_size >= _alloc)
- {
- _alloc += (_alloc * _inc) + 1;
- T* temp = new T[_alloc];
- for(unsigned i = 0; i < _size; ++i)
- temp[i] = _array[i];
- delete []_array;
- _array = temp;
- }
- _array[_size++] = t;
- }
- template <class T>
- void ExpandArray<T>::append(const ExpandArray<T>& ea)
- {
- cout << "Full Append..." << endl;
- for(unsigned i = 0; i < ea._size; ++i)
- ExpandArray<T>::append(ea._array[i]);
- }
- template <class T>
- unsigned ExpandArray<T>::size() const
- {
- return _size;
- }
- template <class T>
- void ExpandArray<T>::remove(int i)
- {
- cout << "In Remove" << endl;
- int Aindex = i - _Vindex;
- if(Aindex < 0 || Aindex >= (signed)_size)
- throw IOOR("remove", Aindex, _size);
- ExpandArray<T> temp(_Vindex, _inc);
- for(unsigned j = 0; j < _size; ++j)
- {
- if((signed)j != Aindex)
- temp.append(_array[j]);
- }
- *this = temp;
- }
- template <class T>
- void ExpandArray<T>::remove(int first, int last)
- {
- cout << "First/Last Remove Function" << endl;
- int Aindex = first - _Vindex;
- int Zindex = last - _Vindex;
- if(Aindex < 0 || Aindex >= (signed)_size)
- throw IOOR("remove", Aindex, _size);
- else if(Zindex < Aindex || Zindex >= (signed)_size)
- throw IOOR("remove", Zindex, _size);
- for(int i = Aindex; i <= Zindex; ++i)
- {
- ExpandArray<T>::remove(i);
- --i;
- --Zindex;
- }
- }
- template <class T>
- void ExpandArray<T>::remove()
- {
- cout << "no parameter remove function" << endl;
- ExpandArray<T>::remove(_size - 1);
- }
- template <class T>
- void ExpandArray<T>::insert(int i, const T& t)
- {
- cout << "In Insert" << endl;
- int Aindex = i - _Vindex;
- bool put = false;
- if(Aindex < 0 || Aindex >= (signed)_size)
- throw IOOR("insert", Aindex, _size);
- ExpandArray<T> temp(_Vindex, _inc);
- for(unsigned j = 0; j < _size; ++j)
- {
- if((signed)j == Aindex)
- {
- temp.append(t);
- put = true;
- }
- else
- {
- if(put == true)
- {
- temp.append(_array[j - 1]);
- put = false;
- }
- temp.append(_array[j]);
- }
- }
- *this = temp;
- }
- template <class T>
- void ExpandArray<T>::insert(const T& t)
- {
- cout << "In std insert" << endl;
- ExpandArray<T>::insert(0, t);
- }
- /*template <class T>
- void ExpandArray<T>::write(const char* filename) const
- {
- ofstream fout(filename, ios::out | ios::binary);
- if(filename)
- {
- fout.write(reinterpret_cast<const char*>(&_Vindex), sizeof(_Vindex));
- fout.write(reinterpret_cast<const char*>(&_inc), sizeof(_inc));
- fout.write(reinterpret_cast<const char*>(&_size), sizeof(_size));
- fout.write(reinterpret_cast<const char*>(_array), sizeof(T) * _size);
- fout.close();
- }
- }
- template <class T>
- void ExpandArray<T>::read(const char* filename)
- {
- cout << "In read" << endl;
- T temparray[sizeof(T)];
- float tempinc;
- unsigned tempsize;
- int tempVindex;
- ifstream fin(filename, ios::in | ios::binary);
- if(fin)
- {
- fin.read(reinterpret_cast<char*>(&tempVindex), sizeof(_Vindex));
- fin.read(reinterpret_cast<char*>(&tempinc), sizeof(_inc));
- fin.read(reinterpret_cast<char*>(&tempsize), sizeof(_size));
- fin.read(reinterpret_cast<char*>(temparray), sizeof(T) * _size);
- cout << temparray[3] << endl;
- ExpandArray<T> temp(temparray, tempsize, tempVindex, tempinc);
- *this = temp;
- fin.close();
- }
- }*/
- template <class T>
- void ExpandArray<T>::write( const char* ch) const
- {
- ofstream fout(ch, ios::out | ios::binary);
- fout.write(reinterpret_cast<const char*> (&_Vindex), sizeof(_Vindex));
- fout.write(reinterpret_cast<const char*> (&_inc), sizeof(_inc));
- fout.write(reinterpret_cast<const char*> (&_size), sizeof(_size));
- fout.write(reinterpret_cast<const char*> (&_alloc), sizeof(_alloc));
- if(_array!=NULL)
- fout.write(reinterpret_cast<const char*>(_array), sizeof(T)*_size);
- fout.close();
- }
- template <class T>
- void ExpandArray<T>::read( const char* ch)
- {
- T array[sizeof(T)];
- unsigned alloc;
- unsigned size;
- int Vindex;
- float incr;
- ifstream fin(ch,ios::in | ios::binary);
- fin.read(reinterpret_cast<char*> (&Vindex), sizeof(_Vindex));
- fin.read(reinterpret_cast<char*> (&incr), sizeof(_inc));
- fin.read(reinterpret_cast<char*> (&size), sizeof(_size));
- fin.read(reinterpret_cast<char*> (&alloc), sizeof(_alloc));
- fin.read(reinterpret_cast<char*> (array), sizeof(T)*size);
- ExpandArray<T> temp(array,size,Vindex,incr);
- *this=temp;
- fin.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement