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
|
/*____________________________________________________________________________
FreeAmp - The Free MP3 Player
Portions Copyright (C) 1998 GoodNoise
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Id: vector.h,v 1.1.1.1 1998/10/09 00:07:09 jdw Exp $
____________________________________________________________________________*/
// vector.h
#ifndef _VECTOR_H_
#define _VECTOR_H_
#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include "config.h"
template<class T>
class Vector {
private:
T *pObjs; // pointer to array of objects
int32 threshhold;
int32 insertionPoint;
int32 currentLength;
public:
Vector(int32 th = 10); // set threshhold to its arg
~Vector();
int32 RemoveAll();
int32 DeleteAll();
T elementAt(int32);
T removeElementAt(int32);
int32 deleteElementAt(int32);
int32 insert(T &);
int32 numElements();
};
template<class T> Vector<T>::Vector(int32 th) {
threshhold = th;
pObjs = new T[threshhold];
for (int32 i=0;i<threshhold;i++) pObjs[i] = NULL;
insertionPoint = 0;
currentLength = threshhold;
}
template<class T> Vector<T>::~Vector() {
delete pObjs;
}
template<class T> int32 Vector<T>::numElements() {
return insertionPoint;
}
template<class T> int32 Vector<T>::RemoveAll() {
for (int32 i=0;i<insertionPoint;i++) pObjs[i] = NULL;
insertionPoint = 0;
return 0;
}
template<class T> int32 Vector<T>::DeleteAll() {
for(int32 i=0;i<insertionPoint;i++) {
delete pObjs[i];
pObjs[i] = NULL;
}
insertionPoint = 0;
return 0;
}
template<class T> T Vector<T>::elementAt(int32 e) {
if ((e >= insertionPoint) || (e < 0)) {
return NULL;
}
return pObjs[e];
}
template<class T> T Vector<T>::removeElementAt(int32 e) {
if ((e >= insertionPoint) || (e < 0)) {
return NULL;
}
T rtnval = pObjs[e];
memmove(&(pObjs[e]),&(pObjs[e+1]),sizeof(T)*(insertionPoint-e-1));
//for (int32 i = e;i<insertionPoint;i++) {
// pObjs[i] = pObjs[i+1];
//}
insertionPoint--;
return rtnval;
}
template<class T> int32 Vector<T>::deleteElementAt(int32 e) {
T* p = removeElementAt(e);
if (p) {
delete p;
return 0;
} else {
return 255;
}
}
template<class T> int32 Vector<T>::insert(T &pE) {
// cout << "v:inserting" << endl;
if (pE) {
if (insertionPoint == currentLength) {
//cout << "v:adding more" << endl;
// add more and copy over
T *pNewObjs = new T[currentLength+threshhold];
//cout << "v:got it " << currentLength+threshhold << endl;
memcpy(pNewObjs,pObjs,(sizeof (T))*currentLength);
//cout << "v:did copy" << endl;
delete pObjs;
pObjs = pNewObjs;
for (int32 i=currentLength;i<currentLength + threshhold;i++) pObjs[i] = NULL;
currentLength = currentLength + threshhold;
}
pObjs[insertionPoint] = pE;
insertionPoint++;
//cout << "v:did it" << endl;
return insertionPoint-1;
} else {
return -1;
}
}
#endif // _VECTOR_H_
|