completed ToH disk ADT implementation

This commit is contained in:
MarcUs7i 2025-01-19 09:22:49 +01:00
parent 88ea1e8453
commit a2930f2134
2 changed files with 76 additions and 5 deletions

View file

@ -9,3 +9,74 @@
* Implementation of toh_disk.h. * Implementation of toh_disk.h.
* ---------------------------------------------------------- * ----------------------------------------------------------
*/ */
#include "toh_disk.h"
#include "config.h"
#include <stdlib.h>
struct DiskData {
unsigned short size;
};
/**
* Provides the instance of the disk with the given size.
* Valid disk sizes are from 1 to MAX_DISKS.
*
* @param size The size of the desired disk.
* @return The disk of the given size or 0,
* if no such disk is available.
*/
Disk td_get_disk(unsigned short size) {
if(size < 1 || size > MAX_DISKS) {
return 0;
}
Disk disk = (Disk)calloc(sizeof(struct DiskData), 1);
disk->size = size;
return disk;
}
/**
* Determines whether or not the given disk is valid.
* It is valid if it is not 0 and has a size between 1 and MAX_DISKS.
*
* @param disk The disk in focus of this ADT.
* @return True if the disk is valid, false otherwise.
*/
bool td_is_valid(Disk disk) {
if (disk == 0 || disk->size < 1 || disk->size > MAX_DISKS) {
return false;
}
return true;
}
/**
* Provides the size of the given disk.
*
* @param disk The disk in focus of this ADT.
* @return The size of the disk, if it is valid or 0 otherwise.
*/
unsigned short td_get_size(Disk disk) {
if (!td_is_valid(disk)) {
return 0;
}
return disk->size;
}
/**
* Compares the size of the candidate disk with size of the given disk.
*
* @param disk The disk in focus of this ADT.
* @param smaller_candidate The disk to evaluate.
* @return True if the 'smaller_candidate' disk is smaller than the
* given disk of this ADT and both disks are valid, false otherwise.
*/
bool td_is_smaller(Disk disk, Disk smaller_candidate) {
if (!td_is_valid(disk) || !td_is_valid(smaller_candidate)) {
return false;
}
return disk->size > smaller_candidate->size;
}

View file

@ -20,7 +20,7 @@
#include <stdbool.h> #include <stdbool.h>
/** Declares a disk. */ /** Declares a disk. */
typedef struct TohDiskData* TohDisk; typedef struct DiskData* Disk;
/** /**
* Provides the instance of the disk with the given size. * Provides the instance of the disk with the given size.
@ -30,7 +30,7 @@ typedef struct TohDiskData* TohDisk;
* @return The disk of the given size or 0, * @return The disk of the given size or 0,
* if no such disk is available. * if no such disk is available.
*/ */
TohDisk td_get_disk(unsigned short size); Disk td_get_disk(unsigned short size);
/** /**
* Determines whether or not the given disk is valid. * Determines whether or not the given disk is valid.
@ -39,7 +39,7 @@ TohDisk td_get_disk(unsigned short size);
* @param disk The disk in focus of this ADT. * @param disk The disk in focus of this ADT.
* @return True if the disk is valid, false otherwise. * @return True if the disk is valid, false otherwise.
*/ */
bool td_is_valid(TohDisk disk); bool td_is_valid(Disk disk);
/** /**
* Provides the size of the given disk. * Provides the size of the given disk.
@ -47,7 +47,7 @@ bool td_is_valid(TohDisk disk);
* @param disk The disk in focus of this ADT. * @param disk The disk in focus of this ADT.
* @return The size of the disk, if it is valid or 0 otherwise. * @return The size of the disk, if it is valid or 0 otherwise.
*/ */
unsigned short td_get_size(TohDisk disk); unsigned short td_get_size(Disk disk);
/** /**
* Compares the size of the candidate disk with size of the given disk. * Compares the size of the candidate disk with size of the given disk.
@ -57,6 +57,6 @@ unsigned short td_get_size(TohDisk disk);
* @return True if the 'smaller_candidate' disk is smaller than the * @return True if the 'smaller_candidate' disk is smaller than the
* given disk of this ADT and both disks are valid, false otherwise. * given disk of this ADT and both disks are valid, false otherwise.
*/ */
bool td_is_smaller(TohDisk disk, TohDisk smaller_candidate); bool td_is_smaller(Disk disk, Disk smaller_candidate);
#endif #endif