core: make "callback_cmp" optional in call to function arraylist_new()

If no callback is given, a default callback is used, which just compares
pointers.
v2.8-utf8proc
Sébastien Helleu 2017-03-30 20:35:16 +02:00
parent 0436fff31b
commit 467f482ea6
6 changed files with 39 additions and 10 deletions

View File

@ -3875,7 +3875,8 @@ Arguments:
* _sorted_: 1 to sort the array list, 0 for no sort
* _allow_duplicates_: 1 to allow duplicate entries, 0 to prevent a same entry
to be added again
* _callback_cmp_: callback used to compare two items, arguments and return value:
* _callback_cmp_: callback used to compare two items (optional), arguments and
return value:
** _void *data_: pointer
** _struct t_arraylist *arraylist_: array list pointer
** _void *pointer1_: pointer to first item

View File

@ -3937,8 +3937,8 @@ Paramètres :
* _sorted_ : 1 pour trier la liste avec tableau, 0 pour ne pas trier
* _allow_duplicates_ : 1 pour autoriser les entrées dupliquées, 0 pour empêcher
une même entrée d'être ajoutée à nouveau
* _callback_cmp_ : fonction appelée pour comparer deux éléments, paramètres et
valeur de retour :
* _callback_cmp_ : fonction appelée pour comparer deux éléments (optionnelle),
paramètres et valeur de retour :
** _void *data_ : pointeur
** _struct t_arraylist *arraylist_ : pointeur vers la liste avec tableau
** _void *pointer1_ : pointeur vers le premier élément

View File

@ -4006,7 +4006,8 @@ Argomenti:
* _sorted_: 1 to sort the array list, 0 for no sort
* _allow_duplicates_: 1 to allow duplicate entries, 0 to prevent a same entry
to be added again
* _callback_cmp_: callback used to compare two items, arguments and return value:
* _callback_cmp_: callback used to compare two items (optional), arguments and
return value:
** _void *data_: pointer
** _struct t_arraylist *arraylist_: array list pointer
** _void *pointer1_: pointer to first item

View File

@ -3882,7 +3882,9 @@ struct t_arraylist *weechat_arraylist_new (int initial_size,
* _sorted_: 1 の場合には配列リストをソートし、0 の場合にはソートしません
* _allow_duplicates_: 1 の場合にはエントリの重複を許可し、0
の場合にはエントリが重複して追加されることを防ぎます
* _callback_cmp_: 2 つの要素を比較する際に使われるコールバック、引数と戻り値は以下:
// TRANSLATION MISSING
* _callback_cmp_: callback used to compare two items (optional), arguments and
return value:
** _void *data_: ポインタ
** _struct t_arraylist *arraylist_: 配列リストポインタ
** _void *pointer1_: 1 番目の要素へのポインタ

View File

@ -32,6 +32,31 @@
#include "wee-string.h"
/*
* Compares two arraylist entries (default comparator).
* It just compares pointers.
*
* Returns:
* -1: pointer1 < pointer2
* 0: pointer1 == pointer2
* 1: pointer1 > pointer2
*/
int
arraylist_cmp_default_cb (void *data, struct t_arraylist *arraylist,
void *pointer1, void *pointer2)
{
/* make C compiler happy */
(void) data;
(void) arraylist;
if (pointer1 < pointer2)
return -1;
if (pointer1 > pointer2)
return 1;
return 0;
}
/*
* Creates a new arraylist.
*
@ -48,7 +73,7 @@ arraylist_new (int initial_size,
struct t_arraylist *new_arraylist;
/* check arguments */
if ((initial_size < 0) || !callback_cmp)
if (initial_size < 0)
return NULL;
new_arraylist = malloc (sizeof (*new_arraylist));
@ -76,8 +101,10 @@ arraylist_new (int initial_size,
}
new_arraylist->sorted = sorted;
new_arraylist->allow_duplicates = allow_duplicates;
new_arraylist->callback_cmp = callback_cmp;
new_arraylist->callback_cmp_data = callback_cmp_data;
new_arraylist->callback_cmp = (callback_cmp) ?
callback_cmp : &arraylist_cmp_default_cb;
new_arraylist->callback_cmp_data = (callback_cmp) ?
callback_cmp_data : NULL;
new_arraylist->callback_free = callback_free;
new_arraylist->callback_free_data = callback_free_data;

View File

@ -489,8 +489,6 @@ TEST(Arraylist, New)
arraylist_new (-1, 0, 0, NULL, NULL, NULL, NULL));
POINTERS_EQUAL(NULL,
arraylist_new (-1, 0, 0, &test_cmp_cb, NULL, NULL, NULL));
POINTERS_EQUAL(NULL,
arraylist_new (0, 0, 0, NULL, NULL, NULL, NULL));
/* tests on arraylists */
for (initial_size = 0; initial_size < 2; initial_size++)