Skip to content

Commit 02c7862

Browse files
ADD
1 parent 2442c8d commit 02c7862

File tree

1 file changed

+131
-15
lines changed

1 file changed

+131
-15
lines changed

src/Bootstrap.md renamed to Bootstrap.md

Lines changed: 131 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,130 @@ typedef struct {
7979
#define vector_back(v) ((v) && vector_size(v) > 0 ? (TODO) : NULL) // Complétez pour retourner le dernier élément
8080

8181
// Create a new vector of type T with initial capacity c
82-
#define VECTOR(T, c) ((T*)(TODO)) // Complétez pour initialiser un nouveau vecteur
82+
#define VECTOR(T, c) ((T*)vector_init(sizeof(T), (c)))
8383

8484
// Push back n elements (from pointer val) to the vector
85-
#define vector_push_back(v, val, n) (TODO) // Complétez pour ajouter des éléments au vecteur
85+
#define vector_push_back(v, val, n) vector_push_back_impl((void**)&(v), (const void*)(val), (n), sizeof(*(v))) // Complétez pour ajouter des éléments au vecteur
8686

8787
// Pop back n elements
88-
#define vector_pop_back(v, n) (TODO) // Complétez pour retirer des éléments du vecteur
88+
#define vector_pop_back(v, n) vector_pop_back_impl(TODO, (n), TODO) // Complétez pour retirer des éléments du vecteur
8989

9090
// Clear the vector (size = 0, but doesn't change capacity)
9191
#define vector_clear(v) (TODO) // Complétez pour réinitialiser la taille à 0
9292

9393
// Destroy the vector completely (free memory)
9494
#define vector_destroy(v) (TODO) // Complétez pour libérer la mémoire allouée au vecteur
9595

96+
// Reserve capacity
97+
#define vector_reserve(v, n) vector_reserve_impl(TODO, (n), TODO) // Complétez pour réserver de la capacité
98+
99+
// Resize the vector (increase or decrease size)
100+
#define vector_resize(v, new_size) vector_resize_impl(TODO, (new_size), TODO) // Complétez pour redimensionner le vecteur
101+
102+
// Shrink to fit (reduce capacity to current size)
103+
#define vector_shrink_to_fit(v) vector_shrink_to_fit_impl(TODO, TODO)
104+
105+
// Insert elements at the specified index
106+
#define vector_insert(v, index, val, count) vector_insert_impl(TODO, (index), (val), (count), TODO)
107+
108+
// Erase elements at the specified index
109+
#define vector_erase(v, index, count) vector_erase_impl(TODO, (index), (count), TODO)
110+
96111
#ifdef __cplusplus
97112
extern "C" {
98113
#endif
99114

115+
/**
116+
* @brief Initialize a new vector with given element size and capacity.
117+
*
118+
* @param item_size Size of each element.
119+
* @param capacity Initial capacity.
120+
* @return Pointer to the newly allocated vector, or NULL on failure.
121+
*/
100122
void *vector_init(size_t item_size, size_t capacity) WARN_UNUSED_RESULT;
123+
124+
/**
125+
* @brief Ensure the vector has enough capacity for at least item_count new elements.
126+
*
127+
* @param v Pointer to the vector.
128+
* @param item_count Number of additional elements required.
129+
* @param item_size Size of each element.
130+
* @return A pointer to the (possibly reallocated) vector, or NULL on failure.
131+
*/
101132
void *vector_ensure_capacity(void *v, size_t item_count, size_t item_size) WARN_UNUSED_RESULT;
102-
int vector_push_back_impl(void **v, const void *val, size_t n, size_t item_size) WARN_UNUSED_RESULT;
103-
void vector_pop_back_impl(void **v, size_t n, size_t item_size);
104-
int vector_resize_impl(void **v, size_t new_size, size_t item_size);
105-
int vector_shrink_to_fit_impl(void **v, size_t item_size);
106-
int vector_insert_impl(void **v, size_t index, const void *val, size_t count, size_t item_size) WARN_UNUSED_RESULT;
107-
void vector_erase_impl(void **v, size_t index, size_t count, size_t item_size);
133+
134+
/**
135+
* @brief Push back n elements from val to the end of the vector.
136+
*
137+
* @param v Address of the vector pointer.
138+
* @param val Pointer to the data to insert.
139+
* @param n Number of elements to insert.
140+
* @param item_size Size of each element.
141+
* @return VECTOR_SUCCESS if successful, VECTOR_FAILURE otherwise.
142+
*/
143+
int vector_push_back_impl(TODO v, const void *val, size_t n, size_t item_size) WARN_UNUSED_RESULT;
144+
145+
/**
146+
* @brief Pop back n elements from the vector.
147+
*
148+
* @param v Address of the vector pointer.
149+
* @param n Number of elements to remove from the end.
150+
* @param item_size Size of each element.
151+
*/
152+
void vector_pop_back_impl(TODO v, size_t n, size_t item_size);
153+
154+
/**
155+
* @brief Reserve capacity for at least 'c' elements.
156+
*
157+
* @param v Address of the vector pointer.
158+
* @param n Number of elements to reserve.
159+
* @param item_size Size of each element.
160+
* @return VECTOR_SUCCESS on success, VECTOR_FAILURE on failure.
161+
*/
162+
int vector_reserve_impl(TODO v, size_t n, size_t item_size);
163+
164+
/**
165+
* @brief Resize the vector to have new_size elements.
166+
* If new_size > current_size, new elements are zero-initialized.
167+
* If new_size < current_size, elements are truncated.
168+
*
169+
* @param v Address of the vector pointer.
170+
* @param new_size New desired size.
171+
* @param item_size Size of each element.
172+
* @return VECTOR_SUCCESS on success, VECTOR_FAILURE on failure.
173+
*/
174+
int vector_resize_impl(TODO v, size_t new_size, size_t item_size);
175+
176+
/**
177+
* @brief Shrink the capacity of the vector to match its current size.
178+
*
179+
* @param v Address of the vector pointer.
180+
* @param item_size Size of each element.
181+
* @return VECTOR_SUCCESS on success, VECTOR_FAILURE on failure.
182+
*/
183+
int vector_shrink_to_fit_impl(TODO v, size_t item_size);
184+
185+
/**
186+
* @brief Insert an element at the specified index (like std::vector::insert).
187+
*
188+
* @param v Address of the vector pointer.
189+
* @param index Position where to insert the elements.
190+
* @param val Pointer to the element(s) to insert.
191+
* @param count Number of elements to insert.
192+
* @param item_size Size of each element.
193+
* @return VECTOR_SUCCESS on success, VECTOR_FAILURE on failure.
194+
*/
195+
int vector_insert_impl(TODO v, size_t index, const void *val, size_t count, size_t item_size) WARN_UNUSED_RESULT;
196+
197+
/**
198+
* @brief Erase elements at the specified index (like std::vector::erase).
199+
*
200+
* @param v Address of the vector pointer.
201+
* @param index Position of the first element to erase.
202+
* @param count Number of elements to erase.
203+
* @param item_size Size of each element.
204+
*/
205+
void vector_erase_impl(TODO v, size_t index, size_t count, size_t item_size);
108206

109207
#ifdef __cplusplus
110208
}
@@ -132,12 +230,12 @@ void *vector_ensure_capacity(void *v, size_t item_count, size_t item_size)
132230
// TODO : Implémentez la logique pour garantir la capacité
133231
}
134232
135-
int vector_push_back_impl(void **v, const void *val, size_t count, size_t item_size)
233+
int vector_push_back_impl(TODO v, const void *val, size_t count, size_t item_size)
136234
{
137235
// TODO : Implémentez l'ajout d'éléments à la fin
138236
}
139237
140-
void vector_pop_back_impl(void **v, size_t count, size_t item_size)
238+
void vector_pop_back_impl(TODO v, size_t count, size_t item_size)
141239
{
142240
// TODO : Implémentez la suppression des éléments en fin de vecteur
143241
}
@@ -152,12 +250,12 @@ Dans ce fichier, implémentez les fonctions d'insertion et de suppression.
152250
```c
153251
#include "cvector.h"
154252

155-
int vector_insert_impl(void **v, const size_t index, const void *val, const size_t count, size_t item_size)
253+
int vector_insert_impl(TODO v, const size_t index, const void *val, const size_t count, size_t item_size)
156254
{
157255
// TODO : Implémentez la logique pour insérer des éléments
158256
}
159257

160-
void vector_erase_impl(void **v, const size_t index, size_t count, const size_t item_size)
258+
void vector_erase_impl(TODO v, const size_t index, size_t count, const size_t item_size)
161259
{
162260
// TODO : Implémentez la logique pour supprimer des éléments
163261
}
@@ -172,14 +270,32 @@ Dans ce fichier, implémentez les fonctions de redimensionnement.
172270
```c
173271
#include "cvector.h"
174272
175-
int vector_resize_impl(void **v, const size_t new_size, const size_t item_size)
273+
int vector_reserve_impl(void **v, const size_t n,
274+
const size_t item_size)
275+
{
276+
277+
// TODO : Implémentez la logique pour réserver de la capacité
278+
}
279+
280+
static inline void *vector_init_zeroed(size_t item_size, size_t capacity)
281+
{
282+
// TODO : Implémentez la logique pour initialiser un vecteur avec des éléments à zéro
283+
284+
-> man calloc
285+
}
286+
287+
int vector_resize_impl(TODO v, const size_t new_size, const size_t item_size)
176288
{
177289
// TODO : Implémentez la logique pour redimensionner le vecteur
290+
291+
-> utiliser vector_init_zeroed si !*v
178292
}
179293
180-
int vector_shrink_to_fit_impl(void **v, const size_t item_size)
294+
int vector_shrink_to_fit_impl(TODO v, const size_t item_size)
181295
{
182296
// TODO : Implémentez la logique pour réduire la capacité
297+
298+
-> man realloc
183299
}
184300
```
185301

0 commit comments

Comments
 (0)