Guardtime KSI c SDK
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 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
37 typedef struct KSI_List_st KSI_List;
38 
43 #define KSI_LIST(type) type##List
44 
50 #define KSI_NEW_LIST(type, list) KSI_List_new(type##_free, (list))
51 
57 #define KSI_NEW_REFLIST(type, list) KSI_RefList_new(type##_free, type##_ref, (list))
58 
64 #define KSI_LIST_FN_NAME(type, name) type##List_##name
65 
66 #define KSI_DEFINE_LIST_STRUCT(ltype, rtype) \
67  \
74  int (*append)(ltype *, rtype *); \
75  \
84  int (*removeElement)(ltype *, size_t, rtype **); \
85  \
91  int (*indexOf)(ltype *, rtype *, size_t **); \
92  \
101  int (*insertAt)(ltype *, size_t, rtype *); \
102  \
111  int (*replaceAt)(ltype *, size_t, rtype *); \
112  \
120  int (*elementAt)(ltype *, size_t pos, rtype **); \
121  \
125  size_t (*length)(ltype *list); \
126  void (*obj_free)(rtype *); \
127  \
131  int (*sort)(ltype *list, int (*cmp)(const rtype **, const rtype **)); \
132  \
138  int (*foldl)(ltype *list, void *foldCtx, int (*fn)(rtype *el, void *foldCtx)); \
139  \
140  void *pImpl; \
141 
142 
146 #define KSI_DEFINE_LIST(type) \
147  typedef struct type##_list_st KSI_LIST(type); \
148  struct type##_list_st { \
149  KSI_DEFINE_LIST_STRUCT(KSI_LIST(type), type) \
150  }; \
151  int KSI_LIST_FN_NAME(type, new)(KSI_LIST(type) **list); \
152  void KSI_LIST_FN_NAME(type, free)(KSI_LIST(type) *list); \
153 
154 
155 void KSI_List_free(KSI_List *list);
156 int KSI_List_new(void (*obj_free)(void *), KSI_List **list);
157 int KSI_List_append(KSI_List *list, void *o);
158 int KSI_List_remove(KSI_List *list, size_t pos, void **o);
159 int KSI_List_indexOf(KSI_List *list, void *o, size_t **i);
160 int KSI_List_insertAt(KSI_List *list, size_t pos, void *o);
161 int KSI_List_replaceAt(KSI_List *list, size_t pos, void *o);
162 int KSI_List_elementAt(KSI_List *list, size_t pos, void **o);
163 size_t KSI_List_length(KSI_List *list);
164 int KSI_List_sort(KSI_List *list, int (*)(const void **, const void **));
165 int KSI_List_foldl(KSI_List *list, void *foldCtx, int (*fn)(void *el, void *foldCtx));
166 
172 #define KSI_IMPLEMENT_LIST(type, free_fn) \
173 int KSI_LIST_FN_NAME(type, new)(KSI_LIST(type) **list) { \
174  return KSI_List_new((void (*)(void *))free_fn, (KSI_List **)list); \
175 } \
176 void KSI_LIST_FN_NAME(type, free)(KSI_LIST(type) *list) { \
177  KSI_List_free((KSI_List *)list); \
178 } \
179 
180 
183 #ifdef __cplusplus
184 }
185 #endif
186 
187 #endif /* KSI_LIST_H_ */
void KSI_List_free(KSI_List *list)
int KSI_List_indexOf(KSI_List *list, void *o, size_t **i)
int KSI_List_foldl(KSI_List *list, void *foldCtx, int(*fn)(void *el, void *foldCtx))
int KSI_List_remove(KSI_List *list, size_t pos, void **o)
int KSI_List_elementAt(KSI_List *list, size_t pos, void **o)
size_t KSI_List_length(KSI_List *list)
int KSI_List_new(void(*obj_free)(void *), KSI_List **list)
int KSI_List_insertAt(KSI_List *list, size_t pos, void *o)
int KSI_List_sort(KSI_List *list, int(*)(const void **, const void **))
int KSI_List_append(KSI_List *list, void *o)
int KSI_List_replaceAt(KSI_List *list, size_t pos, void *o)
struct KSI_List_st KSI_List
Definition: list.h:37