97 lines
2.3 KiB
C
97 lines
2.3 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){
|
|
for(int i =0;i<station->track_count;i++){
|
|
struct car* thisObj = station->tracks[i];
|
|
while(thisObj!=NULL){
|
|
struct car* delet = thisObj;
|
|
thisObj = thisObj->next;
|
|
free(delet);
|
|
}
|
|
}
|
|
free(station->tracks);
|
|
free(station);
|
|
}
|
|
unsigned long hash_string(unsigned char* str){
|
|
unsigned long hash = 0;
|
|
int c;
|
|
while (c = *str++){
|
|
hash = c + (hash << 6) + (hash << 16) - hash;
|
|
}
|
|
return hash;
|
|
}
|
|
|
|
int select_track(struct station* station, const char* target){
|
|
unsigned long hash= hash_code((unsigned char*)target);
|
|
int index = hash % station->track_count;
|
|
|
|
return index;
|
|
}
|
|
|
|
void add_target_capacity(struct station* station,const char* target, int capacity){
|
|
int index = select_track(station,target);
|
|
struct car* thisObj = station->tracks[indexH];
|
|
|
|
if(thisObj!=NULL){
|
|
while(thisObj!=NULL){
|
|
if(strcmp(thisObj->value,target)==0){
|
|
thisObj->capacity+=capacity;
|
|
}
|
|
thisObj = thisObj->next;
|
|
}
|
|
}else{
|
|
struct car* newCar = calloc(1,sizeof(struct car));
|
|
newCar->capacity = capacity;
|
|
strcpy(newCar->value,target);
|
|
station->tracks[indexHash] = newCar;
|
|
}
|
|
}
|
|
|
|
int get_target_capacity(struct station* station,const char* target){
|
|
for(int i=0;i<station->track_count;i++){
|
|
struct car* thisObj = station->tracks[i];
|
|
while(thisObj!=NULL){
|
|
if(strcmp(thisObj->value,target)==0){
|
|
return thisObj->capacity;
|
|
}
|
|
thisObj = thisObj->next;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int count(struct station* station){
|
|
int count = 0;
|
|
for(int i =0;i<station->track_count;i++){
|
|
struct car* thisObj = station->tracks[i];
|
|
while(thisObj!=NULL){
|
|
count++;
|
|
thisObj = thisObj->next;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
int count_capacity(struct station* station){
|
|
int capacity = 0;
|
|
|
|
for(int i =0;i<station->track_count;i++){
|
|
struct car* thisObj = station->tracks[i];
|
|
while(thisObj!=NULL){
|
|
capacity+=thisObj->capacity;
|
|
thisObj = thisObj->next;
|
|
}
|
|
}
|
|
return capacity;
|
|
}
|
|
|