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
|
/*
* PROPRIETARY INFORMATION. This software is proprietary to POWDER
* Development, and is not to be reproduced, transmitted, or disclosed
* in any way without written permission.
*
* Produced by: Jeff Lait
*
* POWDER Development
*
* NAME: itemstack.cpp ( POWDER Library, C++ )
*
* COMMENTS:
* Implementation of the itemstack functions
*/
#include <string.h>
#include "assert.h"
#include "itemstack.h"
template <typename PTR>
PTRSTACK<PTR>::PTRSTACK()
{
myEntries = 0;
myExtraSize = 0;
myExtraList = 0;
}
template <typename PTR>
PTRSTACK<PTR>::~PTRSTACK()
{
delete [] myExtraList;
}
template <typename PTR>
void
PTRSTACK<PTR>::append(PTR item)
{
if (myEntries >= PTRSTACK_MINSIZE)
{
// Need to add to the extra stack.
if (myEntries >= PTRSTACK_MINSIZE + myExtraSize)
{
// Need to expand the extra stack.
PTR *newlist;
myExtraSize = myExtraSize * 2 + PTRSTACK_MINSIZE;
newlist = new PTR[myExtraSize];
if (myExtraList)
memcpy(newlist, myExtraList,
sizeof(PTR) * (myEntries - PTRSTACK_MINSIZE));
delete [] myExtraList;
myExtraList = newlist;
}
myExtraList[myEntries - PTRSTACK_MINSIZE] = item;
}
else
myLocal[myEntries] = item;
myEntries++;
}
template <typename PTR>
void
PTRSTACK<PTR>::set(int idx, PTR item)
{
UT_ASSERT(idx < entries());
UT_ASSERT(idx >= 0);
if (idx < 0)
return;
if (idx >= entries())
return;
if (idx >= PTRSTACK_MINSIZE)
{
idx -= PTRSTACK_MINSIZE;
myExtraList[idx] = item;
}
else
myLocal[idx] = item;
}
template <typename PTR>
void
PTRSTACK<PTR>::setEntries(int entries)
{
UT_ASSERT(entries <= myEntries);
if (entries > myEntries)
return;
myEntries = entries;
}
template <typename PTR>
PTR
PTRSTACK<PTR>::operator()(int idx) const
{
UT_ASSERT(idx >= 0 && idx < myEntries);
if (idx < 0 || idx >= myEntries)
{
// This is so you only have to implement an int constructor.
return 0;
}
if (idx < PTRSTACK_MINSIZE)
return myLocal[idx];
idx -= PTRSTACK_MINSIZE;
return myExtraList[idx];
}
template <typename PTR>
int
PTRSTACK<PTR>::entries() const
{
return myEntries;
}
template <typename PTR>
void
PTRSTACK<PTR>::clear()
{
setEntries(0);
}
template <typename PTR>
void
PTRSTACK<PTR>::collapse()
{
int i, j;
for (i = 0, j = 0; i < entries(); i++)
{
if (!(*this)(i))
{
// Don't increment j!
}
else
{
if (i != j)
set(j++, (*this)(i));
else
j++;
}
}
// Update number of entries.
setEntries(j);
}
template <typename PTR>
void
PTRSTACK<PTR>::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);
}
}
|