Data Structures Lerax  v3.0-102-gaf18
Opinionated Data Structures & Algorithms
Loading...
Searching...
No Matches
iterator.h
Go to the documentation of this file.
1#ifndef ITERATOR_H
2#define ITERATOR_H
3
4#include <stdbool.h>
5#include <stdlib.h>
7
13typedef struct Iterator {
14 void *container;
15 void *begin;
16 void* (*next)(struct Iterator*);
17 void (*free)(struct Iterator*);
18 bool (*done)(struct Iterator*);
20
30static inline Iterator* iterator_create(
31 void *container,
32 void* (*next)(Iterator*),
33 void (*free)(Iterator*),
34 bool (*done)(Iterator*)
35) {
36 Iterator* it = malloc(sizeof(Iterator));
37 check_alloc(it);
38 it->container = container;
39 it->begin = container;
40 it->next = next;
41 it->free = free;
42 it->done = done;
43 return it;
44}
45
52static inline bool iterator_done(Iterator* it) {
53 if (it->done != NULL) {
54 return it->done(it);
55 }
56 return it->container == NULL;
57}
58
65static inline void* iterator_next(Iterator* it) {
66 return it->next(it);
67}
68
74static inline void iterator_free(Iterator *it) {
75 if (it->free != NULL) {
76 it->free(it);
77 } else {
78 free(it);
79 }
80}
81
82#endif
A generic iterator struct.
Definition iterator.h:13
void(* free)(struct Iterator *)
Definition iterator.h:17
void * container
Definition iterator.h:14
void * begin
Definition iterator.h:15
void *(* next)(struct Iterator *)
Definition iterator.h:16
bool(* done)(struct Iterator *)
Definition iterator.h:18