summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2017-07-12 22:47:38 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2017-07-12 22:47:38 (GMT)
commitd7f78fe9a75d96b6f3d441335dcf50a5c026d8ea (patch)
tree632744c467641825441b866c4f988c03d9c20cf1 /src/common
parentbdda063b67d8c1d402f6dc17726fed0c800d3d1c (diff)
Taken into account that raw immediate values can be used more than once.
Diffstat (limited to 'src/common')
-rw-r--r--src/common/array.c66
-rw-r--r--src/common/array.h3
2 files changed, 69 insertions, 0 deletions
diff --git a/src/common/array.c b/src/common/array.c
index dcb9344..36c30a1 100644
--- a/src/common/array.c
+++ b/src/common/array.c
@@ -632,3 +632,69 @@ void *get_flat_array_item(flat_array_t *array, size_t index, size_t size)
return result;
}
+
+
+/******************************************************************************
+* *
+* Paramètres : array = tableau compressé à consulter. *
+* size = taille de ce nouvel élément. *
+* compar = méthode de comparaison entre éléments. *
+* key = élément de comparaison fourni. *
+* *
+* Description : Recherche un élément dans un tableau trié. *
+* *
+* Retour : Eventuel élément trouvé ou NULL. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void *find_item_in_flat_array(flat_array_t *array, size_t size, __compar_fn_t compar, const void *key)
+{
+ void *result; /* Trouvaille à retourner */
+ size_t count; /* Nombre d'éléments présents */
+ void *item; /* Elément isolé présent */
+ ext_flat_array_t *extended; /* Version de tableau étendue */
+ size_t index; /* Indice de l'élément trouvé */
+
+ assert(FLAT_ARRAY_IS_LOCKED(array));
+
+ count = count_flat_array_items(array);
+
+ switch (count)
+ {
+ case 0:
+ result = NULL;
+ break;
+
+ case 1:
+
+ assert(FLAT_ARRAY_HAS_NO_INDEX(array));
+
+ item = GET_LONELY_ITEM(array);
+
+ if (compar(key, item) == 0)
+ result = item;
+ else
+ result = NULL;
+
+ break;
+
+ default:
+
+ assert(!FLAT_ARRAY_HAS_NO_INDEX(array));
+
+ extended = EXTENDED_ARRAY(array);
+
+ if (bsearch_index(key, extended->items, extended->count, size, compar, &index))
+ result = (void *)(((char *)extended->items) + index * size);
+ else
+ result = NULL;
+
+ break;
+
+ }
+
+ return result;
+
+}
diff --git a/src/common/array.h b/src/common/array.h
index 546de1a..5ddc9ba 100644
--- a/src/common/array.h
+++ b/src/common/array.h
@@ -67,6 +67,9 @@ void rem_item_from_flat_array(flat_array_t **, size_t, size_t);
/* Fournit un élément présent dans un tableau compressé. */
void *get_flat_array_item(flat_array_t *, size_t, size_t);
+/* Recherche un élément dans un tableau trié. */
+void *find_item_in_flat_array(flat_array_t *, size_t, __compar_fn_t, const void *);
+
#endif /* _COMMON_ARRAY_H */