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>
6
#include "
../utils/check_alloc.h
"
7
13
typedef
struct
Iterator
{
14
void
*
container
;
15
void
*
begin
;
16
void
* (*next)(
struct
Iterator
*);
17
void (*
free
)(
struct
Iterator
*);
18
bool (*
done
)(
struct
Iterator
*);
19
}
Iterator
;
20
30
static
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
52
static
inline
bool
iterator_done(
Iterator
* it) {
53
if
(it->
done
!= NULL) {
54
return
it->
done
(it);
55
}
56
return
it->
container
== NULL;
57
}
58
65
static
inline
void
* iterator_next(
Iterator
* it) {
66
return
it->
next
(it);
67
}
68
74
static
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
check_alloc.h
Iterator
A generic iterator struct.
Definition
iterator.h:13
Iterator::free
void(* free)(struct Iterator *)
Definition
iterator.h:17
Iterator::container
void * container
Definition
iterator.h:14
Iterator::begin
void * begin
Definition
iterator.h:15
Iterator::next
void *(* next)(struct Iterator *)
Definition
iterator.h:16
Iterator::done
bool(* done)(struct Iterator *)
Definition
iterator.h:18
src
iterator
iterator.h
Generated by
1.14.0