-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntCollection.cpp
More file actions
83 lines (70 loc) · 1.36 KB
/
Copy pathIntCollection.cpp
File metadata and controls
83 lines (70 loc) · 1.36 KB
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
#include "IntCollection.h"
#include <iostream>
using namespace std;
IntCollection::IntCollection()
{
size = 0;
data = new int[CHUNK_SIZE];
capacity = CHUNK_SIZE;
}
IntCollection::~IntCollection() {
delete [] data;
}
IntCollection::IntCollection(const IntCollection &c)
{
size = 0;
capacity = CHUNK_SIZE;
data = new int[CHUNK_SIZE];
for(int i = 0; i < c.size; i++) {
add(c.data[i]);
}
}
void IntCollection::add(int value)
{
int *newData;
if ((size > 0) && (size % CHUNK_SIZE == 0))
{
capacity += CHUNK_SIZE;
newData = new int[capacity];
for (int i=0; i<size; i++)
newData[i] = data[i];
delete [] data;
data = newData;
}
data[size++] = value;
}
int IntCollection::get(int index)
{
if (index<0 || index>=size)
{
cout << "ERROR: get() trying to access index out of range.\n";
exit(1);
}
return data[index];
}
int IntCollection::getSize()
{
return size;
}
IntCollection& IntCollection::operator=(const IntCollection &c)
{
for(int i=0; i< c.size; i++)
add(c.data[i]);
return *this;
}
bool IntCollection:: operator==(const IntCollection &c)
{
if(size != c.size)
return false;
for(int i = 0; i < c.size; i++)
{
if(data[i] != c.data[i])
return false;
}
return true;
}
IntCollection& IntCollection::operator<<(int value)
{
add(value);
return *this;
}