Azure IoT common library
Fork of azure_c_shared_utility by
vector.c@10:1be0bc9a9deb, 2016-08-26 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Aug 26 12:59:40 2016 -0700
- Revision:
- 10:1be0bc9a9deb
- Parent:
- 0:fa2de1b79154
- Child:
- 11:77df6d7e65ae
1.0.10
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 | #ifdef _CRTDBG_MAP_ALLOC |
Azure.IoT Build | 0:fa2de1b79154 | 6 | #include <crtdbg.h> |
Azure.IoT Build | 0:fa2de1b79154 | 7 | #endif |
AzureIoTClient | 10:1be0bc9a9deb | 8 | |
Azure.IoT Build | 0:fa2de1b79154 | 9 | #include "azure_c_shared_utility/gballoc.h" |
Azure.IoT Build | 0:fa2de1b79154 | 10 | #include "azure_c_shared_utility/vector.h" |
Azure.IoT Build | 0:fa2de1b79154 | 11 | |
Azure.IoT Build | 0:fa2de1b79154 | 12 | |
Azure.IoT Build | 0:fa2de1b79154 | 13 | typedef struct VECTOR_TAG |
Azure.IoT Build | 0:fa2de1b79154 | 14 | { |
Azure.IoT Build | 0:fa2de1b79154 | 15 | void* storage; |
Azure.IoT Build | 0:fa2de1b79154 | 16 | size_t count; |
Azure.IoT Build | 0:fa2de1b79154 | 17 | size_t elementSize; |
Azure.IoT Build | 0:fa2de1b79154 | 18 | } VECTOR; |
Azure.IoT Build | 0:fa2de1b79154 | 19 | |
Azure.IoT Build | 0:fa2de1b79154 | 20 | VECTOR_HANDLE VECTOR_create(size_t elementSize) |
Azure.IoT Build | 0:fa2de1b79154 | 21 | { |
Azure.IoT Build | 0:fa2de1b79154 | 22 | VECTOR_HANDLE result; |
Azure.IoT Build | 0:fa2de1b79154 | 23 | |
Azure.IoT Build | 0:fa2de1b79154 | 24 | VECTOR* vec = (VECTOR*)malloc(sizeof(VECTOR)); |
Azure.IoT Build | 0:fa2de1b79154 | 25 | if (vec == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 26 | { |
Azure.IoT Build | 0:fa2de1b79154 | 27 | result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 28 | } |
Azure.IoT Build | 0:fa2de1b79154 | 29 | else |
Azure.IoT Build | 0:fa2de1b79154 | 30 | { |
Azure.IoT Build | 0:fa2de1b79154 | 31 | vec->storage = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 32 | vec->count = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 33 | vec->elementSize = elementSize; |
Azure.IoT Build | 0:fa2de1b79154 | 34 | result = (VECTOR_HANDLE)vec; |
Azure.IoT Build | 0:fa2de1b79154 | 35 | } |
Azure.IoT Build | 0:fa2de1b79154 | 36 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 37 | } |
Azure.IoT Build | 0:fa2de1b79154 | 38 | |
Azure.IoT Build | 0:fa2de1b79154 | 39 | static void internal_VECTOR_clear(VECTOR* vec) |
Azure.IoT Build | 0:fa2de1b79154 | 40 | { |
Azure.IoT Build | 0:fa2de1b79154 | 41 | if (vec->storage != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 42 | { |
Azure.IoT Build | 0:fa2de1b79154 | 43 | free(vec->storage); |
Azure.IoT Build | 0:fa2de1b79154 | 44 | vec->storage = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 45 | } |
Azure.IoT Build | 0:fa2de1b79154 | 46 | vec->count = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 47 | } |
Azure.IoT Build | 0:fa2de1b79154 | 48 | |
Azure.IoT Build | 0:fa2de1b79154 | 49 | void VECTOR_destroy(VECTOR_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 50 | { |
Azure.IoT Build | 0:fa2de1b79154 | 51 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 52 | { |
Azure.IoT Build | 0:fa2de1b79154 | 53 | VECTOR* vec = (VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 54 | internal_VECTOR_clear(vec); |
Azure.IoT Build | 0:fa2de1b79154 | 55 | free(vec); |
Azure.IoT Build | 0:fa2de1b79154 | 56 | } |
Azure.IoT Build | 0:fa2de1b79154 | 57 | } |
Azure.IoT Build | 0:fa2de1b79154 | 58 | |
Azure.IoT Build | 0:fa2de1b79154 | 59 | /* insertion */ |
Azure.IoT Build | 0:fa2de1b79154 | 60 | int VECTOR_push_back(VECTOR_HANDLE handle, const void* elements, size_t numElements) |
Azure.IoT Build | 0:fa2de1b79154 | 61 | { |
Azure.IoT Build | 0:fa2de1b79154 | 62 | int result; |
Azure.IoT Build | 0:fa2de1b79154 | 63 | if (handle == NULL || elements == NULL || numElements == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 64 | { |
Azure.IoT Build | 0:fa2de1b79154 | 65 | result = __LINE__; |
Azure.IoT Build | 0:fa2de1b79154 | 66 | } |
Azure.IoT Build | 0:fa2de1b79154 | 67 | else |
Azure.IoT Build | 0:fa2de1b79154 | 68 | { |
Azure.IoT Build | 0:fa2de1b79154 | 69 | VECTOR* vec = (VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 70 | const size_t curSize = vec->elementSize * vec->count; |
Azure.IoT Build | 0:fa2de1b79154 | 71 | const size_t appendSize = vec->elementSize * numElements; |
Azure.IoT Build | 0:fa2de1b79154 | 72 | |
Azure.IoT Build | 0:fa2de1b79154 | 73 | void* temp = realloc(vec->storage, curSize + appendSize); |
Azure.IoT Build | 0:fa2de1b79154 | 74 | if (temp == NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 75 | { |
Azure.IoT Build | 0:fa2de1b79154 | 76 | result = __LINE__; |
Azure.IoT Build | 0:fa2de1b79154 | 77 | } |
Azure.IoT Build | 0:fa2de1b79154 | 78 | else |
Azure.IoT Build | 0:fa2de1b79154 | 79 | { |
Azure.IoT Build | 0:fa2de1b79154 | 80 | memcpy((unsigned char*)temp + curSize, elements, appendSize); |
Azure.IoT Build | 0:fa2de1b79154 | 81 | vec->storage = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 82 | vec->count += numElements; |
Azure.IoT Build | 0:fa2de1b79154 | 83 | result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 84 | } |
Azure.IoT Build | 0:fa2de1b79154 | 85 | } |
Azure.IoT Build | 0:fa2de1b79154 | 86 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 87 | } |
Azure.IoT Build | 0:fa2de1b79154 | 88 | |
Azure.IoT Build | 0:fa2de1b79154 | 89 | /* removal */ |
Azure.IoT Build | 0:fa2de1b79154 | 90 | void VECTOR_erase(VECTOR_HANDLE handle, void* elements, size_t numElements) |
Azure.IoT Build | 0:fa2de1b79154 | 91 | { |
Azure.IoT Build | 0:fa2de1b79154 | 92 | if (handle != NULL && elements != NULL && numElements > 0) |
Azure.IoT Build | 0:fa2de1b79154 | 93 | { |
Azure.IoT Build | 0:fa2de1b79154 | 94 | VECTOR* vec = (VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 95 | unsigned char* src = (unsigned char*)elements + (vec->elementSize * numElements); |
Azure.IoT Build | 0:fa2de1b79154 | 96 | unsigned char* srcEnd = (unsigned char*)vec->storage + (vec->elementSize * vec->count); |
Azure.IoT Build | 0:fa2de1b79154 | 97 | (void)memmove(elements, src, srcEnd - src); |
Azure.IoT Build | 0:fa2de1b79154 | 98 | vec->count -= numElements; |
Azure.IoT Build | 0:fa2de1b79154 | 99 | if (vec->count == 0) |
Azure.IoT Build | 0:fa2de1b79154 | 100 | { |
Azure.IoT Build | 0:fa2de1b79154 | 101 | free(vec->storage); |
Azure.IoT Build | 0:fa2de1b79154 | 102 | vec->storage = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 103 | } |
Azure.IoT Build | 0:fa2de1b79154 | 104 | else |
Azure.IoT Build | 0:fa2de1b79154 | 105 | { |
Azure.IoT Build | 0:fa2de1b79154 | 106 | vec->storage = realloc(vec->storage, (vec->elementSize * vec->count)); |
Azure.IoT Build | 0:fa2de1b79154 | 107 | } |
Azure.IoT Build | 0:fa2de1b79154 | 108 | } |
Azure.IoT Build | 0:fa2de1b79154 | 109 | } |
Azure.IoT Build | 0:fa2de1b79154 | 110 | |
Azure.IoT Build | 0:fa2de1b79154 | 111 | void VECTOR_clear(VECTOR_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 112 | { |
Azure.IoT Build | 0:fa2de1b79154 | 113 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 114 | { |
Azure.IoT Build | 0:fa2de1b79154 | 115 | VECTOR* vec = (VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 116 | internal_VECTOR_clear(vec); |
Azure.IoT Build | 0:fa2de1b79154 | 117 | } |
Azure.IoT Build | 0:fa2de1b79154 | 118 | } |
Azure.IoT Build | 0:fa2de1b79154 | 119 | |
Azure.IoT Build | 0:fa2de1b79154 | 120 | /* access */ |
Azure.IoT Build | 0:fa2de1b79154 | 121 | |
Azure.IoT Build | 0:fa2de1b79154 | 122 | void* VECTOR_element(const VECTOR_HANDLE handle, size_t index) |
Azure.IoT Build | 0:fa2de1b79154 | 123 | { |
Azure.IoT Build | 0:fa2de1b79154 | 124 | void* result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 125 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 126 | { |
Azure.IoT Build | 0:fa2de1b79154 | 127 | const VECTOR* vec = (const VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 128 | if (index <= vec->count) |
Azure.IoT Build | 0:fa2de1b79154 | 129 | { |
Azure.IoT Build | 0:fa2de1b79154 | 130 | result = (unsigned char*)vec->storage + (vec->elementSize * index); |
Azure.IoT Build | 0:fa2de1b79154 | 131 | } |
Azure.IoT Build | 0:fa2de1b79154 | 132 | } |
Azure.IoT Build | 0:fa2de1b79154 | 133 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 134 | } |
Azure.IoT Build | 0:fa2de1b79154 | 135 | |
Azure.IoT Build | 0:fa2de1b79154 | 136 | void* VECTOR_front(const VECTOR_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 137 | { |
Azure.IoT Build | 0:fa2de1b79154 | 138 | void* result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 139 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 140 | { |
Azure.IoT Build | 0:fa2de1b79154 | 141 | const VECTOR* vec = (const VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 142 | result = vec->storage; |
Azure.IoT Build | 0:fa2de1b79154 | 143 | } |
Azure.IoT Build | 0:fa2de1b79154 | 144 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 145 | } |
Azure.IoT Build | 0:fa2de1b79154 | 146 | |
Azure.IoT Build | 0:fa2de1b79154 | 147 | void* VECTOR_back(const VECTOR_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 148 | { |
Azure.IoT Build | 0:fa2de1b79154 | 149 | void* result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 150 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 151 | { |
Azure.IoT Build | 0:fa2de1b79154 | 152 | const VECTOR* vec = (const VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 153 | result = (unsigned char*)vec->storage + (vec->elementSize * (vec->count - 1)); |
Azure.IoT Build | 0:fa2de1b79154 | 154 | } |
Azure.IoT Build | 0:fa2de1b79154 | 155 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 156 | } |
Azure.IoT Build | 0:fa2de1b79154 | 157 | |
Azure.IoT Build | 0:fa2de1b79154 | 158 | void* VECTOR_find_if(const VECTOR_HANDLE handle, PREDICATE_FUNCTION pred, const void* value) |
Azure.IoT Build | 0:fa2de1b79154 | 159 | { |
Azure.IoT Build | 0:fa2de1b79154 | 160 | void* result = NULL; |
Azure.IoT Build | 0:fa2de1b79154 | 161 | size_t i; |
Azure.IoT Build | 0:fa2de1b79154 | 162 | VECTOR* handleData = (VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 163 | if (handle != NULL && pred != NULL && value != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 164 | { |
Azure.IoT Build | 0:fa2de1b79154 | 165 | for (i = 0; i < handleData->count; ++i) |
Azure.IoT Build | 0:fa2de1b79154 | 166 | { |
Azure.IoT Build | 0:fa2de1b79154 | 167 | void* elem = (unsigned char*)handleData->storage + (handleData->elementSize * i); |
Azure.IoT Build | 0:fa2de1b79154 | 168 | if (!!pred(elem, value)) |
Azure.IoT Build | 0:fa2de1b79154 | 169 | { |
Azure.IoT Build | 0:fa2de1b79154 | 170 | result = elem; |
Azure.IoT Build | 0:fa2de1b79154 | 171 | break; |
Azure.IoT Build | 0:fa2de1b79154 | 172 | } |
Azure.IoT Build | 0:fa2de1b79154 | 173 | } |
Azure.IoT Build | 0:fa2de1b79154 | 174 | } |
Azure.IoT Build | 0:fa2de1b79154 | 175 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 176 | } |
Azure.IoT Build | 0:fa2de1b79154 | 177 | |
Azure.IoT Build | 0:fa2de1b79154 | 178 | /* capacity */ |
Azure.IoT Build | 0:fa2de1b79154 | 179 | |
Azure.IoT Build | 0:fa2de1b79154 | 180 | size_t VECTOR_size(const VECTOR_HANDLE handle) |
Azure.IoT Build | 0:fa2de1b79154 | 181 | { |
Azure.IoT Build | 0:fa2de1b79154 | 182 | size_t result = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 183 | if (handle != NULL) |
Azure.IoT Build | 0:fa2de1b79154 | 184 | { |
Azure.IoT Build | 0:fa2de1b79154 | 185 | const VECTOR* vec = (const VECTOR*)handle; |
Azure.IoT Build | 0:fa2de1b79154 | 186 | result = vec->count; |
Azure.IoT Build | 0:fa2de1b79154 | 187 | } |
Azure.IoT Build | 0:fa2de1b79154 | 188 | return result; |
Azure.IoT Build | 0:fa2de1b79154 | 189 | } |