reduced line number of functions

This commit is contained in:
Phuntsok Drak-pa 2018-10-31 09:56:24 +01:00
parent 9b263c63e0
commit 3701a20d22
2 changed files with 32 additions and 19 deletions

View File

@ -3,7 +3,7 @@
* \brief Implémentation des fonctions pour le type \ref darray_t
*/
#include <darray.h>
#include "darray.h"
#include <stdio.h>
#include <string.h>
@ -40,31 +40,41 @@ darray_t *darrayNew(size_t element_size) {
* \param elem Élément que lon souhaite insérer
*/
void darrayInsert(darray_t *self, void *pos, void *elem) {
void *new_array;
char *itr;
unsigned long pos_aux;
size_t size;
long pos_aux;
pos_aux = (char *)pos - (char *)self->begin;
if (darraySize(self) >= self->capacity) {
size = darraySize(self);
new_array =
realloc(self->begin, (darraySize(self) + 1) * self->element_size);
if (!new_array) {
fprintf(stderr, "Failed memory reallocation at %s:%d\nAborting...",
__FILE__, __LINE__ - 2);
exit(PTR_ERROR);
}
self->begin = new_array;
++self->capacity;
self->end = (char *)self->begin + size * self->element_size;
darrayExtend(self);
}
itr = (char *)self->begin + pos_aux;
memmove(itr + self->element_size, itr, (char *)self->end - itr);
memmove(itr + self->element_size, itr, (unsigned long)((char *)self->end - itr));
memcpy(itr, elem, self->element_size);
self->end = (char *)self->end + self->element_size;
}
/**
* Étend la capacité d'un \ref darray_t en réallouant sa mémoire, multipliant
* sa capacité par deux. Si la réallocation mémoire ne réussit pas, le
* programme sarrête immédiatement, renvoyant la valeur de \ref PTR_ERROR
*
* \param self L'objet darray_t à étendre
*/
void darrayExtend(darray_t *self) {
void *new_array;
size_t size;
size = darraySize(self);
new_array =
realloc(self->begin, (darraySize(self) + 1) * self->element_size);
if (!new_array) {
fprintf(stderr, "Failed memory reallocation at %s:%d\nAborting...",
__FILE__, __LINE__ - 2);
exit(PTR_ERROR);
}
self->begin = new_array;
++self->capacity;
self->end = (char *)self->begin + size * self->element_size;
}
/**
* `darrayErase` supprime lélément de objet \ref darray_t `self` pointé par
* `pos`. Cela décalera tous les éléments suivants dans le tableau dun cran
@ -76,7 +86,7 @@ void darrayInsert(darray_t *self, void *pos, void *elem) {
*/
void darrayErase(darray_t *self, void *pos) {
memmove(pos, (char *)pos + self->element_size,
((char *)self->end - self->element_size) - (char *)pos);
(unsigned long)(((char *)self->end - self->element_size) - (char *)pos));
self->end = (char *)self->end - self->element_size;
}
@ -120,7 +130,7 @@ void darrayDelete(darray_t *self) {
* \return Nombre déléments contenus dans `self`
*/
size_t darraySize(darray_t *self) {
return ((char *)self->end - (char *)self->begin) / self->element_size;
return (size_t)((char *)self->end - (char *)self->begin) / self->element_size;
}
/**

View File

@ -25,6 +25,9 @@ typedef struct darray_s {
/// \brief Créé un nouvel objet \ref darray_t vide
darray_t *darrayNew(size_t element_size);
/// \brief Augmente la capacité d'un \ref darray_t
void darrayExtend(darray_t *self);
/// \brief Insère un élément à lendroit pointé dans un \ref darray_t
void darrayInsert(darray_t *self, void *pos, void *elem);