toolbox

Check-in [8b222eb2dc]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:lib: rework jtlst_iter
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 8b222eb2dc3adc338dd91e9c303c1d3c207721191ca03876d5e77a25ec2651d4
User & Date: jef 2020-07-18 08:46:10
Context
2020-07-18
08:46
retab: adapt to new jtlst_iter design check-in: 83160f9bc8 user: jef tags: trunk
08:46
lib: rework jtlst_iter check-in: 8b222eb2dc user: jef tags: trunk
2020-07-17
19:29
retab: use generic struct jtlst_str to store columns check-in: 8db1f2b13e user: jef tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/include/jt.h.

66
67
68
69
70
71
72








73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

struct jtlst {
	size_t offset;
	size_t size;
	struct jtlst_node head;
};









#define jtlst_init(list, type, member) \
	jtlst_init_offset(list, offsetof(type, member))

struct jtlst *jtlst_new();
void jtlst_init_offset(struct jtlst *list, size_t offset);
void jtlst_prepend(struct jtlst *list, struct jtlst_node *node);
void jtlst_append(struct jtlst *list, struct jtlst_node *node);
void *jtlst_pop(struct jtlst *list);
size_t jtlst_size(struct jtlst *list);
void jtlst_free(struct jtlst *list);

#define jtlst_foreach(elem, list) \
	for (struct jtlst_node *jtlst_it_##elem = (list)->head.next; \
			jtlst_it_##elem != &(list)->head \
			&& ((elem) = ((void *) ((char *) (jtlst_it_##elem) - (list)->offset))); \
			jtlst_it_##elem = (jtlst_it_##elem)->next)

struct jtlst_iter {
	struct jtlst *list;
	struct jtlst_node *current;
};

void jtlst_iter(struct jtlst *list, struct jtlst_iter *iter);
void * jtlst_iter_next(struct jtlst_iter *iter);

/*
** str
*/ 

struct jtlst_str {
	struct jtlst_node node;







>
>
>
>
>
>
>
>












|
|
<
<
<
<
<
<
<
<
<
<







66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94










95
96
97
98
99
100
101

struct jtlst {
	size_t offset;
	size_t size;
	struct jtlst_node head;
};

struct jtlst_iter {
	struct jtlst *list;
	struct jtlst_node *current;
};

struct jtlst_iter jtlst_iter_start(struct jtlst *list);
void *jtlst_iter_next(struct jtlst_iter *iter);

#define jtlst_init(list, type, member) \
	jtlst_init_offset(list, offsetof(type, member))

struct jtlst *jtlst_new();
void jtlst_init_offset(struct jtlst *list, size_t offset);
void jtlst_prepend(struct jtlst *list, struct jtlst_node *node);
void jtlst_append(struct jtlst *list, struct jtlst_node *node);
void *jtlst_pop(struct jtlst *list);
size_t jtlst_size(struct jtlst *list);
void jtlst_free(struct jtlst *list);

#define jtlst_foreach(elem, list) \
	for (struct jtlst_iter jtlst_it_##elem = jtlst_iter_start(list); \
			((elem) = jtlst_iter_next(&(jtlst_it_##elem)));)











/*
** str
*/ 

struct jtlst_str {
	struct jtlst_node node;

Changes to src/lib/jtlst.c.

56
57
58
59
60
61
62
63
64

65
66
67


68
69
70
71
72
73
74
75
76
77
78
79
	void *elem;

	while ((elem = jtlst_pop(list)))
		xfree(elem);
	xfree(list);
}

void
jtlst_iter(struct jtlst *list, struct jtlst_iter *iter)

{
	iter->list = list;
	iter->current = &list->head;


}

void *
jtlst_iter_next(struct jtlst_iter *iter)
{
	if (iter->current->next == &iter->list->head)
		return NULL;

	iter->current = iter->current->next;

	return (void *) ((char *) iter->current - iter->list->offset);
}







|
|
>

<
|
>
>












56
57
58
59
60
61
62
63
64
65
66

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
	void *elem;

	while ((elem = jtlst_pop(list)))
		xfree(elem);
	xfree(list);
}

inline
struct jtlst_iter
jtlst_iter_start(struct jtlst *list)
{

	struct jtlst_node *current = &list->head;

	return (struct jtlst_iter) {list, current};
}

void *
jtlst_iter_next(struct jtlst_iter *iter)
{
	if (iter->current->next == &iter->list->head)
		return NULL;

	iter->current = iter->current->next;

	return (void *) ((char *) iter->current - iter->list->offset);
}