1 /* 2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3 * 4 * HDF is dual licensed: you can use it either under the terms of 5 * the GPL, or the BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 9 #ifndef HDF_SINGLE_LIST_H 10 #define HDF_SINGLE_LIST_H 11 12 #include "hdf_base.h" 13 14 #ifdef __cplusplus 15 extern "C" { 16 #endif /* __cplusplus */ 17 18 struct HdfSListNode { 19 struct HdfSListNode *next; // next element in list, or NULL 20 }; 21 22 struct HdfSList { 23 struct HdfSListNode *root; 24 }; 25 26 struct HdfSListIterator { 27 int stepOnNext; 28 struct HdfSListNode *prev; // points to the item before the current one 29 struct HdfSListNode *curr; // points to the current item (to detect item removal) 30 }; 31 32 typedef void (*HdfSListDeleter)(struct HdfSListNode *); 33 34 typedef bool (*HdfSListSearchComparer)(struct HdfSListNode *, uint32_t); 35 36 typedef bool (*HdfSListAddComparer)(struct HdfSListNode *, struct HdfSListNode *); 37 38 /* 39 * @brief Init the list 40 * 41 * @param[in] list the operation list. 42 * 43 * @return None 44 */ 45 void HdfSListInit(struct HdfSList *list); 46 47 /* 48 * @brief search to see whether specific element is attach into the list. 49 * 50 * @param[in] list the operation list. 51 * @param[in] searchKey search key of list node. 52 * @param[in] comparer comparer of list node. 53 * 54 * @return struct HdfSListNode of search result 55 * -# NULL if not found. 56 */ 57 struct HdfSListNode *HdfSListSearch(const struct HdfSList *list, uint32_t keyValue, HdfSListSearchComparer comparer); 58 59 /* 60 * @brief tests if list is empty 61 * 62 * @param[in] list the operation list. 63 * 64 * @return bool result of queue status. 65 */ 66 bool HdfSListIsEmpty(const struct HdfSList *list); 67 68 /* 69 * @brief find the last item in the list 70 * 71 * @param[in] list the operation list. 72 * 73 * @return last link of list. 74 */ 75 struct HdfSListNode *HdfSListGetLast(const struct HdfSList *list); 76 77 /* 78 * @brief add item to the head of the list 79 * 80 * @param[in] list the operation list. 81 * @param[in] link the new element to add. 82 * 83 * @return None 84 */ 85 void HdfSListAdd(struct HdfSList *list, struct HdfSListNode *link); 86 87 /* 88 * @brief add item to list as last element 89 * 90 * @param[in] list the operation list. 91 * @param[in] link the new element to add. 92 * 93 * @return None 94 */ 95 void HdfSListAddTail(struct HdfSList *list, struct HdfSListNode *link); 96 97 /* 98 * @brief add item to list as ordered 99 * 100 * @param[in] list the operation list. 101 * @param[in] link the new element to add. 102 * @param[in] comparer comparer of list node. 103 * 104 * @return bool result of queue status. 105 */ 106 bool HdfSListAddOrder(struct HdfSList *list, struct HdfSListNode *link, HdfSListAddComparer comparer); 107 108 /* 109 * @brief remove item from list 110 * 111 * @param[in] list the operation list. 112 * @param[in] link the element to remove. 113 * 114 */ 115 void HdfSListRemove(struct HdfSList *list, struct HdfSListNode *link); 116 117 /* 118 * @brief flush the list and free memory 119 * 120 * @param[in] list the operation list. 121 * @param[in] deleter the function of free memory. 122 * 123 * @return None 124 */ 125 void HdfSListFlush(struct HdfSList *list, HdfSListDeleter deleter); 126 127 /* 128 * @brief calculate the element count in the list. 129 * 130 * @param[in] list the list to count. 131 * 132 * @return the count of list. 133 */ 134 int HdfSListCount(const struct HdfSList *list); 135 136 /* 137 * @brief get first element 138 * 139 * @param[in] list the operation list. 140 * 141 * @return the first element in the list. 142 */ 143 struct HdfSListNode *HdfSListPeek(const struct HdfSList *list); 144 145 /* 146 * @brief get next element of link; 147 * 148 * @param[in] link the operation link. 149 * 150 * @return the next element of the link pass in 151 */ 152 struct HdfSListNode *HdfSListNext(const struct HdfSListNode *link); 153 154 /* 155 * @brief get and remove first element 156 * 157 * @param[in] list the operation list. 158 * 159 * @return the first element in the list. 160 */ 161 struct HdfSListNode *HdfSListPop(struct HdfSList *list); 162 163 /* 164 * @brief initialize iterator 165 * 166 * @param[in] iterator the point of iterator. 167 * @param[in] list the point of operation list. 168 * 169 * @return None 170 */ 171 void HdfSListIteratorInit(struct HdfSListIterator *iterator, const struct HdfSList *list); 172 173 /* 174 * @brief check whether list has next node. 175 * 176 * @param[in] iterator the point of iterator. 177 * 178 * @return the result of check next. 179 */ 180 bool HdfSListIteratorHasNext(const struct HdfSListIterator *iterator); 181 182 /* 183 * @brief get next link in the list and move iterator to next. 184 * 185 * @param[in] iterator the point of iterator. 186 * 187 * @return point to next element of it. 188 */ 189 struct HdfSListNode *HdfSListIteratorNext(struct HdfSListIterator *iterator); 190 191 /* 192 * @brief remove current node that iterator point to and move iterator to next node 193 * 194 * @param[in] iterator the point of iterator. 195 * 196 * @return None 197 */ 198 void HdfSListIteratorRemove(struct HdfSListIterator *iterator); 199 200 /* 201 * @brief insert new node before the node that iterator point to.and hold current iterator. 202 * 203 * @param[in] iterator the point of iterator. 204 * @param[in] link the point of operation list. 205 * 206 * @return None 207 */ 208 void HdfSListIteratorInsert(struct HdfSListIterator *iterator, struct HdfSListNode *link); 209 210 #define OFFSET_OF(type, mem) ((size_t) &((type *)0)->mem) 211 212 #define HDF_SLIST_CONTAINER_OF(mtype, ptr, type, mem) ( { \ 213 const mtype*__mptr = (ptr); \ 214 (type *)((char *)__mptr - OFFSET_OF(type, mem)); \ 215 }) 216 217 #ifdef __cplusplus 218 } 219 #endif /* __cplusplus */ 220 221 #endif /* HDF_SINGLE_LIST_H */ 222