Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sagiegurari/c_vector
A simple growable vector for C
https://github.com/sagiegurari/c_vector
c c-lib c-library data-structures vector
Last synced: about 2 months ago
JSON representation
A simple growable vector for C
- Host: GitHub
- URL: https://github.com/sagiegurari/c_vector
- Owner: sagiegurari
- License: apache-2.0
- Created: 2020-11-07T09:04:58.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2023-09-27T07:12:35.000Z (over 1 year ago)
- Last Synced: 2024-10-15T04:26:56.753Z (3 months ago)
- Topics: c, c-lib, c-library, data-structures, vector
- Language: C
- Homepage:
- Size: 41 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# vector
[![CI](https://github.com/sagiegurari/c_vector/workflows/CI/badge.svg?branch=master)](https://github.com/sagiegurari/c_vector/actions)
[![Release](https://img.shields.io/github/v/release/sagiegurari/c_vector)](https://github.com/sagiegurari/c_vector/releases)
[![license](https://img.shields.io/github/license/sagiegurari/c_vector)](https://github.com/sagiegurari/c_vector/blob/master/LICENSE)> A simple growable vector for C.
* [Overview](#overview)
* [Usage](#usage)
* [Contributing](.github/CONTRIBUTING.md)
* [Release History](CHANGELOG.md)
* [License](#license)
## Overview
This library provides a simple growable vector with many different manipulation functions.```c
#include "vector.h"
#includeint main()
{
// create new vector which can auto grow as needed.
// Can also specify initial size and if allowed to resize using
// the following: vector_new_with_options(size, allow_resize)
struct Vector *vector = vector_new();// populate vector using multiple available functions
vector_push(vector, "test push");
vector_insert(vector, 1, "test insert"); // shifts all items from index 1 forward
vector_prepend(vector, "test prepend");
char *previous_value = (char *)vector_set(vector, 1, "test set"); // replaces the item at index 1
printf("Replaced value at index 1, old value: %s\n", previous_value);// can fetch any item
char *value = (char *)vector_get(vector, 1);
printf("Value at index 1: %s\n", value);
value = (char *)vector_pop(vector);
printf("Last Value: %s\n", value);// or fetch all items
void **all_items = vector_to_array(vector);
printf("First item: %s\n", (char *)all_items[0]);// can remove any item
value = (char *)vector_remove(vector, 0); // shifts all items after index backward
printf("Removed first item: %s\n", value);// modify the vector size
size_t size = vector_size(vector);
size_t capacity = vector_capacity(vector);
printf("Current size: %zu capacity: %zu\n", size, capacity);
vector_shrink(vector);
size = vector_size(vector);
capacity = vector_capacity(vector);
printf("Current size: %zu capacity: %zu\n", size, capacity);
vector_shrink(vector);
size = vector_ensure_capacity(vector, 100);
capacity = vector_capacity(vector);
printf("Current size: %zu capacity: %zu\n", size, capacity);// when we are done with the vector, we release it
vector_release(vector);
} /* main */
```## Contributing
See [contributing guide](.github/CONTRIBUTING.md)See [Changelog](CHANGELOG.md)
## License
Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.