-
Notifications
You must be signed in to change notification settings - Fork 1
/
cdsList.cc
183 lines (148 loc) · 2.84 KB
/
cdsList.cc
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "cdsList.hh"
template<class T, int S>
CDSList<T,S>&
CDSList<T,S>::resize(const int nsize)
{
if ( nsize>asize ) {
while ( nsize>asize ) {
asize += (blockSize?blockSize:1);
}
T *ndata = new T[asize];
for (int i=0 ; i<size_ ; i++) ndata[i] = data[i];
delete [] data;
data = ndata;
}
size_ = nsize;
return *this;
} /*resize*/
template<class T, int S>
CDSList<T,S>::CDSList(const CDSList<T,S> &l)
//
// copy constructor
//
{
size_ = l.size_;
asize = l.asize;
blockSize = l.blockSize;
data = new T[l.asize];
for (int i=0 ; i<size_ ; i++)
data[i] = l.data[i];
} /* CDSList(const CDSList&) */
template<class T, int S>
CDSList<T,S>&
CDSList<T,S>::operator=(const CDSList<T,S> &l)
//
// assignment of list to list
//
{
if (&l==this) //self-assignment
return *this;
if (asize != l.asize) {
delete [] data;
data = new T[l.asize];
}
size_ = l.size_;
asize = l.asize;
blockSize = l.blockSize;
for (int i=0 ; i<size_ ; i++)
data[i] = l.data[i];
return *this;
}
template<class T, int S>
int
CDSList<T,S>::getIndex(const T &member)
{
for (int i=0 ; i<size_ ; i++)
if ( data[i] == member )
return i;
return -1;
}
template<class T, int S>
bool
CDSList<T,S>::contains(const T &member)
{
if ( getIndex(member) >= 0 )
return 1;
else
return 0;
}
template<class T, int S>
void
CDSList<T,S>::remove(const int i)
//
// remove an element of the list
//
{
#ifdef CHECKRANGE
if (i+1>size_ || i<0) {
cerr << "CDSList::remove: index out of range:" << i << "\n";
abort();
}
#endif
for (int j=i+1 ; j<size_ ; j++)
data[j-1] = data[j];
size_--;
}
template<class T, int S>
void
CDSList<T,S>::setBlockSize(const int nsize)
{
blockSize = nsize;
}
template <class T, int S>
ostream&
operator<<(ostream& os, const CDSList<T,S>& v)
{
os << '{';
for (int i=0 ; i<v.size() ; i++) {
os << ' ' << v[i];
if ( i<v.size()-1 ) os << ',';
}
os << " }";
return os;
}
#ifdef TESTING
#include <stdlib.h>
#include <string.h>
#include <strstream.h>
template<class T, int S>
int
CDSList<T,S>::test()
{
int exit=0;
cout << "testing CDSList...";
CDSList<int> l;
CDSList<int> l2(1,2);
CDSList<int> l3 = l2;
l.append(1);
if (l[0] != 1) {
cerr << "index failed: " << l[0] << " != 1\n";
exit=1;
}
if (l3.size()!=1 || l3.asize!=2) {
cerr << "internal error 1\n";
exit=1;
}
l3.resize(3);
if (l3.size()!=3 || l3.asize!=4) {
cerr << "internal error 2\n";
exit=1;
}
l.append(4);
if ( !l.contains(4) ) {
cerr << "contains error\n";
exit=1;
}
if ( l.getIndex(4)!=1 ) {
cerr << "getIndex error\n";
exit=1;
}
ostrstream os; os << l;
if ( strcmp(os.str(),"{ 1, 4 }") ) {
cerr << "operator<< error: " << os.str() << " != { 1, 4 }\n";
exit=1;
}
cout << (exit?"failed":"ok") << endl;
return exit;
} /* test */
#endif /* TESTING */