2 * This file is part of the PCEPlib, a PCEP protocol library.
4 * Copyright (C) 2020 Volta Networks https://voltanet.io/
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * Author : Brady Johnson <brady@voltanet.io>
24 #ifndef INCLUDE_PCEPUTILSORDEREDLIST_H_
25 #define INCLUDE_PCEPUTILSORDEREDLIST_H_
29 typedef struct ordered_list_node_
{
30 struct ordered_list_node_
*next_node
;
35 /* The implementation of this function will receive a pointer to the
36 * new data to be inserted and a pointer to the list_entry, and should
38 * < 0 if new_entry < list_entry
39 * == 0 if new_entry == list_entry (new_entry will be inserted after
40 * list_entry) > 0 if new_entry > list_entry
42 typedef int (*ordered_compare_function
)(void *list_entry
, void *new_entry
);
44 /* Compare function that compares pointers */
45 int pointer_compare_function(void *list_entry
, void *new_entry
);
47 typedef struct ordered_list_handle_
{
48 ordered_list_node
*head
;
49 unsigned int num_entries
;
50 ordered_compare_function compare_function
;
52 } ordered_list_handle
;
54 ordered_list_handle
*ordered_list_initialize(ordered_compare_function func_ptr
);
55 void ordered_list_destroy(ordered_list_handle
*handle
);
57 /* Add a new ordered_list_node to the list, using the ordered_compare_function
58 * to determine where in the list to add it. The newly created ordered_list_node
61 ordered_list_node
*ordered_list_add_node(ordered_list_handle
*handle
,
64 /* Find an entry in the ordered_list using the ordered_compare_function to
65 * compare the data passed in.
66 * Return the node if found, NULL otherwise.
68 ordered_list_node
*ordered_list_find(ordered_list_handle
*handle
, void *data
);
70 /* The same as the previous function, but with a specific orderedComparefunction
72 ordered_list_node
*ordered_list_find2(ordered_list_handle
*handle
, void *data
,
73 ordered_compare_function compare_func
);
75 /* Remove the first entry in the list and return the data it points to.
76 * Will return NULL if the handle is NULL or if the list is empty.
78 void *ordered_list_remove_first_node(ordered_list_handle
*handle
);
80 /* Remove the first entry in the list that has the same data, using the
81 * ordered_compare_function, and return the data it points to.
82 * Will return NULL if the handle is NULL or if the list is empty or
83 * if no entry is found that equals data.
85 void *ordered_list_remove_first_node_equals(ordered_list_handle
*handle
,
88 /* The same as the previous function, but with a specific orderedComparefunction
90 void *ordered_list_remove_first_node_equals2(ordered_list_handle
*handle
,
92 ordered_compare_function func_ptr
);
94 /* Remove the node "node_to_remove" and adjust the "prev_node" pointers
95 * accordingly, returning the data pointed to by "node_to_remove". Will return
96 * NULL if the handle is NULL or if the list is empty.
98 void *ordered_list_remove_node(ordered_list_handle
*handle
,
99 ordered_list_node
*prev_node
,
100 ordered_list_node
*node_to_remove
);
102 /* Remove the node "node_to_remove" by searching for it in the entire list,
103 * returning the data pointed to by "node_to_remove".
104 * Will return NULL if the handle is NULL or if the list is empty.
106 void *ordered_list_remove_node2(ordered_list_handle
*handle
,
107 ordered_list_node
*node_to_remove
);
109 #endif /* INCLUDE_PCEPUTILSORDEREDLIST_H_ */