Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of azure_c_shared_utility by
vector.c@21:b92006c5b9ff, 2017-02-24 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Feb 24 14:01:41 2017 -0800
- Revision:
- 21:b92006c5b9ff
- Parent:
- 19:2e0811512ceb
1.1.8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Azure.IoT Build | 0:fa2de1b79154 | 1 | // Copyright (c) Microsoft. All rights reserved. |
Azure.IoT Build | 0:fa2de1b79154 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
Azure.IoT Build | 0:fa2de1b79154 | 3 | |
Azure.IoT Build | 0:fa2de1b79154 | 4 | #include <stdlib.h> |
Azure.IoT Build | 0:fa2de1b79154 | 5 | #include "azure_c_shared_utility/gballoc.h" |
Azure.IoT Build | 0:fa2de1b79154 | 6 | #include "azure_c_shared_utility/vector.h" |
AzureIoTClient | 21:b92006c5b9ff | 7 | #include "azure_c_shared_utility/optimize_size.h" |
AzureIoTClient | 19:2e0811512ceb | 8 | #include "azure_c_shared_utility/xlogging.h" |
Azure.IoT Build | 0:fa2de1b79154 | 9 | |
AzureIoTClient | 19:2e0811512ceb | 10 | #include "azure_c_shared_utility/vector_types_internal.h" |
Azure.IoT Build | 0:fa2de1b79154 | 11 | |
Azure.IoT Build | 0:fa2de1b79154 | 12 | VECTOR_HANDLE VECTOR_create(size_t elementSize) |
Azure.IoT Build | 0:fa2de1b79154 | 13 | { |
Azure.IoT Build | 0:fa2de1b79154 | 14 | VECTOR_HANDLE result; |
Azure.IoT Build | 0:fa2de1b79154 | 15 | |
AzureIoTClient | 19:2e0811512ceb | 16 | /* Codes_SRS_VECTOR_10_002: [VECTOR_create shall fail and return NULL if elementsize is 0.] */ |
AzureIoTClient | 19:2e0811512ceb | 17 | if (elementSize == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 18 | { |
AzureIoTClient | 19:2e0811512ceb | 19 | LogError("invalid elementSize(%zd).", elementSize); |
Azure.IoT Build | 0:fa2de1b79154 | 20 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 21 | } |
Azure.IoT Build | 0:fa2de1b79154 | 22 | else |
Azure.IoT Build | 0:fa2de1b79154 | 23 | { |
AzureIoTClient | 19:2e0811512ceb | 24 | result = (VECTOR*)malloc(sizeof(VECTOR)); |
AzureIoTClient | 19:2e0811512ceb | 25 | /* Codes_SRS_VECTOR_10_002 : [VECTOR_create shall fail and return NULL if malloc fails.] */ |
AzureIoTClient | 19:2e0811512ceb | 26 | if (result == NULL) |
AzureIoTClient | 19:2e0811512ceb | 27 | { |
AzureIoTClient | 19:2e0811512ceb | 28 | LogError("malloc failed."); |
AzureIoTClient | 19:2e0811512ceb | 29 | } |
AzureIoTClient | 19:2e0811512ceb | 30 | else |
AzureIoTClient | 19:2e0811512ceb | 31 | { |
AzureIoTClient | 19:2e0811512ceb | 32 | /* Codes_SRS_VECTOR_10_001: [VECTOR_create shall allocate a VECTOR_HANDLE that will contain an empty vector.The size of each element is given with the parameter elementSize.] */ |
AzureIoTClient | 19:2e0811512ceb | 33 | result->storage = NULL; |
AzureIoTClient | 19:2e0811512ceb | 34 | result->count = 0; |
AzureIoTClient | 19:2e0811512ceb | 35 | result->elementSize = elementSize; |
AzureIoTClient | 19:2e0811512ceb | 36 | } |
Azure.IoT Build | 0:fa2de1b79154 | 37 | } |
Azure.IoT Build | 0:fa2de1b79154 | 38 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 39 | } |
Azure.IoT Build | 0:fa2de1b79154 | 40 | |
Azure.IoT Build | 0:fa2de1b79154 | 41 | void VECTOR_destroy(VECTOR_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 42 | { |
AzureIoTClient | 19:2e0811512ceb | 43 | /* Codes_SRS_VECTOR_10_009: [VECTOR_destroy shall return if the given handle is NULL.] */ |
AzureIoTClient | 19:2e0811512ceb | 44 | if (handle == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 45 | { |
AzureIoTClient | 19:2e0811512ceb | 46 | LogError("invalid argument handle(NULL)."); |
AzureIoTClient | 19:2e0811512ceb | 47 | } |
AzureIoTClient | 19:2e0811512ceb | 48 | else |
AzureIoTClient | 19:2e0811512ceb | 49 | { |
AzureIoTClient | 19:2e0811512ceb | 50 | /* Codes_SRS_VECTOR_10_008: [VECTOR_destroy shall free the given handle and its internal storage.] */ |
AzureIoTClient | 19:2e0811512ceb | 51 | free(handle->storage); |
AzureIoTClient | 19:2e0811512ceb | 52 | free(handle); |
Azure.IoT Build | 0:fa2de1b79154 | 53 | } |
Azure.IoT Build | 0:fa2de1b79154 | 54 | } |
Azure.IoT Build | 0:fa2de1b79154 | 55 | |
AzureIoTClient | 19:2e0811512ceb | 56 | VECTOR_HANDLE VECTOR_move(VECTOR_HANDLE handle) |
AzureIoTClient | 19:2e0811512ceb | 57 | { |
AzureIoTClient | 19:2e0811512ceb | 58 | VECTOR_HANDLE result; |
AzureIoTClient | 19:2e0811512ceb | 59 | if (handle == NULL) |
AzureIoTClient | 19:2e0811512ceb | 60 | { |
AzureIoTClient | 19:2e0811512ceb | 61 | /* Codes_SRS_VECTOR_10_005: [VECTOR_move shall fail and return NULL if the given handle is NULL.] */ |
AzureIoTClient | 19:2e0811512ceb | 62 | LogError("invalid argument - handle(NULL)."); |
AzureIoTClient | 19:2e0811512ceb | 63 | result = NULL; |
AzureIoTClient | 19:2e0811512ceb | 64 | } |
AzureIoTClient | 19:2e0811512ceb | 65 | else |
AzureIoTClient | 19:2e0811512ceb | 66 | { |
AzureIoTClient | 19:2e0811512ceb | 67 | result = (VECTOR*)malloc(sizeof(VECTOR)); |
AzureIoTClient | 19:2e0811512ceb | 68 | if (result == NULL) |
AzureIoTClient | 19:2e0811512ceb | 69 | { |
AzureIoTClient | 19:2e0811512ceb | 70 | /* Codes_SRS_VECTOR_10_006: [VECTOR_move shall fail and return NULL if malloc fails.] */ |
AzureIoTClient | 19:2e0811512ceb | 71 | LogError("malloc failed."); |
AzureIoTClient | 19:2e0811512ceb | 72 | } |
AzureIoTClient | 19:2e0811512ceb | 73 | else |
AzureIoTClient | 19:2e0811512ceb | 74 | { |
AzureIoTClient | 19:2e0811512ceb | 75 | /* Codes_SRS_VECTOR_10_004: [VECTOR_move shall allocate a VECTOR_HANDLE and move the data to it from the given handle.] */ |
AzureIoTClient | 19:2e0811512ceb | 76 | result->count = handle->count; |
AzureIoTClient | 19:2e0811512ceb | 77 | result->elementSize = handle->elementSize; |
AzureIoTClient | 19:2e0811512ceb | 78 | result->storage = handle->storage; |
AzureIoTClient | 19:2e0811512ceb | 79 | |
AzureIoTClient | 19:2e0811512ceb | 80 | handle->storage = NULL; |
AzureIoTClient | 19:2e0811512ceb | 81 | handle->count = 0; |
AzureIoTClient | 19:2e0811512ceb | 82 | } |
AzureIoTClient | 19:2e0811512ceb | 83 | } |
AzureIoTClient | 19:2e0811512ceb | 84 | return result; |
AzureIoTClient | 19:2e0811512ceb | 85 | } |
AzureIoTClient | 19:2e0811512ceb | 86 | |
Azure.IoT Build | 0:fa2de1b79154 | 87 | /* insertion */ |
AzureIoTClient | 19:2e0811512ceb | 88 | |
Azure.IoT Build | 0:fa2de1b79154 | 89 | int VECTOR_push_back(VECTOR_HANDLE handle, const void* elements, size_t numElements) |
Azure.IoT Build | 0:fa2de1b79154 | 90 | { |
Azure.IoT Build | 0:fa2de1b79154 | 91 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 92 | if (handle == NULL || elements == NULL || numElements == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 93 | { |
AzureIoTClient | 19:2e0811512ceb | 94 | /* Codes_SRS_VECTOR_10_011: [VECTOR_push_back shall fail and return non-zero if `handle` is NULL.] */ |
AzureIoTClient | 19:2e0811512ceb | 95 | /* Codes_SRS_VECTOR_10_034: [VECTOR_push_back shall fail and return non-zero if `elements` is NULL.] */ |
AzureIoTClient | 19:2e0811512ceb | 96 | /* Codes_SRS_VECTOR_10_035: [VECTOR_push_back shall fail and return non-zero if `numElements` is 0.] */ |
AzureIoTClient | 19:2e0811512ceb | 97 | LogError("invalid argument - handle(%p), elements(%p), numElements(%zd).", handle, elements, numElements); |
AzureIoTClient | 21:b92006c5b9ff | 98 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 99 | } |
Azure.IoT Build | 0:fa2de1b79154 | 100 | else |
Azure.IoT Build | 0:fa2de1b79154 | 101 | { |
AzureIoTClient | 19:2e0811512ceb | 102 | size_t curSize = handle->elementSize * handle->count; |
AzureIoTClient | 19:2e0811512ceb | 103 | size_t appendSize = handle->elementSize * numElements; |
Azure.IoT Build | 0:fa2de1b79154 | 104 | |
AzureIoTClient | 19:2e0811512ceb | 105 | void* temp = realloc(handle->storage, curSize + appendSize); |
Azure.IoT Build | 0:fa2de1b79154 | 106 | if (temp == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 107 | { |
AzureIoTClient | 19:2e0811512ceb | 108 | /* Codes_SRS_VECTOR_10_012: [VECTOR_push_back shall fail and return non-zero if memory allocation fails.] */ |
AzureIoTClient | 19:2e0811512ceb | 109 | LogError("realloc failed."); |
AzureIoTClient | 21:b92006c5b9ff | 110 | result = __FAILURE__; |
Azure.IoT Build | 0:fa2de1b79154 | 111 | } |
Azure.IoT Build | 0:fa2de1b79154 | 112 | else |
Azure.IoT Build | 0:fa2de1b79154 | 113 | { |
AzureIoTClient | 19:2e0811512ceb | 114 | /* Codes_SRS_VECTOR_10_013: [VECTOR_push_back shall append the given elements and return 0 indicating success.] */ |
AzureIoTClient | 19:2e0811512ceb | 115 | (void)memcpy((unsigned char*)temp + curSize, elements, appendSize); |
AzureIoTClient | 19:2e0811512ceb | 116 | handle->storage = temp; |
AzureIoTClient | 19:2e0811512ceb | 117 | handle->count += numElements; |
Azure.IoT Build | 0:fa2de1b79154 | 118 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 119 | } |
Azure.IoT Build | 0:fa2de1b79154 | 120 | } |
Azure.IoT Build | 0:fa2de1b79154 | 121 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 122 | } |
Azure.IoT Build | 0:fa2de1b79154 | 123 | |
Azure.IoT Build | 0:fa2de1b79154 | 124 | /* removal */ |
AzureIoTClient | 19:2e0811512ceb | 125 | |
Azure.IoT Build | 0:fa2de1b79154 | 126 | void VECTOR_erase(VECTOR_HANDLE handle, void* elements, size_t numElements) |
Azure.IoT Build | 0:fa2de1b79154 | 127 | { |
AzureIoTClient | 19:2e0811512ceb | 128 | if (handle == NULL || elements == NULL || numElements == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 129 | { |
AzureIoTClient | 19:2e0811512ceb | 130 | /* Codes_SRS_VECTOR_10_015: [VECTOR_erase shall return if `handle` is NULL.] */ |
AzureIoTClient | 19:2e0811512ceb | 131 | /* Codes_SRS_VECTOR_10_038: [VECTOR_erase shall return if `elements` is NULL.] */ |
AzureIoTClient | 19:2e0811512ceb | 132 | /* Codes_SRS_VECTOR_10_039: [VECTOR_erase shall return if `numElements` is 0.] */ |
AzureIoTClient | 19:2e0811512ceb | 133 | LogError("invalid argument - handle(%p), elements(%p), numElements(%zd).", handle, elements, numElements); |
AzureIoTClient | 19:2e0811512ceb | 134 | } |
AzureIoTClient | 19:2e0811512ceb | 135 | else |
AzureIoTClient | 19:2e0811512ceb | 136 | { |
AzureIoTClient | 19:2e0811512ceb | 137 | if (elements < handle->storage) |
Azure.IoT Build | 0:fa2de1b79154 | 138 | { |
AzureIoTClient | 19:2e0811512ceb | 139 | /* Codes_SRS_VECTOR_10_040: [VECTOR_erase shall return if `elements` is out of bound.] */ |
AzureIoTClient | 19:2e0811512ceb | 140 | LogError("invalid argument elements(%p) is not a member of this object.", elements); |
Azure.IoT Build | 0:fa2de1b79154 | 141 | } |
Azure.IoT Build | 0:fa2de1b79154 | 142 | else |
Azure.IoT Build | 0:fa2de1b79154 | 143 | { |
AzureIoTClient | 19:2e0811512ceb | 144 | ptrdiff_t diff = ((unsigned char*)elements) - ((unsigned char*)handle->storage); |
AzureIoTClient | 19:2e0811512ceb | 145 | if ((diff % handle->elementSize) != 0) |
AzureIoTClient | 19:2e0811512ceb | 146 | { |
AzureIoTClient | 19:2e0811512ceb | 147 | /* Codes_SRS_VECTOR_10_041: [VECTOR_erase shall return if elements is misaligned.] */ |
AzureIoTClient | 19:2e0811512ceb | 148 | LogError("invalid argument - elements(%p) is misaligned", elements); |
AzureIoTClient | 19:2e0811512ceb | 149 | } |
AzureIoTClient | 19:2e0811512ceb | 150 | else |
AzureIoTClient | 19:2e0811512ceb | 151 | { |
AzureIoTClient | 19:2e0811512ceb | 152 | /* Compute the arguments needed for memmove. */ |
AzureIoTClient | 19:2e0811512ceb | 153 | unsigned char* src = (unsigned char*)elements + (handle->elementSize * numElements); |
AzureIoTClient | 19:2e0811512ceb | 154 | unsigned char* srcEnd = (unsigned char*)handle->storage + (handle->elementSize * handle->count); |
AzureIoTClient | 19:2e0811512ceb | 155 | if (src > srcEnd) |
AzureIoTClient | 19:2e0811512ceb | 156 | { |
AzureIoTClient | 19:2e0811512ceb | 157 | /* Codes_SRS_VECTOR_10_040: [VECTOR_erase shall return if `elements` is out of bound.] */ |
AzureIoTClient | 19:2e0811512ceb | 158 | LogError("invalid argument - numElements(%zd) is out of bound.", numElements); |
AzureIoTClient | 19:2e0811512ceb | 159 | } |
AzureIoTClient | 19:2e0811512ceb | 160 | else |
AzureIoTClient | 19:2e0811512ceb | 161 | { |
AzureIoTClient | 19:2e0811512ceb | 162 | /* Codes_SRS_VECTOR_10_014: [VECTOR_erase shall remove the 'numElements' starting at 'elements' and reduce its internal storage.] */ |
AzureIoTClient | 19:2e0811512ceb | 163 | handle->count -= numElements; |
AzureIoTClient | 19:2e0811512ceb | 164 | if (handle->count == 0) |
AzureIoTClient | 19:2e0811512ceb | 165 | { |
AzureIoTClient | 19:2e0811512ceb | 166 | free(handle->storage); |
AzureIoTClient | 19:2e0811512ceb | 167 | handle->storage = NULL; |
AzureIoTClient | 19:2e0811512ceb | 168 | } |
AzureIoTClient | 19:2e0811512ceb | 169 | else |
AzureIoTClient | 19:2e0811512ceb | 170 | { |
AzureIoTClient | 19:2e0811512ceb | 171 | void* tmp; |
AzureIoTClient | 19:2e0811512ceb | 172 | (void)memmove(elements, src, srcEnd - src); |
AzureIoTClient | 19:2e0811512ceb | 173 | tmp = realloc(handle->storage, (handle->elementSize * handle->count)); |
AzureIoTClient | 19:2e0811512ceb | 174 | if (tmp == NULL) |
AzureIoTClient | 19:2e0811512ceb | 175 | { |
AzureIoTClient | 19:2e0811512ceb | 176 | LogInfo("realloc failed. Keeping original internal storage pointer."); |
AzureIoTClient | 19:2e0811512ceb | 177 | } |
AzureIoTClient | 19:2e0811512ceb | 178 | else |
AzureIoTClient | 19:2e0811512ceb | 179 | { |
AzureIoTClient | 19:2e0811512ceb | 180 | handle->storage = tmp; |
AzureIoTClient | 19:2e0811512ceb | 181 | } |
AzureIoTClient | 19:2e0811512ceb | 182 | } |
AzureIoTClient | 19:2e0811512ceb | 183 | } |
AzureIoTClient | 19:2e0811512ceb | 184 | } |
Azure.IoT Build | 0:fa2de1b79154 | 185 | } |
Azure.IoT Build | 0:fa2de1b79154 | 186 | } |
Azure.IoT Build | 0:fa2de1b79154 | 187 | } |
Azure.IoT Build | 0:fa2de1b79154 | 188 | |
Azure.IoT Build | 0:fa2de1b79154 | 189 | void VECTOR_clear(VECTOR_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 190 | { |
AzureIoTClient | 19:2e0811512ceb | 191 | /* Codes_SRS_VECTOR_10_017: [VECTOR_clear shall if the object is NULL or empty.] */ |
AzureIoTClient | 19:2e0811512ceb | 192 | if (handle == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 193 | { |
AzureIoTClient | 19:2e0811512ceb | 194 | LogError("invalid argument handle(NULL)."); |
AzureIoTClient | 19:2e0811512ceb | 195 | } |
AzureIoTClient | 19:2e0811512ceb | 196 | else |
AzureIoTClient | 19:2e0811512ceb | 197 | { |
AzureIoTClient | 19:2e0811512ceb | 198 | /* Codes_SRS_VECTOR_10_016: [VECTOR_clear shall remove all elements from the object and release internal storage.] */ |
AzureIoTClient | 19:2e0811512ceb | 199 | free(handle->storage); |
AzureIoTClient | 19:2e0811512ceb | 200 | handle->storage = NULL; |
AzureIoTClient | 19:2e0811512ceb | 201 | handle->count = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 202 | } |
Azure.IoT Build | 0:fa2de1b79154 | 203 | } |
Azure.IoT Build | 0:fa2de1b79154 | 204 | |
Azure.IoT Build | 0:fa2de1b79154 | 205 | /* access */ |
Azure.IoT Build | 0:fa2de1b79154 | 206 | |
AzureIoTClient | 19:2e0811512ceb | 207 | void* VECTOR_element(VECTOR_HANDLE handle, size_t index) |
Azure.IoT Build | 0:fa2de1b79154 | 208 | { |
AzureIoTClient | 19:2e0811512ceb | 209 | void* result; |
AzureIoTClient | 19:2e0811512ceb | 210 | if (handle == NULL) |
AzureIoTClient | 19:2e0811512ceb | 211 | { |
AzureIoTClient | 19:2e0811512ceb | 212 | /* Codes_SRS_VECTOR_10_019: [VECTOR_element shall fail and return NULL if handle is NULL.] */ |
AzureIoTClient | 19:2e0811512ceb | 213 | LogError("invalid argument handle(NULL)."); |
AzureIoTClient | 19:2e0811512ceb | 214 | result = NULL; |
AzureIoTClient | 19:2e0811512ceb | 215 | } |
AzureIoTClient | 19:2e0811512ceb | 216 | else |
Azure.IoT Build | 0:fa2de1b79154 | 217 | { |
AzureIoTClient | 19:2e0811512ceb | 218 | if (index >= handle->count) |
Azure.IoT Build | 0:fa2de1b79154 | 219 | { |
AzureIoTClient | 19:2e0811512ceb | 220 | /* Codes_SRS_VECTOR_10_020: [VECTOR_element shall fail and return NULL if the given index is out of range.] */ |
AzureIoTClient | 19:2e0811512ceb | 221 | LogError("invalid argument - index(%zd); should be >= 0 and < %zd.", index, handle->count); |
AzureIoTClient | 19:2e0811512ceb | 222 | result = NULL; |
AzureIoTClient | 19:2e0811512ceb | 223 | } |
AzureIoTClient | 19:2e0811512ceb | 224 | else |
AzureIoTClient | 19:2e0811512ceb | 225 | { |
AzureIoTClient | 19:2e0811512ceb | 226 | /* Codes_SRS_VECTOR_10_018: [VECTOR_element shall return the element at the given index.] */ |
AzureIoTClient | 19:2e0811512ceb | 227 | result = (unsigned char*)handle->storage + (handle->elementSize * index); |
Azure.IoT Build | 0:fa2de1b79154 | 228 | } |
Azure.IoT Build | 0:fa2de1b79154 | 229 | } |
Azure.IoT Build | 0:fa2de1b79154 | 230 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 231 | } |
Azure.IoT Build | 0:fa2de1b79154 | 232 | |
AzureIoTClient | 19:2e0811512ceb | 233 | void* VECTOR_front(VECTOR_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 234 | { |
AzureIoTClient | 19:2e0811512ceb | 235 | void* result; |
AzureIoTClient | 19:2e0811512ceb | 236 | if (handle == NULL) |
AzureIoTClient | 19:2e0811512ceb | 237 | { |
AzureIoTClient | 19:2e0811512ceb | 238 | /* Codes_SRS_VECTOR_10_022: [VECTOR_front shall fail and return NULL if handle is NULL.] */ |
AzureIoTClient | 19:2e0811512ceb | 239 | LogError("invalid argument handle (NULL)."); |
AzureIoTClient | 19:2e0811512ceb | 240 | result = NULL; |
AzureIoTClient | 19:2e0811512ceb | 241 | } |
AzureIoTClient | 19:2e0811512ceb | 242 | else |
Azure.IoT Build | 0:fa2de1b79154 | 243 | { |
AzureIoTClient | 19:2e0811512ceb | 244 | if (handle->count == 0) |
AzureIoTClient | 19:2e0811512ceb | 245 | { |
AzureIoTClient | 19:2e0811512ceb | 246 | /* Codes_SRS_VECTOR_10_028: [VECTOR_front shall return NULL if the vector is empty.] */ |
AzureIoTClient | 19:2e0811512ceb | 247 | LogError("vector is empty."); |
AzureIoTClient | 19:2e0811512ceb | 248 | result = NULL; |
AzureIoTClient | 19:2e0811512ceb | 249 | } |
AzureIoTClient | 19:2e0811512ceb | 250 | else |
AzureIoTClient | 19:2e0811512ceb | 251 | { |
AzureIoTClient | 19:2e0811512ceb | 252 | /* Codes_SRS_VECTOR_10_021: [VECTOR_front shall return a pointer to the element at index 0.] */ |
AzureIoTClient | 19:2e0811512ceb | 253 | result = handle->storage; |
AzureIoTClient | 19:2e0811512ceb | 254 | } |
Azure.IoT Build | 0:fa2de1b79154 | 255 | } |
Azure.IoT Build | 0:fa2de1b79154 | 256 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 257 | } |
Azure.IoT Build | 0:fa2de1b79154 | 258 | |
AzureIoTClient | 19:2e0811512ceb | 259 | void* VECTOR_back(VECTOR_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 260 | { |
AzureIoTClient | 19:2e0811512ceb | 261 | void* result; |
AzureIoTClient | 19:2e0811512ceb | 262 | if (handle == NULL) |
AzureIoTClient | 19:2e0811512ceb | 263 | { |
AzureIoTClient | 19:2e0811512ceb | 264 | /* Codes_SRS_VECTOR_10_024: [VECTOR_back shall fail and return NULL if handle is NULL.] */ |
AzureIoTClient | 19:2e0811512ceb | 265 | LogError("invalid argument handle (NULL)."); |
AzureIoTClient | 19:2e0811512ceb | 266 | result = NULL; |
AzureIoTClient | 19:2e0811512ceb | 267 | } |
AzureIoTClient | 19:2e0811512ceb | 268 | else |
Azure.IoT Build | 0:fa2de1b79154 | 269 | { |
AzureIoTClient | 19:2e0811512ceb | 270 | if (handle->count == 0) |
AzureIoTClient | 19:2e0811512ceb | 271 | { |
AzureIoTClient | 19:2e0811512ceb | 272 | /* Codes_SRS_VECTOR_10_029: [VECTOR_back shall return NULL if the vector is empty.] */ |
AzureIoTClient | 19:2e0811512ceb | 273 | LogError("vector is empty."); |
AzureIoTClient | 19:2e0811512ceb | 274 | result = NULL; |
AzureIoTClient | 19:2e0811512ceb | 275 | } |
AzureIoTClient | 19:2e0811512ceb | 276 | else |
AzureIoTClient | 19:2e0811512ceb | 277 | { |
AzureIoTClient | 19:2e0811512ceb | 278 | /* Codes_SRS_VECTOR_10_023: [VECTOR_front shall return the last element of the vector.] */ |
AzureIoTClient | 19:2e0811512ceb | 279 | result = (unsigned char*)handle->storage + (handle->elementSize * (handle->count - 1)); |
AzureIoTClient | 19:2e0811512ceb | 280 | } |
Azure.IoT Build | 0:fa2de1b79154 | 281 | } |
Azure.IoT Build | 0:fa2de1b79154 | 282 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 283 | } |
Azure.IoT Build | 0:fa2de1b79154 | 284 | |
AzureIoTClient | 19:2e0811512ceb | 285 | void* VECTOR_find_if(VECTOR_HANDLE handle, PREDICATE_FUNCTION pred, const void* value) |
Azure.IoT Build | 0:fa2de1b79154 | 286 | { |
AzureIoTClient | 19:2e0811512ceb | 287 | void* result; |
AzureIoTClient | 19:2e0811512ceb | 288 | if (handle == NULL || pred == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 289 | { |
AzureIoTClient | 19:2e0811512ceb | 290 | /* Codes_SRS_VECTOR_10_030: [VECTOR_find_if shall fail and return NULL if `handle` is NULL.] */ |
AzureIoTClient | 19:2e0811512ceb | 291 | /* Codes_SRS_VECTOR_10_036: [VECTOR_find_if shall fail and return NULL if `pred` is NULL.] */ |
AzureIoTClient | 19:2e0811512ceb | 292 | LogError("invalid argument - handle(%p), pred(%p)", handle, pred); |
AzureIoTClient | 19:2e0811512ceb | 293 | result = NULL; |
AzureIoTClient | 19:2e0811512ceb | 294 | } |
AzureIoTClient | 19:2e0811512ceb | 295 | else |
AzureIoTClient | 19:2e0811512ceb | 296 | { |
AzureIoTClient | 19:2e0811512ceb | 297 | size_t i; |
AzureIoTClient | 19:2e0811512ceb | 298 | for (i = 0; i < handle->count; ++i) |
Azure.IoT Build | 0:fa2de1b79154 | 299 | { |
AzureIoTClient | 19:2e0811512ceb | 300 | if (true == pred((unsigned char*)handle->storage + (handle->elementSize * i), value)) |
Azure.IoT Build | 0:fa2de1b79154 | 301 | { |
AzureIoTClient | 19:2e0811512ceb | 302 | /* Codes_SRS_VECTOR_10_031: [VECTOR_find_if shall return the first element in the vector that matches `pred`.] */ |
Azure.IoT Build | 0:fa2de1b79154 | 303 | break; |
Azure.IoT Build | 0:fa2de1b79154 | 304 | } |
Azure.IoT Build | 0:fa2de1b79154 | 305 | } |
AzureIoTClient | 19:2e0811512ceb | 306 | |
AzureIoTClient | 19:2e0811512ceb | 307 | if (i == handle->count) |
AzureIoTClient | 19:2e0811512ceb | 308 | { |
AzureIoTClient | 19:2e0811512ceb | 309 | /* Codes_SRS_VECTOR_10_032: [VECTOR_find_if shall return NULL if no matching element is found.] */ |
AzureIoTClient | 19:2e0811512ceb | 310 | result = NULL; |
AzureIoTClient | 19:2e0811512ceb | 311 | } |
AzureIoTClient | 19:2e0811512ceb | 312 | else |
AzureIoTClient | 19:2e0811512ceb | 313 | { |
AzureIoTClient | 19:2e0811512ceb | 314 | /* Codes_SRS_VECTOR_10_031: [VECTOR_find_if shall return the first element in the vector that matches `pred`.]*/ |
AzureIoTClient | 19:2e0811512ceb | 315 | result = (unsigned char*)handle->storage + (handle->elementSize * i); |
AzureIoTClient | 19:2e0811512ceb | 316 | } |
Azure.IoT Build | 0:fa2de1b79154 | 317 | } |
Azure.IoT Build | 0:fa2de1b79154 | 318 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 319 | } |
Azure.IoT Build | 0:fa2de1b79154 | 320 | |
Azure.IoT Build | 0:fa2de1b79154 | 321 | /* capacity */ |
Azure.IoT Build | 0:fa2de1b79154 | 322 | |
AzureIoTClient | 19:2e0811512ceb | 323 | size_t VECTOR_size(VECTOR_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 324 | { |
AzureIoTClient | 19:2e0811512ceb | 325 | size_t result; |
AzureIoTClient | 19:2e0811512ceb | 326 | if (handle == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 327 | { |
AzureIoTClient | 19:2e0811512ceb | 328 | /* Codes_SRS_VECTOR_10_026: [**VECTOR_size shall return 0 if the given handle is NULL.] */ |
AzureIoTClient | 19:2e0811512ceb | 329 | LogError("invalid argument handle(NULL)."); |
AzureIoTClient | 19:2e0811512ceb | 330 | result = 0; |
AzureIoTClient | 19:2e0811512ceb | 331 | } |
AzureIoTClient | 19:2e0811512ceb | 332 | else |
AzureIoTClient | 19:2e0811512ceb | 333 | { |
AzureIoTClient | 19:2e0811512ceb | 334 | /* Codes_SRS_VECTOR_10_025: [VECTOR_size shall return the number of elements stored with the given handle.] */ |
AzureIoTClient | 19:2e0811512ceb | 335 | result = handle->count; |
Azure.IoT Build | 0:fa2de1b79154 | 336 | } |
Azure.IoT Build | 0:fa2de1b79154 | 337 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 338 | } |