Survival Royale 1.0.0
A very simple yet funny card game.
Loading...
Searching...
No Matches
vector.c
Go to the documentation of this file.
1// Copyright (C) 2025 Giulio Salvi, Jacopo Paradisi
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
20
21#include "vector.h"
22
24 vector* v = (vector*)malloc(sizeof(vector));
25
26 if(v == NULL) {
27 free(v);
29 }
30
31 v->size = 0;
33 v->data = (data_type*)malloc(MIN_CAPACITY*sizeof(data_type));
34
35 if(v->data == NULL) {
36 free(v);
38 }
39
40 return v;
41}
42
43void freeVector(vector* const vector) {
44 free(vector->data);
45 free(vector);
46}
47
48void resize(vector* const vector, const size_t newCapacity) {
49 data_type* newDataBuffer = (data_type*)malloc(newCapacity*sizeof(data_type));
50 if(newDataBuffer == NULL) {
53 }
54
55 for(size_t i = 0; i < vector->size; i++)
56 newDataBuffer[i] = vector->data[i];
57
58 free(vector->data);
59 vector->data = newDataBuffer;
60 vector->capacity = newCapacity;
61}
62
63void pushBack(vector* const vector, const data_type element) {
64 if(vector->size == vector->capacity)
66
67 vector->data[vector->size++] = element;
68}
69
71 if(vector->size == 0) {
73 exit(EXIT_POP_FAILURE);
74 }
75
76 data_type rs = vector->data[vector->size-- - 1];
79
80 return rs;
81}
82
83void pushFront(vector* const vector, const data_type element) {
84 if(vector->size == vector->capacity)
86
87 for(size_t i = 0; i < vector->size - 1; i++)
88 vector->data[i + 1] = vector->data[i];
89
90 vector->data[0] = element;
91 vector->size++;
92}
93
95 if(vector->size == 0) {
97 exit(EXIT_POP_FAILURE);
98 }
99
100 data_type rs = vector->data[0];
101 for(size_t i = 0; i < vector->size - 1; i++)
102 vector->data[i] = vector->data[i + 1];
103 vector->size--;
104
107
108 return rs;
109}
110
111data_type at(vector* vector, size_t index) {
112 if(index >= vector->size)
114
115 return vector->data[index];
116}
#define EXIT_ILLEGAL_INDEX_FAILURE
Exit code for requests with illegal indexes.
Definition consts.h:26
#define EXIT_ALLOC_FAILURE
Exit code when dynamic memory allocation fails.
Definition consts.h:22
#define EXIT_POP_FAILURE
Exit code when popping from a vector fails.
Definition consts.h:24
The vector structure.
Definition vector.h:39
data_type * data
Contiguous memory segment with in data is stored.
Definition vector.h:45
size_t size
Field for determining the amount of elements actually stored in memory.
Definition vector.h:41
size_t capacity
Field for determining the amount of possible elements that can be stored in memory without resizing t...
Definition vector.h:43
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 allocati...
Definition vector.c:48
void freeVector(vector *const vector)
Frees the memory used from the vector structure.
Definition vector.c:43
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.
Definition vector.c:83
data_type popBack(vector *const vector)
Pops the element at the back of the vector. If necessary, the vector is resized automatically....
Definition vector.c:70
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 vecto...
Definition vector.c:111
vector * buildVector()
Builds a vector structure initializing its fields. If vector's allocation or data's block allocation ...
Definition vector.c:23
void pushBack(vector *const vector, const data_type element)
Pushes an element at the back of the vector.
Definition vector.c:63
data_type popFront(vector *const vector)
Pops the element at the back of the vector. If necessary, the vector is resized automatically....
Definition vector.c:94
Definition of vector structure and related functions.
#define MIN_CAPACITY
Defines the minimum allocated memory for the vector. If data_type is char, this is 2B of data.
Definition vector.h:29
char data_type
The type of data wrapped in the vector structure. For convenience char has been choosen so it can wra...
Definition vector.h:36
#define RESIZE_COEFFICIENT
Defines the multiplicative coefficient for which the vector has to be resized.
Definition vector.h:33