diff --git a/README.md b/README.md index e2ab5d3..5b88a9a 100644 --- a/README.md +++ b/README.md @@ -32,50 +32,6 @@ In the application, add the component: #include "zh_vector.h" ``` -## Example +## Examples -Create, add, read, modify and delete items: - -```c -#include "zh_vector.h" - -zh_vector_t vector = {0}; - -char example[10] = {0}; - -void app_main(void) -{ - esp_log_level_set("zh_vector", ESP_LOG_ERROR); - zh_vector_init(&vector, sizeof(example)); - printf("Initial vector size is: %d\n", zh_vector_get_size(&vector)); - strcpy(example, "Item 1"); - zh_vector_push_back(&vector, &example); - strcpy(example, "Item 2"); - zh_vector_push_back(&vector, &example); - strcpy(example, "Item 3"); - zh_vector_push_back(&vector, &example); - strcpy(example, "Item 4"); - zh_vector_push_back(&vector, &example); - strcpy(example, "Item 5"); - zh_vector_push_back(&vector, &example); - printf("Add 5 items. New vector size is: %d\n", zh_vector_get_size(&vector)); - for (uint16_t i = 0; i < zh_vector_get_size(&vector); ++i) - { - printf("Item position %d is: %s\n", i, (char *)zh_vector_get_item(&vector, i)); - } - strcpy(example, "Item 6"); - zh_vector_change_item(&vector, 3, &example); - printf("Change item on 3 position.\n"); - for (uint16_t i = 0; i < zh_vector_get_size(&vector); ++i) - { - printf("Item position %d is: %s\n", i, (char *)zh_vector_get_item(&vector, i)); - } - zh_vector_delete_item(&vector, 2); - printf("Delete item on 2 position. New vector size is: %d\n", zh_vector_get_size(&vector)); - for (uint16_t i = 0; i < zh_vector_get_size(&vector); ++i) - { - printf("Item position %d is: %s\n", i, (char *)zh_vector_get_item(&vector, i)); - } - zh_vector_free(&vector); -} -``` +See Wiki [EN](WIKI_EN.md#usage-examples) | [RU](WIKI_RU.md#примеры-использования) diff --git a/WIKI_EN.md b/WIKI_EN.md index c184344..775a1a8 100644 --- a/WIKI_EN.md +++ b/WIKI_EN.md @@ -133,6 +133,26 @@ Gets the current number of elements in the vector. --- +### zh_vector_push_front() + +Adds an element to the beginning of the vector. + +**Parameters:** + +- `vector` - Pointer to the vector structure +- `item` - Pointer to the element to add + +**Returns:** + +- `ESP_OK` - Success +- `ESP_ERR_INVALID_ARG` - Invalid argument (NULL vector or item) +- `ESP_ERR_INVALID_STATE` - Vector not initialized +- `ESP_ERR_NO_MEM` - Memory allocation failed + +**Note:** The function allocates memory for a copy of the item and copies the data. All existing elements are shifted right by one position. + +--- + ### zh_vector_push_back() Adds an element to the end of the vector. @@ -228,7 +248,7 @@ void app_main(void) int val1 = 10; int val2 = 20; int val3 = 30; - zh_vector_push_back(&int_vector, &val1); + zh_vector_push_front(&int_vector, &val1); zh_vector_push_back(&int_vector, &val2); zh_vector_push_back(&int_vector, &val3); printf("Vector size: %d\n", zh_vector_get_size(&int_vector)); @@ -268,7 +288,7 @@ void app_main(void) // Add struct elements my_struct_t item1 = {1, "Item 1", 1.5f}; my_struct_t item2 = {2, "Item 2", 2.5f}; - zh_vector_push_back(&struct_vector, &item1); + zh_vector_push_front(&struct_vector, &item1); zh_vector_push_back(&struct_vector, &item2); // Access and modify my_struct_t *ptr = (my_struct_t *)zh_vector_get_item(&struct_vector, 0); @@ -294,7 +314,7 @@ void app_main(void) zh_vector_init(&string_vector, sizeof(buffer)); // Add strings strcpy(buffer, "Hello"); - zh_vector_push_back(&string_vector, &buffer); + zh_vector_push_front(&string_vector, &buffer); strcpy(buffer, "World"); zh_vector_push_back(&string_vector, &buffer); // Print all strings @@ -384,4 +404,4 @@ limitations under the License. --- -*Generated for zh_vector v1.1.2* +*Generated for zh_vector v1.2.0* diff --git a/WIKI_RU.md b/WIKI_RU.md index 55d55c8..460d74d 100644 --- a/WIKI_RU.md +++ b/WIKI_RU.md @@ -133,6 +133,26 @@ zh_vector_init(&vector, sizeof(int)); // Для целых чисел --- +### zh_vector_push_front() + +Добавляет элемент в начало вектора. + +**Параметры:** + +- `vector` - Указатель на структуру вектора +- `item` - Указатель на элемент для добавления + +**Возвращает:** + +- `ESP_OK` - Успех +- `ESP_ERR_INVALID_ARG` - Неверный аргумент (NULL вектор или item) +- `ESP_ERR_INVALID_STATE` - Вектор не инициализирован +- `ESP_ERR_NO_MEM` - Ошибка выделения памяти + +**Примечание:** Функция выделяет память для копии элемента и копирует данные. Все существующие элементы сдвигаются на одну позицию вправо. + +--- + ### zh_vector_push_back() Добавляет элемент в конец вектора. @@ -228,7 +248,7 @@ void app_main(void) int val1 = 10; int val2 = 20; int val3 = 30; - zh_vector_push_back(&int_vector, &val1); + zh_vector_push_front(&int_vector, &val1); zh_vector_push_back(&int_vector, &val2); zh_vector_push_back(&int_vector, &val3); printf("Размер вектора: %d\n", zh_vector_get_size(&int_vector)); @@ -268,7 +288,7 @@ void app_main(void) // Добавление элементов-структур my_struct_t item1 = {1, "Item 1", 1.5f}; my_struct_t item2 = {2, "Item 2", 2.5f}; - zh_vector_push_back(&struct_vector, &item1); + zh_vector_push_front(&struct_vector, &item1); zh_vector_push_back(&struct_vector, &item2); // Доступ и модификация my_struct_t *ptr = (my_struct_t *)zh_vector_get_item(&struct_vector, 0); @@ -294,7 +314,7 @@ void app_main(void) zh_vector_init(&string_vector, sizeof(buffer)); // Добавление строк strcpy(buffer, "Привет"); - zh_vector_push_back(&string_vector, &buffer); + zh_vector_push_front(&string_vector, &buffer); strcpy(buffer, "Мир"); zh_vector_push_back(&string_vector, &buffer); // Печать всех строк @@ -384,4 +404,4 @@ void app_main(void) --- -*Сгенерировано для zh_vector v1.1.2* +*Сгенерировано для zh_vector v1.2.0* diff --git a/idf_component.yml b/idf_component.yml index f6d9c94..a513dba 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: "1.1.2" +version: "1.2.0" license: "Apache-2.0" maintainers: - "Alexey Zholtikov " diff --git a/include/zh_vector.h b/include/zh_vector.h index a7dd37d..e38ca91 100644 --- a/include/zh_vector.h +++ b/include/zh_vector.h @@ -54,6 +54,16 @@ extern "C" */ esp_err_t zh_vector_get_size(zh_vector_t *vector); + /** + * @brief Add item at beginning of vector. + * + * @param[in] vector Pointer to main structure of vector data. + * @param[in] item Pointer to item for add. + * + * @return ESP_OK if success or an error code otherwise. + */ + esp_err_t zh_vector_push_front(zh_vector_t *vector, void *item); + /** * @brief Add item at end of vector. * diff --git a/version.txt b/version.txt index 8428158..867e524 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.1.2 \ No newline at end of file +1.2.0 \ No newline at end of file diff --git a/zh_vector.c b/zh_vector.c index 2d73ceb..519f346 100644 --- a/zh_vector.c +++ b/zh_vector.c @@ -57,6 +57,27 @@ esp_err_t zh_vector_get_size(zh_vector_t *vector) return vector->size; } +esp_err_t zh_vector_push_front(zh_vector_t *vector, void *item) // -V2008 +{ + ZH_LOGI("Adding item to beginning of vector begin."); + ZH_ERROR_CHECK(vector != NULL && item != NULL, ESP_ERR_INVALID_ARG, NULL, "Adding item to beginning of vector fail. Invalid argument."); + ZH_ERROR_CHECK(vector->is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "Adding item to beginning of vector fail. Vector not initialized."); + if (vector->capacity == vector->size) + { + ZH_ERROR_CHECK(_resize(vector, vector->capacity + 1) == ESP_OK, ESP_ERR_NO_MEM, NULL, "Adding item to beginning of vector fail. Memory allocation fail or no free memory in the heap."); + } + for (uint16_t i = vector->size; i > 0; --i) + { + vector->items[i] = vector->items[i - 1]; + } + vector->items[0] = heap_caps_calloc(1, vector->unit, MALLOC_CAP_8BIT); + ZH_ERROR_CHECK(vector->items[0] != NULL, ESP_ERR_NO_MEM, NULL, "Adding item to beginning of vector fail. Memory allocation fail or no free memory in the heap."); + memcpy(vector->items[0], item, vector->unit); + ++vector->size; + ZH_LOGI("Adding item to beginning of vector success."); + return ESP_OK; +} + esp_err_t zh_vector_push_back(zh_vector_t *vector, void *item) // -V2008 { ZH_LOGI("Adding item to vector begin.");