This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynArray.cpp
102 lines (80 loc) · 1.93 KB
/
DynArray.cpp
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
// DynArray.cpp: implementation of the CDynArray class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "zsIRC.h"
#include "DynArray.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDynArray::CDynArray()
{
pItems = NULL;
nItems = 0;
}
CDynArray::~CDynArray()
{
Flush();
}
void CDynArray::Add(ARRAYITEM a) {
if (!nItems) {
ARRAYITEM * pNew = new ARRAYITEM[1];
pNew[0] = a;
nItems = 1;
pItems = pNew;
return;
}
ARRAYITEM * pNew = new ARRAYITEM[nItems+1];
CopyMemory(pNew,pItems,sizeof(ARRAYITEM)*nItems);
pNew[nItems] = a;
if (pItems) delete[] pItems;
pItems = pNew;
nItems++;
}
void CDynArray::Delete(int i) {
if (!nItems) return;
delete pItems[i];
if (nItems == 1) {
if (pItems) delete[] pItems;
nItems = 0;
return;
}
ARRAYITEM * pNew = new ARRAYITEM[nItems-1];
CopyMemory(pNew,pItems,sizeof(ARRAYITEM)*i);
CopyMemory(pNew+i,pItems+i+1,sizeof(ARRAYITEM)*(nItems-i-1));
if (pItems) delete[] pItems;
pItems = pNew;
nItems--;
}
ARRAYITEM CDynArray::operator[] (int i) {
return pItems[i];
}
int CDynArray::Num() {
return nItems;
}
void CDynArray::Flush() {
int nMsg = Num();
for (int i=0; i<nMsg; i++)
Delete(0);
// for (int i=0; i<nItems; i++)
// delete pItems[i];
// if (pItems) delete[] pItems;
pItems = NULL;
nItems = 0;
}
void CDynArray::Sort(DYNARRAY_SORTCALLBACK cb) {
if (!nItems) return;
qsort(pItems,nItems,sizeof(ARRAYITEM),cb);
}
int CDynArray::Find(ARRAYITEM a) {
int nMsg = Num();
for (int i=0; i<nMsg; i++)
if (a == pItems[i])
return i;
return -1;
}