| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2003-2010, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: blst_slist.h $ |
|---|
| 11 | * $brcm_Revision: Hydra_Software_Devel/11 $ |
|---|
| 12 | * $brcm_Date: 2/5/10 3:23p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /magnum/commonutils/lst/blst_slist.h $ |
|---|
| 19 | * |
|---|
| 20 | * Hydra_Software_Devel/11 2/5/10 3:23p erickson |
|---|
| 21 | * SWDEPRECATED-2425: improve comments |
|---|
| 22 | * |
|---|
| 23 | * Hydra_Software_Devel/10 7/21/09 1:42p vsilyaev |
|---|
| 24 | * PR 56629: Uses the name prefix for local variables to prevent name |
|---|
| 25 | * clash |
|---|
| 26 | * |
|---|
| 27 | * Hydra_Software_Devel/9 11/6/08 4:48p erickson |
|---|
| 28 | * PR47030: BLST_S_DICT_REMOVE should not set the elm out param until the |
|---|
| 29 | * end of the function to avoid side effects on other fields |
|---|
| 30 | * |
|---|
| 31 | * Hydra_Software_Devel/8 12/5/07 3:09p vsilyaev |
|---|
| 32 | * PR 37934: Implemented BLST_S_DICT |
|---|
| 33 | * |
|---|
| 34 | * Hydra_Software_Devel/7 7/18/07 10:34a vsilyaev |
|---|
| 35 | * PR 32813: Added BLST_S_REMOVE_NEXT macro |
|---|
| 36 | * |
|---|
| 37 | * Hydra_Software_Devel/6 12/11/03 4:38p vsilyaev |
|---|
| 38 | * PR 8962: Remove semicolons after while(0) |
|---|
| 39 | * |
|---|
| 40 | * Hydra_Software_Devel/5 8/28/03 12:54p vsilyaev |
|---|
| 41 | * Fixed bug in BLST_S_REMOVE |
|---|
| 42 | * |
|---|
| 43 | * Hydra_Software_Devel/4 6/23/03 10:38a jasonh |
|---|
| 44 | * Fixed argument names for documentation. |
|---|
| 45 | * |
|---|
| 46 | * Hydra_Software_Devel/3 3/17/03 2:15p vsilyaev |
|---|
| 47 | * Fixed INSERT_AFTER. |
|---|
| 48 | * |
|---|
| 49 | * Hydra_Software_Devel/2 3/14/03 7:52p vsilyaev |
|---|
| 50 | * Added function description. |
|---|
| 51 | * |
|---|
| 52 | * Hydra_Software_Devel/1 3/10/03 9:45a vsilyaev |
|---|
| 53 | * singly-linked list |
|---|
| 54 | * |
|---|
| 55 | ***************************************************************************/ |
|---|
| 56 | |
|---|
| 57 | #ifndef BUTIL_SLIST_H |
|---|
| 58 | #define BUTIL_SLIST_H |
|---|
| 59 | |
|---|
| 60 | /*================== Module Overview ===================================== |
|---|
| 61 | This modules defines macros to control singly-linked list. |
|---|
| 62 | All operations are typesafe (doesn't required typecasting) and constant time. |
|---|
| 63 | |
|---|
| 64 | This list allow: |
|---|
| 65 | o Insert a new entry at the head of the list |
|---|
| 66 | o Insert a new entry after any element in the list |
|---|
| 67 | o O(1) removal of an entry from the list head |
|---|
| 68 | o O(n) removal of any entry from the list |
|---|
| 69 | o Forward traversal through the list |
|---|
| 70 | o Each element requires one pointers |
|---|
| 71 | o Code size and execution time is the smallest and fastest compared to other lists |
|---|
| 72 | |
|---|
| 73 | ========================================================================*/ |
|---|
| 74 | |
|---|
| 75 | /*************************************************************************** |
|---|
| 76 | Summary: |
|---|
| 77 | Creates new data type for the list head |
|---|
| 78 | |
|---|
| 79 | Description: |
|---|
| 80 | Creates new data type for the list head, this type used to create variable for the lists head. |
|---|
| 81 | User should create new the list head data type for every different element datatype. |
|---|
| 82 | |
|---|
| 83 | Input: |
|---|
| 84 | name - name for the new data type (structure) |
|---|
| 85 | type - existing user data type used for element of the list |
|---|
| 86 | |
|---|
| 87 | Example: |
|---|
| 88 | BLST_S_HEAD(block_head, block); |
|---|
| 89 | struct block_head head; |
|---|
| 90 | |
|---|
| 91 | Returns: |
|---|
| 92 | <none> |
|---|
| 93 | ****************************************************************************/ |
|---|
| 94 | #define BLST_S_HEAD(name, type) struct name { struct type *sl_first;} |
|---|
| 95 | |
|---|
| 96 | /*************************************************************************** |
|---|
| 97 | Summary: |
|---|
| 98 | Defines links entry |
|---|
| 99 | |
|---|
| 100 | Description: |
|---|
| 101 | Defines entrys for the list inside the user structure.for the element. |
|---|
| 102 | |
|---|
| 103 | Input: |
|---|
| 104 | type - the datatype for element |
|---|
| 105 | |
|---|
| 106 | Example: |
|---|
| 107 | struct block { |
|---|
| 108 | BLST_S_ENTRY(block) link; |
|---|
| 109 | char string[256]; |
|---|
| 110 | }; |
|---|
| 111 | |
|---|
| 112 | Returns: |
|---|
| 113 | <none> |
|---|
| 114 | ****************************************************************************/ |
|---|
| 115 | #define BLST_S_ENTRY(type) struct { struct type *sl_next; } |
|---|
| 116 | |
|---|
| 117 | /*************************************************************************** |
|---|
| 118 | Summary: |
|---|
| 119 | Initializes lists head |
|---|
| 120 | |
|---|
| 121 | Description: |
|---|
| 122 | Initializes the head of the list. The head shall be initialized before list can be used. |
|---|
| 123 | This macro used for dynamic initialization. |
|---|
| 124 | |
|---|
| 125 | Input: |
|---|
| 126 | phead - pointer to the list head |
|---|
| 127 | |
|---|
| 128 | See also: |
|---|
| 129 | BLST_S_INITIALIZER |
|---|
| 130 | |
|---|
| 131 | Example: |
|---|
| 132 | BLST_S_INIT(&head); |
|---|
| 133 | |
|---|
| 134 | Returns: |
|---|
| 135 | <none> |
|---|
| 136 | ****************************************************************************/ |
|---|
| 137 | #define BLST_S_INIT(phead) (phead)->sl_first = NULL |
|---|
| 138 | |
|---|
| 139 | /*************************************************************************** |
|---|
| 140 | Summary: |
|---|
| 141 | Initializes lists head |
|---|
| 142 | |
|---|
| 143 | Description: |
|---|
| 144 | Initializes the head of the list. The head shall be initialized before list can be used. |
|---|
| 145 | This macro used for static initialization. |
|---|
| 146 | |
|---|
| 147 | Input: |
|---|
| 148 | head - pointer to the list head |
|---|
| 149 | |
|---|
| 150 | See also: |
|---|
| 151 | BLST_S_INIT |
|---|
| 152 | |
|---|
| 153 | Example: |
|---|
| 154 | static struct block_head head = BLST_S_INITIALIZER(head); |
|---|
| 155 | |
|---|
| 156 | Returns: |
|---|
| 157 | <none> |
|---|
| 158 | ****************************************************************************/ |
|---|
| 159 | #define BLST_S_INITIALIZER(head) { NULL } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | /*************************************************************************** |
|---|
| 163 | Summary: |
|---|
| 164 | Tests if list is empty |
|---|
| 165 | |
|---|
| 166 | Description: |
|---|
| 167 | Tests if list is empty. |
|---|
| 168 | |
|---|
| 169 | Input: |
|---|
| 170 | phead - pointer to the list head |
|---|
| 171 | |
|---|
| 172 | Returns: |
|---|
| 173 | true - list empty |
|---|
| 174 | false - list has elements |
|---|
| 175 | |
|---|
| 176 | Example: |
|---|
| 177 | if (BLST_S_EMPTY(&head) { return ; } |
|---|
| 178 | |
|---|
| 179 | ****************************************************************************/ |
|---|
| 180 | #define BLST_S_EMPTY(phead) ((phead)->sl_first==NULL) |
|---|
| 181 | |
|---|
| 182 | /*************************************************************************** |
|---|
| 183 | Summary: |
|---|
| 184 | Returns the lists first element |
|---|
| 185 | |
|---|
| 186 | Description: |
|---|
| 187 | Returns pointer to the first element from the list |
|---|
| 188 | |
|---|
| 189 | Input: |
|---|
| 190 | phead - pointer to the list head |
|---|
| 191 | |
|---|
| 192 | Returns: |
|---|
| 193 | pointer to the first element from the list. |
|---|
| 194 | |
|---|
| 195 | Example: |
|---|
| 196 | struct block *first=BLST_S_FIRST(&head); |
|---|
| 197 | ****************************************************************************/ |
|---|
| 198 | #define BLST_S_FIRST(phead) (phead)->sl_first |
|---|
| 199 | |
|---|
| 200 | /*************************************************************************** |
|---|
| 201 | Summary: |
|---|
| 202 | Returns next element from the lists |
|---|
| 203 | |
|---|
| 204 | Description: |
|---|
| 205 | Returns pointer to the next element from the list |
|---|
| 206 | |
|---|
| 207 | Input: |
|---|
| 208 | elm - pointer to the list element |
|---|
| 209 | field - name of the elements link field |
|---|
| 210 | |
|---|
| 211 | Returns: |
|---|
| 212 | pointer to the next element from the list |
|---|
| 213 | |
|---|
| 214 | Example: |
|---|
| 215 | struct block *second=BLST_S_NEXT(first, link); |
|---|
| 216 | ****************************************************************************/ |
|---|
| 217 | #define BLST_S_NEXT(pitem, field) (pitem->field.sl_next) |
|---|
| 218 | |
|---|
| 219 | /*************************************************************************** |
|---|
| 220 | Summary: |
|---|
| 221 | Inserts element into the list |
|---|
| 222 | |
|---|
| 223 | Description: |
|---|
| 224 | Inserts new element into the head of the list. |
|---|
| 225 | |
|---|
| 226 | Input: |
|---|
| 227 | phead - pointer to the list head |
|---|
| 228 | pitem - pointer to the new element |
|---|
| 229 | field - name of the elements link field |
|---|
| 230 | |
|---|
| 231 | Returns: |
|---|
| 232 | <none> |
|---|
| 233 | |
|---|
| 234 | Example: |
|---|
| 235 | BLST_S_INSERT_HEAD(&head, new_block, link); |
|---|
| 236 | ****************************************************************************/ |
|---|
| 237 | #define BLST_S_INSERT_HEAD(phead, pitem, field) do { \ |
|---|
| 238 | (pitem)->field.sl_next = (phead)->sl_first; \ |
|---|
| 239 | (phead)->sl_first = (pitem); \ |
|---|
| 240 | } while(0) |
|---|
| 241 | |
|---|
| 242 | /*************************************************************************** |
|---|
| 243 | Summary: |
|---|
| 244 | Inserts element into the list |
|---|
| 245 | |
|---|
| 246 | Description: |
|---|
| 247 | Inserts new element after existing element. |
|---|
| 248 | |
|---|
| 249 | Input: |
|---|
| 250 | head - pointer to the list head |
|---|
| 251 | elm - pointer to the element from the list |
|---|
| 252 | new_elm - pointer to the new element |
|---|
| 253 | field - name of the elements link field |
|---|
| 254 | |
|---|
| 255 | Returns: |
|---|
| 256 | <none> |
|---|
| 257 | |
|---|
| 258 | Example: |
|---|
| 259 | BLST_S_INSERT_AFTER(&head, first, second, link); |
|---|
| 260 | ****************************************************************************/ |
|---|
| 261 | #define BLST_S_INSERT_AFTER(head, elm, new_elm, field) do { \ |
|---|
| 262 | (new_elm)->field.sl_next = (elm)->field.sl_next; \ |
|---|
| 263 | (elm)->field.sl_next = new_elm; \ |
|---|
| 264 | } while(0) |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | /*************************************************************************** |
|---|
| 268 | Summary: |
|---|
| 269 | Removes element from the list |
|---|
| 270 | |
|---|
| 271 | Description: |
|---|
| 272 | Removes element from the head of the list. |
|---|
| 273 | |
|---|
| 274 | Input: |
|---|
| 275 | head - pointer to the list head |
|---|
| 276 | field - name of the elements link field |
|---|
| 277 | |
|---|
| 278 | See also: |
|---|
| 279 | BLST_S_REMOVE |
|---|
| 280 | |
|---|
| 281 | Returns: |
|---|
| 282 | <none> |
|---|
| 283 | |
|---|
| 284 | Example: |
|---|
| 285 | BLST_S_REMOVE_HEAD(&head, link); |
|---|
| 286 | ****************************************************************************/ |
|---|
| 287 | #define BLST_S_REMOVE_HEAD(phead, field) (phead)->sl_first = (phead)->sl_first->field.sl_next |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | /*************************************************************************** |
|---|
| 291 | Summary: |
|---|
| 292 | Removes element from the list |
|---|
| 293 | |
|---|
| 294 | Description: |
|---|
| 295 | Removes element from the of the list. This implementation is O(n), |
|---|
| 296 | where n it's position of the element in the list |
|---|
| 297 | |
|---|
| 298 | Input: |
|---|
| 299 | head - pointer to the list head |
|---|
| 300 | elm - pointer to the list element |
|---|
| 301 | type - datatype for an element of the list |
|---|
| 302 | field - name of the elements link field |
|---|
| 303 | |
|---|
| 304 | See also: |
|---|
| 305 | BLST_S_REMOVE_HEAD |
|---|
| 306 | |
|---|
| 307 | Returns: |
|---|
| 308 | <none> |
|---|
| 309 | |
|---|
| 310 | Example: |
|---|
| 311 | BLST_S_REMOVE(&head, first, block, link); |
|---|
| 312 | ****************************************************************************/ |
|---|
| 313 | #define BLST_S_REMOVE(phead, elm, type, field) do { \ |
|---|
| 314 | if ((phead)->sl_first == (elm)) { \ |
|---|
| 315 | BLST_S_REMOVE_HEAD(phead, field);\ |
|---|
| 316 | } else { \ |
|---|
| 317 | struct type *blst_s_remove_cur; \ |
|---|
| 318 | \ |
|---|
| 319 | for (blst_s_remove_cur=(phead)->sl_first; blst_s_remove_cur->field.sl_next != (elm); blst_s_remove_cur = blst_s_remove_cur->field.sl_next) {} \ |
|---|
| 320 | blst_s_remove_cur->field.sl_next=blst_s_remove_cur->field.sl_next->field.sl_next; \ |
|---|
| 321 | } \ |
|---|
| 322 | } while(0) |
|---|
| 323 | |
|---|
| 324 | /*************************************************************************** |
|---|
| 325 | Summary: |
|---|
| 326 | Removes element from the list |
|---|
| 327 | |
|---|
| 328 | Description: |
|---|
| 329 | Removes next element from the list. |
|---|
| 330 | |
|---|
| 331 | Input: |
|---|
| 332 | head - pointer to the list head |
|---|
| 333 | elm - pointer to the element from the list |
|---|
| 334 | field - name of the elements link field |
|---|
| 335 | |
|---|
| 336 | See also: |
|---|
| 337 | BLST_S_REMOVE |
|---|
| 338 | |
|---|
| 339 | Returns: |
|---|
| 340 | <none> |
|---|
| 341 | |
|---|
| 342 | Example: |
|---|
| 343 | BLST_S_REMOVE_NEXT(elm, link); |
|---|
| 344 | ****************************************************************************/ |
|---|
| 345 | #define BLST_S_REMOVE_NEXT(head, elm, field) \ |
|---|
| 346 | elm->field.sl_next=elm->field.sl_next->field.sl_next; |
|---|
| 347 | |
|---|
| 348 | /*************************************************************************** |
|---|
| 349 | Summary: |
|---|
| 350 | Adds new entry to the dictionary |
|---|
| 351 | |
|---|
| 352 | Description: |
|---|
| 353 | Adds new element to the dictionary implemented as sordted single linked list |
|---|
| 354 | |
|---|
| 355 | Input: |
|---|
| 356 | head - pointer to the list head |
|---|
| 357 | elm - pointer to the element from the list |
|---|
| 358 | type - datatype for an element of the list |
|---|
| 359 | key - name of the key element |
|---|
| 360 | link - name of the elements link field |
|---|
| 361 | duplicate - label that would be used (gotoed) if duplicate element is found |
|---|
| 362 | |
|---|
| 363 | See also: |
|---|
| 364 | BLST_S_DICT_FIND |
|---|
| 365 | BLST_S_DICT_REMOVE |
|---|
| 366 | |
|---|
| 367 | Returns: |
|---|
| 368 | <none> |
|---|
| 369 | |
|---|
| 370 | Example: |
|---|
| 371 | BLST_S_DICT_ADD(head, elm, block, key, link, err_duplicate); |
|---|
| 372 | ****************************************************************************/ |
|---|
| 373 | #define BLST_S_DICT_ADD(head, elm, type, key, link, duplicate) do { \ |
|---|
| 374 | struct type *blst_s_dict_add_i, *blst_s_dict_add_prev; \ |
|---|
| 375 | for(blst_s_dict_add_prev=NULL,blst_s_dict_add_i=BLST_S_FIRST((head));blst_s_dict_add_i!=NULL;blst_s_dict_add_prev=blst_s_dict_add_i,blst_s_dict_add_i=BLST_S_NEXT(blst_s_dict_add_i, link)) { \ |
|---|
| 376 | if((elm)->key > blst_s_dict_add_i->key) { break;} \ |
|---|
| 377 | if((elm)->key == blst_s_dict_add_i->key) { goto duplicate; } \ |
|---|
| 378 | } \ |
|---|
| 379 | if(blst_s_dict_add_prev) { BLST_S_INSERT_AFTER((head), blst_s_dict_add_prev, (elm), link);} \ |
|---|
| 380 | else { BLST_S_INSERT_HEAD(head, (elm), link);}\ |
|---|
| 381 | } while(0) |
|---|
| 382 | |
|---|
| 383 | /*************************************************************************** |
|---|
| 384 | Summary: |
|---|
| 385 | Finds element in the dictionary |
|---|
| 386 | |
|---|
| 387 | Description: |
|---|
| 388 | Finds element in the dictinary with matching key |
|---|
| 389 | |
|---|
| 390 | Input: |
|---|
| 391 | head - pointer to the list head |
|---|
| 392 | elm - pointer to the element from the list |
|---|
| 393 | key - value of key to find |
|---|
| 394 | field - name of the key element |
|---|
| 395 | link - name of the elements link field |
|---|
| 396 | |
|---|
| 397 | See also: |
|---|
| 398 | BLST_S_DICT_ADD |
|---|
| 399 | BLST_S_DICT_REMOVE |
|---|
| 400 | |
|---|
| 401 | Returns: |
|---|
| 402 | elm = NULL, if element not found in the list |
|---|
| 403 | elm - pointer to the found element |
|---|
| 404 | |
|---|
| 405 | Example: |
|---|
| 406 | BLST_S_DICT_FIND(head, elm, 123, key, link); |
|---|
| 407 | ****************************************************************************/ |
|---|
| 408 | #define BLST_S_DICT_FIND(head, elm, key, field, link) do { \ |
|---|
| 409 | for((elm) = BLST_S_FIRST(head); (elm)!=NULL ; (elm) = BLST_S_NEXT((elm), link)) { \ |
|---|
| 410 | if(key == (elm)->field ) { break; } \ |
|---|
| 411 | else if( key > (elm)->field ) { (elm) = NULL;break; } } } while(0) |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | /*************************************************************************** |
|---|
| 415 | Summary: |
|---|
| 416 | Removed element in the dictionary |
|---|
| 417 | |
|---|
| 418 | Description: |
|---|
| 419 | Removes element in the dictinary with matching key |
|---|
| 420 | |
|---|
| 421 | Input: |
|---|
| 422 | head - pointer to the list head |
|---|
| 423 | elm - pointer to the element from the list |
|---|
| 424 | type - datatype for an element of the list |
|---|
| 425 | key - value of key to find |
|---|
| 426 | field - name of the key element |
|---|
| 427 | link - name of the elements link field |
|---|
| 428 | |
|---|
| 429 | See also: |
|---|
| 430 | BLST_S_DICT_ADD |
|---|
| 431 | BLST_S_DICT_FIND |
|---|
| 432 | |
|---|
| 433 | Returns: |
|---|
| 434 | elm = NULL, if element not found in the list |
|---|
| 435 | elm - pointer to the found element |
|---|
| 436 | |
|---|
| 437 | Example: |
|---|
| 438 | BLST_S_DICT_REMOVE(head, elm, 123, block, key, link); |
|---|
| 439 | ****************************************************************************/ |
|---|
| 440 | #define BLST_S_DICT_REMOVE(head, elm_, key, type, field, link) do { \ |
|---|
| 441 | struct type *blst_s_dict_remove_prev, *blst_s_dict_remove_elm; \ |
|---|
| 442 | for(blst_s_dict_remove_prev=NULL,(blst_s_dict_remove_elm)=BLST_S_FIRST((head));(blst_s_dict_remove_elm)!=NULL;blst_s_dict_remove_prev=blst_s_dict_remove_elm, blst_s_dict_remove_elm=BLST_S_NEXT((blst_s_dict_remove_elm), link)) { \ |
|---|
| 443 | if( (key) == (blst_s_dict_remove_elm)->field ) { \ |
|---|
| 444 | if(blst_s_dict_remove_prev) { BLST_S_REMOVE_NEXT((head), blst_s_dict_remove_prev, link); } \ |
|---|
| 445 | else { BLST_S_REMOVE_HEAD((head), link);} break; \ |
|---|
| 446 | } else if( key > blst_s_dict_remove_elm->field ) { blst_s_dict_remove_elm = NULL;break; } \ |
|---|
| 447 | } (elm_)=blst_s_dict_remove_elm;} while(0) |
|---|
| 448 | |
|---|
| 449 | |
|---|
| 450 | |
|---|
| 451 | |
|---|
| 452 | #endif /* BUTIL_SLIST_H */ |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | |
|---|