Azure IoT common library
Fork of azure_c_shared_utility by
singlylinkedlist.c@13:920e00014ee3, 2016-10-20 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Thu Oct 20 17:08:18 2016 -0700
- Revision:
- 13:920e00014ee3
- Child:
- 19:2e0811512ceb
1.0.10
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 13:920e00014ee3 | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 13:920e00014ee3 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 13:920e00014ee3 | 3 | |
AzureIoTClient | 13:920e00014ee3 | 4 | #include <stdlib.h> |
AzureIoTClient | 13:920e00014ee3 | 5 | #ifdef _CRTDBG_MAP_ALLOC |
AzureIoTClient | 13:920e00014ee3 | 6 | #include <crtdbg.h> |
AzureIoTClient | 13:920e00014ee3 | 7 | #endif |
AzureIoTClient | 13:920e00014ee3 | 8 | #include <stdbool.h> |
AzureIoTClient | 13:920e00014ee3 | 9 | |
AzureIoTClient | 13:920e00014ee3 | 10 | #include "azure_c_shared_utility/gballoc.h" |
AzureIoTClient | 13:920e00014ee3 | 11 | #include "azure_c_shared_utility/singlylinkedlist.h" |
AzureIoTClient | 13:920e00014ee3 | 12 | |
AzureIoTClient | 13:920e00014ee3 | 13 | typedef struct LIST_ITEM_INSTANCE_TAG |
AzureIoTClient | 13:920e00014ee3 | 14 | { |
AzureIoTClient | 13:920e00014ee3 | 15 | const void* item; |
AzureIoTClient | 13:920e00014ee3 | 16 | void* next; |
AzureIoTClient | 13:920e00014ee3 | 17 | } LIST_ITEM_INSTANCE; |
AzureIoTClient | 13:920e00014ee3 | 18 | |
AzureIoTClient | 13:920e00014ee3 | 19 | typedef struct SINGLYLINKEDLIST_INSTANCE_TAG |
AzureIoTClient | 13:920e00014ee3 | 20 | { |
AzureIoTClient | 13:920e00014ee3 | 21 | LIST_ITEM_INSTANCE* head; |
AzureIoTClient | 13:920e00014ee3 | 22 | } LIST_INSTANCE; |
AzureIoTClient | 13:920e00014ee3 | 23 | |
AzureIoTClient | 13:920e00014ee3 | 24 | SINGLYLINKEDLIST_HANDLE singlylinkedlist_create(void) |
AzureIoTClient | 13:920e00014ee3 | 25 | { |
AzureIoTClient | 13:920e00014ee3 | 26 | LIST_INSTANCE* result; |
AzureIoTClient | 13:920e00014ee3 | 27 | |
AzureIoTClient | 13:920e00014ee3 | 28 | /* Codes_SRS_LIST_01_001: [singlylinkedlist_create shall create a new list and return a non-NULL handle on success.] */ |
AzureIoTClient | 13:920e00014ee3 | 29 | result = (LIST_INSTANCE*)malloc(sizeof(LIST_INSTANCE)); |
AzureIoTClient | 13:920e00014ee3 | 30 | if (result != NULL) |
AzureIoTClient | 13:920e00014ee3 | 31 | { |
AzureIoTClient | 13:920e00014ee3 | 32 | /* Codes_SRS_LIST_01_002: [If any error occurs during the list creation, singlylinkedlist_create shall return NULL.] */ |
AzureIoTClient | 13:920e00014ee3 | 33 | result->head = NULL; |
AzureIoTClient | 13:920e00014ee3 | 34 | } |
AzureIoTClient | 13:920e00014ee3 | 35 | |
AzureIoTClient | 13:920e00014ee3 | 36 | return result; |
AzureIoTClient | 13:920e00014ee3 | 37 | } |
AzureIoTClient | 13:920e00014ee3 | 38 | |
AzureIoTClient | 13:920e00014ee3 | 39 | void singlylinkedlist_destroy(SINGLYLINKEDLIST_HANDLE list) |
AzureIoTClient | 13:920e00014ee3 | 40 | { |
AzureIoTClient | 13:920e00014ee3 | 41 | /* Codes_SRS_LIST_01_004: [If the list argument is NULL, no freeing of resources shall occur.] */ |
AzureIoTClient | 13:920e00014ee3 | 42 | if (list != NULL) |
AzureIoTClient | 13:920e00014ee3 | 43 | { |
AzureIoTClient | 13:920e00014ee3 | 44 | LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list; |
AzureIoTClient | 13:920e00014ee3 | 45 | |
AzureIoTClient | 13:920e00014ee3 | 46 | while (list_instance->head != NULL) |
AzureIoTClient | 13:920e00014ee3 | 47 | { |
AzureIoTClient | 13:920e00014ee3 | 48 | LIST_ITEM_INSTANCE* current_item = list_instance->head; |
AzureIoTClient | 13:920e00014ee3 | 49 | list_instance->head = (LIST_ITEM_INSTANCE*)current_item->next; |
AzureIoTClient | 13:920e00014ee3 | 50 | free(current_item); |
AzureIoTClient | 13:920e00014ee3 | 51 | } |
AzureIoTClient | 13:920e00014ee3 | 52 | |
AzureIoTClient | 13:920e00014ee3 | 53 | /* Codes_SRS_LIST_01_003: [singlylinkedlist_destroy shall free all resources associated with the list identified by the handle argument.] */ |
AzureIoTClient | 13:920e00014ee3 | 54 | free(list_instance); |
AzureIoTClient | 13:920e00014ee3 | 55 | } |
AzureIoTClient | 13:920e00014ee3 | 56 | } |
AzureIoTClient | 13:920e00014ee3 | 57 | |
AzureIoTClient | 13:920e00014ee3 | 58 | LIST_ITEM_HANDLE singlylinkedlist_add(SINGLYLINKEDLIST_HANDLE list, const void* item) |
AzureIoTClient | 13:920e00014ee3 | 59 | { |
AzureIoTClient | 13:920e00014ee3 | 60 | LIST_ITEM_INSTANCE* result; |
AzureIoTClient | 13:920e00014ee3 | 61 | |
AzureIoTClient | 13:920e00014ee3 | 62 | /* Codes_SRS_LIST_01_006: [If any of the arguments is NULL, singlylinkedlist_add shall not add the item to the list and return NULL.] */ |
AzureIoTClient | 13:920e00014ee3 | 63 | if ((list == NULL) || |
AzureIoTClient | 13:920e00014ee3 | 64 | (item == NULL)) |
AzureIoTClient | 13:920e00014ee3 | 65 | { |
AzureIoTClient | 13:920e00014ee3 | 66 | result = NULL; |
AzureIoTClient | 13:920e00014ee3 | 67 | } |
AzureIoTClient | 13:920e00014ee3 | 68 | else |
AzureIoTClient | 13:920e00014ee3 | 69 | { |
AzureIoTClient | 13:920e00014ee3 | 70 | LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list; |
AzureIoTClient | 13:920e00014ee3 | 71 | result = (LIST_ITEM_INSTANCE*)malloc(sizeof(LIST_ITEM_INSTANCE)); |
AzureIoTClient | 13:920e00014ee3 | 72 | |
AzureIoTClient | 13:920e00014ee3 | 73 | if (result == NULL) |
AzureIoTClient | 13:920e00014ee3 | 74 | { |
AzureIoTClient | 13:920e00014ee3 | 75 | /* Codes_SRS_LIST_01_007: [If allocating the new list node fails, singlylinkedlist_add shall return NULL.] */ |
AzureIoTClient | 13:920e00014ee3 | 76 | result = NULL; |
AzureIoTClient | 13:920e00014ee3 | 77 | } |
AzureIoTClient | 13:920e00014ee3 | 78 | else |
AzureIoTClient | 13:920e00014ee3 | 79 | { |
AzureIoTClient | 13:920e00014ee3 | 80 | /* Codes_SRS_LIST_01_005: [singlylinkedlist_add shall add one item to the tail of the list and on success it shall return a handle to the added item.] */ |
AzureIoTClient | 13:920e00014ee3 | 81 | result->next = NULL; |
AzureIoTClient | 13:920e00014ee3 | 82 | result->item = item; |
AzureIoTClient | 13:920e00014ee3 | 83 | |
AzureIoTClient | 13:920e00014ee3 | 84 | if (list_instance->head == NULL) |
AzureIoTClient | 13:920e00014ee3 | 85 | { |
AzureIoTClient | 13:920e00014ee3 | 86 | list_instance->head = result; |
AzureIoTClient | 13:920e00014ee3 | 87 | } |
AzureIoTClient | 13:920e00014ee3 | 88 | else |
AzureIoTClient | 13:920e00014ee3 | 89 | { |
AzureIoTClient | 13:920e00014ee3 | 90 | LIST_ITEM_INSTANCE* current = list_instance->head; |
AzureIoTClient | 13:920e00014ee3 | 91 | while (current->next != NULL) |
AzureIoTClient | 13:920e00014ee3 | 92 | { |
AzureIoTClient | 13:920e00014ee3 | 93 | current = (LIST_ITEM_INSTANCE*)current->next; |
AzureIoTClient | 13:920e00014ee3 | 94 | } |
AzureIoTClient | 13:920e00014ee3 | 95 | |
AzureIoTClient | 13:920e00014ee3 | 96 | current->next = result; |
AzureIoTClient | 13:920e00014ee3 | 97 | } |
AzureIoTClient | 13:920e00014ee3 | 98 | } |
AzureIoTClient | 13:920e00014ee3 | 99 | } |
AzureIoTClient | 13:920e00014ee3 | 100 | |
AzureIoTClient | 13:920e00014ee3 | 101 | return result; |
AzureIoTClient | 13:920e00014ee3 | 102 | } |
AzureIoTClient | 13:920e00014ee3 | 103 | |
AzureIoTClient | 13:920e00014ee3 | 104 | int singlylinkedlist_remove(SINGLYLINKEDLIST_HANDLE list, LIST_ITEM_HANDLE item) |
AzureIoTClient | 13:920e00014ee3 | 105 | { |
AzureIoTClient | 13:920e00014ee3 | 106 | int result; |
AzureIoTClient | 13:920e00014ee3 | 107 | |
AzureIoTClient | 13:920e00014ee3 | 108 | /* Codes_SRS_LIST_01_024: [If any of the arguments list or item_handle is NULL, singlylinkedlist_remove shall fail and return a non-zero value.] */ |
AzureIoTClient | 13:920e00014ee3 | 109 | if ((list == NULL) || |
AzureIoTClient | 13:920e00014ee3 | 110 | (item == NULL)) |
AzureIoTClient | 13:920e00014ee3 | 111 | { |
AzureIoTClient | 13:920e00014ee3 | 112 | result = __LINE__; |
AzureIoTClient | 13:920e00014ee3 | 113 | } |
AzureIoTClient | 13:920e00014ee3 | 114 | else |
AzureIoTClient | 13:920e00014ee3 | 115 | { |
AzureIoTClient | 13:920e00014ee3 | 116 | LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list; |
AzureIoTClient | 13:920e00014ee3 | 117 | LIST_ITEM_INSTANCE* current_item = list_instance->head; |
AzureIoTClient | 13:920e00014ee3 | 118 | LIST_ITEM_INSTANCE* previous_item = NULL; |
AzureIoTClient | 13:920e00014ee3 | 119 | |
AzureIoTClient | 13:920e00014ee3 | 120 | while (current_item != NULL) |
AzureIoTClient | 13:920e00014ee3 | 121 | { |
AzureIoTClient | 13:920e00014ee3 | 122 | if (current_item == item) |
AzureIoTClient | 13:920e00014ee3 | 123 | { |
AzureIoTClient | 13:920e00014ee3 | 124 | if (previous_item != NULL) |
AzureIoTClient | 13:920e00014ee3 | 125 | { |
AzureIoTClient | 13:920e00014ee3 | 126 | previous_item->next = current_item->next; |
AzureIoTClient | 13:920e00014ee3 | 127 | } |
AzureIoTClient | 13:920e00014ee3 | 128 | else |
AzureIoTClient | 13:920e00014ee3 | 129 | { |
AzureIoTClient | 13:920e00014ee3 | 130 | list_instance->head = (LIST_ITEM_INSTANCE*)current_item->next; |
AzureIoTClient | 13:920e00014ee3 | 131 | } |
AzureIoTClient | 13:920e00014ee3 | 132 | |
AzureIoTClient | 13:920e00014ee3 | 133 | free(current_item); |
AzureIoTClient | 13:920e00014ee3 | 134 | |
AzureIoTClient | 13:920e00014ee3 | 135 | break; |
AzureIoTClient | 13:920e00014ee3 | 136 | } |
AzureIoTClient | 13:920e00014ee3 | 137 | previous_item = current_item; |
AzureIoTClient | 13:920e00014ee3 | 138 | current_item = (LIST_ITEM_INSTANCE*)current_item->next; |
AzureIoTClient | 13:920e00014ee3 | 139 | } |
AzureIoTClient | 13:920e00014ee3 | 140 | |
AzureIoTClient | 13:920e00014ee3 | 141 | if (current_item == NULL) |
AzureIoTClient | 13:920e00014ee3 | 142 | { |
AzureIoTClient | 13:920e00014ee3 | 143 | /* Codes_SRS_LIST_01_025: [If the item item_handle is not found in the list, then singlylinkedlist_remove shall fail and return a non-zero value.] */ |
AzureIoTClient | 13:920e00014ee3 | 144 | result = __LINE__; |
AzureIoTClient | 13:920e00014ee3 | 145 | } |
AzureIoTClient | 13:920e00014ee3 | 146 | else |
AzureIoTClient | 13:920e00014ee3 | 147 | { |
AzureIoTClient | 13:920e00014ee3 | 148 | /* Codes_SRS_LIST_01_023: [singlylinkedlist_remove shall remove a list item from the list and on success it shall return 0.] */ |
AzureIoTClient | 13:920e00014ee3 | 149 | result = 0; |
AzureIoTClient | 13:920e00014ee3 | 150 | } |
AzureIoTClient | 13:920e00014ee3 | 151 | } |
AzureIoTClient | 13:920e00014ee3 | 152 | |
AzureIoTClient | 13:920e00014ee3 | 153 | return result; |
AzureIoTClient | 13:920e00014ee3 | 154 | } |
AzureIoTClient | 13:920e00014ee3 | 155 | |
AzureIoTClient | 13:920e00014ee3 | 156 | LIST_ITEM_HANDLE singlylinkedlist_get_head_item(SINGLYLINKEDLIST_HANDLE list) |
AzureIoTClient | 13:920e00014ee3 | 157 | { |
AzureIoTClient | 13:920e00014ee3 | 158 | LIST_ITEM_HANDLE result; |
AzureIoTClient | 13:920e00014ee3 | 159 | |
AzureIoTClient | 13:920e00014ee3 | 160 | if (list == NULL) |
AzureIoTClient | 13:920e00014ee3 | 161 | { |
AzureIoTClient | 13:920e00014ee3 | 162 | /* Codes_SRS_LIST_01_009: [If the list argument is NULL, singlylinkedlist_get_head_item shall return NULL.] */ |
AzureIoTClient | 13:920e00014ee3 | 163 | result = NULL; |
AzureIoTClient | 13:920e00014ee3 | 164 | } |
AzureIoTClient | 13:920e00014ee3 | 165 | else |
AzureIoTClient | 13:920e00014ee3 | 166 | { |
AzureIoTClient | 13:920e00014ee3 | 167 | LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list; |
AzureIoTClient | 13:920e00014ee3 | 168 | |
AzureIoTClient | 13:920e00014ee3 | 169 | /* Codes_SRS_LIST_01_008: [singlylinkedlist_get_head_item shall return the head of the list.] */ |
AzureIoTClient | 13:920e00014ee3 | 170 | /* Codes_SRS_LIST_01_010: [If the list is empty, singlylinkedlist_get_head_item_shall_return NULL.] */ |
AzureIoTClient | 13:920e00014ee3 | 171 | result = list_instance->head; |
AzureIoTClient | 13:920e00014ee3 | 172 | } |
AzureIoTClient | 13:920e00014ee3 | 173 | |
AzureIoTClient | 13:920e00014ee3 | 174 | return result; |
AzureIoTClient | 13:920e00014ee3 | 175 | } |
AzureIoTClient | 13:920e00014ee3 | 176 | |
AzureIoTClient | 13:920e00014ee3 | 177 | LIST_ITEM_HANDLE singlylinkedlist_get_next_item(LIST_ITEM_HANDLE item_handle) |
AzureIoTClient | 13:920e00014ee3 | 178 | { |
AzureIoTClient | 13:920e00014ee3 | 179 | LIST_ITEM_HANDLE result; |
AzureIoTClient | 13:920e00014ee3 | 180 | |
AzureIoTClient | 13:920e00014ee3 | 181 | if (item_handle == NULL) |
AzureIoTClient | 13:920e00014ee3 | 182 | { |
AzureIoTClient | 13:920e00014ee3 | 183 | /* Codes_SRS_LIST_01_019: [If item_handle is NULL then singlylinkedlist_get_next_item shall return NULL.] */ |
AzureIoTClient | 13:920e00014ee3 | 184 | result = NULL; |
AzureIoTClient | 13:920e00014ee3 | 185 | } |
AzureIoTClient | 13:920e00014ee3 | 186 | else |
AzureIoTClient | 13:920e00014ee3 | 187 | { |
AzureIoTClient | 13:920e00014ee3 | 188 | /* Codes_SRS_LIST_01_018: [singlylinkedlist_get_next_item shall return the next item in the list following the item item_handle.] */ |
AzureIoTClient | 13:920e00014ee3 | 189 | result = (LIST_ITEM_HANDLE)((LIST_ITEM_INSTANCE*)item_handle)->next; |
AzureIoTClient | 13:920e00014ee3 | 190 | } |
AzureIoTClient | 13:920e00014ee3 | 191 | |
AzureIoTClient | 13:920e00014ee3 | 192 | return result; |
AzureIoTClient | 13:920e00014ee3 | 193 | } |
AzureIoTClient | 13:920e00014ee3 | 194 | |
AzureIoTClient | 13:920e00014ee3 | 195 | const void* singlylinkedlist_item_get_value(LIST_ITEM_HANDLE item_handle) |
AzureIoTClient | 13:920e00014ee3 | 196 | { |
AzureIoTClient | 13:920e00014ee3 | 197 | const void* result; |
AzureIoTClient | 13:920e00014ee3 | 198 | |
AzureIoTClient | 13:920e00014ee3 | 199 | if (item_handle == NULL) |
AzureIoTClient | 13:920e00014ee3 | 200 | { |
AzureIoTClient | 13:920e00014ee3 | 201 | /* Codes_SRS_LIST_01_021: [If item_handle is NULL, singlylinkedlist_item_get_value shall return NULL.] */ |
AzureIoTClient | 13:920e00014ee3 | 202 | result = NULL; |
AzureIoTClient | 13:920e00014ee3 | 203 | } |
AzureIoTClient | 13:920e00014ee3 | 204 | else |
AzureIoTClient | 13:920e00014ee3 | 205 | { |
AzureIoTClient | 13:920e00014ee3 | 206 | /* Codes_SRS_LIST_01_020: [singlylinkedlist_item_get_value shall return the value associated with the list item identified by the item_handle argument.] */ |
AzureIoTClient | 13:920e00014ee3 | 207 | result = ((LIST_ITEM_INSTANCE*)item_handle)->item; |
AzureIoTClient | 13:920e00014ee3 | 208 | } |
AzureIoTClient | 13:920e00014ee3 | 209 | |
AzureIoTClient | 13:920e00014ee3 | 210 | return result; |
AzureIoTClient | 13:920e00014ee3 | 211 | } |
AzureIoTClient | 13:920e00014ee3 | 212 | |
AzureIoTClient | 13:920e00014ee3 | 213 | LIST_ITEM_HANDLE singlylinkedlist_find(SINGLYLINKEDLIST_HANDLE list, LIST_MATCH_FUNCTION match_function, const void* match_context) |
AzureIoTClient | 13:920e00014ee3 | 214 | { |
AzureIoTClient | 13:920e00014ee3 | 215 | LIST_ITEM_HANDLE result; |
AzureIoTClient | 13:920e00014ee3 | 216 | |
AzureIoTClient | 13:920e00014ee3 | 217 | if ((list == NULL) || |
AzureIoTClient | 13:920e00014ee3 | 218 | (match_function == NULL)) |
AzureIoTClient | 13:920e00014ee3 | 219 | { |
AzureIoTClient | 13:920e00014ee3 | 220 | /* Codes_SRS_LIST_01_012: [If the list or the match_function argument is NULL, singlylinkedlist_find shall return NULL.] */ |
AzureIoTClient | 13:920e00014ee3 | 221 | result = NULL; |
AzureIoTClient | 13:920e00014ee3 | 222 | } |
AzureIoTClient | 13:920e00014ee3 | 223 | else |
AzureIoTClient | 13:920e00014ee3 | 224 | { |
AzureIoTClient | 13:920e00014ee3 | 225 | LIST_INSTANCE* list_instance = (LIST_INSTANCE*)list; |
AzureIoTClient | 13:920e00014ee3 | 226 | LIST_ITEM_INSTANCE* current = list_instance->head; |
AzureIoTClient | 13:920e00014ee3 | 227 | |
AzureIoTClient | 13:920e00014ee3 | 228 | /* Codes_SRS_LIST_01_011: [singlylinkedlist_find shall iterate through all items in a list and return the first one that satisfies a certain match function.] */ |
AzureIoTClient | 13:920e00014ee3 | 229 | while (current != NULL) |
AzureIoTClient | 13:920e00014ee3 | 230 | { |
AzureIoTClient | 13:920e00014ee3 | 231 | /* Codes_SRS_LIST_01_014: [list find shall determine whether an item satisfies the match criteria by invoking the match function for each item in the list until a matching item is found.] */ |
AzureIoTClient | 13:920e00014ee3 | 232 | /* Codes_SRS_LIST_01_013: [The match_function shall get as arguments the list item being attempted to be matched and the match_context as is.] */ |
AzureIoTClient | 13:920e00014ee3 | 233 | if (match_function((LIST_ITEM_HANDLE)current, match_context) == true) |
AzureIoTClient | 13:920e00014ee3 | 234 | { |
AzureIoTClient | 13:920e00014ee3 | 235 | /* Codes_SRS_LIST_01_017: [If the match function returns true, singlylinkedlist_find shall consider that item as matching.] */ |
AzureIoTClient | 13:920e00014ee3 | 236 | break; |
AzureIoTClient | 13:920e00014ee3 | 237 | } |
AzureIoTClient | 13:920e00014ee3 | 238 | |
AzureIoTClient | 13:920e00014ee3 | 239 | /* Codes_SRS_LIST_01_016: [If the match function returns false, singlylinkedlist_find shall consider that item as not matching.] */ |
AzureIoTClient | 13:920e00014ee3 | 240 | current = (LIST_ITEM_INSTANCE*)current->next; |
AzureIoTClient | 13:920e00014ee3 | 241 | } |
AzureIoTClient | 13:920e00014ee3 | 242 | |
AzureIoTClient | 13:920e00014ee3 | 243 | if (current == NULL) |
AzureIoTClient | 13:920e00014ee3 | 244 | { |
AzureIoTClient | 13:920e00014ee3 | 245 | /* Codes_SRS_LIST_01_015: [If the list is empty, singlylinkedlist_find shall return NULL.] */ |
AzureIoTClient | 13:920e00014ee3 | 246 | result = NULL; |
AzureIoTClient | 13:920e00014ee3 | 247 | } |
AzureIoTClient | 13:920e00014ee3 | 248 | else |
AzureIoTClient | 13:920e00014ee3 | 249 | { |
AzureIoTClient | 13:920e00014ee3 | 250 | result = current; |
AzureIoTClient | 13:920e00014ee3 | 251 | } |
AzureIoTClient | 13:920e00014ee3 | 252 | } |
AzureIoTClient | 13:920e00014ee3 | 253 | |
AzureIoTClient | 13:920e00014ee3 | 254 | return result; |
AzureIoTClient | 13:920e00014ee3 | 255 | } |