Created
July 15, 2025 14:09
-
-
Save NaviTheCoderboi/3331c0a45064db53f6405f614df82f60 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "vector.h" | |
int main(void) { | |
Vector* intVector = vInit(sizeof(int)); | |
for (int i = 0; i < 10; i++) { | |
vPush(intVector, &i); | |
} | |
printf("Vector contents:\n"); | |
for (size_t i = 0; i < intVector->length; i++) { | |
int* value = (int*)vGet(intVector, i); | |
printf("%zu: %d\n", i, *value); | |
} | |
int newValue = 42; | |
vSet(intVector, 5, &newValue); | |
printf("\nVector after modification:\n"); | |
for (size_t i = 0; i < intVector->length; i++) { | |
int* value = (int*)vGet(intVector, i); | |
printf("%zu: %d\n", i, *value); | |
} | |
vFree(intVector); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "vector.h" | |
#include <stdlib.h> | |
#include <string.h> | |
#include <assert.h> | |
Vector* vInit(const size_t elementSize) { | |
Vector* vector = (Vector*)malloc(sizeof(Vector)); | |
if (!vector) return NULL; | |
vector->elementSize = elementSize; | |
vector->length = 0; | |
vector->capacity = 0; | |
vector->data = NULL; | |
return vector; | |
} | |
void vFree(Vector* vector) { | |
if (vector) { | |
free(vector->data); | |
free(vector); | |
} | |
} | |
void* vGet(const Vector* vector, const size_t index) { | |
if (index >= vector->length) return NULL; | |
return (char*)vector->data + index * vector->elementSize; | |
} | |
void vSet(Vector* vector, const size_t index, const void* value) { | |
if (index >= vector->length) return; | |
void* target = (char*)vector->data + index * vector->elementSize; | |
memcpy(target, value, vector->elementSize); | |
} | |
void vPush(Vector* vector, const void* value) { | |
if (((vector->length + 1)*vector->elementSize) >= vector->capacity) { | |
size_t newCapacity = (vector->length +1) * vector->elementSize; | |
void* newData = realloc(vector->data, newCapacity); | |
if (!newData) return; | |
vector->data = newData; | |
vector->capacity = newCapacity; | |
} | |
memcpy((char*)vector->data + vector->length * vector->elementSize, value, vector->elementSize); | |
vector->length++; | |
} | |
void vPop(Vector* vector) { | |
if (vector && vector->length > 0) { | |
vector->length--; | |
size_t newCapacity = vector->length * vector->elementSize; | |
void* newData = realloc(vector->data, newCapacity); | |
if (!newData) { | |
vector->length++; | |
return; | |
} | |
vector->data = newData; | |
vector->capacity = newCapacity; | |
} | |
} | |
void vClear(Vector* vector) { | |
if (vector) { | |
vector->length = 0; | |
vector->capacity = 0; | |
free(vector->data); | |
vector->data = NULL; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef VECTOR_H | |
#define VECTOR_H | |
#include <stddef.h> | |
typedef struct { | |
void* data; | |
size_t length; | |
size_t capacity; | |
size_t elementSize; | |
} Vector; | |
Vector* vInit(const size_t elementSize); | |
void vFree(Vector* vector); | |
void* vGet(const Vector* vector, const size_t index); | |
void vSet(Vector* vector, const size_t index, const void* value); | |
void vPush(Vector* vector, const void* value); | |
void vPop(Vector* vector); | |
void vResize(Vector* vector, const size_t newLength); | |
void vClear(Vector* vector); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment