-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayList.h
236 lines (214 loc) · 10.1 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#ifndef ARRAYLIST_H_
#define ARRAYLIST_H_
#include <stdbool.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "GameBasicBuildingBlocks.h"
/**
* ArrayList summary:
*
* A container that represents a fixed size linked list. The capcity of the list
* is ecified at the creation. The container supports typical list
* functionalities with the addition of random access as in arrays.
* Upon insertion, if the maximum capacity is reached then an error message is
* returned and the list is not affected. A summary of the supported functions
* is given below:
*
* ArrayListCreate - Creates an empty array list with a ecified
* max capacity.
* ArrayListCopy - Creates an exact copy of a ecified array list.
* ArrayListDestroy - Frees all memory resources associated with an array
* list.
* ArrayListClear - Clears all elements from a ecified array list.
* ArrayListAddAt - Inserts an element at a ecified index, elements
* will be shifted to make place.
* ArrayListAddFirst - Inserts an element at the beginning of the array
* list, elements will be shifted to make place.
* ArrayListAddLast - Inserts an element at the end of the array list.
* ArrayListRemoveAt - Removes an element at the ecified index, elements
* elements will be shifted as a result.
* ArrayListRemoveFirst - Removes an element from the beginning of the array
* list, elements will be shifted as a result.
* ArrayListRemoveLast - Removes an element from the end of the array list
* ArrayListGetAt - Accesses the element at the ecified index.
* ArrayListGetFirst - Accesses the first element of the array list.
* ArrayListGetLast - Accesses the last element of the array list.
* ArrayListMaxCapcity - Returns the maximum capcity of the array list.
* ArrayListSize - Returns the number of elements in that array list.
* ArrayListIsFull - Returns if the array list reached its max capacity.
* ArrayListIsEmpty - Returns true if the array list contains no elements.
*/
typedef struct ARRAY_list_t {
Step** elements;
int actualSize;
int maxSize;
} ArrayList;
/**
* A type used for errors
*/
typedef enum ARRAY_list_message_t {
ARRAY_LIST_SUCCESS,
ARRAY_LIST_INVALID_ARGUMENT,
ARRAY_LIST_FULL,
ARRAY_LIST_EMPTY
} ARRAY_LIST_MESSAGE;
/* Creates an empty array list with the ecified maximum capacity.
* @param maxSize - the maximum capacity of the target array list.
* @return
* NULL, if an allocation error occurred or maxSize <= 0.
* An instant of an array list otherwise. */
ArrayList* ArrayListCreate(int maxSize);
/* Creates an exact copy of the src array list. Elements in the new copy will
* be in the same order as they appeared in the source list.
* @param src - the source array list.
* @return
* NULL if either an allocation error occurs or src == NULL.
* A new copy of the source array list, otherwise. */
ArrayList* ArrayListCopy(ArrayList* src);
/* Frees all memory resources associated with the source array list. If the
* source array is NULL, then the function does nothing.
* @param src - the source array list */
void ArrayListDestroy(ArrayList* src);
/* Clears all elements from the source array list. After invoking this function,
* the size of the source list will be reduced to zero and maximum capacity is
* not affected.
* @param src - the source array list
* @return
* ARRAY_LIST_INVALID_ARGUMENT if src == NULL
* ARRAY_LIST_SUCCESS otherwise */
ARRAY_LIST_MESSAGE ArrayListClear(ArrayList* src);
/* Inserts element at a specified index. The elements residing at and after the
* specified index will be shifted to make place for the new element. If the
* array list reached its maximum capacity and error message is returned and
* the source list is not affected
* @param src - the source array list
* @param elem - the new element to be inserted
* @param index - the index where the new element will be placed. The index is
* 0-based.
* @return
* ARRAY_LIST_INVALID_ARGUMENT - if src == NULL or the index is out of bound
* ARRAY_LIST_FULL - if the source array list reached its maximum capacity
* ARRAY_LIST_SUCCESS - otherwise */
ARRAY_LIST_MESSAGE ArrayListAddAt(ArrayList* src, Step *elem, int index);
/* Inserts element at a the beginning of the source element. The elements
* will be shifted to make place for the new element. If the
* array list reached its maximum capacity and error message is returned and
* the source list is not affected
* @param src - the source array list
* @param elem - the new element to be inserted
* @return
* ARRAY_LIST_INVALID_ARGUMENT - if src == NULL or the index is out of bound
* ARRAY_LIST_FULL - if the source array list reached its maximum capacity
* ARRAY_LIST_SUCCESS - otherwise */
ARRAY_LIST_MESSAGE ArrayListAddFirst(ArrayList* src, Step *elem);
/* Inserts element at a the end of the source element. If the array list
* reached its maximum capacity and error message is returned and the source
* list is not affected.
* @param src - the source array list
* @param elem - the new element to be inserted
* @return
* ARRAY_LIST_INVALID_ARGUMENT - if src == NULL or the index is out of bound
* ARRAY_LIST_FULL - if the source array list reached its maximum capacity
* ARRAY_LIST_SUCCESS - otherwise */
ARRAY_LIST_MESSAGE ArrayListAddLast(ArrayList* src, Step *elem);
/* Removes an element from a ecified index. The elements residing after the
* ecified index will be shifted to make to keep the list continuous. If the
* array list is empty then an error message is returned and the source list
* is not affected
* @param src - The source array list
* @param elem - The new element to be inserted
* @param index - The index from where the new element will be removed.
* The index is 0-based.
* @return
* ARRAY_LIST_INVALID_ARGUMENT - if src == NULL or the index is out of bound
* ARRAY_LIST_EMPTY - if the source array list is empty
* ARRAY_LIST_SUCCESS - otherwise */
ARRAY_LIST_MESSAGE ArrayListRemoveAt(ArrayList* src, int index);
/* Removes an element from a the beginning of the list.
* The elements will be shifted to make to keep the list continuous. If the
* array list is empty then an error message is returned and the source list
* is not affected
* @param src - The source array list
* @param elem - The new element to be inserted
* @return
* ARRAY_LIST_INVALID_ARGUMENT - if src == NULL
* ARRAY_LIST_EMPTY - if the source array list is empty
* ARRAY_LIST_SUCCESS - otherwise */
ARRAY_LIST_MESSAGE ArrayListRemoveFirst(ArrayList* src);
/* Removes an element from a the end of the list.
* The elements will be shifted to make to keep the list continuous. If the
* array list is empty then an error message is returned and the source list
* is not affected
* @param src - The source array list
* @param elem - The new element to be inserted
* @return
* ARRAY_LIST_INVALID_ARGUMENT - if src == NULL
* ARRAY_LIST_EMPTY - if the source array list is empty
* ARRAY_LIST_SUCCESS - otherwise. */
ARRAY_LIST_MESSAGE ArrayListRemoveLast(ArrayList* src);
/* Returns the element at the ecified index. The function is called
* with the assertion that all arguments are valid. If any of the arguments is
* invalid then an undefined value is returned.
* @param src - the source array list
* @param index - the ecified index, the index is 0-based.
* @return
* Undefined value if either src == NULL or index out of bound.
* Otherwise, the element at the ecified index is returned. */
Step *ArrayListGetAt(ArrayList* src, int index);
/* Returns the element at the beginning of the list. The function is called
* with the assertion that all arguments are valid. If any of the arguments is
* invalid then an undefined value is returned.
* @param src - the source array list
* @return
* Undefined value if either src == NULL or the list is empty
* Otherwise, the element at the beginning of the list is returned. */
Step *ArrayListGetFirst(ArrayList* src);
/* Returns the element at the end of the list. The function is called
* with the assertion that all arguments are valid. If any of the arguments is
* invalid then an undefined value is returned.
* @param src - the source array list
* @return
* Undefined value if either src == NULL or the list is empty
* Otherwise, the element at the end of the list is returned. */
Step *ArrayListGetLast(ArrayList* src);
/* Returns the maximum capacity of the list. The function is called
* with the assertion that all arguments are valid. If any of the arguments is
* invalid then an undefined value is returned.
* @param src - the source array list
* @return
* Undefined value if either src == NULL
* Otherwise, the maximum capacity of the list is returned. */
int ArrayListMaxCapacity(ArrayList* src);
/* Returns the number of elements in the list. The function is called
* with the assertion that all arguments are valid. If any of the arguments is
* invalid then an undefined value is returned.
* @param src - the source array list
* @return
* Undefined value if either src == NULL
* Otherwise, the number of the elements in the list is returned. */
int ArrayListSize(ArrayList* src);
/* Returns true if the list is full, that is the number of elements in the list
* equals its maximum capacity.
* @param src - the source array list
* @return
* false if either src == NULL or the number of elements in the list is less
* than its maximum capacity.
* Otherwise, true is returned. */
bool ArrayListIsFull(ArrayList* src);
/* Returns true if the list is empty, that is the number of elements in the list
* equals to zero.
* @param src - the source array list
* @return
* false if either src == NULL or the number of elements in the list is not zero.
* Otherwise, true is returned. */
bool ArrayListIsEmpty(ArrayList* src);
/* push element at first, if the src is full:
* remove the last element and then add at first */
ARRAY_LIST_MESSAGE ArrayListPushFirst(ArrayList* src, Step *elem);
/**
* print the list (for debugging)
* */
void ArrayListPrint(ArrayList* src);
#endif