Grafalgo
Library of useful data structures and algorithms
|
00001 00008 #include "RlistSet.h" 00009 00010 namespace grafalgo { 00011 00015 RlistSet::RlistSet(int nn) : Adt(nn) { makeSpace(n()); } 00016 00018 RlistSet::~RlistSet() { freeSpace(); } 00019 00023 void RlistSet::makeSpace(int size) { 00024 try { 00025 node = new ListNode[size+1]; canon = new bool[size+1]; 00026 } catch (std::bad_alloc e) { 00027 stringstream ss; 00028 ss << "RlistSet::makeSpace: insufficient space for " 00029 << size << "index values"; 00030 string s = ss.str(); 00031 throw OutOfSpaceException(s); 00032 } 00033 nn = size; clear(); 00034 } 00035 00037 void RlistSet::freeSpace() { delete [] node; } 00038 00043 void RlistSet::resize(int size) { 00044 freeSpace(); 00045 try { makeSpace(size); } catch(OutOfSpaceException e) { 00046 string s; s = "RlistSet::resize:" + e.toString(s); 00047 throw OutOfSpaceException(s); 00048 } 00049 } 00050 00055 void RlistSet::expand(int size) { 00056 if (size <= n()) return; 00057 RlistSet old(this->n()); old.copyFrom(*this); 00058 resize(size); this->copyFrom(old); 00059 } 00060 00062 void RlistSet::clear() { 00063 for (index x = 0; x <= n(); x++) { 00064 node[x].p1 = node[x].p2 = x; canon[x] = true; 00065 } 00066 } 00067 00069 void RlistSet::copyFrom(const RlistSet& source) { 00070 if (&source == this) return; 00071 if (source.n() > n()) resize(source.n()); 00072 else clear(); 00073 for (index x = 1; x <= source.n(); x++) { 00074 node[x].p1 = source.node[x].p1; 00075 node[x].p2 = source.node[x].p2; 00076 canon[x] = source.canon[x]; 00077 } 00078 } 00079 00086 index RlistSet::pop(index t) { 00087 index h = first(t); 00088 if (h == t) return h; 00089 index nuHead = suc(h,t); 00090 if (node[h].p2 == t) node[t].p1 = node[h].p1; 00091 else node[t].p1 = node[h].p2; 00092 if (node[nuHead].p1 == h) node[nuHead].p1 = t; 00093 else node[nuHead].p2 = t; 00094 node[h].p1 = node[h].p2 = h; 00095 canon[h] = true; 00096 return t; 00097 } 00098 00105 index RlistSet::join(index t1, index t2) { 00106 if (t1 == 0) return t2; 00107 else if (t2 == 0 || t2 == t1) return t1; 00108 00109 index h1 = node[t1].p1; index h2 = node[t2].p1; 00110 node[t1].p1 = h2; node[t2].p1 = h1; 00111 if (t1 == node[h1].p2) node[h1].p2 = t2; 00112 else node[h1].p1 = t2; 00113 if (t2 == node[h2].p2) node[h2].p2 = t1; 00114 else node[h2].p1 = t1; 00115 00116 canon[t1] = false; 00117 return t2; 00118 } 00119 00125 index RlistSet::reverse(index t) { 00126 index h = first(t); 00127 if (t == 0 || h == t) return t; 00128 if (t == node[h].p2) node[h].p2 = node[h].p1; 00129 node[h].p1 = t; 00130 canon[h] = true; canon[t] = false; 00131 return h; 00132 } 00133 00139 string& RlistSet::toString(string& s) const { 00140 s = ""; 00141 for (index x = 1; x <= n(); x++) { 00142 if (canon[x] && first(x) != x) { 00143 string s1; s += toString(x,s1) + "\n"; 00144 } 00145 } 00146 return s; 00147 } 00148 00154 string& RlistSet::toString(index t, string& s) const { 00155 index h = first(t); 00156 s = "[ "; string s1; 00157 if (t == 0) s += "-"; 00158 else if (h == t) s += Adt::item2string(h,s1) + " "; 00159 else { 00160 index x = h; index y = t; 00161 do { 00162 s += item2string(x,s1) + " "; 00163 advance(x,y); 00164 } while (x != h); 00165 } 00166 s += "]"; 00167 return s; 00168 } 00169 00170 } // ends namespace