# vec **Repository Path**: MacRsh/vec ## Basic Information - **Project Name**: vec - **Description**: No description available - **Primary Language**: C - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 1 - **Created**: 2024-08-31 - **Last Updated**: 2025-08-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Vec [中文文档](README.md) This project contains a simple implementation of a dynamic array (vector), providing basic vector operations. ## Features - **Memory Management**: Provides functions to initialize, deinitialize, and delete vectors. - **Basic Operations**: Supports operations like insertion, deletion, and element retrieval. - **Dynamic Adjustment**: Automatically adjusts capacity to accommodate more elements. ## Usage To use this vector implementation, include this project in your work and include the header file `vec.h`. ## Functions ### Initialization and Destruction - `int vec_init(Vec_t *vec, size_t capacity)`: Initialize the vector. - `void vec_deinit(Vec_t *vec)`: Deinitialize the vector. - `Vec_t *vec_new(size_t capacity)`: Create a new vector. - `void vec_del(Vec_t *vec)`: Destroy the vector. ### Basic Operations - `int vec_push(Vec_t *vec, void *data)`: Insert data at the end of the vector. - `void *vec_pop(Vec_t *vec)`: Remove and return data from the end of the vector. - `int vec_insert(Vec_t *vec, size_t index, void *data)`: Insert data at a specified index. - `void *vec_remove(Vec_t *vec, size_t index)`: Remove and return data from a specified index. - `void *vec_get(Vec_t *vec, size_t index)`: Get data at a specified index. - `void *vec_first(Vec_t *vec)`: Get the first data in the vector. - `void *vec_last(Vec_t *vec)`: Get the last data in the vector. ### Utility Functions - `void vec_clear(Vec_t *vec)`: Clear the vector. - `size_t vec_len(Vec_t *vec)`: Return the number of elements in the vector. - `bool vec_is_empty(Vec_t *vec)`: Check if the vector is empty. - `size_t vec_capacity(Vec_t *vec)`: Return the capacity of the vector. - `int vec_reserve(Vec_t *vec, size_t reserve_size)`: Adjust vector capacity to reserve extra space. - `int vec_shrink_to(Vec_t *vec, size_t capacity)`: Shrink vector capacity to a specified size. - `int vec_shrink_to_fit(Vec_t *vec)`: Shrink vector capacity to fit the current number of elements. ## Simple Example ```c #include #include int main(void) { /* Create vector */ Vec_t *vec = vec_new(10); /* Insert data */ vec_push(vec, "data1"); vec_push(vec, "data2"); /* Get first data */ void *data = vec_first(vec); printf("First data: %s\n", (char *)data); /* Traverse vector */ for (size_t i = 0; i < vec_len(vec); i++) { printf("Traverse vector: %s\n", (char *)vec_get(vec, i)); } /* Destroy vector */ vec_del(vec); return 0; } ``` ## License This project is licensed under the Apache-2.0 License. For more details, please refer to the LICENSE file. ## Author (c) 2024, MacRsh