Grafalgo
Library of useful data structures and algorithms
|
00001 00009 #include "Dlist.h" 00010 00011 namespace grafalgo { 00012 00017 Dlist::Dlist(int n1) : List(n1) { 00018 assert(n() >= 0); makeSpace(n()); 00019 } 00020 00022 Dlist::~Dlist() { freeSpace(); } 00023 00027 void Dlist::makeSpace(int size) { 00028 try { prv = new index[size+1]; } catch (std::bad_alloc e) { 00029 stringstream ss; 00030 ss << "makeSpace:: insufficient space for " 00031 << size << "index values"; 00032 string s = ss.str(); 00033 throw OutOfSpaceException(s); 00034 } 00035 for (index i = 1; i <= size; i++) prv[i] = -1; 00036 prv[0] = 0; 00037 nn = size; 00038 } 00039 00041 void Dlist::freeSpace() { delete [] prv; } 00042 00047 void Dlist::resize(int size) { 00048 List::resize(size); freeSpace(); makeSpace(size); 00049 } 00050 00055 void Dlist::expand(int size) { 00056 if (size <= n()) return; 00057 Dlist old(this->n()); old.copyFrom(*this); 00058 resize(size); this->copyFrom(old); 00059 } 00060 00066 index Dlist::get(int i) const { 00067 if (i >= 0) return List::get(i); 00068 index j; 00069 for (j = last(); j != 0 && ++i; j = prv[j]) {} 00070 return j; 00071 } 00072 00080 bool Dlist::insert(index i, index j) { 00081 if (!List::insert(i,j)) return false; 00082 // now update prv 00083 prv[i] = j; 00084 if (next(i) != 0) prv[next(i)] = i; 00085 return true; 00086 } 00087 00092 bool Dlist::remove(index i) { 00093 if (i == 0 || !List::removeNext(prev(i))) 00094 return false; 00095 if (prv[i] == 0) prv[first()] = 0; 00096 else { 00097 index j = prv[i]; 00098 if (next(j) != 0) prv[next(j)] = j; 00099 } 00100 prv[i] = -1; 00101 return true; 00102 } 00103 00104 } // ends namespace