Guardtime KSI c SDK
Loading...
Searching...
No Matches
list.h
Go to the documentation of this file.
1/*
2 * Copyright 2013-2015 Guardtime, Inc.
3 *
4 * This file is part of the Guardtime client SDK.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License").
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES, CONDITIONS, OR OTHER LICENSES OF ANY KIND, either
13 * express or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
15 * "Guardtime" and "KSI" are trademarks or registered trademarks of
16 * Guardtime, Inc., and no license to trademarks is granted; Guardtime
17 * reserves and retains all trademark rights.
18 */
19
20#ifndef KSI_LIST_H_
21#define KSI_LIST_H_
22
23#include <stddef.h>
24#include "common.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
34
38typedef struct KSI_List_st KSI_List;
39
44#define KSI_LIST(type) type##List
45
51#define KSI_NEW_LIST(type, list) KSI_List_new(type##_free, (list))
52
58#define KSI_NEW_REFLIST(type, list) KSI_RefList_new(type##_free, type##_ref, (list))
59
65#define KSI_LIST_FN_NAME(type, name) type##List_##name
66
67#define KSI_DEFINE_LIST_STRUCT(ltype, rtype) \
68 \
75 int (*append)(ltype *, rtype *); \
76 \
85 int (*removeElement)(ltype *, size_t, rtype **); \
86
92 \
93 int (*indexOf)(ltype *, rtype *, size_t **); \
94
96 \param[in] list Pointer to the list.
97 \param[in] pos Position where to insert the element.
98 \param[in] el Pointer to the element being added.
99 \return status code (#KSI_OK, when operation succeeded, otherwise an error code).
100 \note After add the element to the list, the element belongs to the list
101 and it will be freed if the list is freed.
102 */ \
103 int (*insertAt)(ltype *, size_t, rtype *); \
104 \
113 int (*replaceAt)(ltype *, size_t, rtype *); \
122 int (*elementAt)(ltype *, size_t pos, rtype **); \
127 size_t (*length)(ltype *list); \
128 void (*obj_free)(rtype *); \
129
131 \
133 int (*sort)(ltype *list, int (*cmp)(const rtype **, const rtype **)); \
134 \
140 int (*foldl)(ltype *list, void *foldCtx, int (*fn)(rtype *el, void *foldCtx)); \
141 \
142 void *pImpl;\
143 \
153 int (*find)(ltype *list, rtype *el, int *found, size_t *pos); \
154
159#define KSI_DEFINE_LIST(type) \
160 typedef struct type##_list_st KSI_LIST(type); \
161 struct type##_list_st { \
162 KSI_DEFINE_LIST_STRUCT(KSI_LIST(type), type) \
163 }; \
164 int KSI_LIST_FN_NAME(type, new)(KSI_LIST(type) **list); \
165 void KSI_LIST_FN_NAME(type, free)(KSI_LIST(type) *list); \
166
167
168void KSI_List_free(KSI_List *list);
169int KSI_List_new(void (*obj_free)(void *), KSI_List **list);
170int KSI_List_append(KSI_List *list, void *o);
171int KSI_List_remove(KSI_List *list, size_t pos, void **o);
172int KSI_List_indexOf(KSI_List *list, void *o, size_t **i);
173int KSI_List_insertAt(KSI_List *list, size_t pos, void *o);
174int KSI_List_replaceAt(KSI_List *list, size_t pos, void *o);
175int KSI_List_elementAt(KSI_List *list, size_t pos, void **o);
176size_t KSI_List_length(KSI_List *list);
177int KSI_List_sort(KSI_List *list, int (*)(const void **, const void **));
178int KSI_List_foldl(KSI_List *list, void *foldCtx, int (*fn)(void *el, void *foldCtx));
179int KSI_List_find(KSI_List *list, void *el, int *found, size_t *pos);
180
186#define KSI_IMPLEMENT_LIST(type, free_fn) \
187int KSI_LIST_FN_NAME(type, new)(KSI_LIST(type) **list) { \
188 return KSI_List_new((void (*)(void *))free_fn, (KSI_List **)list); \
189} \
190void KSI_LIST_FN_NAME(type, free)(KSI_LIST(type) *list) { \
191 KSI_List_free((KSI_List *)list); \
192} \
193
197#ifdef __cplusplus
198}
199#endif
200
201#endif /* KSI_LIST_H_ */
struct KSI_List_st KSI_List
Definition list.h:38
int KSI_List_append(KSI_List *list, void *o)
int KSI_List_sort(KSI_List *list, int(*)(const void **, const void **))
int KSI_List_indexOf(KSI_List *list, void *o, size_t **i)
int KSI_List_replaceAt(KSI_List *list, size_t pos, void *o)
int KSI_List_new(void(*obj_free)(void *), KSI_List **list)
int KSI_List_foldl(KSI_List *list, void *foldCtx, int(*fn)(void *el, void *foldCtx))
size_t KSI_List_length(KSI_List *list)
void KSI_List_free(KSI_List *list)
int KSI_List_insertAt(KSI_List *list, size_t pos, void *o)
int KSI_List_remove(KSI_List *list, size_t pos, void **o)
int KSI_List_find(KSI_List *list, void *el, int *found, size_t *pos)
int KSI_List_elementAt(KSI_List *list, size_t pos, void **o)