All Projects → eteran → C Vector

eteran / C Vector

Licence: mit
A dynamic array implementation in C similar to the one found in standard C++

Programming Languages

c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to C Vector

CuVec
Unifying Python/C++/CUDA memory: Python buffered array ↔️ `std::vector` ↔️ CUDA managed memory
Stars: ✭ 73 (-68.8%)
Mutual labels:  vector, array
Staticvec
Implements a fixed-capacity stack-allocated Vec alternative backed by an array, using const generics.
Stars: ✭ 236 (+0.85%)
Mutual labels:  vector, array
deque
A highly optimized double-ended queue
Stars: ✭ 75 (-67.95%)
Mutual labels:  vector, array
BitLens
🔎 Have your bits and eat them too! A C++17 bit lens container for vector types.
Stars: ✭ 20 (-91.45%)
Mutual labels:  vector, array
rrbit-js
No description or website provided.
Stars: ✭ 11 (-95.3%)
Mutual labels:  vector, array
Graphical Debugging
GraphicalDebugging extension for Visual Studio
Stars: ✭ 88 (-62.39%)
Mutual labels:  vector, array
Blitz
Blitz++ Multi-Dimensional Array Library for C++
Stars: ✭ 257 (+9.83%)
Mutual labels:  vector, array
Containers
This library provides various containers. Each container has utility functions to manipulate the data it holds. This is an abstraction as to not have to manually manage and reallocate memory.
Stars: ✭ 125 (-46.58%)
Mutual labels:  vector, array
Python Geospatial
A collection of Python packages for geospatial analysis with binder-ready notebook examples
Stars: ✭ 187 (-20.09%)
Mutual labels:  vector
Earthenterprise
Google Earth Enterprise - Open Source
Stars: ✭ 2,425 (+936.32%)
Mutual labels:  vector
Immutable
Thread-safe, persistent, immutable collections for the Crystal language
Stars: ✭ 179 (-23.5%)
Mutual labels:  vector
Richpath
💪 Rich Android Path. 🤡 Draw as you want. 🎉 Animate much as you can.
Stars: ✭ 2,259 (+865.38%)
Mutual labels:  vector
Android Animation Set
📚 Android 所有动画系列详尽教程。 Explain all animations in Android.
Stars: ✭ 2,452 (+947.86%)
Mutual labels:  vector
Awesome Design
🌟 Curated design resources from all over the world.
Stars: ✭ 13,333 (+5597.86%)
Mutual labels:  vector
Twitterlaunchanimation
The library with Twitter-like launch animation
Stars: ✭ 221 (-5.56%)
Mutual labels:  vector
Feathericon
simply generic vector icon collection - including sketch file, svg files, and font files.
Stars: ✭ 178 (-23.93%)
Mutual labels:  vector
Nalgebra
Linear algebra library for Rust.
Stars: ✭ 2,433 (+939.74%)
Mutual labels:  vector
Core
Elm's core libraries
Stars: ✭ 2,634 (+1025.64%)
Mutual labels:  array
Static Frame
Immutable and grow-only Pandas-like DataFrames with a more explicit and consistent interface.
Stars: ✭ 217 (-7.26%)
Mutual labels:  array
Pymilvus
Python SDK for Milvus.
Stars: ✭ 199 (-14.96%)
Mutual labels:  vector

This is an implementation of a std::vector like growable array, but in plain C89 code. The result is a type safe, easy to use, dynamic array that has a familiar set of operations.

It works by using the same trick as many allocators, which is to slightly allocate more data than requested, and using that extra padding in the front as storage for meta-data. Thus any non-null vector looks like this in memory:

+------+----------+---------+
| size | capacity | data... |
+------+----------+---------+
                  ^
                  | user's pointer

Where the user is given a pointer to first element of data. This way the code has trivial access to the necessary meta-data, but the user need not be concerned with these details. The total overhead is 2 * sizeof(size_t) per vector.

To allow the code to be maximally generic, it is implemented as all macros, and is thus header only. Usage is simple:

/* if this is defined, then the vector will double in capacity each
 * time it runs out of space. if it is not defined, then the vector will
 * be conservative, and will have a capcity no larger than necessary.
 * having this defined will minimize how often realloc gets called.
 */
#define CVECTOR_LOGARITHMIC_GROWTH

#include "cvector.h"
#include <stdio.h>

int main(int argc, char *argv[]) {

	/* this is the variable that will store the array, you can have
	 * a vector of any type! For example, you may write float *v = NULL,
	 * and you'd have a vector of floats :-). NULL will have a size
	 * and capacity of 0. additionally, vector_begin and vector_end will
	 * return NULL on a NULL vector. Alternatively, for clarity of writing
	 * use can use the cvector_vector_type macro to define a vector of a
	 * given type.
	 */
	cvector_vector_type(int) v = NULL;

	(void)argc;
	(void)argv;

	/* add some elements to the back */
	cvector_push_back(v, 10);
	cvector_push_back(v, 20);
	cvector_push_back(v, 30);

	/* and remove one too */
	cvector_pop_back(v);

	/* print out some stats about the vector */
	printf("pointer : %p\n", (void *)v);
	printf("capacity: %lu\n", cvector_capacity(v));
	printf("size    : %lu\n", cvector_size(v));

	/* iterator over the vector using "iterator" style */
	if (v) {
		int *it;
		int i = 0;
		for (it = cvector_begin(v); it != cvector_end(v); ++it) {
			printf("v[%d] = %d\n", i, *it);
			++i;
		}
	}

	/* iterator over the vector standard indexing too! */
	if (v) {
		size_t i;
		for (i = 0; i < cvector_size(v); ++i) {
			printf("v[%lu] = %d\n", i, v[i]);
		}
	}

	/* well, we don't have destructors, so let's clean things up */
	cvector_free(v);

	return 0;
}

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].