127 lines
3.5 KiB
C
127 lines
3.5 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;
|
|
while (currentTracks->next != NULL)
|
|
{
|
|
struct car* tempTrack = currentTracks;
|
|
currentTracks = currentTracks->next;
|
|
free(tempTrack);
|
|
}
|
|
}
|
|
free(station->tracks);
|
|
free(station);
|
|
}
|
|
|
|
int select_track(struct station* station, const char* target)
|
|
{
|
|
if (station == NULL)
|
|
{
|
|
station = (struct station*)calloc(1, sizeof(station));
|
|
}
|
|
|
|
// 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++;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void add_target_capacity(struct station* station,const char* target, int capacity)
|
|
{
|
|
//helper lok. premeny
|
|
int i = select_track(station, target);
|
|
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))
|
|
{
|
|
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
|
|
new_car->next = station->tracks[index];
|
|
station->tracks[index] = new_car;
|
|
}
|
|
|
|
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->next != NULL)
|
|
{
|
|
if (strcmp(currentTracks->value, target) != 0)
|
|
{
|
|
return currentTracks->capacity;
|
|
}
|
|
currentTracks = currentTracks->next;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int count_targets(struct station* station)
|
|
{
|
|
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)
|
|
{
|
|
count += 1;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
int count_capacity(struct station* station){
|
|
int count = 0;
|
|
//preiteruje cez vsetky instancie...
|
|
for (int i = 0; i < station->track_count, i++)
|
|
{
|
|
struct car* currentTracks = station->tracks[i];
|
|
while (currentTracks->next != NULL)
|
|
{
|
|
count += currentTracks->capacity;
|
|
}
|
|
}
|
|
return count;
|
|
} |