Advertisement
Guest User

Untitled

a guest
May 6th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.77 KB | None | 0 0
  1. #ifndef EXPANDARRAY_H_INCLUDED
  2. #define EXPANDARRAY_H_INCLUDED
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <cstdlib>
  7. using namespace std;
  8.  
  9. const unsigned short MIN = 10;
  10.  
  11. class IOOR
  12. {
  13. const char* _message;
  14. const int _i;
  15. const int _size;
  16.  
  17. public:
  18. IOOR( const char* m, const int i, const int s )
  19. : _message(m), _i(i), _size(s) { };
  20.  
  21. friend ostream& operator<< ( ostream& os, const IOOR& p )
  22. {
  23. os << endl << endl << "Exception thrown in: ";
  24. os << p._message <<endl;
  25. os << "index: " << p._i << endl;
  26. os << "range 0 - " << (p._size-1) << endl << endl;
  27. return os;
  28. }
  29. };
  30.  
  31. template <class T>
  32. class ExpandArray
  33. {
  34.  
  35. T* _array; //dynamically allocated array of T
  36. unsigned _alloc; //size allocated to store
  37. unsigned _size;
  38. int _Vindex; //virtual index
  39. float _inc; // % we increase
  40. void _initial_alloc();
  41.  
  42. public:
  43.  
  44. // Constructors and Destructor
  45. ExpandArray( int i = 0, float p = .5 );
  46. // i is the index of the first element in your ExpandArray
  47. // p is the percentage that this ExpandArray will grow or shrink
  48. ExpandArray( const T*, unsigned s, int i = 0, float p = .5 );
  49. // i is the index of the first element in your ExpandArray
  50. // p is the percentage that this ExpandArray will grow or shrink
  51. // s is the number of items pointed to by T*
  52. ExpandArray( const ExpandArray& );
  53. ~ExpandArray( );//1
  54. ExpandArray& operator=( const ExpandArray& );
  55. // Insert the array or a pointer to an array into an out stream
  56. template<class OT>
  57. friend ostream& operator<<( ostream&, const ExpandArray<OT>& );
  58. template<class OT>
  59. friend ostream& operator<<( ostream&, const ExpandArray<OT>* );
  60. // access the item at index 0 <= i < size (assuming standard indexing)
  61. T& operator[]( int i );
  62. const T& operator[]( int i ) const; //same except const
  63. // Returns a sub array beginning at index first and going to index last-1
  64. ExpandArray operator( )( int first, int last )const;
  65. // Append either a new item or another ExpandArray beginning at index size
  66. void append ( const T& ); //2
  67. void append ( const ExpandArray& );
  68. // return the number of items in the array
  69. unsigned size () const;
  70. // Remove the item at index 0 <= i < size (assuming standard indexing)
  71. void remove( int i );
  72. // Remove the items from index first through index last-1
  73. void remove ( int first, int last );
  74. // Remove the item at index size-1 (assuming standard indexing)
  75. void remove( );
  76. // Insert at index 0 <= i < size (assuming standard indexing)
  77. void insert ( int i, const T& );
  78. // Insert at index 0 (assuming standard indexing)
  79. void insert ( const T& );
  80. //project 7
  81. void write(const char* filename) const;
  82. void read(const char* filename);
  83.  
  84. };
  85.  
  86. #endif // EXPANDARRAY_H_INCLUDED
  87.  
  88. template <class T>
  89. void ExpandArray<T>::_initial_alloc()
  90. {
  91. if(_array == 0)
  92. {
  93. _array = new T[MIN];
  94. _alloc = MIN;
  95. }
  96. }
  97.  
  98. template <class T>
  99. ExpandArray<T>::ExpandArray(int i, float p)
  100. {
  101. _array = NULL;
  102. _alloc = 0;
  103. _size = 0;
  104. _Vindex = i;
  105. _inc = p;
  106. _initial_alloc();
  107. }
  108.  
  109. template <class T>
  110. ExpandArray<T>::ExpandArray(const T* a, unsigned s, int i, float p)
  111. {
  112. _array = NULL;
  113. _alloc = 0;
  114. _size = 0;
  115. _Vindex = i;
  116. _inc = p;
  117. _initial_alloc();
  118.  
  119. for(unsigned j = 0; j < s; ++j)
  120. append (a[j]);
  121. }
  122.  
  123. template <class T>
  124. ExpandArray<T>::ExpandArray(const ExpandArray& ea)
  125. {
  126. _array = 0;
  127. _alloc = 0;
  128. _size = 0;
  129. _Vindex = ea._Vindex;
  130. cout << _Vindex << endl;
  131. _inc = ea._inc;
  132.  
  133. for(unsigned j = 0; j < ea._size; ++j)
  134. append(ea._array[j]);
  135. }
  136.  
  137. template <class T>
  138. ExpandArray<T>::~ExpandArray()
  139. {
  140. delete[] _array;
  141. }
  142.  
  143. template <class T>
  144. ExpandArray<T>& ExpandArray<T>::operator=( const ExpandArray<T>& ea )
  145. {
  146. if(this==&ea)
  147. return *this;
  148. if(_array!=NULL)
  149. {
  150. delete [] _array;
  151. _size=0;
  152. _alloc=0;
  153. }
  154. _array=NULL;
  155. _Vindex=ea._Vindex;
  156. _inc=ea._inc;
  157. append(ea);
  158. return *this;
  159. }
  160.  
  161. template<class OT>
  162. ostream& operator<<(ostream& os, const ExpandArray<OT>& ea)
  163. {
  164. cout << "alloc: " << ea._alloc << endl;
  165. cout << "size: " << ea._size<< endl;
  166. for(unsigned i = 0; i < ea._size; ++i)
  167. os << "array: " << ea._array[i] << endl;
  168. return os;
  169. }
  170.  
  171. template<class OT>
  172. ostream& operator<<(ostream& os, const ExpandArray<OT>* ea) //figure out how this should even be tested
  173. {
  174. cout << "inside reference ostream function" << endl;
  175. return operator<<(os, *ea);
  176. }
  177.  
  178. template<class T>
  179. T& ExpandArray<T>::operator[](int i)
  180. {
  181. cout << "regular[] function" << endl;
  182. int Aindex = i - _Vindex;
  183.  
  184. if(Aindex < 0 || Aindex >= (signed)_size)
  185. {
  186. /*cout << "Index Out of Range" << endl;
  187. cin.get();
  188. exit(200);*/ // old method
  189. throw IOOR("operator[]", Aindex, _size);
  190. }
  191. return _array[Aindex];
  192. }
  193.  
  194. template<class T>
  195. const T& ExpandArray<T>::operator[](int i) const
  196. {
  197. cout << "yo, you're in the const [] function." << endl;
  198. int Aindex = i - _Vindex;
  199.  
  200. if(Aindex < 0 || Aindex >= (signed)_size)
  201. throw IOOR("operator[]", Aindex, _size);
  202.  
  203. return _array[Aindex];
  204. //return ExpandArray<T>::operator[](i); //see if this is possible later...
  205. }
  206.  
  207. template<class T>
  208. ExpandArray<T> ExpandArray<T>::operator( )( int first, int last )const
  209. {
  210. int Aindex = first - _Vindex;
  211. int Zindex = last - _Vindex;
  212.  
  213. if(Aindex < 0 || Aindex >= (signed)_size)
  214. throw IOOR("operator()", Aindex, _size);
  215. else if(Zindex < Aindex || Zindex >= (signed)_size)
  216. throw IOOR("operator()", Zindex, _size);
  217.  
  218. ExpandArray<T> temp(_Vindex, _inc);
  219. for(int i = Aindex; i <= Zindex; ++i)
  220. temp.append(_array[i]);
  221. return temp;
  222. }
  223.  
  224. template <class T>
  225. void ExpandArray<T>::append(const T& t)
  226. {
  227. cout << "Appending..." << endl;
  228. if(_size >= _alloc)
  229. {
  230. _alloc += (_alloc * _inc) + 1;
  231. T* temp = new T[_alloc];
  232. for(unsigned i = 0; i < _size; ++i)
  233. temp[i] = _array[i];
  234.  
  235. delete []_array;
  236. _array = temp;
  237. }
  238. _array[_size++] = t;
  239. }
  240.  
  241. template <class T>
  242. void ExpandArray<T>::append(const ExpandArray<T>& ea)
  243. {
  244. cout << "Full Append..." << endl;
  245. for(unsigned i = 0; i < ea._size; ++i)
  246. ExpandArray<T>::append(ea._array[i]);
  247. }
  248.  
  249. template <class T>
  250. unsigned ExpandArray<T>::size() const
  251. {
  252. return _size;
  253. }
  254.  
  255. template <class T>
  256. void ExpandArray<T>::remove(int i)
  257. {
  258. cout << "In Remove" << endl;
  259. int Aindex = i - _Vindex;
  260.  
  261. if(Aindex < 0 || Aindex >= (signed)_size)
  262. throw IOOR("remove", Aindex, _size);
  263.  
  264. ExpandArray<T> temp(_Vindex, _inc);
  265. for(unsigned j = 0; j < _size; ++j)
  266. {
  267. if((signed)j != Aindex)
  268. temp.append(_array[j]);
  269. }
  270. *this = temp;
  271. }
  272.  
  273. template <class T>
  274. void ExpandArray<T>::remove(int first, int last)
  275. {
  276. cout << "First/Last Remove Function" << endl;
  277. int Aindex = first - _Vindex;
  278. int Zindex = last - _Vindex;
  279.  
  280. if(Aindex < 0 || Aindex >= (signed)_size)
  281. throw IOOR("remove", Aindex, _size);
  282. else if(Zindex < Aindex || Zindex >= (signed)_size)
  283. throw IOOR("remove", Zindex, _size);
  284.  
  285. for(int i = Aindex; i <= Zindex; ++i)
  286. {
  287. ExpandArray<T>::remove(i);
  288. --i;
  289. --Zindex;
  290. }
  291. }
  292.  
  293. template <class T>
  294. void ExpandArray<T>::remove()
  295. {
  296. cout << "no parameter remove function" << endl;
  297. ExpandArray<T>::remove(_size - 1);
  298. }
  299.  
  300. template <class T>
  301. void ExpandArray<T>::insert(int i, const T& t)
  302. {
  303. cout << "In Insert" << endl;
  304. int Aindex = i - _Vindex;
  305. bool put = false;
  306.  
  307. if(Aindex < 0 || Aindex >= (signed)_size)
  308. throw IOOR("insert", Aindex, _size);
  309.  
  310. ExpandArray<T> temp(_Vindex, _inc);
  311. for(unsigned j = 0; j < _size; ++j)
  312. {
  313. if((signed)j == Aindex)
  314. {
  315. temp.append(t);
  316. put = true;
  317. }
  318. else
  319. {
  320. if(put == true)
  321. {
  322. temp.append(_array[j - 1]);
  323. put = false;
  324. }
  325. temp.append(_array[j]);
  326. }
  327. }
  328. *this = temp;
  329. }
  330.  
  331. template <class T>
  332. void ExpandArray<T>::insert(const T& t)
  333. {
  334. cout << "In std insert" << endl;
  335. ExpandArray<T>::insert(0, t);
  336. }
  337.  
  338. /*template <class T>
  339. void ExpandArray<T>::write(const char* filename) const
  340. {
  341. ofstream fout(filename, ios::out | ios::binary);
  342. if(filename)
  343. {
  344. fout.write(reinterpret_cast<const char*>(&_Vindex), sizeof(_Vindex));
  345. fout.write(reinterpret_cast<const char*>(&_inc), sizeof(_inc));
  346. fout.write(reinterpret_cast<const char*>(&_size), sizeof(_size));
  347. fout.write(reinterpret_cast<const char*>(_array), sizeof(T) * _size);
  348.  
  349. fout.close();
  350. }
  351. }
  352.  
  353. template <class T>
  354. void ExpandArray<T>::read(const char* filename)
  355. {
  356. cout << "In read" << endl;
  357. T temparray[sizeof(T)];
  358. float tempinc;
  359. unsigned tempsize;
  360. int tempVindex;
  361.  
  362. ifstream fin(filename, ios::in | ios::binary);
  363. if(fin)
  364. {
  365.  
  366. fin.read(reinterpret_cast<char*>(&tempVindex), sizeof(_Vindex));
  367. fin.read(reinterpret_cast<char*>(&tempinc), sizeof(_inc));
  368. fin.read(reinterpret_cast<char*>(&tempsize), sizeof(_size));
  369. fin.read(reinterpret_cast<char*>(temparray), sizeof(T) * _size);
  370.  
  371. cout << temparray[3] << endl;
  372.  
  373. ExpandArray<T> temp(temparray, tempsize, tempVindex, tempinc);
  374. *this = temp;
  375.  
  376. fin.close();
  377. }
  378. }*/
  379.  
  380. template <class T>
  381. void ExpandArray<T>::write( const char* ch) const
  382. {
  383. ofstream fout(ch, ios::out | ios::binary);
  384. fout.write(reinterpret_cast<const char*> (&_Vindex), sizeof(_Vindex));
  385. fout.write(reinterpret_cast<const char*> (&_inc), sizeof(_inc));
  386. fout.write(reinterpret_cast<const char*> (&_size), sizeof(_size));
  387. fout.write(reinterpret_cast<const char*> (&_alloc), sizeof(_alloc));
  388. if(_array!=NULL)
  389. fout.write(reinterpret_cast<const char*>(_array), sizeof(T)*_size);
  390. fout.close();
  391. }
  392. template <class T>
  393. void ExpandArray<T>::read( const char* ch)
  394. {
  395. T array[sizeof(T)];
  396. unsigned alloc;
  397. unsigned size;
  398. int Vindex;
  399. float incr;
  400. ifstream fin(ch,ios::in | ios::binary);
  401. fin.read(reinterpret_cast<char*> (&Vindex), sizeof(_Vindex));
  402. fin.read(reinterpret_cast<char*> (&incr), sizeof(_inc));
  403. fin.read(reinterpret_cast<char*> (&size), sizeof(_size));
  404. fin.read(reinterpret_cast<char*> (&alloc), sizeof(_alloc));
  405. fin.read(reinterpret_cast<char*> (array), sizeof(T)*size);
  406. ExpandArray<T> temp(array,size,Vindex,incr);
  407. *this=temp;
  408. fin.close();
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement