List iterator
This commit is contained in:
1 parent
945fd71c12
commit
21c4fd836d
4 files changed
+113
-19
No files matched your search
+8
-8
@@ -57,7 +57,7 @@
|
||||
{ \
|
||||
struct SNAME##_s *target; \
|
||||
struct SNAME##_node_s *cursor; \
|
||||
intmax_t count; \
|
||||
size_t index; \
|
||||
bool start; \
|
||||
bool end; \
|
||||
}; \
|
||||
@@ -379,7 +379,7 @@
|
||||
{ \
|
||||
iter->target = target; \
|
||||
iter->cursor = target->head; \
|
||||
iter->count = 0; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = false; \
|
||||
} \
|
||||
@@ -397,7 +397,7 @@
|
||||
FMOD void PFX##_iter_tostart(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->cursor = iter->target->head; \
|
||||
iter->count = 0; \
|
||||
iter->index = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = false; \
|
||||
} \
|
||||
@@ -405,7 +405,7 @@
|
||||
FMOD void PFX##_iter_toend(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->cursor = iter->target->tail; \
|
||||
iter->count = iter->target->count - 1; \
|
||||
iter->index = iter->target->count - 1; \
|
||||
iter->start = false; \
|
||||
iter->end = true; \
|
||||
} \
|
||||
@@ -415,7 +415,7 @@
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
*index = iter->count; \
|
||||
*index = iter->index; \
|
||||
*result = iter->cursor->data; \
|
||||
iter->start = false; \
|
||||
\
|
||||
@@ -424,7 +424,7 @@
|
||||
else \
|
||||
{ \
|
||||
iter->cursor = iter->cursor->next; \
|
||||
iter->count++; \
|
||||
iter->index++; \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
@@ -435,7 +435,7 @@
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
*index = iter->count; \
|
||||
*index = iter->index; \
|
||||
*result = iter->cursor->data; \
|
||||
iter->end = false; \
|
||||
\
|
||||
@@ -444,7 +444,7 @@
|
||||
else \
|
||||
{ \
|
||||
iter->cursor = iter->cursor->prev; \
|
||||
iter->count--; \
|
||||
iter->index--; \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
|
||||
+62
-7
@@ -50,6 +50,8 @@
|
||||
{ \
|
||||
struct SNAME##_s *target; \
|
||||
size_t cursor; \
|
||||
bool start; \
|
||||
bool end; \
|
||||
}; \
|
||||
\
|
||||
/* HEADER ********************************************************************/
|
||||
@@ -77,7 +79,12 @@
|
||||
FMOD size_t PFX##_capacity(SNAME *_list_); \
|
||||
\
|
||||
FMOD void PFX##_iter(SNAME##_iter *iter, SNAME *target); \
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter); \
|
||||
FMOD void PFX##_iter_tostart(SNAME##_iter *iter); \
|
||||
FMOD void PFX##_iter_toend(SNAME##_iter *iter); \
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter, T *result, size_t *index); \
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter, T *result, size_t *index); \
|
||||
\
|
||||
/* SOURCE ********************************************************************/
|
||||
#define LIST_GENERATE_SOURCE(PFX, SNAME, FMOD, K, T) \
|
||||
@@ -318,18 +325,66 @@
|
||||
{ \
|
||||
iter->target = target; \
|
||||
iter->cursor = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = false; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_start(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->cursor == 0 && iter->start; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_end(SNAME##_iter *iter) \
|
||||
{ \
|
||||
return iter->cursor == iter->target->count - 1 && iter->end; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_tostart(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->cursor = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = false; \
|
||||
} \
|
||||
\
|
||||
FMOD void PFX##_iter_toend(SNAME##_iter *iter) \
|
||||
{ \
|
||||
iter->cursor = iter->target->count - 1; \
|
||||
iter->start = false; \
|
||||
iter->end = true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_next(SNAME##_iter *iter, T *result, size_t *index) \
|
||||
{ \
|
||||
if (iter->cursor < iter->target->count) \
|
||||
{ \
|
||||
*index = iter->cursor; \
|
||||
*result = iter->target->buffer[iter->cursor++]; \
|
||||
return true; \
|
||||
} \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
return false; \
|
||||
*index = iter->cursor; \
|
||||
*result = iter->target->buffer[iter->cursor]; \
|
||||
iter->start = false; \
|
||||
\
|
||||
if (iter->cursor == iter->target->count - 1) \
|
||||
iter->end = true; \
|
||||
else \
|
||||
iter->cursor++; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
FMOD bool PFX##_iter_prev(SNAME##_iter *iter, T *result, size_t *index) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
return false; \
|
||||
\
|
||||
*index = iter->cursor; \
|
||||
*result = iter->target->buffer[iter->cursor]; \
|
||||
iter->end = false; \
|
||||
\
|
||||
if (iter->cursor == 0) \
|
||||
iter->start = true; \
|
||||
else \
|
||||
iter->cursor--; \
|
||||
\
|
||||
return true; \
|
||||
}
|
||||
|
||||
#endif /* CMC_LIST_H */
|
||||
@@ -4,9 +4,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "../../src/macro_collections.h"
|
||||
#include "../../src/linkedlist.h"
|
||||
|
||||
CONTAINER_GENERATE(LINKEDLIST, PUBLIC, ll, linked, static, , int)
|
||||
LINKEDLIST_GENERATE(ll, linked, static, , int)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
@@ -26,10 +26,10 @@ int main(int argc, char const *argv[])
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
while (ll_iter_next(&iter, &r, &i))
|
||||
printf("list[%2d] = %2d\n", i, r);
|
||||
printf("C[%2d] = %2d\n", i, r);
|
||||
printf("\n");
|
||||
while (ll_iter_prev(&iter, &r, &i))
|
||||
printf("list[%2d] = %2d\n", i, r);
|
||||
printf("C[%2d] = %2d\n", i, r);
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
// Checks if the list iterator can go back and forward in the list without
|
||||
// hitting invalid states or a nul pointer. Check is done visually.
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "../../src/list.h"
|
||||
|
||||
LIST_GENERATE(l, list, static, , int)
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
size_t i;
|
||||
int r;
|
||||
|
||||
// Initliazie list and add some elements
|
||||
list *l = l_new(10);
|
||||
for (int i = 0; i < 10; i++)
|
||||
l_push_back(l, i);
|
||||
|
||||
// Initialize iterator
|
||||
list_iter iter;
|
||||
l_iter(&iter, l);
|
||||
|
||||
// Make the iterator go back and forward
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
while (l_iter_next(&iter, &r, &i))
|
||||
printf("C[%2d] = %2d\n", i, r);
|
||||
printf("\n");
|
||||
while (l_iter_prev(&iter, &r, &i))
|
||||
printf("C[%2d] = %2d\n", i, r);
|
||||
printf("\n\n");
|
||||
}
|
||||
|
||||
l_free(l);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user