-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.tpp
More file actions
102 lines (90 loc) · 2.01 KB
/
Array.tpp
File metadata and controls
102 lines (90 loc) · 2.01 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#pragma once
#include "Array.hpp"
#include "defines.hpp"
template <typename T> Array<T>::Array () : _array (NULL), _size (0){};
template <typename T> Array<T>::Array (size_t n) : _array (NULL), _size (n)
{
if (n <= 0 || n > MAX_SIZE)
throw Array<T>::InvalidArraySize ();
_array = new T[n];
for (size_t i = 0; i < n; i++)
_array[i] = T ();
};
template <typename T>
Array<T>::Array (Array<T> const &other) : _array (NULL), _size (other.size ())
{
if (other.size () == 0)
throw Array<T>::EmptyArray ();
_array = new T[other.size ()];
for (size_t i = 0; i < other.size (); i++)
_array[i] = other._array[i];
};
template <typename T>
size_t
Array<T>::size () const
{
return _size;
};
template <typename T>
T &
Array<T>::operator[] (size_t i)
{
if (_array == NULL)
throw Array<T>::EmptyArray ();
if (i >= _size)
throw Array<T>::OutOfLimitsException ();
return _array[i];
};
template <typename T>
const T &
Array<T>::operator[] (size_t i) const
{
if (_array == NULL)
throw Array<T>::EmptyArray ();
if (i >= _size)
throw Array<T>::OutOfLimitsException ();
return _array[i];
};
template <typename T>
Array<T> &
Array<T>::operator= (const Array<T> &rhs)
{
if (rhs.size () == 0)
throw Array<T>::EmptyArray ();
if (this != &rhs)
{
if (this->size () != rhs.size ())
{
if (_array)
delete[] _array;
_array = new T[rhs.size ()];
_size = rhs.size ();
}
for (size_t i = 0; i < rhs.size (); i++)
_array[i] = rhs[i];
}
return *this;
};
template <typename T>
const char *
Array<T>::InvalidArraySize::what () const throw ()
{
return RED "Invalid array size in constructor" RESET;
};
template <typename T>
const char *
Array<T>::OutOfLimitsException::what () const throw ()
{
return RED "Invalid index" RESET;
};
template <typename T>
const char *
Array<T>::EmptyArray::what () const throw ()
{
return RED "Empty array" RESET;
};
template <typename T> Array<T>::~Array ()
{
if (_array)
delete[] _array;
}