summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/array.c61
-rw-r--r--src/common/array.h4
2 files changed, 65 insertions, 0 deletions
diff --git a/src/common/array.c b/src/common/array.c
index 641a885..e4e7bb1 100644
--- a/src/common/array.c
+++ b/src/common/array.c
@@ -34,6 +34,9 @@
#include <string.h>
+#include "sort.h"
+
+
/**
* L'expression du besoin d'une gestion optimisée des tableaux se base sur la
@@ -259,6 +262,64 @@ void add_item_to_flat_array(flat_array_t **array, const void *item, size_t size)
/******************************************************************************
* *
+* Paramètres : array = tableau compressé à mettre à jour. [OUT] *
+* item = adresse de l'élément à rajouter. *
+* size = taille de ce nouvel élément. *
+* compar = méthode de comparaison entre éléments. *
+* *
+* Description : Ajoute un élément supplémentaire à un tableau trié. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void insert_item_into_flat_array(flat_array_t **array, void *item, size_t size, __compar_fn_t compar)
+{
+ ext_flat_array_t *extended; /* Version de tableau étendue */
+
+ assert(FLAT_ARRAY_IS_LOCKED(*array));
+
+ if (FLAT_ARRAY_IS_EMPTY(*array))
+ {
+ *array = malloc(size);
+ memcpy(*array, item, size);
+
+ lock_flat_array(array);
+
+ }
+
+ else
+ {
+ if (FLAT_ARRAY_HAS_NO_INDEX(*array))
+ {
+ extended = (ext_flat_array_t *)malloc(sizeof(ext_flat_array_t));
+
+ extended->items = malloc(size);
+ extended->count = 1;
+
+ memcpy(extended->items, GET_LONELY_ITEM(*array), size);
+
+ FLAT_ARRAY_SET_INDEX(extended);
+
+ *array = (flat_array_t *)extended;
+
+ lock_flat_array(array);
+
+ }
+
+ extended = EXTENDED_ARRAY(*array);
+
+ extended->items = qinsert(extended->items, &extended->count, size, compar, item);
+
+ }
+
+}
+
+
+/******************************************************************************
+* *
* Paramètres : array = tableau compressé à mettre à jour. *
* index = indice de l'élément à remplacer. *
* new = adresse de l'élément à rajouter. *
diff --git a/src/common/array.h b/src/common/array.h
index 416800d..2dd5b9f 100644
--- a/src/common/array.h
+++ b/src/common/array.h
@@ -25,6 +25,7 @@
#define _COMMON_ARRAY_H
+#include <stdlib.h>
#include <sys/types.h>
@@ -45,6 +46,9 @@ size_t count_flat_array_items(const flat_array_t *);
/* Ajoute un élément supplémentaire à un tableau. */
void add_item_to_flat_array(flat_array_t **, const void *, size_t);
+/* Ajoute un élément supplémentaire à un tableau trié. */
+void insert_item_into_flat_array(flat_array_t **, void *, size_t, __compar_fn_t);
+
/* Remplace un élément d'un tableau compressé par un autre. */
void rpl_item_in_flat_array(flat_array_t *, size_t, void *, size_t);