-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDArray.h
156 lines (133 loc) · 3.96 KB
/
DArray.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
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
#ifndef ADS_DYNAMIC_ARRAY_H
#define ADS_DYNAMIC_ARRAY_H
#include <stddef.h>
#include <stdio.h>
#include "Utils.h"
/**
* DArray.h
*
* Implementation of a dynamic array with configurable first index.
*
* author: Giacomo Benincasa (me@gbenin.casa)
*/
namespace ADS
{
template<class T>
class DArray
{
public:
/* Creates an array of T of length uiLen where
the first index of the first element is
uiStartIndex.
defaulValue value is the default value assigned
to the elements of the array */
explicit DArray (unsigned int uiLen,
unsigned int uiStartIndex=0, bool bCanGrow=true);
virtual ~DArray (void);
/* add element at position uiIndex and replace
the element at the current position (if any)
with the current one. The formed element is
returned */
T add (unsigned int uiIndex, T element);
/* Returns the element at position uiIndex */
T get (unsigned int uiIndex);
/* Removes and returns the element at position uiIndex
if any, returns NULL otherwise */
T remove (unsigned int uiIndex);
/* Returns the length of the array */
unsigned int size (void);
void display (void);
private:
bool checkBound (unsigned int uiIndex);
bool grow (unsigned int uiLastIndex);
unsigned int shiftIndex (unsigned int uiIndex);
private:
bool _bCanGrow;
unsigned int _uiLen; /* Length of the array */
unsigned int _uiOff; /* The index of the first element */
T *_pDArray;
};
template <class T>
DArray<T>::DArray (unsigned int uiLen,
unsigned int uiStartIndex, bool bCanGrow)
{
_uiLen = uiLen;
_uiOff = uiStartIndex;
_bCanGrow = bCanGrow;
if (uiLen == 0)
_pDArray = NULL;
else
_pDArray = (T *) allocateArray (uiLen, sizeof (T));
}
template <class T>
DArray<T>::~DArray()
{
deallocate ((void **)&_pDArray);
}
template <class T> T
DArray<T>::add (unsigned int uiIndex, T element)
{
if (!checkBound (shiftIndex (uiIndex)))
return NULL;
T tmp = _pDArray[shiftIndex (uiIndex)];
_pDArray[shiftIndex (uiIndex)] = element;
return tmp;
}
template <class T>
T DArray<T>::get (unsigned int uiIndex)
{
if (!checkBound (shiftIndex (uiIndex)))
return NULL;
return _pDArray[shiftIndex (uiIndex)];
}
template <class T>
T DArray<T>::remove (unsigned int uiIndex)
{
T tmp = get (uiIndex);
if (tmp == NULL)
return NULL;
_pDArray[shiftIndex (uiIndex)] = NULL;
return tmp;
}
template <class T>
unsigned int DArray<T>::size()
{
return _uiLen;
}
template <class T>
bool DArray<T>::checkBound (unsigned int uiIndex)
{
if (uiIndex < _uiOff)
return false;
if (uiIndex >= _uiLen) {
if (_bCanGrow)
return grow (uiIndex);
else
return false;
}
return true;
}
template <class T>
bool DArray<T>::grow (unsigned int uiLastIndex)
{
unsigned int uiNewLen = uiLastIndex - _uiOff + 1;
_pDArray = (T*) reallocateArray (_pDArray, _uiLen, uiNewLen, sizeof (T));
if (_pDArray == NULL)
return false;
_uiLen = uiNewLen;
return true;
}
template <class T>
unsigned int DArray<T>::shiftIndex (unsigned int uiIndex)
{
return (uiIndex - _uiOff);
}
template <class T>
void DArray<T>::display (void)
{
for (unsigned int i = 0; i < size(); i++)
printf ("%u ", get (i));
printf ("\n");
}
}
#endif /* ADS_ARRAY_H */