feat: added deletion of the first or last element

This commit is contained in:
aZholtikov committed 2026-07-19 17:51:09 +03:00
1 parent f46ab3f5fb
commit 1632cb4d28
6 files changed
+135 -5

No files matched your search

+38 -1
View File
@@ -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() ### zh_vector_find_item()
Finds the first occurrence of an element in the vector. 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*
+38 -1
View File
@@ -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() ### zh_vector_find_item()
Находит первое вхождение элемента в векторе. Находит первое вхождение элемента в векторе.
@@ -734,4 +771,4 @@ void app_main(void)
--- ---
*Сгенерировано для zh_vector v2.4.1* *Сгенерировано для zh_vector v2.5.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: "2.4.1" version: "2.5.0"
license: "Apache-2.0" license: "Apache-2.0"
maintainers: maintainers:
- "Alexey Zholtikov <alexey.zholtikov@yandex.ru>" - "Alexey Zholtikov <alexey.zholtikov@yandex.ru>"
+32
View File
@@ -191,6 +191,38 @@ extern "C"
*/ */
esp_err_t zh_vector_delete_item(zh_vector_t **vector, uint16_t index); 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. * @brief Removes duplicate items from the vector, keeping only the first occurrence of each value.
* *
+1 -1
View File
@@ -1 +1 @@
2.4.1 2.5.0
+25 -1
View File
@@ -214,6 +214,30 @@ esp_err_t zh_vector_delete_item(zh_vector_t **vector, uint16_t index) // -V2008
return ESP_OK; 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 esp_err_t zh_vector_remove_duplicates(zh_vector_t **vector) // -V2008
{ {
ZH_LOGI("Removing duplicates from vector begin."); 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; 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_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."); 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.");