Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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.


## Usage

```c
#include "vector.h"
#include

int 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)


## Release History

See [Changelog](CHANGELOG.md)


## License
Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.