pvjc26/du6/a_station.c

139 lines
3.8 KiB
C

#include "a_station.h"
#include <stdlib.h>
#include <string.h>
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;
}
void destroy_station(struct station* station)
{
if (station == NULL)
{
return;
}
//preiteruje cez kazdy jeden 'track/carar' v poli 'tracks' a
for (int i = 0; i < station->track_count; i++)
{
struct car* currentTracks = station->tracks[i];
while (currentTracks != NULL)
{
struct car* tempTrack = currentTracks;
currentTracks = currentTracks->next;
free(tempTrack);
}
}
//uvolnenie pamate vrchnych prvkov/'nodeov'..
free(station->tracks);
free(station);
}
int select_track(struct station* station, const char* target)
{
if (station == NULL)
{
return 0;
}
// vyuzitie 'djb2' hashovacieho algorithmu..
unsigned long hash_value = 5381;
int i = 0;
while (target[i] != '\0') {
// viacej 'human readable' verzia algorithmu.
//Pri hashovacich funkciach v programovani je zvykom pouzivat bitwise operator ('>>') na docielenie rovnakeho vysledku
hash_value = (hash_value * 33) + target[i];
i++;
}
unsigned long index = hash_value % station->track_count;
return index;
}
void add_target_capacity(struct station* station,const char* target, int capacity)
{
//helper lok. premeny
int i = select_track(station, target); //vyber track
struct car* current = station->tracks[i];
int lastPosIndex = TARGET_SIZE - 1; //index s poziciou null terminatora
//iterovnie cez vsetky tracky
//Pocas iteracie porovna pole s cielovou stanicou pre aktualny smernik/pointer (current->value) versus vstupny argument 'target'
while (current != NULL)
{
if (strcmp(current->value, target) == 0)
{
current->capacity += capacity;
return;
}
current = current->next;
}
//Poistka -> ak neexistuje novy zaznam, vyvtori ho
struct car* newCar = malloc(sizeof(struct car));
strncpy(newCar->value, target, lastPosIndex);
newCar->value[lastPosIndex] = '\0'; //null terminovanie..
newCar->capacity = capacity;
//vlozi na zaciatok tracku
newCar->next = station->tracks[i];
station->tracks[i] = newCar;
}
int get_target_capacity(struct station* station,const char* target)
{
for (int i = 0; i < station->track_count; i++)
{
struct car* currentTracks = station->tracks[i];
while (currentTracks != NULL)
{
if (strcmp(currentTracks->value, target) != 0)
{
return currentTracks->capacity;
}
currentTracks = currentTracks->next;
}
}
return 0;
}
int count_targets(struct station* station)
{
if (station == NULL)
{
return 0;
}
int count = 0;
for (int i = 0; i < station->track_count; i++)
{
struct car* currentTracks = station->tracks[i];
// while (currentTracks->next->value != NULL && currentTracks->next != NULL)
while (currentTracks != NULL)
{
count += 1;
currentTracks = currentTracks->next;
}
}
return count;
}
int count_capacity(struct station* station){
int count = 0;
for (int i = 0; i < station->track_count; i++)
{
struct car* currentTracks = station->tracks[i];
while (currentTracks != NULL)
{
//skontroluje individualne ich int 'capacity' pole a inkrementuje do vysledneho poctu
count += currentTracks->capacity;
currentTracks = currentTracks->next;
}
}
return count;
}