/* Written by Dale Bryant for 6121 - Computer Science II at Hudson Valley Community College - http://www.hvcc.edu/~bryandal/ Templated by Warren M Myers Upshift feature added 6 Feb 2001 deletes the first block.. run again for multiple deletions same effect as sliding all values up BLOCKSIZE, blowing away first BLOCKSIZE Insert feature added 15 Dec 2001 inserts a new chunk of BLOCKSIZE added to list at front end */ #ifndef _IA_PUB_H_ #define _IA_PUB_H_ using namespace std; #include template class InfiniteArray{ private: class Node{ public: Node(){ next = NULL;} IA data[BLOCKSIZE]; Node *next; }; Node *start; bool upshift(){ Node *t = start->next; if(NULL==t) return false; start = start->next; delete t; return true; } bool insert(){ Node *t = new Node; if(NULL==t){ cerr << "Infinite Array put Out of Memory"; return false; } t->next = start->next; start->next = t; return true; } public: typedef Node* NPtr; InfiniteArray(){ start = new Node;} ~InfiniteArray(){ NPtr tmp = start; while (NULL!=tmp){ delete tmp; start = start->next; tmp = start; } } IA & operator[](int index){ NPtr curptr=start; int ci = index; while (BLOCKSIZE<=ci){ if (NULL==curptr->next){ curptr->next = new Node; if (NULL==curptr){ cerr << "Infinite Array put Out of Memory \n"; exit(1); } } curptr = curptr->next; ci -= BLOCKSIZE; } return( curptr->data[ci]); } bool operator--(){ return upshift();} bool operator++(){ return insert();} }; #endif