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 #include "common.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
38 typedef 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  \
93  int (*indexOf)(ltype *, rtype *, size_t **); \
94  \
103  int (*insertAt)(ltype *, size_t, rtype *); \
104  \
113  int (*replaceAt)(ltype *, size_t, rtype *); \
114  \
122  int (*elementAt)(ltype *, size_t pos, rtype **); \
123  \
127  size_t (*length)(ltype *list); \
128  void (*obj_free)(rtype *); \
129  \
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 
155 
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 
168 void KSI_List_free(KSI_List *list);
169 int KSI_List_new(void (*obj_free)(void *), KSI_List **list);
170 int KSI_List_append(KSI_List *list, void *o);
171 int KSI_List_remove(KSI_List *list, size_t pos, void **o);
172 int KSI_List_indexOf(KSI_List *list, void *o, size_t **i);
173 int KSI_List_insertAt(KSI_List *list, size_t pos, void *o);
174 int KSI_List_replaceAt(KSI_List *list, size_t pos, void *o);
175 int KSI_List_elementAt(KSI_List *list, size_t pos, void **o);
176 size_t KSI_List_length(KSI_List *list);
177 int KSI_List_sort(KSI_List *list, int (*)(const void **, const void **));
178 int KSI_List_foldl(KSI_List *list, void *foldCtx, int (*fn)(void *el, void *foldCtx));
179 int KSI_List_find(KSI_List *list, void *el, int *found, size_t *pos);
180 
186 #define KSI_IMPLEMENT_LIST(type, free_fn) \
187 int KSI_LIST_FN_NAME(type, new)(KSI_LIST(type) **list) { \
188  return KSI_List_new((void (*)(void *))free_fn, (KSI_List **)list); \
189 } \
190 void KSI_LIST_FN_NAME(type, free)(KSI_LIST(type) *list) { \
191  KSI_List_free((KSI_List *)list); \
192 } \
193 
194 
197 #ifdef __cplusplus
198 }
199 #endif
200 
201 #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)
int KSI_List_find(KSI_List *list, void *el, int *found, size_t *pos)
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:38