From 1632cb4d28e8cec4329ac568e2fe418d68405edd Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Sun, 19 Jul 2026 17:51:09 +0300 Subject: [PATCH] feat: added deletion of the first or last element --- WIKI_EN.md | 39 ++++++++++++++++++++++++++++++++++++++- WIKI_RU.md | 39 ++++++++++++++++++++++++++++++++++++++- idf_component.yml | 2 +- include/zh_vector.h | 32 ++++++++++++++++++++++++++++++++ version.txt | 2 +- zh_vector.c | 26 +++++++++++++++++++++++++- 6 files changed, 135 insertions(+), 5 deletions(-) diff --git a/WIKI_EN.md b/WIKI_EN.md index 305655b..b41eb50 100644 --- a/WIKI_EN.md +++ b/WIKI_EN.md @@ -260,6 +260,43 @@ Deletes an element at a specific index and shifts all subsequent elements. --- +### zh_vector_delete_back() + +Removes the last element from the vector. + +**Parameters:** + +- `vector` - Pointer to pointer to vector structure (`zh_vector_t **`). Must not be NULL. + +**Returns:** + +- `ESP_OK` - Success +- `ESP_ERR_INVALID_ARG` - Invalid argument (NULL vector pointer or NULL vector pointer or vector is empty) +- `ESP_ERR_INVALID_STATE` - Failed to acquire mutex (rare system error) + +**Note:** The deleted item's memory is freed. If the size drops below half of the capacity, the capacity is reduced (same behaviour as zh_vector_delete_item). + +--- + +### zh_vector_delete_front() + +Removes the first element from the vector. + +**Parameters:** + +- `vector` - Pointer to pointer to vector structure (`zh_vector_t **`). Must not be NULL. + +**Returns:** + +- `ESP_OK` - Success +- `ESP_ERR_INVALID_ARG` - Invalid argument (NULL vector pointer or NULL vector pointer or vector is empty) +- `ESP_ERR_NO_MEM` - Memory allocation failed (during reallocation) +- `ESP_ERR_INVALID_STATE` - Failed to acquire mutex (rare system error) + +**Note:** The deleted item's memory is freed, and all subsequent elements are shifted left by one position. If the size drops below half of the capacity, the capacity is reduced (same behaviour as zh_vector_delete_item). + +--- + ### zh_vector_find_item() Finds the first occurrence of an element in the vector. @@ -734,4 +771,4 @@ limitations under the License. --- -*Generated for zh_vector v2.4.1* +*Generated for zh_vector v2.5.0* diff --git a/WIKI_RU.md b/WIKI_RU.md index f05767c..dfc814d 100644 --- a/WIKI_RU.md +++ b/WIKI_RU.md @@ -260,6 +260,43 @@ if (ret != ESP_OK) { --- +### zh_vector_delete_back() + +Удаляет последний элемент из вектора. + +**Параметры:** + +- `vector` - Указатель на указатель на структуру вектора (`zh_vector_t **`). Не должен быть NULL. + +**Возвращает:** + +- `ESP_OK` - Успех +- `ESP_ERR_INVALID_ARG` - Неверный аргумент (NULL указатель на указатель на вектор или NULL указатель на вектор или вектор пуст) +- `ESP_ERR_INVALID_STATE` - Не удалось захватить mutex (редкий системный сбой) + +**Примечание:** Память удаленного элемента освобождается. Если размер падает ниже половины емкости, емкость уменьшается (такое же поведение, как в zh_vector_delete_item). + +--- + +### zh_vector_delete_front() + +Удаляет первый элемент из вектора. + +**Параметры:** + +- `vector` - Указатель на указатель на структуру вектора (`zh_vector_t **`). Не должен быть NULL. + +**Возвращает:** + +- `ESP_OK` - Успех +- `ESP_ERR_INVALID_ARG` - Неверный аргумент (NULL указатель на указатель на вектор или NULL указатель на вектор или вектор пуст) +- `ESP_ERR_NO_MEM` - Ошибка выделения памяти (при перераспределении) +- `ESP_ERR_INVALID_STATE` - Не удалось захватить mutex (редкий системный сбой) + +**Примечание:** Память удаленного элемента освобождается, и все последующие элементы сдвигаются влево на одну позицию. Если размер падает ниже половины емкости, емкость уменьшается (такое же поведение, как в zh_vector_delete_item). + +--- + ### zh_vector_find_item() Находит первое вхождение элемента в векторе. @@ -734,4 +771,4 @@ void app_main(void) --- -*Сгенерировано для zh_vector v2.4.1* +*Сгенерировано для zh_vector v2.5.0* diff --git a/idf_component.yml b/idf_component.yml index 6025522..f39a2a5 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.1" +version: "2.5.0" license: "Apache-2.0" maintainers: - "Alexey Zholtikov " diff --git a/include/zh_vector.h b/include/zh_vector.h index 00799cf..836172a 100644 --- a/include/zh_vector.h +++ b/include/zh_vector.h @@ -191,6 +191,38 @@ extern "C" */ esp_err_t zh_vector_delete_item(zh_vector_t **vector, uint16_t index); + /** + * @brief Removes the last element from the vector. + * + * The element's memory is freed. If the size drops below half of the capacity, + * the capacity is reduced (same behaviour as zh_vector_delete_item). + * + * @note Thread-safe: internally locks/unlocks the mutex. + * + * @param[in,out] vector Double pointer to vector structure (must be initialized). + * + * @return ESP_OK on success. + * @return ESP_ERR_INVALID_ARG if `vector == NULL` or `*vector == NULL` (not initialized) or vector is empty. + * @return ESP_ERR_INVALID_STATE if the internal mutex cannot be acquired. + */ + esp_err_t zh_vector_delete_back(zh_vector_t **vector); + + /** + * @brief Removes the first element from the vector. + * + * The element's memory is freed, and all subsequent elements are shifted left by one. + * If the size drops below half of the capacity, the capacity is reduced (same behaviour as zh_vector_delete_item). + * + * @note Thread-safe: internally locks/unlocks the mutex. + * + * @param[in,out] vector Double pointer to vector structure (must be initialized). + * + * @return ESP_OK on success. + * @return ESP_ERR_INVALID_ARG if `vector == NULL` or `*vector == NULL` (not initialized) or vector is empty. + * @return ESP_ERR_INVALID_STATE if the internal mutex cannot be acquired. + */ + esp_err_t zh_vector_delete_front(zh_vector_t **vector); + /** * @brief Removes duplicate items from the vector, keeping only the first occurrence of each value. * diff --git a/version.txt b/version.txt index 58073ef..fad066f 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -2.4.1 \ No newline at end of file +2.5.0 \ No newline at end of file diff --git a/zh_vector.c b/zh_vector.c index 1ebcbb2..72ef5bb 100644 --- a/zh_vector.c +++ b/zh_vector.c @@ -214,6 +214,30 @@ esp_err_t zh_vector_delete_item(zh_vector_t **vector, uint16_t index) // -V2008 return ESP_OK; } +esp_err_t zh_vector_delete_back(zh_vector_t **vector) // -V2008 +{ + ZH_LOGI("Deleting item in back begin."); + ZH_ERROR_CHECK(vector != NULL && *vector != NULL, ESP_ERR_INVALID_ARG, NULL, "Deleting item in back failed. Invalid argument."); + ZH_ERROR_CHECK(xSemaphoreTake((*vector)->mutex, portMAX_DELAY) == pdTRUE, ESP_ERR_INVALID_STATE, NULL, "Deleting item in back failed. Failed to acquire mutex."); + ZH_ERROR_CHECK((*vector)->size > 0, ESP_ERR_INVALID_ARG, xSemaphoreGive((*vector)->mutex), "Deleting item in back failed. Vector is empty."); + ZH_ERROR_CHECK(_delete(*vector, (*vector)->size - 1) == ESP_OK, ESP_ERR_INVALID_STATE, xSemaphoreGive((*vector)->mutex), "Deleting item in back failed. Internal error."); + xSemaphoreGive((*vector)->mutex); + ZH_LOGI("Deleting item in back success."); + return ESP_OK; +} + +esp_err_t zh_vector_delete_front(zh_vector_t **vector) // -V2008 +{ + ZH_LOGI("Deleting item in front begin."); + ZH_ERROR_CHECK(vector != NULL && *vector != NULL, ESP_ERR_INVALID_ARG, NULL, "Deleting item in front failed. Invalid argument."); + ZH_ERROR_CHECK(xSemaphoreTake((*vector)->mutex, portMAX_DELAY) == pdTRUE, ESP_ERR_INVALID_STATE, NULL, "Deleting item in front failed. Failed to acquire mutex."); + ZH_ERROR_CHECK((*vector)->size > 0, ESP_ERR_INVALID_ARG, xSemaphoreGive((*vector)->mutex), "Deleting item in front failed. Vector is empty."); + ZH_ERROR_CHECK(_delete(*vector, 0) == ESP_OK, ESP_ERR_INVALID_STATE, xSemaphoreGive((*vector)->mutex), "Deleting item in front failed. Internal error."); + xSemaphoreGive((*vector)->mutex); + ZH_LOGI("Deleting item in front success."); + return ESP_OK; +} + esp_err_t zh_vector_remove_duplicates(zh_vector_t **vector) // -V2008 { ZH_LOGI("Removing duplicates from vector begin."); @@ -263,7 +287,7 @@ esp_err_t zh_vector_find_item(zh_vector_t **vector, const void *item, int32_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, int32_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) // -V2008 { 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.");