This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RG_DynArray.h
182 lines (151 loc) · 3.49 KB
/
RG_DynArray.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* @file RG_DynArray.h
* @author Yacine Smaoui, Florian Hemmelgarn
*
* @brief Definition and implementation of the DynArray class
*
*
*/
#ifndef DYNARRAY_H_
#define DYNARRAY_H_
#include <iostream>
#include <cassert> // for assert
using namespace std;
#define MAX_INITIAL_ARRAY_SIZE 5
/**
* @class DynArray
* @brief Array of dynamic size.
* This is a container template class where the different components of a Grammar are stored.
* It represents a dynamic array with the possibility of dynamically increasing the length.
* (a self implemented vector class)
*/
template <class T > class DynArray
{
private:
/** initial maximum size reserved for the array */
unsigned int uiMaxSize;
/** the actual size of the array */
unsigned int uiSize;
/** a pointer that defines the array address */
T* pArray;
public:
DynArray();
~DynArray();
void doubleSize();
void add(T element);
T& operator[] (const unsigned int nIndex);
unsigned int getLength();
int exist( T element);
void printArray();
};
/**
* @brief DynArray Constructor
* Dynamically allocates Memory and initialises the size
* */
template <class T > DynArray<T>::DynArray()
: uiMaxSize(MAX_INITIAL_ARRAY_SIZE) , uiSize(0)
{
pArray = new T [uiMaxSize];
/*
cout << "****DynArray constructor called" << endl ;
*/
}
/**
* Destructor
*/
template <class T > DynArray<T>::~DynArray()
{
//cout << "**DynArray destructor called" << endl ;
}
/**
* @brief Doubles the Size of the DynArray
*
* called when trying to add an element to a full DynArray.
*/
template <class T > void DynArray<T>::doubleSize()
{
unsigned int i;
T* temp ;
uiMaxSize = uiMaxSize * 2 ;
temp = new T[uiMaxSize];
for (i=0 ; i<uiSize; i++)
{
temp[i] = pArray[i];
}
delete [] pArray ;
pArray = temp ;
}
/**
* @brief adds an element to the DynArray
* @param element the element to add
*/
template <class T > void DynArray<T>::add( T element )
{
if(uiSize < uiMaxSize)
{
pArray[uiSize] = element;
uiSize++ ;
}
else
{
this->doubleSize();
this->add(element);
}
}
/**
* @brief prints the elements of the DynArray
*
* works only for Scalar types and strings for now
*/
template <class T > void DynArray<T>::printArray()
{
unsigned int i;
cout << "printing elements: " <<endl;
for (i=0; i<uiSize; i++ )
{
cout<<pArray[i]<<endl;
}
}
/**
* @brief overrides the [] operator
*
* The elements of the DynArray can then be accessed with the [] operator.
* This Method also checks is the index can be accepted within the size of the DynArray.
*
* @param nIndex
* @return
*/
template <class T > T& DynArray<T>::operator[]( const unsigned int nIndex )
{
assert(nIndex >= 0 && nIndex < uiSize);
return pArray[nIndex];
}
/**
* @brief returns the Size of the DynArray
* @return Size of the DynArray
*/
template <class T > unsigned int DynArray<T>::getLength()
{
return uiSize;
}
/**
* @brief checks the existance of a string element in the Array.
*
* a specialized method. works with DynArrays of type string.
*
* @param element the searched for element in the array.
* @return 1 if the element exists, 0 if not .
*/
template<> inline int DynArray<string>::exist( string element)
{
unsigned int i;
for(i=0; i<this->uiSize ; i++)
{
if (pArray[i].compare(element)==0)
{
return 1;
}
}
return 0;
}
#endif /* DYNARRAY_H_ */