-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharea.c
442 lines (417 loc) · 11.8 KB
/
area.c
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#define _CRT_SECURE_NO_WARNINGS
#include "mtm_map/map.h"
#include "area.h"
#include "assist.h"
#include "tribe.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
#define UNDEFINED_ID -1
struct area_t
{
int id;
char* name;
Tribe tribe;
Area next;
};
Area areaCreate();
void areaDestroy(Area area);
AreaResult areaAddTribe(Area area, int tribe_id, const char* tribe_name);
AreaResult areaAdd(Area area, int area_id, const char* area_name);
char* areaGetTribeName(Area area, int tribe_id);
AreaResult areaAddVote(Area area, int area_id, int tribe_id, int num_of_votes);
AreaResult areaRemoveVote(Area area, int area_id, int tribe_id, int num_of_votes);
AreaResult areaSetTribeName(Area area, int tribe_id, const char* tribe_name);
AreaResult areaRemoveTribe(Area area, int tribe_id);
AreaResult areaRemove(Area area, AreaConditionFunction should_delete_area);
Map areaComputeAreasToTribesMapping(Area area);
static void areaElementsDelete(Area area);
static AreaResult handleResult(TribeResult result);
static Area getAreaById(Area area, int area_id);
static bool areaPutElemtnes(Area area, int area_id, const char* area_name);
AreaResult areaUpdateVote(Area area, int area_id, int tribe_id, int num_of_votes, UpdateVotesCondition condition);
static void swapArea(Area area1, Area area2);
static void removeNodeArea(Area area);
static Area getFormerAreaNode(Area area, int area_id);
bool areaContains(Area area, int area_id);
bool areaTribeContains(Area area, int tribe_id);
bool areaTribeContains(Area area, int tribe_id)
{
if (area == NULL)
{
return false;
}
return tribeContains(area->tribe, tribe_id);
}
bool areaContains(Area area, int area_id)
{
if (getAreaById(area, area_id) == NULL)
{
return false;
}
return true;
}
Area areaCreate()
{
Area area = malloc(sizeof(*area));
if (area == NULL)
{
return NULL;//allocation failed
}
area->tribe = tribeCreate();
if (area->tribe == NULL)
{
free(area);
return NULL;//allocation failed
}
area->id = UNDEFINED_ID;
area->name = NULL;
area->next = NULL;
return area;
}
void areaDestroy(Area area)
{
while (area != NULL)//if the id is undefined the area elements are already NULL
{
Area area_to_Delete = area;
area = area->next;
areaElementsDelete(area_to_Delete);
free(area_to_Delete);
}
}
AreaResult areaAddTribe(Area area, int tribe_id, const char* tribe_name)
{
assert(area != NULL && tribe_name != NULL);
assert(tribe_id >= 0);
TribeResult result;
while (area != NULL)
{
result = tribeAdd(area->tribe, tribe_id, tribe_name);
if (result != TRIBE_SUCCESS)
{
return handleResult(result);
}
area = area->next;
}
return handleResult(result);
}
AreaResult areaAdd(Area area, int area_id, const char* area_name)
{
assert(area != NULL && area_name != NULL);
assert(area_id >= 0);
if (getAreaById(area, area_id) != NULL) //found an area with the given id
{
return AREA_ALREADY_EXIST;
}
if (area->id == UNDEFINED_ID) //only one empty area
{
if (!areaPutElemtnes(area, area_id, area_name))
{
return AREA_OUT_OF_MEMORY;
}
tribeSetAllVotesToZero(area->tribe);
}
else
{
Area new_area = areaCreate();
if (new_area == NULL)
{
return AREA_OUT_OF_MEMORY;
}
tribeDestroy(new_area->tribe);
destroyString(new_area->name);
new_area->tribe = tribeCopy(area->tribe);//copy the tribe
if (new_area->tribe == NULL)//check if copying tribe failed
{
return AREA_OUT_OF_MEMORY;
}
if (!areaPutElemtnes(new_area, area_id, area_name))
{
areaDestroy(new_area);
return AREA_OUT_OF_MEMORY;
}
tribeSetAllVotesToZero(new_area->tribe);
while (area->next != NULL)
{
area = area->next;
}
area->next = new_area;
}
return AREA_SUCCESS;
}
char* areaGetTribeName(Area area, int tribe_id)
{
if (area == NULL || area->tribe == NULL)
{
return NULL;
}
return tribeGetName(area->tribe, tribe_id);
}
AreaResult areaUpdateVote(Area area, int area_id, int tribe_id, int num_of_votes, UpdateVotesCondition condition)
{
TribeResult result;
assert(area != NULL && area->tribe != NULL && area_id >= 0 && tribe_id >= 0 && num_of_votes >= 0);
Area area_to_update = getAreaById(area, area_id);//look for the area with the given id
if (area_to_update == NULL)
{
return AREA_NOT_EXIST;
}
result = tribeUpdateVote(area_to_update->tribe, tribe_id, num_of_votes, condition);
return handleResult(result);
}
AreaResult areaSetTribeName(Area area, int tribe_id, const char* tribe_name)
{
assert(area != NULL && tribe_id >= 0 && tribe_name != NULL);
TribeResult result;
while (area != NULL)
{//run on all area nodes, need to change the name in all the areas since each one has a tribe map
result = tribeSetName(area->tribe, tribe_id, tribe_name);
if (result != TRIBE_SUCCESS)
{
return handleResult(result);
}
area = area->next;
}
return handleResult(result);
}
AreaResult areaRemoveTribe(Area area, int tribe_id)
{
assert(area != NULL && tribe_id >= 0);
TribeResult result;
while (area != NULL)// need to remove from all the area since each area has a tribe map of itself
{
if (area->tribe != NULL) {
result = tribeRemove(area->tribe, tribe_id);
}
if (result != TRIBE_SUCCESS)
{
return handleResult(result);
}
area = area->next;
}
return handleResult(result);
}
AreaResult areaRemove(Area area, AreaConditionFunction should_delete_area)
{
assert(area != NULL);
if (area->next == NULL) //first and only node
{
if (should_delete_area(area->id))//need to delete the only area in the list
{
areaElementsDelete(area);//only delete the node elements, don't delete the list itself
}
return AREA_SUCCESS;
}
Area tmp = area;
while (tmp != NULL)
{
Area formerNode = getFormerAreaNode(area, tmp->id);
if (should_delete_area(tmp->id))
{
if (tmp->next == NULL)
{
if (formerNode == NULL)//only one node exsits in the list and we want to delete it
{
areaElementsDelete(tmp);//keep an empty list
tmp->tribe = NULL;
}
else
{
formerNode->next = NULL;//we need to delete the last node in the list
areaElementsDelete(tmp);
free(tmp);
}
return AREA_SUCCESS;
}
if (formerNode == NULL)//need to delete the first node, but its not the only node in the list
{
swapArea(tmp, tmp->next);//swap and deletes the second node
removeNodeArea(tmp);
}
else
{
Area toDelete = tmp;
formerNode->next = tmp->next;//remove a note from the middle of the list
tmp = tmp->next;
areaElementsDelete(toDelete);
free(toDelete);
}
}
else // progress in the list only if we dont need to delete this node
{
tmp = tmp->next;
}
}
return AREA_SUCCESS;
}
Map areaComputeAreasToTribesMapping(Area area)
{
char* string_area_id, * tribe_max_vote_id;
Map map_of_max = mapCreate();//create the map of the mapped areas to tribes
if (map_of_max == NULL)
{
return NULL;
}
if (area == NULL)
{
return map_of_max;
}
MapResult result;
while (area != NULL)//do for all areas in the list
{
string_area_id = intToString(area->id);
tribe_max_vote_id = tribeGetMaxVotesForArea(area->tribe);// map the tribe with the most votes to the area
if (tribe_max_vote_id == NULL)
{
destroyString(string_area_id);
return map_of_max;
}
result = mapPut(map_of_max, (const char*)string_area_id, (const char*)tribe_max_vote_id);
destroyString(string_area_id);
if (result != MAP_SUCCESS)//allocation failed
{
mapDestroy(map_of_max);
return NULL;
}
area = area->next;
}
return map_of_max;
}
/*
getAreaById: get a pointer to the areas list and return a pointer to the area with the given id
if no area with the specified id exists return NULL
*/
static Area getAreaById(Area area, int area_id)
{
if (area == NULL)
{
return NULL;
}
Area tmp = area;
while (tmp != NULL)//run on all the areas
{
if (tmp->id == area_id)
{
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
/*
areaPutElemtnes: get an initialized area and update its elements by copying the given
varibels (by value) if any memeory allocation failed return false otherwise true
*/
static bool areaPutElemtnes(Area new_area, int area_id, const char* area_name)
{
new_area->id = area_id;
new_area->name = createString(strlen(area_name));//allocate memory for the area name
if (new_area->name == NULL)//check if allocation failed
{
return false;
}
strcpy(new_area->name, area_name);//copy the name
return true;
}
/*
get a pointer to a area and free all of the area varibels and afterwards set them to NULL
*/
static void areaElementsDelete(Area area)
{
assert(area != NULL);
if (area->tribe != NULL)
{
tribeDestroy(area->tribe);
}
area->next = NULL;
destroyString(area->name);
area->name = NULL;
area->id = UNDEFINED_ID;
}
/*
handle the results from tribe
*/
static AreaResult handleResult(TribeResult result)
{
switch (result)
{
case TRIBE_SUCCESS:
return AREA_SUCCESS;
case TRIBE_OUT_OF_MEMORY:
return AREA_OUT_OF_MEMORY;
case TRIBE_NULL_ARGUMENT:
return AREA_NULL_ARGUMENT;
case TRIBE_ITEM_ALREADY_EXISTS:
return AREA_TRIBE_ALREADY_EXIST;
case TRIBE_ITEM_DOES_NOT_EXIST:
return AREA_TRIBE_NOT_EXIST;
default:
return AREA_SUCCESS;
}
}
/*
getFormerAreaNode get an area id and return a pointer to the node before the node with this id
if there is no node with the given id, return NULL
*/
static Area getFormerAreaNode(Area area, int area_id)
{
if (area == NULL || area->next == NULL)
{
return NULL;
}
if (area->id == area_id)
{
return NULL;
}
while (area->next != NULL)
{
if (area->next->id == area_id)
{
return area;
}
area = area->next;
}
return NULL;
/*
if (area != NULL && area->next != NULL && area->id == undef)
{
while (area->next != NULL)
{
if (area->next->id == area_id)
{
return area;
}
}
}
return NULL;
*/
}
/*
removeNodeArea: gets a pointer to the node before the node we want to remove from the list
disconnect the node from the list and disallocates its elenents
*/
static void removeNodeArea(Area area)
{
Area toDelete = area->next;
area->next = area->next->next;//go over the node we want to delete
areaElementsDelete(toDelete);//free the node elements
free(toDelete);
}
/*
swapArea: swaps between two area elements
*/
static void swapArea(Area area1, Area area2)
{
char* tmp = area1->name;
area1->name = area2->name;
area2->name = tmp;
int tmp2 = area1->id;
area1->id = area2->id;
area2->id = tmp2;
Tribe tmp3 = area1->tribe;
area1->tribe = area2->tribe;
area2->tribe = tmp3;
}