Survival Royale 1.0.0
A very simple yet funny card game.
Loading...
Searching...
No Matches
vector.h File Reference

Definition of vector structure and related functions. More...

#include "consts.h"
#include "includes.h"

Go to the source code of this file.

Data Structures

struct  vector
 The vector structure. More...

Macros

#define _VECTOR_H_
 Include guard.
#define MIN_CAPACITY   2
 Defines the minimum allocated memory for the vector. If data_type is char, this is 2B of data.
#define MAX_CAPACITY   2048
 Defines the maximum allocable memory for the vector. If data_type is char, this is 2KiB of data.
#define RESIZE_COEFFICIENT   2
 Defines the multiplicative coefficient for which the vector has to be resized.

Typedefs

typedef char data_type
 The type of data wrapped in the vector structure. For convenience char has been choosen so it can wrap, by casting, any other primitive data type.
typedef struct vector vector
 The vector structure.

Functions

vectorbuildVector ()
 Builds a vector structure initializing its fields. If vector's allocation or data's block allocation fails, it exits with EXIT_ALLOC_FAILED exit code.
void freeVector (vector *const vector)
 Frees the memory used from the vector structure.
void resize (vector *const vector, const size_t newCapacity)
 Resizes the capacity of the vector with the new capacity specified. If vector's data's block allocation fails, it exits with EXIT_ALLOC_FAILED exit code.
void pushBack (vector *const vector, const data_type element)
 Pushes an element at the back of the vector.
data_type popBack (vector *const vector)
 Pops the element at the back of the vector. If necessary, the vector is resized automatically. If the vector is empty, it exits with EXIT_POP_FAILURE exit code.
void pushFront (vector *const vector, const data_type element)
 Pushes an element at the front of the vector. If necessary, the vector is resized automatically.
data_type popFront (vector *const vector)
 Pops the element at the back of the vector. If necessary, the vector is resized automatically. If the vector is empty, it exits with EXIT_POP_FAILURE exit code.
data_type at (vector *vector, size_t index)
 Access at the element in the given index of the vector. If the index is greater or equal to the vector's size, it exits with EXIT_ILLEGAL_INDEX_FAILURE exit code.

Detailed Description

Definition of vector structure and related functions.

Definition in file vector.h.

Macro Definition Documentation

◆ _VECTOR_H_

#define _VECTOR_H_

Include guard.

Definition at line 26 of file vector.h.

◆ MAX_CAPACITY

#define MAX_CAPACITY   2048

Defines the maximum allocable memory for the vector. If data_type is char, this is 2KiB of data.

Definition at line 31 of file vector.h.

◆ MIN_CAPACITY

#define MIN_CAPACITY   2

Defines the minimum allocated memory for the vector. If data_type is char, this is 2B of data.

Definition at line 29 of file vector.h.

◆ RESIZE_COEFFICIENT

#define RESIZE_COEFFICIENT   2

Defines the multiplicative coefficient for which the vector has to be resized.

Definition at line 33 of file vector.h.

Typedef Documentation

◆ data_type

typedef char data_type

The type of data wrapped in the vector structure. For convenience char has been choosen so it can wrap, by casting, any other primitive data type.

Definition at line 36 of file vector.h.

◆ vector

typedef struct vector vector

The vector structure.

Function Documentation

◆ at()

data_type at ( vector * vector,
size_t index )

Access at the element in the given index of the vector. If the index is greater or equal to the vector's size, it exits with EXIT_ILLEGAL_INDEX_FAILURE exit code.

Parameters
vectorThe vector.
indexThe index to access to.
Returns
The element at the index position in the vector.

Definition at line 111 of file vector.c.

◆ buildVector()

vector * buildVector ( )

Builds a vector structure initializing its fields. If vector's allocation or data's block allocation fails, it exits with EXIT_ALLOC_FAILED exit code.

Returns
A vector structure.

Definition at line 23 of file vector.c.

◆ freeVector()

void freeVector ( vector *const vector)

Frees the memory used from the vector structure.

Parameters
vectorThe vector to be free.

Definition at line 43 of file vector.c.

◆ popBack()

data_type popBack ( vector *const vector)

Pops the element at the back of the vector. If necessary, the vector is resized automatically. If the vector is empty, it exits with EXIT_POP_FAILURE exit code.

Parameters
vectorThe vector from which the element is poped.
Returns
Returns the element at the back of the vector.

Definition at line 70 of file vector.c.

◆ popFront()

data_type popFront ( vector *const vector)

Pops the element at the back of the vector. If necessary, the vector is resized automatically. If the vector is empty, it exits with EXIT_POP_FAILURE exit code.

Parameters
vectorThe vector from which the element is poped.
Returns
Returns the element at the front of the vector.

Definition at line 94 of file vector.c.

◆ pushBack()

void pushBack ( vector *const vector,
const data_type element )

Pushes an element at the back of the vector.

Parameters
vectorThe vector into the element has to be pushed. If necessary, the vector is resized automatically.
elementThe element to be pushed.

Definition at line 63 of file vector.c.

◆ pushFront()

void pushFront ( vector *const vector,
const data_type element )

Pushes an element at the front of the vector. If necessary, the vector is resized automatically.

Parameters
vectorThe vector into the element has to be pushed.
elementThe element to be pushed.

Definition at line 83 of file vector.c.

◆ resize()

void resize ( vector *const vector,
const size_t newCapacity )

Resizes the capacity of the vector with the new capacity specified. If vector's data's block allocation fails, it exits with EXIT_ALLOC_FAILED exit code.

Parameters
vectorThe vector that has to be resized.
newCapacityThe new capacity of the vector.

Definition at line 48 of file vector.c.