Freeciv-3.2
Loading...
Searching...
No Matches
genlist.h
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13#ifndef FC__GENLIST_H
14#define FC__GENLIST_H
15
16#ifdef __cplusplus
17extern "C" {
18#endif /* __cplusplus */
19
20/****************************************************************************
21 MODULE: genlist
22
23 A "genlist" is a generic doubly-linked list. That is:
24 generic: stores (void*) pointers to arbitrary user data;
25 doubly-linked: can be efficiently traversed both "forwards"
26 and "backwards".
27
28 The list data structures are allocated dynamically, and list elements can
29 be added or removed at arbitrary positions.
30
31 Positions in the list are specified starting from 0, up to n - 1 for a
32 list with n elements. The position -1 can be used to refer to the last
33 element (that is, the same as n - 1, or n when adding a new element), but
34 other negative numbers are not meaningful.
35
36 A trap to beware of with iterators is modifying the list while the
37 iterator is active, in particular removing the next element pointed
38 to by the iterator (see further comments below).
39
40 See also the speclist module.
41****************************************************************************/
42
43/* utility */
44#include "fcthread.h"
45#include "support.h" /* bool, fc__warn_unused_result */
46
47/* A single element of a genlist, opaque type. */
48struct genlist_link;
49
50/* Function type definitions. */
51typedef void (*genlist_free_fn_t) (void *);
52typedef void * (*genlist_copy_fn_t) (const void *);
53typedef bool (*genlist_cond_fn_t) (const void *);
54typedef bool (*genlist_comp_fn_t) (const void *, const void *);
55
56/* A genlist, storing the number of elements (for quick retrieval and
57 * testing for empty lists), and pointers to the first and last elements
58 * of the list. */
66
70void genlist_destroy(struct genlist *pgenlist);
71
72struct genlist *genlist_copy(const struct genlist *pgenlist)
74struct genlist *genlist_copy_full(const struct genlist *pgenlist,
78
79void genlist_clear(struct genlist *pgenlist);
80
81void genlist_unique(struct genlist *pgenlist);
84
85void genlist_append(struct genlist *pgenlist, void *data);
86void genlist_prepend(struct genlist *pgenlist, void *data);
87void genlist_insert(struct genlist *pgenlist, void *data, int idx);
88void genlist_insert_after(struct genlist *pgenlist, void *data,
89 struct genlist_link *plink);
90void genlist_insert_before(struct genlist *pgenlist, void *data,
91 struct genlist_link *plink);
92
93bool genlist_remove(struct genlist *pgenlist, const void *data);
96int genlist_remove_all(struct genlist *pgenlist, const void *data);
99void genlist_erase(struct genlist *pgenlist, struct genlist_link *plink);
101void genlist_pop_back(struct genlist *pgenlist);
102
103int genlist_size(const struct genlist *pgenlist);
104void *genlist_get(const struct genlist *pgenlist, int idx);
105void *genlist_front(const struct genlist *pgenlist);
106void *genlist_back(const struct genlist *pgenlist);
107struct genlist_link *genlist_link_get(const struct genlist *pgenlist, int idx);
108inline static struct genlist_link *genlist_head(const struct genlist *pgenlist)
109{
110 return (NULL != pgenlist ? pgenlist->head_link : NULL);
111}
112struct genlist_link *genlist_tail(const struct genlist *pgenlist);
113
114struct genlist_link *genlist_search(const struct genlist *pgenlist,
115 const void *data);
116struct genlist_link *genlist_search_if(const struct genlist *pgenlist,
118
119void genlist_sort(struct genlist *pgenlist,
120 int (*compar) (const void *, const void *));
121void genlist_shuffle(struct genlist *pgenlist);
122void genlist_reverse(struct genlist *pgenlist);
123
126
127
128/* A single element of a genlist, storing the pointer to user
129 * data, and pointers to the next and previous elements: */
132 void *dataptr;
133};
134
135/************************************************************************/
140static inline void *genlist_link_data(const struct genlist_link *plink)
141{
142 return (NULL != plink ? plink->dataptr : NULL);
143}
144
145/************************************************************************/
151static inline struct genlist_link *genlist_link_prev(const struct genlist_link *plink)
152{
153 return plink->prev;
154}
155
156/************************************************************************/
162static inline struct genlist_link *genlist_link_next(const struct genlist_link *plink)
163{
164 return plink->next;
165}
166
167#ifdef __cplusplus
168}
169#endif /* __cplusplus */
170
171#endif /* FC__GENLIST_H */
char * incite_cost
Definition comments.c:75
void genlist_insert(struct genlist *pgenlist, void *data, int idx)
Definition genlist.c:477
void genlist_allocate_mutex(struct genlist *pgenlist)
Definition genlist.c:682
void genlist_release_mutex(struct genlist *pgenlist)
Definition genlist.c:690
void * genlist_get(const struct genlist *pgenlist, int idx)
Definition genlist.c:224
bool(* genlist_cond_fn_t)(const void *)
Definition genlist.h:53
void genlist_erase(struct genlist *pgenlist, struct genlist_link *plink)
Definition genlist.c:436
struct genlist_link * genlist_search_if(const struct genlist *pgenlist, genlist_cond_fn_t cond_data_func)
Definition genlist.c:568
void genlist_clear(struct genlist *pgenlist)
Definition genlist.c:250
void genlist_prepend(struct genlist *pgenlist, void *data)
Definition genlist.c:526
bool genlist_remove_if(struct genlist *pgenlist, genlist_cond_fn_t cond_data_func)
Definition genlist.c:379
void genlist_append(struct genlist *pgenlist, void *data)
Definition genlist.c:536
struct genlist * genlist_copy(const struct genlist *pgenlist) fc__warn_unused_result
Definition genlist.c:157
bool(* genlist_comp_fn_t)(const void *, const void *)
Definition genlist.h:54
static struct genlist_link * genlist_head(const struct genlist *pgenlist)
Definition genlist.h:108
static void * genlist_link_data(const struct genlist_link *plink)
Definition genlist.h:140
void genlist_unique_full(struct genlist *pgenlist, genlist_comp_fn_t comp_data_func)
Definition genlist.c:292
struct genlist * genlist_copy_full(const struct genlist *pgenlist, genlist_copy_fn_t copy_data_func, genlist_free_fn_t free_data_func) fc__warn_unused_result
Definition genlist.c:165
void genlist_destroy(struct genlist *pgenlist)
Definition genlist.c:57
int genlist_remove_all(struct genlist *pgenlist, const void *data)
Definition genlist.c:351
void genlist_pop_front(struct genlist *pgenlist)
Definition genlist.c:448
void genlist_insert_before(struct genlist *pgenlist, void *data, struct genlist_link *plink)
Definition genlist.c:514
void * genlist_back(const struct genlist *pgenlist)
Definition genlist.c:240
int genlist_remove_all_if(struct genlist *pgenlist, genlist_cond_fn_t cond_data_func)
Definition genlist.c:404
void genlist_unique(struct genlist *pgenlist)
Definition genlist.c:282
void genlist_insert_after(struct genlist *pgenlist, void *data, struct genlist_link *plink)
Definition genlist.c:502
void *(* genlist_copy_fn_t)(const void *)
Definition genlist.h:52
void genlist_sort(struct genlist *pgenlist, int(*compar)(const void *, const void *))
Definition genlist.c:597
struct genlist * genlist_new_full(genlist_free_fn_t free_data_func) fc__warn_unused_result
Definition genlist.c:39
void genlist_reverse(struct genlist *pgenlist)
Definition genlist.c:659
struct genlist_link * genlist_tail(const struct genlist *pgenlist)
Definition genlist.c:213
struct genlist * genlist_new(void) fc__warn_unused_result
Definition genlist.c:31
void * genlist_front(const struct genlist *pgenlist)
Definition genlist.c:232
struct genlist_link * genlist_link_get(const struct genlist *pgenlist, int idx)
Definition genlist.c:203
void genlist_pop_back(struct genlist *pgenlist)
Definition genlist.c:460
void(* genlist_free_fn_t)(void *)
Definition genlist.h:51
struct genlist_link * genlist_search(const struct genlist *pgenlist, const void *data)
Definition genlist.c:548
static fc__warn_unused_result struct genlist_link * genlist_link_prev(const struct genlist_link *plink)
Definition genlist.h:151
int genlist_size(const struct genlist *pgenlist)
Definition genlist.c:192
void genlist_shuffle(struct genlist *pgenlist)
Definition genlist.c:629
bool genlist_remove(struct genlist *pgenlist, const void *data)
Definition genlist.c:329
static fc__warn_unused_result struct genlist_link * genlist_link_next(const struct genlist_link *plink)
Definition genlist.h:162
struct genlist_link * tail_link
Definition genlist.h:63
struct genlist_link * head_link
Definition genlist.h:62
int nelements
Definition genlist.h:60
fc_mutex mutex
Definition genlist.h:61
genlist_free_fn_t free_data_func
Definition genlist.h:64
#define fc__warn_unused_result
Definition support.h:109
#define bool
Definition support.h:71