Website Summary Files Commits Branches @commit @diff @stats
path: root / Week04 / Exercise02 / IntArray.cpp
blob: 2dc052179d9389d056335294a87e36a8b052ca04 (plain) (blame)
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
#include "IntArray.h"

int IntArray::At(unsigned index) {
	if (index < capacity)
		return elems[index];
	return 0;
}

void IntArray::Resize(unsigned capacity) {
	int* newElems = new int[capacity];

	unsigned copyCount = this->capacity < capacity ? this->capacity : capacity;
	for (unsigned i = 0; i < copyCount; ++i)
		newElems[i] = elems[i];

	delete[] elems;
	elems = newElems;
	this->capacity = capacity;
}

void IntArray::Concat(const IntArray& endElements) {
	unsigned oldCapacity = capacity;
	Resize(capacity + endElements.capacity);

	for (unsigned i = oldCapacity; i < capacity; ++i)
		elems[i] = endElements.elems[i - oldCapacity];
}