Coder Social home page Coder Social logo

vec's Introduction

Vec

The Vec library (written under the POSIX standard) is meant to add generic and fast vectors to C (and is compatible with C++). A "vec" in a sense is a fat pointer to anything. The Vec API not only provides ways of creating, destroying, and manipulating generic fat pointers, but also functionality common to the traditional C++ vector type. Refer to the greatly commented header file for details.

Usage

This library is self contained in one single header file and can be used by simply #including the 'vec.h' file, e.g.:

#include "vec.h"

Also optionally define the symbols listed in the header's subtitled section "OPTIONAL DEFINES" before the #include if you want to use additional functionality or need more control over the library.

API

The following is a list of available functions (all prefixed with "vec_"):

  • new
  • new_cap
  • dup
  • free
  • cap
  • len, size
  • is_empty
  • clear
  • push
  • pop
  • insert
  • remove
  • swap
  • reserve
  • shrink

Example

#include <stdlib.h>
#include <stdio.h>


#define VEC_PRIVATE
#include "vec.h"


struct matrix {
	size_t row;
	size_t col;
	double value;
};


void do_something(struct matrix *ptr) {
	for (size_t c = 0; c < vec_len(ptr); ++c) {
		printf(
			"Index: %zd, Row: %zd, Col: %zd, VecCap: %zd, VecLen: %zd, Value: %.3f\n",
			c, ptr[c].row, ptr[c].col, vec_cap(ptr), vec_len(ptr), ptr[c].value
		);
	}
}


int main(void) {
	struct matrix *foo = vec_new(sizeof(struct matrix));
	
	{
		struct matrix bar = { 1, 1, 1.125 };
		for (int c = 0; c < 10; ++c) {
			vec_push(foo, bar);
			
			if (bar.col == 5) {
				++bar.row;
				bar.col = 1;
			} else {
				++bar.col;
			}

			bar.value *= bar.value;
		}
	}

	do_something(foo);
	vec_free(foo);

    return 0;
}

vec's People

Contributors

kelvyne avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.