Skip to content

Getting started with Vector.

Anirudh Lakhanpal edited this page Jun 1, 2024 · 7 revisions

LibC-STL Vector.

The vector offered by LibC-STL is somewhat similar in terms of methods and usage to C++ STL.

Vector Overview

Creating a Vector

LibC-STL offers creation of vectors of basic data types such as int, char, float and double. You can create vectors as follows.

Vector *int_vector = create_Vector(5, INT);
Vector *float_vector = create_Vector(5, FLOAT);
Vector *char_vector = create_Vector(5, CHAR);
Vector *double_vector = create_Vector(5, DOUBLE);

To create a vector you must call the create_vector() function read about [create_Vector]("link goes here"). The first argument for the create_Vector() function is the initial capacity for your vector, first argument you pass denotes the number of elements you want your vector to hold upon creation and the second argument is the datatype. You can create vectors of following data types:

  • INT
  • FLOAT
  • CHAR
  • DOUBLE If, in future your initially set vector capacity exceeds the current capacity the vector automatically grows to accommodate more elements.

Adding elements to a vector

You can add elements to your vector by using generic push_Back method. The syntax and usage is relatively easy. The first argument is the pointer to your vector and second argument is the element or data you want to push into the vector.

#include <stdio.h>
#include <Vector.h>
int main(){
Vector *intVector = create_Vector(0, INT);
    for(int i = 50; i < 100; ++i)
        push_Back(intVector, i);

    for(int i = 0; i < vector_size(intVector); ++i)
        printf("Value at %d is %d \n", i, AtVector(intVector, i, 0));
    return 0;
}

Brief Explanation

  • You can use vector_size() method to get the current size of the vector, The size of the vector is the number of elements currently present in the vector, note that size and capacity are different. We are using it as a loop control.
  • Next we are printing the vector contents using the generic AtVector() function. Read about [AtVector]("link goes here").

Getting Vector size and capacity

You can access the vector size and capacity of your vector using the vector_size() and vector_capacity() functions.

#include <Vector.h>
int main(){
    Vector* vector1 = create_Vector(10, INT);
    Vector* vector2 = create_Vector(10, FLOAT);

    size_t size1 = vector_size(vector1);
    size_t size2 = vector_size(vector2);

    size_t cap1 = vector_size(vector1);
    size_t cap2 = vector_size(vector2);

The values will be as follows. Vector size is initially 0 while the capacity is set to 10 initially to accommodate future elements and avoid reallocation and resizing.

size1 0 
size2 0 
cap1 10 
cap2 10 

Vector.h

The file contains the definitions of Vector functions, and _Generic macros. And corresponding function definitions are defined in Vector.c

Vector.h Structs and Enums

typedef enum
{
    INT,
    FLOAT,
    CHAR,
    DOUBLE,
} DataType;
typedef enum
{
    INSERT_VECTOR_SUCCESS,
    INSERT_VECTOR_FAILED,

    VECTOR_RESIZE_SUCCESS,
    VECTOR_RESIZE_FAILED,

    PUSH_BACK_VECTOR_SUCCESS,
    PUSH_BACK_VECTOR_FAILED,
} StatusCodes;
typedef struct vector
{
    void **data;
    size_t capacity;
    size_t size;
    DataType type;

} Vector;

Vector.h Typedefs

typedef int** VectorDataInt ;
typedef float** VectorDataFloat ;
typedef char** VectorDataChar ;
typedef double** VectorDataDouble ;

Vector.h Functions

Vector *create_Vector(size_t, DataType);
void delete_vector(Vector *);
int resize_vector(Vector *, size_t);
StatusCodes LIBC_INTERNAL_pb_int(Vector *, int);
StatusCodes LIBC_INTERNAL_pb_float(Vector *, float);
StatusCodes LIBC_INTERNAL_pb_char(Vector *, char);
StatusCodes LIBC_INTERNAL_pb_double(Vector *, double);
//Not recommended to call LIBC_INTERNAL functions directly
StatusCodes LIBC_INTERNAL_insert_int(Vector *, int, size_t);
StatusCodes LIBC_INTERNAL_insert_float(Vector *, float, size_t);
StatusCodes LIBC_INTERNAL_insert_char(Vector *, char, size_t);
StatusCodes LIBC_INTERNAL_insert_double(Vector *, double, size_t);
int AtInt(Vector *, int);
float AtFloat(Vector *, int);
char AtChar(Vector *, int);
double AtDouble(Vector *, int);
int** get_vector_int(Vector *);
int empty_vector(Vector *);
void *at(Vector *, size_t);
size_t vector_size(Vector *);
size_t vector_capacity(Vector *);

Vector.h Macros

#define insert(Container, index, value) _Generic((value), \
    int: LIBC_INTERNAL_insert_int, \
    float: LIBC_INTERNAL_insert_float, \
    char: LIBC_INTERNAL_insert_char, \
    double: LIBC_INTERNAL_insert_double, \
)

#define AtVector(container, index, type) _Generic((type), \
    int: AtInt, \
    float: AtFloat, \
    char: AtChar, \
    double: AtDouble \
)(container, index)

#define push_Back(Container, T) _Generic((T), \
    int: LIBC_INTERNAL_pb_int,                \
    float: LIBC_INTERNAL_pb_float,            \
    char: LIBC_INTERNAL_pb_char,              \
    double: LIBC_INTERNAL_pb_double)(Container, T)
Clone this wiki locally