1 /*
2  * Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
3  * License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may
6  * not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 #ifndef _LIST_H
21 #define _LIST_H
22 
23 #include <stdlib.h>
24 
25 /*
26  * list_t iterator direction.
27  */
28 typedef enum {
29     LIST_HEAD,
30     LIST_TAIL
31 } list_direction_t;
32 
33 /*
34  * list_t node struct.
35  */
36 typedef struct list_node {
37     struct list_node *prev;
38     struct list_node *next;
39     void *val;
40 } list_node_t;
41 
42 /*
43  * list_t struct.
44  */
45 typedef struct {
46     list_node_t *head;
47     list_node_t *tail;
48     unsigned int len;
49     void (*free)(void *val);
50     int (*match)(void *a, void *b);
51 } list_t;
52 
53 /*
54  * list_t iterator struct.
55  */
56 typedef struct {
57     list_node_t *next;
58     list_direction_t direction;
59 } list_iterator_t;
60 
61 /* Node prototypes. */
62 list_node_t *list_node_new_du(void *val);
63 
64 /* list_t prototypes. */
65 list_t *list_new_du(void);
66 
67 list_node_t *list_rpush_du(list_t *self, list_node_t *node);
68 
69 list_node_t *list_lpush_du(list_t *self, list_node_t *node);
70 
71 list_node_t *list_find_du(list_t *self, void *val);
72 
73 list_node_t *list_at_du(list_t *self, int index);
74 
75 list_node_t *list_rpop_du(list_t *self);
76 
77 list_node_t *list_lpop_du(list_t *self);
78 
79 void list_remove_du(list_t *self, list_node_t *node);
80 
81 void list_destroy_du(list_t *self);
82 
83 /* list_t iterator prototypes. */
84 list_iterator_t *list_iterator_new_du(list_t *list, list_direction_t direction);
85 
86 list_iterator_t *list_iterator_new_from_node_du(list_node_t *node, list_direction_t direction);
87 
88 list_node_t *list_iterator_next_du(list_iterator_t *self);
89 
90 void list_iterator_destroy_du(list_iterator_t *self);
91 
92 #endif
93 
94