reactor-c
C Runtime for Lingua Franca
Loading...
Searching...
No Matches
vector.c File Reference
#include <stddef.h>
#include <stdlib.h>
#include <assert.h>
#include "vector.h"

Macros

#define REQUIRED_VOTES_TO_SHRINK   15
 
#define CAPACITY_TO_SIZE_RATIO_FOR_SHRINK_VOTE   4
 
#define SCALE_FACTOR   2
 

Functions

vector_t vector_new (size_t initial_capacity)
 
void vector_free (vector_t *v)
 
void vector_push (vector_t *v, void *element)
 
void vector_pushall (vector_t *v, void **array, size_t size)
 
void * vector_pop (vector_t *v)
 
void ** vector_at (vector_t *v, size_t idx)
 
size_t vector_size (vector_t *v)
 Return the size of the vector.
 
void vector_vote (vector_t *v)
 

Macro Definition Documentation

◆ CAPACITY_TO_SIZE_RATIO_FOR_SHRINK_VOTE

#define CAPACITY_TO_SIZE_RATIO_FOR_SHRINK_VOTE   4

◆ REQUIRED_VOTES_TO_SHRINK

#define REQUIRED_VOTES_TO_SHRINK   15

◆ SCALE_FACTOR

#define SCALE_FACTOR   2

Function Documentation

◆ vector_at()

void ** vector_at ( vector_t * v,
size_t idx )

Return a pointer to where the vector element at 'idx' is stored. This can be used to set the value of the element or to read it. If the index is past the end of the vector, then the vector is automatically expanded and filled with NULL pointers as needed. If no element at idx has been previously set, then the value pointed to by the returned pointer will be NULL.

Parameters
vThe vector.
idxThe index into the vector.
Returns
A pointer to the element at 'idx', which is itself a pointer.

◆ vector_free()

void vector_free ( vector_t * v)

Free the memory held by the given vector, invalidating it.

Parameters
vAny vector.

◆ vector_new()

vector_t vector_new ( size_t initial_capacity)

Allocate and initialize a new vector.

Parameters
initial_capacityThe desired initial capacity to allocate.

◆ vector_pop()

void * vector_pop ( vector_t * v)

Remove and return some pointer that is contained in the given vector, or return NULL if the given vector is empty.

Parameters
vAny vector.

◆ vector_push()

void vector_push ( vector_t * v,
void * element )

Add the given element to the vector.

Parameters
vA vector that is to grow.
elementAn element that the vector should contain.

◆ vector_pushall()

void vector_pushall ( vector_t * v,
void ** array,
size_t size )

Add all elements of the given array to the vector. Elements should be non-null.

Parameters
vA vector that is to grow.
arrayAn array of items to be added to the vector.
sizeThe size of the given array.

◆ vector_size()

size_t vector_size ( vector_t * v)

Return the size of the vector.

Parameters
vAny vector
Returns
size_t The size of the vector.

◆ vector_vote()

void vector_vote ( vector_t * v)

Vote on whether this vector should be given less memory. If v contains few elements, it becomes more likely to shrink.

It is suggested that this function be called when the number of elements in v reaches a local maximum.

Parameters
vAny vector.