Grafalgo
Library of useful data structures and algorithms
/Users/jst/src/grafalgo/cpp/dataStructures/basic/ListSet.cpp
Go to the documentation of this file.
00001 
00009 #include "ListSet.h"
00010 
00011 namespace grafalgo {
00012 
00018 ListSet::ListSet(int nitems, int nlists) : Adt(nitems), nlst(nlists) {
00019         makeSpace(nitems,nlists);
00020 }
00021 
00023 ListSet::~ListSet() { freeSpace(); }
00024 
00029 void ListSet::makeSpace(int nitems, int nlists) {
00030         try {
00031                 nxt = new index[nitems+1]; lh = new listhdr[nlists+1];
00032         } catch (std::bad_alloc e) {
00033                 stringstream ss;
00034                 ss << "ListSet::makeSpace: insufficient space for "
00035                    << nitems << "index values and " << nlists << " lists";
00036                 string s = ss.str();
00037                 throw OutOfSpaceException(s);
00038         }
00039         for (int i = 0; i <= nlst; i++) { lh[i].head = lh[i].tail = 0; }
00040         for (int i = 0; i <= nitems; i++) nxt[i] = -1;
00041         nn = nitems; nlst = nlists;
00042 }
00043 
00045 void ListSet::freeSpace() { delete [] lh; delete [] nxt; }
00046 
00048 void ListSet::clear() {
00049         for (alist ll = 1; ll <= nlst; ll++) {
00050                 while (!empty(ll)) removeFirst(ll);
00051         }
00052 }
00053 
00059 void ListSet::resize(int nitems, int nlists) {
00060         freeSpace();
00061         try { makeSpace(nitems,nlists); } catch(OutOfSpaceException e) {
00062                 string s; s = "ListSet::resize:" + e.toString(s);
00063                 throw OutOfSpaceException(s);
00064         }
00065 }
00066 
00071 void ListSet::expand(int nitems, int nlists) {
00072         if (nitems <= n() && nlists <= nlst) return;
00073         ListSet old(this->n(),this->nlst); old.copyFrom(*this);
00074         resize(nitems,nlists); this->copyFrom(old);
00075 }
00076 
00078 void ListSet::copyFrom(const ListSet& src) {
00079         if (&src == this) return;
00080         if (src.n() > n() || src.nlst > nlst) resize(src.n(),src.nlst);
00081         else clear();
00082         for (alist ll = 1; ll <= src.nlst; ll++) {
00083                 for (index x = src.first(ll); x != 0; x = src.next(ll))
00084                         addLast(x,ll);
00085         }
00086 }
00087 
00092 void ListSet::addLast(index i, alist j) {
00093         if (i == 0) return;
00094         if (lh[j].head == 0) lh[j].head = i;
00095         else nxt[lh[j].tail] = i;
00096         lh[j].tail = i; nxt[i] = 0;
00097 }
00098 
00103 int ListSet::removeFirst(alist j) {
00104         int i = lh[j].head;
00105         if (i == 0) return 0;
00106         lh[j].head = nxt[i]; nxt[i] = -1;
00107         return i;
00108 }
00109 
00114 void ListSet::addFirst(index i, alist j) {
00115         if (i == 0) return;
00116         if (lh[j].head == 0) lh[j].tail = i;
00117         nxt[i] = lh[j].head;
00118         lh[j].head = i;
00119 }
00120 
00126 string& ListSet::list2string(alist j, string& s) const {
00127         int i;
00128         s = "";
00129         s += j + ": ";
00130         for (i = first(j); i != 0; i = next(i)) {
00131                 string s1;
00132                 s += item2string(i,s1) + " ";
00133         }
00134         s += "\n";
00135         return s;
00136 }
00137 
00142 string& ListSet::toString(string& s) const {
00143         alist j;
00144         s = "";
00145         for (j = 1; j <= nlst; j++) {
00146                 string s1;
00147                 if (lh[j].head != 0) s += list2string(j,s1);
00148         }
00149         return s;
00150 }
00151 
00152 } // ends namespace
 All Classes Files Functions Variables Typedefs Friends