Skip to content

VxidDev/c-vect

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

84 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

c-vect

Zero-dependency dynamic arrays for C int*, float*, char*, char** vectors with auto-resizing. Features

  • 4 types: int, float, char, char** (strings)
  • Auto-grow: capacity *= 2 (starts at 16)
  • Zero-init: calloc() safety
  • Leak-proof: FreeVec() double-free protection

🛑 Project Status

c-vect is feature-complete. Core dynamic vector + FP pipeline (Map/Filter/Reduce/Sort).

New Features

  • Sponsor for priority (Ko-fi profile)
  • Issues for bugs/backlog
  • Feature requests → GitHub Discussions

Pull requests welcome for docs/benchmarks/perf!

Quick Start

#include <vectors.h>

int main() {
    Vector* IntVector = InitVec(VEC_INT , (int[]){1 , 2 , 3} , &(size_t){3}); // Initialize Vector

    if (!IntVector) {  // Check if successfully created vector
        printf("Failed to initialize IntVec.\n");
        return 1;
    }

    for (ptrdiff_t i = 0; i < GetSizeVec(IntVector); i++) { // Print vector's data
         printf("Item @ %zu -> %i\n" , i , GetItemFromVec(IntVector , i));
    }
    
    FreeVec(IntVector); // Free Vector.
    return 0;
}

Build

git clone https://github.com/VxidDev/c-vect.git
cd c-vect
make          # Build libvectors.a
make install  # Install system-wide

Build Targets

Command Does
make Build libvectors.a
make clean Delete build files
make install Install to /usr/local/
make format Format code (clang-format)
make check Static analysis (cppcheck)

Using the Library

  1. System-wide (after make install)
#include <vectors.h>  // Standard include!
gcc main.c -lvectors -o app
./app

API

'*' before a function name means function is re-factored and now usable. '!' before a functions name means function may be broken or unusable and overall not recommended for usage.

Function Usage Description
* InitVec(enum VecType type, void arr, void arrlen)** vec = InitVec(VEC_INT, arr, &arrlen) Initialize a new vector with optional source array and length. If arr and arrlen is NULL, creates an empty Vector* (default capacity = 16).
* AppendVec(Vector* vec, void item)** AppendVec(vec, &value) Append an element to the end of the vector. Automatically resizes (×2) as needed.
* FreeVec(Vector* vec)* FreeVec(vec) Free all allocated memory associated with the vector.
* GetLastVec(Vector* vec)* ptr = GetLastVec(vec) Get a pointer to the last element, or NULL if the vector is empty.
* PopVec(Vector* vec)* ptr = PopVec(vec) Remove and return a pointer to the last element. Returns NULL if the vector is empty.
* RemoveFromVec(Vector* vec, size_t index)* ok = RemoveFromVec(vec, 2) Remove element at a given index (shifts left). Returns true if successful.
* GetItemFromVec(Vector* vec, size_t index)* ptr = GetItemFromVec(vec, 2) Return a pointer to the element at index, or NULL if invalid.
* ClearVec(Vector* vec)* ClearVec(vec) Clear all elements but keep allocated memory (capacity unchanged, size = 0).
* ResetVec(Vector* vec)* ResetVec(vec) Reset vector to its initial empty state (capacity reset to default, e.g., 16). Returns true if successful.
* ShrinkToFitVec(Vector* vec)* ShrinkToFitVec(vec) Shrink capacity to match the current number of elements. Returns true if reallocation succeeded.
* ShrinkVec(Vector* vec, size_t size)* ShrinkVec(vec, 8) Reduce vector capacity to the specified new size (if smaller than current capacity). Returns true if successful.
* ExtendVec(Vector* vec, size_t size)* ExtendVec(vec, 64) Increase vector capacity to at least the specified size. Does not modify the element count. Returns true if successful.
* GetSizeVec(Vector* vec) size = GetSizeVec(vec) Return size (ptrdiff_t): ≥0=valid, -1=ERROR (NULL/corrupted vec).
* GetCapacityVec(Vector* vec) cap = GetCapacityVec(vec) Return capacity (ptrdiff_t): ≥0=valid, -1=ERROR.
* InsertVec(Vector* vec, void* item, size_t index) ok = InsertVec(Vector , &(int){1} , 3) Insert specified item at selected index. Returns true if success, otherwise returns false.
* SetItemVec(Vector* vec, void* item, size_t index) ok = SetItemVec(Vector, &(int){7}, 7) Change replace value of specified index with given item. Returns true if success, otherwise returns false.
* SortVec(Vector* vec, int(* compare)(const void* , const void*)) ok = SortVec(Vector , NULL) Sort vector via quick sort with pre-made or your own criteria. Returns true if success, otherwise returns false.
* ContainsVec(Vector* vec , void* item) ok = ContainsVec(Vector , &(int){3} Check if vector contains given item. Return true if does, otherwise returns false. Algorithm complexity O(n).
* ForEachVec(Vector* vec , void (* func)(void* item , size_t idx)) ForEachVec(Vector , PrintItem) Repeat given instruction for each item in the vector.
* IndexOfVec(Vector* vec , void* item) idx = IndexOfVec(Vector , &(int){3}) (ptrdiff) Return index of given item, otherwise return -1.
* FrontVec(Vector* vec) front = FrontVec(Vector) Returns pointer to first element.
!* BackVec(Vector* vec) back = BackVec(Vector) Returns pointer to last element.
* FilterVec(Vector* vec , bool (* func)(void* , void*) , void* ctx) FilterVec(Vector , is_even , NULL) Remove items which dont match criteria from given vector.
* MapVec(Vector* vec , void (* func)(void* , void*) , void* ctx) MapVec(Vector , square , NULL) Apply given function to each item in vector
* ReduceVec(Vector* vec , void* (* reducer)(void* , void* , void*) ) ReduceVec(Vector , sum , NULL , &InitRslt) Reduce given vector to a single value.
* SumVec(Vector* vec) int* result = (int*)SumVec(Vector) Returns sum of vector values for VEC_INT and VEC_FLOAT vectors.
* MaxVec(Vector* vec) int* max = (int*)MaxVec(Vector) Returns highest value of vector for VEC_INT and VEC_FLOAT vectors.
* MinVec(Vector* vec) int* min = (int*)MinVec(Vector) Returns lowest value in vector for VEC_INT and VEC_FLOAT vectors.

Types: VEC_INT, VEC_FLOAT, VEC_CHAR, VEC_STRING

Memory Layout

[ struct: size/capacity ] → vec → [ data array ]
free(data) → free(struct)  ✅

Safety Rules

  • Always check InitVec != NULL

  • Use LastVec/GetItemFromVec results IMMEDIATELY (internal pointers become invalid after AppendVec/PopVec)

  • VEC_STRING: Don't free returned pointers

  • Capacity doubles automatically on append

  • Invalid index prints error message, returns NULL

About

Implementation of dynamic arrays in pure C.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

 
 
 

Contributors