• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef COMMON_LINKED_LIST_H
17 #define COMMON_LINKED_LIST_H
18 
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include "defines.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 typedef void (*DestroyDataFunc)(void *data);
28 typedef bool (*MatchFunc)(void *data, void *condition);
29 
30 typedef struct LinkedListNode {
31     void *data;
32     struct LinkedListNode *next;
33 } LinkedListNode;
34 
35 typedef struct LinkedListIterator {
36     LinkedListNode *current;
37     bool (*hasNext)(struct LinkedListIterator *iterator);
38     void *(*next)(struct LinkedListIterator *iterator);
39 } LinkedListIterator;
40 
41 typedef struct LinkedList {
42     uint32_t size;
43     LinkedListNode *head;
44     DestroyDataFunc destroyDataFunc;
45     uint32_t (*getSize)(struct LinkedList *list);
46     ResultCode (*insert)(struct LinkedList *list, void *data);
47     ResultCode (*remove)(struct LinkedList *list, void *condition, MatchFunc matchFunc, bool destroyNode);
48     LinkedListIterator *(*createIterator)(struct LinkedList *list);
49     void (*destroyIterator)(LinkedListIterator *iterator);
50 } LinkedList;
51 
52 LinkedList *CreateLinkedList(DestroyDataFunc destroyDataFunc);
53 void DestroyLinkedList(LinkedList *list);
54 
55 #ifdef __cplusplus
56 }
57 #endif
58 
59 #endif
60