diff options
author | Tilghman Lesher <tilghman@meg.abyt.es> | 2008-03-26 19:19:31 +0000 |
---|---|---|
committer | Tilghman Lesher <tilghman@meg.abyt.es> | 2008-03-26 19:19:31 +0000 |
commit | e6fc9ae52cceaf321d8ed834d6e191d83212b422 (patch) | |
tree | 055f1fd6797ddb44d8650d54d9e4f0eda5fb5836 /include | |
parent | dd2700d0b12fd4b37615137462b9c805172b1569 (diff) |
Add a linkedlist macro that maintains a sorted list
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@111036 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'include')
-rw-r--r-- | include/asterisk/linkedlists.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/include/asterisk/linkedlists.h b/include/asterisk/linkedlists.h index 1f3285dfa..32d0cbe0b 100644 --- a/include/asterisk/linkedlists.h +++ b/include/asterisk/linkedlists.h @@ -456,6 +456,7 @@ struct { \ \li AST_LIST_INSERT_AFTER() \li AST_LIST_INSERT_HEAD() \li AST_LIST_INSERT_TAIL() + \li AST_LIST_INSERT_SORTALPHA() */ #define AST_LIST_TRAVERSE(head,var,field) \ for((var) = (head)->first; (var); (var) = (var)->field.next) @@ -695,6 +696,38 @@ struct { \ #define AST_RWLIST_INSERT_TAIL AST_LIST_INSERT_TAIL /*! + * \brief Inserts a list entry into a alphabetically sorted list + * \param head Pointer to the list head structure + * \param elm Pointer to the entry to be inserted + * \param field Name of the list entry field (declared using AST_LIST_ENTRY()) + * \param sortfield Name of the field on which the list is sorted + */ +#define AST_LIST_INSERT_SORTALPHA(head, elm, field, sortfield) do { \ + if (!(head)->first) { \ + (head)->first = (elm); \ + (head)->last = (elm); \ + } else { \ + typeof((head)->first) cur = (head)->first, prev = NULL; \ + while (cur && strcmp(cur->sortfield, elm->sortfield) < 0) { \ + prev = cur; \ + cur = cur->field.next; \ + } \ + if (!prev) { /* Same as INSERT_HEAD */ \ + (elm)->field.next = (head)->first; \ + (head)->first = (elm); \ + } else if (!cur) { /* Same as INSERT_TAIL */ \ + (head)->last->field.next = (elm); \ + (head)->last = (elm); \ + } else { \ + (elm)->field.next = cur; \ + (prev)->field.next = (elm); \ + } \ + } \ +} while (0) + +#define AST_RWLIST_INSERT_SORTALPHA AST_LIST_INSERT_SORTALPHA + +/*! \brief Appends a whole list to the tail of a list. \param head This is a pointer to the list head structure \param list This is a pointer to the list to be appended. |