feat: adding item at beginning of vector

This commit is contained in:
aZholtikov committed 2026-06-14 10:00:33 +03:00
1 parent 992fabf215
commit 44b05b3be0
7 files changed
+83 -56

No files matched your search

+2 -46
View File
@@ -32,50 +32,6 @@ In the application, add the component:
#include "zh_vector.h" #include "zh_vector.h"
``` ```
## Example ## Examples
Create, add, read, modify and delete items: See Wiki [EN](WIKI_EN.md#usage-examples) | [RU](WIKI_RU.md#примеры-использования)
```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);
}
```
+24 -4
View File
@@ -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() ### zh_vector_push_back()
Adds an element to the end of the vector. Adds an element to the end of the vector.
@@ -228,7 +248,7 @@ void app_main(void)
int val1 = 10; int val1 = 10;
int val2 = 20; int val2 = 20;
int val3 = 30; 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, &val2);
zh_vector_push_back(&int_vector, &val3); zh_vector_push_back(&int_vector, &val3);
printf("Vector size: %d\n", zh_vector_get_size(&int_vector)); printf("Vector size: %d\n", zh_vector_get_size(&int_vector));
@@ -268,7 +288,7 @@ void app_main(void)
// Add struct elements // Add struct elements
my_struct_t item1 = {1, "Item 1", 1.5f}; my_struct_t item1 = {1, "Item 1", 1.5f};
my_struct_t item2 = {2, "Item 2", 2.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); zh_vector_push_back(&struct_vector, &item2);
// Access and modify // Access and modify
my_struct_t *ptr = (my_struct_t *)zh_vector_get_item(&struct_vector, 0); 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)); zh_vector_init(&string_vector, sizeof(buffer));
// Add strings // Add strings
strcpy(buffer, "Hello"); strcpy(buffer, "Hello");
zh_vector_push_back(&string_vector, &buffer); zh_vector_push_front(&string_vector, &buffer);
strcpy(buffer, "World"); strcpy(buffer, "World");
zh_vector_push_back(&string_vector, &buffer); zh_vector_push_back(&string_vector, &buffer);
// Print all strings // 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*
+24 -4
View File
@@ -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() ### zh_vector_push_back()
Добавляет элемент в конец вектора. Добавляет элемент в конец вектора.
@@ -228,7 +248,7 @@ void app_main(void)
int val1 = 10; int val1 = 10;
int val2 = 20; int val2 = 20;
int val3 = 30; 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, &val2);
zh_vector_push_back(&int_vector, &val3); zh_vector_push_back(&int_vector, &val3);
printf("Размер вектора: %d\n", zh_vector_get_size(&int_vector)); 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 item1 = {1, "Item 1", 1.5f};
my_struct_t item2 = {2, "Item 2", 2.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); zh_vector_push_back(&struct_vector, &item2);
// Доступ и модификация // Доступ и модификация
my_struct_t *ptr = (my_struct_t *)zh_vector_get_item(&struct_vector, 0); 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)); zh_vector_init(&string_vector, sizeof(buffer));
// Добавление строк // Добавление строк
strcpy(buffer, "Привет"); strcpy(buffer, "Привет");
zh_vector_push_back(&string_vector, &buffer); zh_vector_push_front(&string_vector, &buffer);
strcpy(buffer, "Мир"); strcpy(buffer, "Мир");
zh_vector_push_back(&string_vector, &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*
+1 -1
View File
@@ -1,6 +1,6 @@
description: ESP32 ESP-IDF component for vector (dynamic array) description: ESP32 ESP-IDF component for vector (dynamic array)
url: https://github.com/aZholtikov/zh_vector url: https://github.com/aZholtikov/zh_vector
version: "1.1.2" version: "1.2.0"
license: "Apache-2.0" license: "Apache-2.0"
maintainers: maintainers:
- "Alexey Zholtikov <alexey.zholtikov@yandex.ru>" - "Alexey Zholtikov <alexey.zholtikov@yandex.ru>"
+10
View File
@@ -54,6 +54,16 @@ extern "C"
*/ */
esp_err_t zh_vector_get_size(zh_vector_t *vector); 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. * @brief Add item at end of vector.
* *
+1 -1
View File
@@ -1 +1 @@
1.1.2 1.2.0
+21
View File
@@ -57,6 +57,27 @@ esp_err_t zh_vector_get_size(zh_vector_t *vector)
return vector->size; 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 esp_err_t zh_vector_push_back(zh_vector_t *vector, void *item) // -V2008
{ {
ZH_LOGI("Adding item to vector begin."); ZH_LOGI("Adding item to vector begin.");