添加 user_data

This commit is contained in:
Aoran Zeng 2025-08-22 12:16:53 +08:00
parent 1eb0e7ee06
commit eb995e055d
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98
2 changed files with 14 additions and 11 deletions

View File

@ -1356,14 +1356,14 @@ xy_seq_pop (XySeq_t *seq)
* @flavor Ruby: Array#each * @flavor Ruby: Array#each
*/ */
void void
xy_seq_each (XySeq_t *seq, void (*func)(void *)) xy_seq_each (XySeq_t *seq, void (*func) (void *, void *), void *user_data)
{ {
xy_cant_be_null (seq); xy_cant_be_null (seq);
xy_cant_be_null (func); xy_cant_be_null (func);
for (XySeqItem_t *it = seq->first_item; it; it = it->next) for (XySeqItem_t *it = seq->first_item; it; it = it->next)
{ {
func (it->data); func (it->data, user_data);
} }
} }
@ -1371,14 +1371,14 @@ xy_seq_each (XySeq_t *seq, void (*func)(void *))
* @flavor Ruby: Enumerable#find * @flavor Ruby: Enumerable#find
*/ */
void * void *
xy_seq_find (XySeq_t *seq, bool (*func)(void *)) xy_seq_find (XySeq_t *seq, bool (*func) (void *, void *), void *user_data)
{ {
xy_cant_be_null (seq); xy_cant_be_null (seq);
xy_cant_be_null (func); xy_cant_be_null (func);
for (XySeqItem_t *it = seq->first_item; it; it = it->next) for (XySeqItem_t *it = seq->first_item; it; it = it->next)
{ {
if (func (it->data)) if (func (it->data, user_data))
{ {
return it->data; return it->data;
} }
@ -1503,7 +1503,10 @@ xy_map_get (XyMap_t *map, const char *key)
* @flavor Ruby: Hash#each * @flavor Ruby: Hash#each
*/ */
void void
xy_map_each (XyMap_t *map, void (*func)(const char *key, void *value)) xy_map_each (
XyMap_t *map,
void (*func) (const char *key, void *value, void *user_data),
void *user_data)
{ {
xy_cant_be_null (map); xy_cant_be_null (map);
xy_cant_be_null (func); xy_cant_be_null (func);
@ -1513,7 +1516,7 @@ xy_map_each (XyMap_t *map, void (*func)(const char *key, void *value))
struct _XyHashBucket_t *bucket = map->buckets[i]; struct _XyHashBucket_t *bucket = map->buckets[i];
while (bucket) while (bucket)
{ {
func (bucket->key, bucket->value); func (bucket->key, bucket->value, user_data);
bucket = bucket->next; bucket = bucket->next;
} }
} }

View File

@ -5,7 +5,7 @@
* Test Authors : <ccmywish@qq.com> * Test Authors : <ccmywish@qq.com>
* Contributors : Mikachu2333 <mikachu.23333@zohomail.com> * Contributors : Mikachu2333 <mikachu.23333@zohomail.com>
* Created On : <2023-08-30> * Created On : <2023-08-30>
* Last Modified : <2025-08-21> * Last Modified : <2025-08-22>
* *
* Test xy.h * Test xy.h
* ------------------------------------------------------------*/ * ------------------------------------------------------------*/
@ -13,13 +13,13 @@
#include "xy.h" #include "xy.h"
void void
print_str_for_seq (void *str) print_str_for_seq (void *str, void *NOUSE)
{ {
print ((char *) str); print ((char *) str);
} }
void void
print_str_for_map (const char *key, void *value) print_str_for_map (const char *key, void *value, void *NOUSE)
{ {
println (xy_strcat (4, " ", key, " => ", (char *) value)); println (xy_strcat (4, " ", key, " => ", (char *) value));
} }
@ -150,7 +150,7 @@ main (int argc, char const *argv[])
xy_seq_push (seq, "World"); xy_seq_push (seq, "World");
assert_str ("Hello", xy_seq_at (seq, 1)); assert_str ("Hello", xy_seq_at (seq, 1));
assert_str ("World", xy_seq_at (seq, 2)); assert_str ("World", xy_seq_at (seq, 2));
xy_seq_each (seq, print_str_for_seq); br(); xy_seq_each (seq, print_str_for_seq, NULL); br();
xy_seq_pop (seq); xy_seq_pop (seq);
assert (1 == xy_seq_len (seq)); assert (1 == xy_seq_len (seq));
@ -164,7 +164,7 @@ main (int argc, char const *argv[])
assert_str ("chsrc", xy_map_get (map, "Hello")); assert_str ("chsrc", xy_map_get (map, "Hello"));
assert (2 == xy_map_len (map)); assert (2 == xy_map_len (map));
echo ("{"); echo ("{");
xy_map_each (map, print_str_for_map); xy_map_each (map, print_str_for_map, NULL);
echo ("}"); echo ("}");
xy_succ ("测试完成", "xy.h 测试全部通过"); xy_succ ("测试完成", "xy.h 测试全部通过");