Skip to content

Commit d26f88b

Browse files
committed
improve foreach
1 parent e1f1482 commit d26f88b

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

src/tkc/darray.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,13 @@ ret_t darray_foreach(darray_t* darray, tk_visit_t visit, void* ctx) {
349349
return_value_if_fail(darray != NULL && visit != NULL, RET_BAD_PARAMS);
350350

351351
if (darray->elms != NULL) {
352-
uint32_t i = 0;
352+
int64_t i = 0;
353353
void** elms = darray->elms;
354354

355355
for (i = 0; i < darray->size; i++) {
356356
void* iter = elms[i];
357357
ret = visit(ctx, iter);
358-
TK_FOREACH_VISIT_RESULT_PROCESSING(ret, darray_remove_index(darray, i));
358+
TK_FOREACH_VISIT_RESULT_PROCESSING(ret, darray_remove_index(darray, i); i--);
359359
}
360360
}
361361

src/tkc/types_def.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ typedef struct _key_type_value_t {
669669
continue; \
670670
} else if (RET_REPEAT == result) { \
671671
log_warn("%s: result type REPEAT is not supported!\n", __FUNCTION__); \
672+
result = RET_OK; \
672673
} else if (result != RET_OK) { \
673674
break; \
674675
} \

tests/darray_test.cc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#include <math.h>
66
#include <stdlib.h>
77

8+
#include <string>
9+
using std::string;
10+
811
#define NR 1000
912

1013
static void test_add(darray_t* darray, void* pv) {
@@ -849,6 +852,52 @@ TEST(DArrayTest, foreach) {
849852
darray_deinit(&darray);
850853
}
851854

855+
static ret_t visit_dump(void* ctx, const void* data) {
856+
char text[32];
857+
string& str = *(string*)ctx;
858+
int32_t n = (const char*)data - (const char*)NULL;
859+
tk_snprintf(text, sizeof(text), "%d:", n);
860+
861+
str += text;
862+
863+
return RET_OK;
864+
}
865+
866+
static ret_t remove_data(void* ctx, const void* data) {
867+
char text[32];
868+
string& str = *(string*)ctx;
869+
int32_t n = (const char*)data - (const char*)NULL;
870+
tk_snprintf(text, sizeof(text), "%d:", n);
871+
872+
str += text;
873+
if (n % 2 == 0) {
874+
return RET_OK;
875+
} else {
876+
return RET_REMOVE;
877+
}
878+
}
879+
880+
TEST(DArrayTest, foreach_ex) {
881+
string log;
882+
darray_t darray;
883+
darray_init(&darray, 10, NULL, NULL);
884+
885+
ASSERT_EQ(darray_push(&darray, tk_pointer_from_int(1)), RET_OK);
886+
ASSERT_EQ(darray_push(&darray, tk_pointer_from_int(2)), RET_OK);
887+
ASSERT_EQ(darray_push(&darray, tk_pointer_from_int(3)), RET_OK);
888+
ASSERT_EQ(darray_push(&darray, tk_pointer_from_int(4)), RET_OK);
889+
890+
log = "";
891+
darray_foreach(&darray, remove_data, &log);
892+
ASSERT_EQ(log, "1:2:3:4:");
893+
894+
log = "";
895+
darray_foreach(&darray, visit_dump, &log);
896+
ASSERT_EQ(log, "2:4:");
897+
898+
darray_deinit(&darray);
899+
}
900+
852901
TEST(DArrayTest, bsearch_index_ex) {
853902
darray_t darray;
854903
int32_t low = -1;

0 commit comments

Comments
 (0)