/* * Licensed under BSD license. See LICENCE.TXT * * Produced by: Jeff Lait * * 7DRL Development * * NAME: ptrlist.cpp ( Utility Library, C++ ) * * COMMENTS: * Implementation of the ptrlist functions */ #include #include "assert.h" #include "ptrlist.h" template PTRLIST::PTRLIST() { myEntries = 0; mySize = 0; myList = 0; myStartPos = 0; } template PTRLIST::~PTRLIST() { delete [] myList; } template PTRLIST::PTRLIST(const PTRLIST &ref) { myList = 0; *this = ref; } template PTRLIST & PTRLIST::operator=(const PTRLIST &ref) { int i; if (this == &ref) return *this; delete [] myList; myList = new PTR[ref.myEntries]; mySize = ref.myEntries; myEntries = ref.myEntries; // Reset our list. myStartPos = 0; for (i = 0; i < myEntries; i++) { set(i, ref(i)); } return *this; } template void PTRLIST::append(PTR item) { if (myEntries + myStartPos >= mySize) { // Need to expand the extra stack. PTR *newlist; mySize = mySize * 2 + 10; newlist = new PTR[mySize]; if (myList) { for (int i = 0; i < myEntries; i++) newlist[i] = myList[i+myStartPos]; } delete [] myList; myList = newlist; // Take this chance to reset our leading edge. myStartPos = 0; } myList[myEntries+myStartPos] = item; myEntries++; } template void PTRLIST::append(const PTRLIST &list) { int i; for (i = 0; i < list.entries(); i++) append(list(i)); } template void PTRLIST::resize(int size) { while (myEntries < size) append((PTR) 0); myEntries = size; } template void PTRLIST::set(int idx, PTR item) { UT_ASSERT(idx < entries()); UT_ASSERT(idx >= 0); if (idx < 0) return; if (idx >= entries()) return; myList[idx + myStartPos] = item; } template void PTRLIST::insert(int idx, PTR item) { int offset; // Deal with simple case. if (idx == entries()) { append(item); return; } UT_ASSERT(idx < entries()); UT_ASSERT(idx >= 0); // Duplicate end. append(top()); // Now make room for (offset = entries()-1; offset > idx; offset--) { set(offset, (*this)(offset-1)); } set(idx, item); } template PTR PTRLIST::operator()(int idx) const { UT_ASSERT(idx >= 0 && idx < myEntries); return myList[idx + myStartPos]; } template int PTRLIST::entries() const { return myEntries; } template void PTRLIST::clear() { myEntries = 0; // Likewise, might as well reset our offset myStartPos = 0; } template void PTRLIST::constant(PTR val) { int i; for (i = 0; i < myEntries; i++) set(i, val); } template void PTRLIST::reverse() { PTR tmp; int i, n, r; n = entries(); for (i = 0; i < n / 2; i++) { // Reversed entry. r = n - 1 - i; tmp = (*this)(i); set(i, (*this)(r)); set(r, tmp); } } template void PTRLIST::removePtr(PTR item) { int s, d, n; n = entries(); s = d = 0; while (s < n) { if ((*this)(s) == item) { // Don't copy this value. } else { // Copy this value. if (s != d) set(d, (*this)(s)); d++; } // Go to next value. s++; } // Final n is d. myEntries = d; } template void PTRLIST::removeAt(int idx) { int s, n; UT_ASSERT(idx >= 0); UT_ASSERT(idx < entries()); n = entries(); for (s = idx; s < n-1; s++) { set(s, (*this)(s+1)); } myEntries--; } template PTR PTRLIST::pop() { int n; PTR result; n = entries(); if (!n) { // We should do an inplace new on result here so it // works with all types. // Inplace newis buggy for enums on AMD64, however, so I'll // play it safe. result = 0; return result; } result = (*this)(n-1); myEntries = n-1; return result; } template void PTRLIST::swapEntries(int i1, int i2) { if (i1 == i2) return; PTR tmp; tmp = (*this)(i1); set(i1, (*this)(i2)); set(i2, tmp); } template PTR PTRLIST::removeFirst() { int n; PTR result; n = entries(); if (!n) { // We should do an inplace new on result here so it // works with all types. // Inplace newis buggy for enums on AMD64, however, so I'll // play it safe. return (PTR) 0; } result = (*this)(0); // A little bit more efficient than moving. // We wait until the next resize to bother with clearing up // since at that resize we have to move anyways. myStartPos++; myEntries--; return result; } template int PTRLIST::find(PTR item) const { int i, n; n = entries(); for (i = 0; i < n; i++) { if ((*this)(i) == item) return i; } return -1; } template void PTRLIST::collapse() { int i, j, n; n = entries(); for (i = j = 0; i < n; i++) { if ((*this)(i)) { // Valid... if (i != j) set(j, (*this)(i)); j++; } else { // Do not copy over, do not inc j. } } myEntries = j; }