-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSPArrayList.h
More file actions
executable file
·255 lines (235 loc) · 10.1 KB
/
SPArrayList.h
File metadata and controls
executable file
·255 lines (235 loc) · 10.1 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#ifndef SPARRAYLIST_H_
#define SPARRAYLIST_H_
#include <stdbool.h>
/**
* SPArrayList summary:
*
* A container that represents a fixed size linked list. The capcity of the list
* is specified 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:
*
* spArrayListCreate - Creates an empty array list with a specified
* max capacity.
* spArrayListCopy - Creates an exact copy of a specified array list.
* spArrayListDestroy - Frees all memory resources associated with an array
* list.
* spArrayListClear - Clears all elements from a specified array list.
* spArrayListAddAt - Inserts an element at a specified index, elements
* will be shifted to make place.
* spArrayListAddFirst - Inserts an element at the beginning of the array
* list, elements will be shifted to make place.
* spArrayListAddLast - Inserts an element at the end of the array list.
* spArrayListRemoveAt - Removes an element at the specified index, elements
* elements will be shifted as a result.
* spArrayListRemoveFirst - Removes an element from the beginning of the array
* list, elements will be shifted as a result.
* spArrayListRemoveLast - Removes an element from the end of the array list
* spArrayListGetAt - Accesses the element at the specified index.
* spArrayListGetFirst - Accesses the first element of the array list.
* spArrayListGetLast - Accesses the last element of the array list.
* spArrayListMaxCapcity - Returns the maximum capcity of the array list.
* spArrayListSize - Returns the number of elements in that array list.
* spArrayListIsFull - Returns if the array list reached its max capacity.
* spArrayListIsEmpty - Returns true if the array list contains no elements.
*/
typedef struct sp_array_list_t {
int* elements; //colomn numbers of previous turns
int actualSize;
int maxSize;
} SPArrayList;
/**
* A type used for errors
*/
typedef enum sp_array_list_message_t {
SP_ARRAY_LIST_SUCCESS,
SP_ARRAY_LIST_INVALID_ARGUMENT,
SP_ARRAY_LIST_FULL,
SP_ARRAY_LIST_EMPTY
} SP_ARRAY_LIST_MESSAGE;
/**
* Creates an empty array list with the specified 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.
*/
SPArrayList* spArrayListCreate(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.
*/
SPArrayList* spArrayListCopy(SPArrayList* 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 spArrayListDestroy(SPArrayList* 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
* SP_ARRAY_LIST_INVALID_ARGUMENT if src == NULL
* SP_ARRAY_LIST_SUCCESS otherwise
*/
SP_ARRAY_LIST_MESSAGE spArrayListClear(SPArrayList* 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
* SP_ARRAY_LIST_INVALID_ARGUMENT - if src == NULL or the index is out of bound
* SP_ARRAY_LIST_FULL - if the source array list reached its maximum capacity
* SP_ARRAY_LIST_SUCCESS - otherwise
*/
SP_ARRAY_LIST_MESSAGE spArrayListAddAt(SPArrayList* src, int 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
* SP_ARRAY_LIST_INVALID_ARGUMENT - if src == NULL or the index is out of bound
* SP_ARRAY_LIST_FULL - if the source array list reached its maximum capacity
* SP_ARRAY_LIST_SUCCESS - otherwise
*/
SP_ARRAY_LIST_MESSAGE spArrayListAddFirst(SPArrayList* src, int 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
* SP_ARRAY_LIST_INVALID_ARGUMENT - if src == NULL or the index is out of bound
* SP_ARRAY_LIST_FULL - if the source array list reached its maximum capacity
* SP_ARRAY_LIST_SUCCESS - otherwise
*/
SP_ARRAY_LIST_MESSAGE spArrayListAddLast(SPArrayList* src, int elem);
/**
* Removes an element from a specified index. The elements residing after the
* specified 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 index - The index from where the element will be removed.
* The index is 0-based.
* @return
* SP_ARRAY_LIST_INVALID_ARGUMENT - if src == NULL or the index is out of bound
* SP_ARRAY_LIST_EMPTY - if the source array list is empty
* SP_ARRAY_LIST_SUCCESS - otherwise
*/
SP_ARRAY_LIST_MESSAGE spArrayListRemoveAt(SPArrayList* 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
* SP_ARRAY_LIST_INVALID_ARGUMENT - if src == NULL
* SP_ARRAY_LIST_EMPTY - if the source array list is empty
* SP_ARRAY_LIST_SUCCESS - otherwise
*/
SP_ARRAY_LIST_MESSAGE spArrayListRemoveFirst(SPArrayList* 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
* SP_ARRAY_LIST_INVALID_ARGUMENT - if src == NULL
* SP_ARRAY_LIST_EMPTY - if the source array list is empty
* SP_ARRAY_LIST_SUCCESS - otherwise.
*/
SP_ARRAY_LIST_MESSAGE spArrayListRemoveLast(SPArrayList* src);
/**
* Returns the element at the specified 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 specified index, the index is 0-based.
* @return
* Undefined value if either src == NULL or index out of bound.
* Otherwise, the element at the specified index is returned.
*/
int spArrayListGetAt(SPArrayList* 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.
*/
int spArrayListGetFirst(SPArrayList* 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.
*/
int spArrayListGetLast(SPArrayList* 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 spArrayListMaxCapacity(SPArrayList* 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 spArrayListSize(SPArrayList* 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 spArrayListIsFull(SPArrayList* 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 spArrayListIsEmpty(SPArrayList* src);
#endif