3106074927847dd8366f3bf1daf07ff05ffc7ef3
[vnfsdk/compliance.git] / veslibrary / ves_clibrary / evel / evel-library / code / evel_library / double_list.c
1
2 /*************************************************************************//**
3  *
4  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5  *
6  * Unless otherwise specified, all software contained herein is
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  *
18  ****************************************************************************/
19
20 /**************************************************************************//**
21  * @file
22  * A simple double-linked list.
23  *
24  * @note  No thread protection so you will need to use appropriate
25  * synchronization if use spans multiple threads.
26  *
27  ****************************************************************************/
28
29 #include <assert.h>
30 #include <malloc.h>
31
32 #include "double_list.h"
33 #include "evel.h"
34
35 /**************************************************************************//**
36  * List initialization.
37  *
38  * Initialize the list supplied to be empty.
39  *
40  * @param   list    Pointer to the list to be initialized.
41
42  * @returns Nothing
43 ******************************************************************************/
44 void dlist_initialize(DLIST * list)
45 {
46   EVEL_ENTER();
47
48   /***************************************************************************/
49   /* Check assumptions.                                                      */
50   /***************************************************************************/
51   assert(list != NULL);
52
53   /***************************************************************************/
54   /* Initialize the list as empty.                                           */
55   /***************************************************************************/
56   list->head = NULL;
57   list->tail = NULL;
58
59   EVEL_EXIT();
60 }
61
62 void * dlist_pop_last(DLIST * list)
63 {
64   void *item = NULL;
65   DLIST_ITEM *current_tail = NULL;
66   DLIST_ITEM *new_tail = NULL;
67
68   assert(list != NULL);
69
70   current_tail = list->tail;
71   if (current_tail != NULL)
72   {
73     item = current_tail->item;
74     new_tail = current_tail->previous;
75     if (new_tail == NULL)
76     {
77       list->head = NULL;
78       list->tail = NULL;
79     }
80     else
81     {
82       new_tail->next = NULL;
83       list->tail = new_tail;
84     }
85     free(current_tail);
86   }
87
88   return item;
89 }
90
91 void dlist_push_first(DLIST * list, void * item)
92 {
93   DLIST_ITEM * new_element = NULL;
94   DLIST_ITEM * current_head = NULL;
95
96   /***************************************************************************/
97   /* Check assumptions.  Note that we do allow putting NULL pointers into    */
98   /* the list - not sure you'd want to, but let it happen.                   */
99   /***************************************************************************/
100   assert(list != NULL);
101
102   current_head = list->head;
103
104   new_element = malloc(sizeof(DLIST_ITEM));
105   assert(new_element != NULL);
106   new_element->next = current_head;
107   new_element->previous = NULL;
108   new_element->item = item;
109   list->head = new_element;
110
111   if (current_head != NULL)
112   {
113     current_head->previous = new_element;
114   }
115   else
116   {
117     list->tail = new_element;
118   }
119 }
120
121 void dlist_push_last(DLIST * list, void * item)
122 {
123   DLIST_ITEM * new_element = NULL;
124   DLIST_ITEM * current_tail = NULL;
125
126   /***************************************************************************/
127   /* Check assumptions.  Note that we do allow putting NULL pointers into    */
128   /* the list - not sure you'd want to, but let it happen.                   */
129   /***************************************************************************/
130   assert(list != NULL);
131
132   current_tail = list->tail;
133
134   new_element = malloc(sizeof(DLIST_ITEM));
135   assert(new_element != NULL);
136   new_element->next = NULL;
137   new_element->previous = current_tail;
138   new_element->item = item;
139   list->tail = new_element;
140
141   if (current_tail != NULL)
142   {
143     current_tail->next = new_element;
144   }
145   else
146   {
147     list->head = new_element;
148   }
149 }
150
151 DLIST_ITEM * dlist_get_first(DLIST * list)
152 {
153   return list->head;
154 }
155
156 DLIST_ITEM * dlist_get_last(DLIST * list)
157 {
158   return list->tail;
159 }
160
161 DLIST_ITEM * dlist_get_next(DLIST_ITEM * item)
162 {
163   return item->next;
164 }
165
166 int dlist_is_empty(DLIST * list)
167 {
168   return (list->head == NULL);
169 }
170
171 int dlist_count(DLIST * list)
172 {
173   int count = 0;
174   DLIST_ITEM * item = list->head;
175
176   while (item != NULL)
177   {
178     count++;
179     item = item->next;
180   }
181
182   return count;
183 }