diff --git a/WIKI_EN.md b/WIKI_EN.md index 88f32c1..305655b 100644 --- a/WIKI_EN.md +++ b/WIKI_EN.md @@ -337,7 +337,8 @@ void app_main(void) zh_vector_t *vector = NULL; // Initialize vector for integers esp_err_t ret = zh_vector_init(&vector, sizeof(int)); - if (ret != ESP_OK) { + if (ret != ESP_OK) + { printf("Vector initialization error\n"); return; } @@ -348,18 +349,23 @@ void app_main(void) zh_vector_push_front(&vector, &val1); zh_vector_push_back(&vector, &val2); zh_vector_push_back(&vector, &val3); - size_t size; + uint16_t size = 0; ret = zh_vector_get_size(&vector, &size); - if (ret == ESP_OK) { + if (ret == ESP_OK) + { printf("Vector size: %zu\n", size); } // Access elements - for (int i = 0; i < size; i++) { - int item_value; + for (int i = 0; i < size; i++) + { + int item_value = 0; esp_err_t err = zh_vector_get_item(&vector, (uint16_t)i, &item_value); - if (err == ESP_OK) { + if (err == ESP_OK) + { printf("Element %d: %d\n", i, item_value); - } else { + } + else + { printf("Error getting element %d: %s\n", i, esp_err_to_name(err)); } } @@ -380,7 +386,8 @@ void app_main(void) ```c #include "zh_vector.h" -typedef struct { +typedef struct +{ int id; char name[32]; float value; @@ -392,7 +399,8 @@ void app_main(void) zh_vector_t *vector = NULL; // Initialize vector for structs esp_err_t ret = zh_vector_init(&vector, sizeof(my_struct_t)); - if (ret != ESP_OK) { + if (ret != ESP_OK) + { printf("Vector initialization error\n"); return; } @@ -402,9 +410,10 @@ void app_main(void) zh_vector_push_front(&vector, &item1); zh_vector_push_back(&vector, &item2); // Access and modify - my_struct_t item_value; + my_struct_t item_value = {0}; esp_err_t err = zh_vector_get_item(&vector, 0, &item_value); - if (err == ESP_OK) { + if (err == ESP_OK) + { item_value.value = 10.5f; zh_vector_change_item(&vector, 0, &item_value); } @@ -427,7 +436,8 @@ void app_main(void) char buffer[100] = {0}; // Initialize vector for strings (100 char max) esp_err_t ret = zh_vector_init(&vector, sizeof(buffer)); - if (ret != ESP_OK) { + if (ret != ESP_OK) + { printf("Vector initialization error\n"); return; } @@ -436,16 +446,21 @@ void app_main(void) zh_vector_push_front(&vector, &buffer); strcpy(buffer, "World"); zh_vector_push_back(&vector, &buffer); - size_t size; + uint16_t size = 0; ret = zh_vector_get_size(&vector, &size); - if (ret == ESP_OK) { + if (ret == ESP_OK) + { // Print all strings - for (int i = 0; i < size; i++) { - char str_value[100]; + for (int i = 0; i < size; i++) + { + char str_value[100] = {0}; esp_err_t err = zh_vector_get_item(&vector, (uint16_t)i, str_value); - if (err == ESP_OK) { + if (err == ESP_OK) + { printf("String %d: %s\n", i, str_value); - } else { + } + else + { printf("Error getting string %d: %s\n", i, esp_err_to_name(err)); } } @@ -470,7 +485,8 @@ void app_main(void) zh_vector_t *vector = NULL; // Initialize vector for integers esp_err_t ret = zh_vector_init(&vector, sizeof(int)); - if (ret != ESP_OK) { + if (ret != ESP_OK) + { printf("Vector initialization error\n"); return; } @@ -485,24 +501,30 @@ void app_main(void) zh_vector_push_back(&vector, &val3); zh_vector_push_back(&vector, &val4); zh_vector_push_back(&vector, &val5); - uint16_t size; + uint16_t size = 0; ret = zh_vector_get_size(&vector, &size); - if (ret == ESP_OK) { + if (ret == ESP_OK) + { printf("Vector size before removing duplicates: %zu\n", size); } // Remove duplicates zh_vector_remove_duplicates(&vector); ret = zh_vector_get_size(&vector, &size); - if (ret == ESP_OK) { + if (ret == ESP_OK) + { printf("Vector size after removing duplicates: %zu\n", size); } // Access remaining elements - for (int i = 0; i < size; i++) { - int item_value; + for (int i = 0; i < size; ++i) + { + int item_value = 0; esp_err_t err = zh_vector_get_item(&vector, (uint16_t)i, &item_value); - if (err == ESP_OK) { + if (err == ESP_OK) + { printf("Element %d: %d\n", i, item_value); - } else { + } + else + { printf("Error getting element %d: %s\n", i, esp_err_to_name(err)); } } @@ -524,7 +546,8 @@ void app_main(void) zh_vector_t *vector = NULL; // Initialize vector for integers esp_err_t ret = zh_vector_init(&vector, sizeof(int)); - if (ret != ESP_OK) { + if (ret != ESP_OK) + { printf("Vector initialization error\n"); return; } @@ -537,21 +560,27 @@ void app_main(void) zh_vector_push_back(&vector, &val3); // Find element int search_item = 20; - int16_t found_index = -1; + int32_t found_index = -1; ret = zh_vector_find_item(&vector, &search_item, &found_index); - if (ret == ESP_OK) { - printf("Element %d found at index %d\n", search_item, found_index); - } else { - printf("Element %d not found (index=%d)\n", search_item, found_index); + if (ret == ESP_OK) + { + printf("Element %d found at index %ld\n", search_item, found_index); + } + else + { + printf("Element %d not found (index=%ld)\n", search_item, found_index); } // Find non-existing element search_item = 100; found_index = -1; ret = zh_vector_find_item(&vector, &search_item, &found_index); - if (ret == ESP_OK) { - printf("Element %d found at index %d\n", search_item, found_index); - } else { - printf("Element %d not found (index=%d)\n", search_item, found_index); + if (ret == ESP_OK) + { + printf("Element %d found at index %ld\n", search_item, found_index); + } + else + { + printf("Element %d not found (index=%ld)\n", search_item, found_index); } // Cleanup zh_vector_free(&vector); @@ -565,7 +594,8 @@ void app_main(void) ```c #include "zh_vector.h" -typedef struct { +typedef struct +{ int id; char name[32]; float value; @@ -577,7 +607,8 @@ void app_main(void) zh_vector_t *vector = NULL; // Initialize vector for structs esp_err_t ret = zh_vector_init(&vector, sizeof(my_struct_t)); - if (ret != ESP_OK) { + if (ret != ESP_OK) + { printf("Vector initialization error\n"); return; } @@ -590,29 +621,36 @@ void app_main(void) zh_vector_push_back(&vector, &item3); // Find struct by field value (searching by id field) int search_id = 2; - int16_t found_index = -1; + int32_t found_index = -1; // Calculate offset of 'id' field within the structure - my_struct_t sample; + my_struct_t sample = {0}; ret = zh_vector_find_item_in_field(&vector, &sample, &sample.id, sizeof(sample.id), &search_id, 0, &found_index); - if (ret == ESP_OK) { - printf("Struct with id=%d found at index %d\n", search_id, found_index); + if (ret == ESP_OK) + { + printf("Struct with id=%d found at index %ld\n", search_id, found_index); // Get the found struct - my_struct_t found_item; + my_struct_t found_item = {0}; ret = zh_vector_get_item(&vector, (uint16_t)found_index, &found_item); - if (ret == ESP_OK) { + if (ret == ESP_OK) + { printf("Found item: id=%d, name=%s, value=%.1f\n", found_item.id, found_item.name, found_item.value); } - } else { - printf("Struct with id=%d not found (index=%d)\n", search_id, found_index); + } + else + { + printf("Struct with id=%d not found (index=%ld)\n", search_id, found_index); } // Find non-existing id search_id = 100; found_index = -1; ret = zh_vector_find_item_in_field(&vector, &sample, &sample.id, sizeof(sample.id), &search_id, 0, &found_index); - if (ret == ESP_OK) { - printf("Struct with id=%d found at index %d\n", search_id, found_index); - } else { - printf("Struct with id=%d not found (index=%d)\n", search_id, found_index); + if (ret == ESP_OK) + { + printf("Struct with id=%d found at index %ld\n", search_id, found_index); + } + else + { + printf("Struct with id=%d not found (index=%ld)\n", search_id, found_index); } // Cleanup zh_vector_free(&vector); @@ -696,4 +734,4 @@ limitations under the License. --- -*Generated for zh_vector v2.4.0* +*Generated for zh_vector v2.4.1* diff --git a/WIKI_RU.md b/WIKI_RU.md index a42b318..f05767c 100644 --- a/WIKI_RU.md +++ b/WIKI_RU.md @@ -337,7 +337,8 @@ void app_main(void) zh_vector_t *vector = NULL; // Инициализация вектора для целых чисел esp_err_t ret = zh_vector_init(&vector, sizeof(int)); - if (ret != ESP_OK) { + if (ret != ESP_OK) + { printf("Ошибка инициализации вектора\n"); return; } @@ -348,18 +349,23 @@ void app_main(void) zh_vector_push_front(&vector, &val1); zh_vector_push_back(&vector, &val2); zh_vector_push_back(&vector, &val3); - size_t size; + uint16_t size = 0; ret = zh_vector_get_size(&vector, &size); - if (ret == ESP_OK) { + if (ret == ESP_OK) + { printf("Размер вектора: %zu\n", size); } // Доступ к элементам - for (int i = 0; i < size; i++) { - int item_value; + for (int i = 0; i < size; i++) + { + int item_value = 0; esp_err_t err = zh_vector_get_item(&vector, (uint16_t)i, &item_value); - if (err == ESP_OK) { + if (err == ESP_OK) + { printf("Элемент %d: %d\n", i, item_value); - } else { + } + else + { printf("Ошибка получения элемента %d: %s\n", i, esp_err_to_name(err)); } } @@ -380,7 +386,8 @@ void app_main(void) ```c #include "zh_vector.h" -typedef struct { +typedef struct +{ int id; char name[32]; float value; @@ -392,7 +399,8 @@ void app_main(void) zh_vector_t *vector = NULL; // Инициализация вектора для структур esp_err_t ret = zh_vector_init(&vector, sizeof(my_struct_t)); - if (ret != ESP_OK) { + if (ret != ESP_OK) + { printf("Ошибка инициализации вектора\n"); return; } @@ -402,9 +410,10 @@ void app_main(void) zh_vector_push_front(&vector, &item1); zh_vector_push_back(&vector, &item2); // Доступ и модификация - my_struct_t item_value; + my_struct_t item_value = {0}; esp_err_t err = zh_vector_get_item(&vector, 0, &item_value); - if (err == ESP_OK) { + if (err == ESP_OK) + { item_value.value = 10.5f; zh_vector_change_item(&vector, 0, &item_value); } @@ -427,25 +436,31 @@ void app_main(void) char buffer[100] = {0}; // Инициализация вектора для строк (макс. 100 символов) esp_err_t ret = zh_vector_init(&vector, sizeof(buffer)); - if (ret != ESP_OK) { + if (ret != ESP_OK) + { printf("Ошибка инициализации вектора\n"); return; } - // Добавление стро�� + // Добавление строк strcpy(buffer, "Привет"); zh_vector_push_front(&vector, &buffer); strcpy(buffer, "Мир"); zh_vector_push_back(&vector, &buffer); - size_t size; + uint16_t size = 0; ret = zh_vector_get_size(&vector, &size); - if (ret == ESP_OK) { + if (ret == ESP_OK) + { // Печать всех строк - for (int i = 0; i < size; i++) { - char str_value[100]; + for (int i = 0; i < size; i++) + { + char str_value[100] = {0}; esp_err_t err = zh_vector_get_item(&vector, (uint16_t)i, str_value); - if (err == ESP_OK) { + if (err == ESP_OK) + { printf("Строка %d: %s\n", i, str_value); - } else { + } + else + { printf("Ошибка получения строки %d: %s\n", i, esp_err_to_name(err)); } } @@ -470,7 +485,8 @@ void app_main(void) zh_vector_t *vector = NULL; // Инициализация вектора для целых чисел esp_err_t ret = zh_vector_init(&vector, sizeof(int)); - if (ret != ESP_OK) { + if (ret != ESP_OK) + { printf("Ошибка инициализации вектора\n"); return; } @@ -485,24 +501,30 @@ void app_main(void) zh_vector_push_back(&vector, &val3); zh_vector_push_back(&vector, &val4); zh_vector_push_back(&vector, &val5); - uint16_t size; + uint16_t size = 0; ret = zh_vector_get_size(&vector, &size); - if (ret == ESP_OK) { + if (ret == ESP_OK) + { printf("Размер вектора перед удалением дубликатов: %zu\n", size); } // Удаление дубликатов zh_vector_remove_duplicates(&vector); ret = zh_vector_get_size(&vector, &size); - if (ret == ESP_OK) { + if (ret == ESP_OK) + { printf("Размер вектора после удаления дубликатов: %zu\n", size); } // Доступ к оставшимся элементам - for (int i = 0; i < size; i++) { - int item_value; + for (int i = 0; i < size; ++i) + { + int item_value = 0; esp_err_t err = zh_vector_get_item(&vector, (uint16_t)i, &item_value); - if (err == ESP_OK) { + if (err == ESP_OK) + { printf("Элемент %d: %d\n", i, item_value); - } else { + } + else + { printf("Ошибка получения элемента %d: %s\n", i, esp_err_to_name(err)); } } @@ -524,7 +546,8 @@ void app_main(void) zh_vector_t *vector = NULL; // Инициализация вектора для целых чисел esp_err_t ret = zh_vector_init(&vector, sizeof(int)); - if (ret != ESP_OK) { + if (ret != ESP_OK) + { printf("Ошибка инициализации вектора\n"); return; } @@ -537,21 +560,27 @@ void app_main(void) zh_vector_push_back(&vector, &val3); // Поиск элемента int search_item = 20; - int16_t found_index = -1; + int32_t found_index = -1; ret = zh_vector_find_item(&vector, &search_item, &found_index); - if (ret == ESP_OK) { - printf("Элемент %d найден по индексу %d\n", search_item, found_index); - } else { - printf("Элемент %d не найден (index=%d)\n", search_item, found_index); + if (ret == ESP_OK) + { + printf("Элемент %d найден по индексу %ld\n", search_item, found_index); + } + else + { + printf("Элемент %d не найден (index=%ld)\n", search_item, found_index); } // Поиск несуществующего элемента search_item = 100; found_index = -1; ret = zh_vector_find_item(&vector, &search_item, &found_index); - if (ret == ESP_OK) { - printf("Элемент %d найден по индексу %d\n", search_item, found_index); - } else { - printf("Элемент %d не найден (index=%d)\n", search_item, found_index); + if (ret == ESP_OK) + { + printf("Элемент %d найден по индексу %ld\n", search_item, found_index); + } + else + { + printf("Элемент %d не найден (index=%ld)\n", search_item, found_index); } // Очистка zh_vector_free(&vector); @@ -565,7 +594,8 @@ void app_main(void) ```c #include "zh_vector.h" -typedef struct { +typedef struct +{ int id; char name[32]; float value; @@ -577,7 +607,8 @@ void app_main(void) zh_vector_t *vector = NULL; // Инициализация вектора для структур esp_err_t ret = zh_vector_init(&vector, sizeof(my_struct_t)); - if (ret != ESP_OK) { + if (ret != ESP_OK) + { printf("Ошибка инициализации вектора\n"); return; } @@ -590,29 +621,36 @@ void app_main(void) zh_vector_push_back(&vector, &item3); // Поиск структуры по значению поля (поиск по полю id) int search_id = 2; - int16_t found_index = -1; + int32_t found_index = -1; // Вычисление смещения поля 'id' внутри структуры - my_struct_t sample; + my_struct_t sample = {0}; ret = zh_vector_find_item_in_field(&vector, &sample, &sample.id, sizeof(sample.id), &search_id, 0, &found_index); - if (ret == ESP_OK) { - printf("Структура с id=%d найдена по индексу %d\n", search_id, found_index); + if (ret == ESP_OK) + { + printf("Структура с id=%d найдена по индексу %ld\n", search_id, found_index); // Получение найденной структуры - my_struct_t found_item; + my_struct_t found_item = {0}; ret = zh_vector_get_item(&vector, (uint16_t)found_index, &found_item); - if (ret == ESP_OK) { + if (ret == ESP_OK) + { printf("Найденный элемент: id=%d, name=%s, value=%.1f\n", found_item.id, found_item.name, found_item.value); } - } else { - printf("Структура с id=%d не найдена (index=%d)\n", search_id, found_index); + } + else + { + printf("Структура с id=%d не найдена (index=%ld)\n", search_id, found_index); } // Поиск несуществующего id search_id = 100; found_index = -1; ret = zh_vector_find_item_in_field(&vector, &sample, &sample.id, sizeof(sample.id), &search_id, 0, &found_index); - if (ret == ESP_OK) { - printf("Структура с id=%d найдена по индексу %d\n", search_id, found_index); - } else { - printf("Структура с id=%d не найдена (index=%d)\n", search_id, found_index); + if (ret == ESP_OK) + { + printf("Структура с id=%d найдена по индексу %ld\n", search_id, found_index); + } + else + { + printf("Структура с id=%d не найдена (index=%ld)\n", search_id, found_index); } // Очистка zh_vector_free(&vector); @@ -696,4 +734,4 @@ void app_main(void) --- -*Сгенерировано для zh_vector v2.4.0* +*Сгенерировано для zh_vector v2.4.1* diff --git a/idf_component.yml b/idf_component.yml index 8d7053a..6025522 100644 --- a/idf_component.yml +++ b/idf_component.yml @@ -1,6 +1,6 @@ description: ESP32 ESP-IDF component for vector (dynamic array) url: https://github.com/aZholtikov/zh_vector -version: "2.4.0" +version: "2.4.1" license: "Apache-2.0" maintainers: - "Alexey Zholtikov " diff --git a/include/zh_vector.h b/include/zh_vector.h index 75e7eb0..00799cf 100644 --- a/include/zh_vector.h +++ b/include/zh_vector.h @@ -230,7 +230,7 @@ extern "C" * @return ESP_ERR_INVALID_ARG if any pointer argument is `NULL` or `*vector == NULL` (not initialized). * @return ESP_ERR_INVALID_STATE if the internal mutex cannot be acquired (rare, system error). */ - esp_err_t zh_vector_find_item(zh_vector_t **vector, const void *item, int16_t *index); + esp_err_t zh_vector_find_item(zh_vector_t **vector, const void *item, int32_t *index); /** * @brief Searches for an item in a vector of structures by comparing a specific field. @@ -263,7 +263,7 @@ extern "C" * @return ESP_ERR_INVALID_ARG if any pointer argument is NULL, `*vector` is NULL (not initialized), `size == 0`, `start >= vector size`, or the field extends beyond the element size. * @return ESP_ERR_INVALID_STATE if the internal mutex cannot be acquired. */ - esp_err_t zh_vector_find_item_in_field(zh_vector_t **vector, const void *sample_struct, const void *field, size_t size, const void *item, uint16_t start, int16_t *index); + esp_err_t zh_vector_find_item_in_field(zh_vector_t **vector, const void *sample_struct, const void *field, size_t size, const void *item, uint16_t start, int32_t *index); #ifdef __cplusplus } diff --git a/version.txt b/version.txt index 9183195..58073ef 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -2.4.0 \ No newline at end of file +2.4.1 \ No newline at end of file diff --git a/zh_vector.c b/zh_vector.c index 2b5264c..1ebcbb2 100644 --- a/zh_vector.c +++ b/zh_vector.c @@ -242,7 +242,7 @@ esp_err_t zh_vector_remove_duplicates(zh_vector_t **vector) // -V2008 return ESP_OK; } -esp_err_t zh_vector_find_item(zh_vector_t **vector, const void *item, int16_t *index) +esp_err_t zh_vector_find_item(zh_vector_t **vector, const void *item, int32_t *index) { ZH_LOGI("Finding item in vector begin."); ZH_ERROR_CHECK(vector != NULL && *vector != NULL && item != NULL && index != NULL, ESP_ERR_INVALID_ARG, NULL, "Finding item in vector failed. Invalid argument."); @@ -252,7 +252,7 @@ esp_err_t zh_vector_find_item(zh_vector_t **vector, const void *item, int16_t *i { if (memcmp((*vector)->items[i], item, (*vector)->unit) == 0) { - *index = (int16_t)i; + *index = (int32_t)i; xSemaphoreGive((*vector)->mutex); ZH_LOGI("Finding item in vector success (found)."); return ESP_OK; @@ -263,7 +263,7 @@ esp_err_t zh_vector_find_item(zh_vector_t **vector, const void *item, int16_t *i return ESP_ERR_NOT_FOUND; } -esp_err_t zh_vector_find_item_in_field(zh_vector_t **vector, const void *sample_struct, const void *item, size_t size, const void *value, uint16_t start, int16_t *index) +esp_err_t zh_vector_find_item_in_field(zh_vector_t **vector, const void *sample_struct, const void *item, size_t size, const void *value, uint16_t start, int32_t *index) { ZH_LOGI("Finding field in structure begin."); ZH_ERROR_CHECK(vector != NULL && *vector != NULL && sample_struct != NULL && item != NULL && value != NULL && index != NULL && size > 0, ESP_ERR_INVALID_ARG, NULL, "Finding field in structure failed. Invalid argument."); @@ -277,7 +277,7 @@ esp_err_t zh_vector_find_item_in_field(zh_vector_t **vector, const void *sample_ const uint8_t *elem = (const uint8_t *)(*vector)->items[i]; if (memcmp(elem + offset, value, size) == 0) { - *index = (int16_t)i; + *index = (int32_t)i; xSemaphoreGive((*vector)->mutex); ZH_LOGI("Finding field in structure success (found)."); return ESP_OK;