Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added kl_size to klist to return number of items #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions klist.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,16 @@
return &q->data; \
} \
SCOPE int kl_shift_##name(kl_##name##_t *kl, kltype_t *d) { \
kl1_##name *p; \
if (kl->head->next == 0) return -1; \
--kl->size; \
p = kl->head; kl->head = kl->head->next; \
kl1_##name *p = kl->head; kl->head = kl->head->next; \
if (d) *d = p->data; \
kmp_free(name, kl->mp, p); \
return 0; \
} \
SCOPE int kl_size_##name(kl_##name##_t *kl) { \
if (kl == 0 || kl->head->next == 0) return 0; \
return kl->size; \
}

#define KLIST_INIT(name, kltype_t, kmpfree_t) \
Expand All @@ -131,5 +134,6 @@
#define kl_destroy(name, kl) kl_destroy_##name(kl)
#define kl_pushp(name, kl) kl_pushp_##name(kl)
#define kl_shift(name, kl, d) kl_shift_##name(kl, d)
#define kl_size(name, kl) kl_size_##name(kl)

#endif
11 changes: 10 additions & 1 deletion test/klist_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ KLIST_INIT(32, int, __int_free)
int main()
{
klist_t(32) *kl;
kliter_t(32) *p;
kl = kl_init(32);
printf("Initial size of list is %d\n", kl_size(32, kl));
*kl_pushp(32, kl) = 1;
*kl_pushp(32, kl) = 2;
*kl_pushp(32, kl) = 3;
*kl_pushp(32, kl) = 4;
*kl_pushp(32, kl) = 5;
*kl_pushp(32, kl) = 6;
*kl_pushp(32, kl) = 7;
*kl_pushp(32, kl) = 10;
printf("Size of list after several pushp is %d\n", kl_size(32, kl));
kl_shift(32, kl, 0);
printf("Size of list after shift is %d\n", kl_size(32, kl));
kliter_t(32) *p;
for (p = kl_begin(kl); p != kl_end(kl); p = kl_next(p))
printf("%d\n", kl_val(p));
kl_destroy(32, kl);
Expand Down