-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharraylist.h
82 lines (66 loc) · 1.6 KB
/
arraylist.h
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
#ifndef ARRAYLIST_
#define ARRAYLIST_
#include <stdint.h>
#include <assert.h>
template<typename T, int32_t maxLength>
class ArrayList
{
public:
typedef int32_t Index;
typedef int32_t LengthT;
enum {
InvalidIndex = -1
};
void append(const T& item);
const T& at(Index index) const;
Index indexOf(const T& item) const;
LengthT length() const;
T& operator[](Index index);
const T& operator[](Index index) const;
private:
T m_data[maxLength];
Index m_length;
};
template<typename T, int32_t maxLength>
void ArrayList<T, maxLength>::append(const T& item)
{
assert(m_length < maxLength);
m_data[m_length] = item;
m_length++;
}
template<typename T, int32_t maxLength>
const T& ArrayList<T, maxLength>::at(ArrayList<T,maxLength>::Index index) const
{
assert(index < m_length);
return m_data[index];
}
template<typename T, int32_t maxLength>
ArrayList<T,maxLength>::Index ArrayList<T, maxLength>::indexOf(const T& item) const
{
for (Index i = 0; i < m_length; i++)
{
if (m_data[i] == item)
{
return i;
}
}
return InvalidIndex;
}
template<typename T, int32_t maxLength>
ArrayList<T,maxLength>::LengthT ArrayList<T, maxLength>::length() const
{
return m_length;
}
template<typename T, int32_t maxLength>
T& ArrayList<T, maxLength>::operator[](Index index)
{
assert(index < m_length);
return m_data[index];
}
template<typename T, int32_t maxLength>
const T& ArrayList<T, maxLength>::operator[](Index index) const
{
assert(index < m_length);
return m_data[index];
}
#endif /* ARRAYLIST_ */