diff --git a/du6/a_station.c b/du6/a_station.c new file mode 100644 index 0000000..cc49a8d --- /dev/null +++ b/du6/a_station.c @@ -0,0 +1,176 @@ +#include "a_station.h" +#include +#include +#include + +static void normalize_target(char normalized[TARGET_SIZE], const char* target) { + if (target == NULL) { + normalized[0] = '\0'; + return; + } + + strncpy(normalized, target, TARGET_SIZE - 1); + normalized[TARGET_SIZE - 1] = '\0'; +} + +struct station* create_station() { + struct station* station = (struct station*)calloc(1, sizeof(struct station)); + if (station == NULL) { + return NULL; + } + + station->tracks = (struct car**)calloc(STATION_SIZE, sizeof(struct car*)); + if (station->tracks == NULL) { + free(station); + return NULL; + } + + station->track_count = STATION_SIZE; + return station; +} + +void destroy_station(struct station* station) { + int i; + + if (station == NULL) { + return; + } + + if (station->tracks != NULL) { + for (i = 0; i < station->track_count; i++) { + struct car* current = station->tracks[i]; + while (current != NULL) { + struct car* next = current->next; + free(current); + current = next; + } + } + free(station->tracks); + } + + free(station); +} + +int select_track(struct station* station, const char* target) { + unsigned long hash = 5381; + const unsigned char* p; + char normalized[TARGET_SIZE]; + + if (station == NULL || station->track_count <= 0 || target == NULL) { + return 0; + } + + normalize_target(normalized, target); + p = (const unsigned char*)normalized; + + while (*p != '\0') { + hash = ((hash << 5) + hash) + *p; + p++; + } + + return (int)(hash % (unsigned long)station->track_count); +} + +void add_target_capacity(struct station* station, const char* target, int capacity) { + int index; + struct car* current; + struct car* new_car; + char normalized[TARGET_SIZE]; + + if (station == NULL || station->tracks == NULL || target == NULL || capacity <= 0) { + return; + } + + normalize_target(normalized, target); + + index = select_track(station, normalized); + current = station->tracks[index]; + + while (current != NULL) { + if (strcmp(current->value, normalized) == 0) { + if (current->capacity > INT_MAX - capacity) { + current->capacity = INT_MAX; + return; + } + current->capacity += capacity; + return; + } + current = current->next; + } + + new_car = (struct car*)calloc(1, sizeof(struct car)); + if (new_car == NULL) { + return; + } + + strncpy(new_car->value, normalized, TARGET_SIZE - 1); + new_car->value[TARGET_SIZE - 1] = '\0'; + new_car->capacity = capacity; + new_car->next = station->tracks[index]; + station->tracks[index] = new_car; +} + +int get_target_capacity(struct station* station, const char* target) { + int index; + struct car* current; + char normalized[TARGET_SIZE]; + + if (station == NULL || station->tracks == NULL || target == NULL) { + return 0; + } + + normalize_target(normalized, target); + + index = select_track(station, normalized); + current = station->tracks[index]; + + while (current != NULL) { + if (strcmp(current->value, normalized) == 0) { + return current->capacity; + } + current = current->next; + } + + return 0; +} + +int count_targets(struct station* station) { + int i; + int count = 0; + + if (station == NULL || station->tracks == NULL) { + return 0; + } + + for (i = 0; i < station->track_count; i++) { + struct car* current = station->tracks[i]; + while (current != NULL) { + count++; + current = current->next; + } + } + + return count; +} + +int count_capacity(struct station* station) { + int i; + int sum = 0; + + if (station == NULL || station->tracks == NULL) { + return 0; + } + + for (i = 0; i < station->track_count; i++) { + struct car* current = station->tracks[i]; + while (current != NULL) { + if (current->capacity > 0 && sum > INT_MAX - current->capacity) { + return INT_MAX; + } + sum += current->capacity; + current = current->next; + } + } + + return sum; +}