2024-11-06 20:52:22 +00:00
|
|
|
#include "a_station.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-11-06 21:00:33 +00:00
|
|
|
struct station* create_station() {
|
|
|
|
struct station* station = (struct station*)calloc(1, sizeof(struct station));
|
|
|
|
station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*));
|
|
|
|
station->track_count = STATION_SIZE;
|
|
|
|
return station;
|
2024-11-06 20:52:22 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 21:00:33 +00:00
|
|
|
void destroy_station(struct station* stanica) {
|
|
|
|
for (int i = 0; i < stanica->track_count; ++i) {
|
|
|
|
struct car* aktualny = stanica->tracks[i];
|
|
|
|
while (aktualny) {
|
|
|
|
struct car* docasny = aktualny;
|
|
|
|
aktualny = aktualny->next;
|
|
|
|
free(docasny);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(stanica->tracks);
|
|
|
|
free(stanica);
|
2024-11-06 20:52:22 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 21:00:33 +00:00
|
|
|
int select_track(struct station* stanica, const char* ciel) {
|
|
|
|
unsigned int hash = 0;
|
|
|
|
for (int i = 0; ciel[i] != '\0'; i++) {
|
|
|
|
hash = (hash * 31 + ciel[i]) % stanica->track_count;
|
|
|
|
}
|
|
|
|
return hash;
|
2024-11-06 20:52:22 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 21:00:33 +00:00
|
|
|
void add_target_capacity(struct station* stanica, const char* ciel, int kapacita) {
|
|
|
|
int index = select_track(stanica, ciel);
|
|
|
|
struct car* aktualny = stanica->tracks[index];
|
2024-11-06 20:52:22 +00:00
|
|
|
|
2024-11-06 21:00:33 +00:00
|
|
|
while (aktualny != NULL) {
|
|
|
|
if (strcmp(aktualny->value, ciel) == 0) {
|
|
|
|
aktualny->capacity += kapacita;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
aktualny = aktualny->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct car* novy_voz = (struct car*)malloc(sizeof(struct car));
|
|
|
|
strncpy(novy_voz->value, ciel, TARGET_SIZE - 1);
|
|
|
|
novy_voz->value[TARGET_SIZE - 1] = '\0';
|
|
|
|
novy_voz->capacity = kapacita;
|
|
|
|
novy_voz->next = stanica->tracks[index];
|
|
|
|
stanica->tracks[index] = novy_voz;
|
2024-11-06 20:52:22 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 21:00:33 +00:00
|
|
|
int get_target_capacity(struct station* stanica, const char* ciel) {
|
|
|
|
int index = select_track(stanica, ciel);
|
|
|
|
struct car* aktualny = stanica->tracks[index];
|
|
|
|
|
|
|
|
while (aktualny != NULL) {
|
|
|
|
if (strcmp(aktualny->value, ciel) == 0) {
|
|
|
|
return aktualny->capacity;
|
|
|
|
}
|
|
|
|
aktualny = aktualny->next;
|
|
|
|
}
|
2024-11-06 20:52:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-11-06 21:00:33 +00:00
|
|
|
int count_targets(struct station* stanica) {
|
|
|
|
int pocet = 0;
|
|
|
|
for (int i = 0; i < stanica->track_count; i++) {
|
|
|
|
struct car* aktualny = stanica->tracks[i];
|
|
|
|
while (aktualny) {
|
|
|
|
pocet++;
|
|
|
|
aktualny = aktualny->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pocet;
|
2024-11-06 20:52:22 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 21:00:33 +00:00
|
|
|
int count_capacity(struct station* stanica) {
|
|
|
|
int celkova_kapacita = 0;
|
|
|
|
for (int i = 0; i < stanica->track_count; i++) {
|
|
|
|
struct car* aktualny = stanica->tracks[i];
|
|
|
|
while (aktualny) {
|
|
|
|
celkova_kapacita += aktualny->capacity;
|
|
|
|
aktualny = aktualny->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return celkova_kapacita;
|
|
|
|
}
|