1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#ifndef _FLEXARRAY_H_
#define _FLEXARRAY_H
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <exception>
#include <stdexcept>
using std::vector;
using std::endl;
using std::cerr;
using std::ostringstream;
using std::runtime_error;
using std::string;
class RangeEx : public runtime_error {
public:
RangeEx() : runtime_error("Index out of range") { }
explicit RangeEx(string st) : runtime_error(st) { }
};
template <typename T> //REQ: T has T() and T.str()
class FlexArray;
//template <typename T>
//class FlexArray::iterator;
template <typename T>
class ChunkNode {
vector<T>* elements;
ChunkNode* prev; //optional, to make doubly-linked list
ChunkNode<T>* next; //typename is illegal here
//FlexArray<T>* myList; //unnecessary?
size_t mySize; //INV: # of elements actually stored
friend class FlexArray<T>;
friend class FlexArray<T>::iterator; //begin lab with this line
public:
ChunkNode<T>(FlexArray<T>* myList, ChunkNode<T>* myPrev,
ChunkNode<T>* myNext)
: elements(new vector<T>()), prev(myPrev), next(myNext),
myList(myList), mySize(0)
{}
~ChunkNode<T>{
if ( prev )
prev->next = next ;
if ( next )
next->prev = prev ;
}
};
};
template <class T>
class FlexArray{
ChunkNode<T>* firstNode;
ChunkNode<T>* endNode;
size_t nodeCap;
size_t numNodes;
public:
explicit FlexArray<T>(size_t nodeCap)
: firstNode(new ChunkNode<T>(this,NULL,NULL))
, endNode(firstNode)
, nodeCap(nodeCap)
, numNodes(1)
{}
FlexArray<T>()
: firstNode(new ChunkNode<T>(this,NULL,NULL))
, endNode(firstNode)
, nodeCap(40)
, numNodes(1)
{ }
class iterator { //does not need "template <typename T>" since nested
const FlexArray<T> myFlex;
ChunkNode<T>* node;
size_t localIndex;
friend class FlexArray<T>;
iterator( const FlexArray<T>* holder, ChunkNode<T>* node, size_t index): myFlex(holder),node(node),localIndex(index){}
public:
T& operator*(){
if(node == NULL || node == myFlex-> endNode){
throw runtime_error("Attemt to de-reference end iterator");
}else{
return node->elements->at(localIndex);
}
}
iterator& operator++(){
if(node == myFlex -> endNode){
throw RangeEx("Attempted to move past the end positon");
}
else{if(node== NULL){
node = myFlex->firstNode;
localIndex = 0;
}
else{
localIndex++;
if(localIndex== node->mySize){
node=node->next;
localIndex=0;
}
}
}
return *this;
}
iterator operator++(int ignored){
iterator copy = *this;
operator++();
return copy;
}
bool operator==(const iterator& rhs) const{
return node== rhs.node && localIndex == rhs.localIndex&& myFlex == rhs.myFlex;
}
bool operator!=(const iterator& rhs) const{
return(!(*this == rhs));
}
};
iterator begin(){return iterator(this, firstNode, 0);}
iterator end(){return iterator(this, endNode, 0);}
iterator rbegin() {return iterator (this,endNode->prev, endNode->prev->MySize-1);}
T at(int j){
myFlex.begin();
for(int i =0; i<j; i++){
operator++();
}
return operator*();
iterator erase(iterator it){
if (it != end())
{
if ( firstNode == it.node)
firstNode = it.node->next ;
if ( lastNode == it.node)
lastNode == it.node->prev ;
--localIndex ;
delete (it++).node ;
}
return it ;
}
void insert(size_t j, const T& item){
ChunkNode<T> *newNode;
ChunkNode<T> *nodePtr;
ChunkNode<T> *previousNode = NULL;
newNode = new ChunkNode<T>;
newNode -> T = T;
nodePtr = firstNode;
previousNode = NULL;
for(int i = 0; i < j; ++i) { // iterate to the two nodes you want to insert between
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if (previousNode) { // if there is a previous node
previousNode->next = newNode; // point the next of the node before to the new node
newNode->next = nodePtr; // point the new node to the next node
} else { // there is no previous node, nodePtr must be at firstNode
firstNode = newNode; // point firstNode to new node
newNode->next = nodePtr; // point next to the old first
}
}
};
#endif
|