]>
Commit | Line | Data |
---|---|---|
74971473 JG |
1 | /* |
2 | * This file is part of the PCEPlib, a PCEP protocol library. | |
3 | * | |
4 | * Copyright (C) 2020 Volta Networks https://voltanet.io/ | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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/>. | |
18 | * | |
19 | * Author : Brady Johnson <brady@voltanet.io> | |
20 | * | |
21 | */ | |
22 | ||
23 | ||
24 | #ifndef INCLUDE_PCEPUTILSORDEREDLIST_H_ | |
25 | #define INCLUDE_PCEPUTILSORDEREDLIST_H_ | |
26 | ||
27 | #include <stdbool.h> | |
28 | ||
29 | typedef struct ordered_list_node_ { | |
30 | struct ordered_list_node_ *next_node; | |
31 | void *data; | |
32 | ||
33 | } ordered_list_node; | |
34 | ||
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 | |
37 | * return: | |
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 | |
41 | */ | |
42 | typedef int (*ordered_compare_function)(void *list_entry, void *new_entry); | |
43 | ||
44 | /* Compare function that compares pointers */ | |
45 | int pointer_compare_function(void *list_entry, void *new_entry); | |
46 | ||
47 | typedef struct ordered_list_handle_ { | |
48 | ordered_list_node *head; | |
49 | unsigned int num_entries; | |
50 | ordered_compare_function compare_function; | |
51 | ||
52 | } ordered_list_handle; | |
53 | ||
54 | ordered_list_handle *ordered_list_initialize(ordered_compare_function func_ptr); | |
55 | void ordered_list_destroy(ordered_list_handle *handle); | |
56 | ||
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 | |
59 | * will be returned. | |
60 | */ | |
61 | ordered_list_node *ordered_list_add_node(ordered_list_handle *handle, | |
62 | void *data); | |
63 | ||
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. | |
67 | */ | |
68 | ordered_list_node *ordered_list_find(ordered_list_handle *handle, void *data); | |
69 | ||
70 | /* The same as the previous function, but with a specific orderedComparefunction | |
71 | */ | |
72 | ordered_list_node *ordered_list_find2(ordered_list_handle *handle, void *data, | |
73 | ordered_compare_function compare_func); | |
74 | ||
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. | |
77 | */ | |
78 | void *ordered_list_remove_first_node(ordered_list_handle *handle); | |
79 | ||
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. | |
84 | */ | |
85 | void *ordered_list_remove_first_node_equals(ordered_list_handle *handle, | |
86 | void *data); | |
87 | ||
88 | /* The same as the previous function, but with a specific orderedComparefunction | |
89 | */ | |
90 | void *ordered_list_remove_first_node_equals2(ordered_list_handle *handle, | |
91 | void *data, | |
92 | ordered_compare_function func_ptr); | |
93 | ||
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. | |
97 | */ | |
98 | void *ordered_list_remove_node(ordered_list_handle *handle, | |
99 | ordered_list_node *prev_node, | |
100 | ordered_list_node *node_to_remove); | |
101 | ||
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. | |
105 | */ | |
106 | void *ordered_list_remove_node2(ordered_list_handle *handle, | |
107 | ordered_list_node *node_to_remove); | |
108 | ||
109 | #endif /* INCLUDE_PCEPUTILSORDEREDLIST_H_ */ |