A dynamic vector struct thing
This file contains 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 "Vectors.h" | |
int main() { | |
IntVector vector; | |
IntVectorInit(&vector); | |
IntVectorPushBack(&vector, 100); | |
IntVectorPushBack(&vector, 130); | |
IntVectorPushBack(&vector, 340); | |
IntVectorPushBack(&vector, 50034); | |
IntVector vector2; | |
IntVectorInit(&vector2); | |
IntVectorPushBack(&vector2, 8); | |
IntVectorPushBack(&vector2, 894); | |
IntVectorPushBack(&vector2, 58); | |
IntVectorPushBack(&vector2, 1833455); | |
IntVector distance; | |
IntVectorInit(&distance); | |
IntVectorGetDistance(&distance, &vector, &vector2); | |
for (int i = 0; i < vector.element_count; i++) { | |
printf("element[%d]: %d\n", i, IntVectorGetValue(&distance, i)); | |
} | |
IntVectorDelete(&vector); | |
IntVectorDelete(&vector2); | |
IntVectorDelete(&distance); | |
return 0; | |
} |
This file contains 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 "Vectors.h" | |
void IntVectorInit(IntVector* vec) { | |
// Initaliase the element count to zero, allocate 0 byes for the | |
// vector pointer, and mark deleted as false. | |
vec->element_count = 0; | |
vec->vector = malloc(0); | |
vec->deleted = false; | |
} | |
void IntVectorPushBack(IntVector* vec, int value) { | |
// set vec+element_count to value | |
*(vec->vector+vec->element_count) = value; | |
// increment element_count | |
vec->element_count++; | |
// reallocate the vector to allow for the size of an int times | |
// the amount of elements in the vector | |
vec->vector = realloc(vec->vector, sizeof(int) * vec->element_count); | |
} | |
int IntVectorGetValue(IntVector* vec, int idx) { | |
// check if trying to get invalid data... | |
if (idx >= vec->element_count || idx < 0) { | |
printf("ERROR: invalid index '%d'\n", idx); | |
exit(-1); | |
} | |
if (vec->deleted) { | |
printf("ERROR: tried to fetch data from deleted IntVector object\n"); | |
exit(-1); | |
} | |
// ... otherwise return the value at vector offsetted by the index | |
return *(vec->vector+idx); | |
} | |
void IntVectorPop(IntVector* vec, int amount) { | |
// decrement element count by amount... | |
vec->element_count -= amount; | |
// ... and update vector accordingly | |
vec->vector = realloc(vec->vector, sizeof(int) * vec->element_count); | |
} | |
void IntVectorDelete(IntVector* vec) { | |
// free the vector | |
free(vec->vector); | |
// set the vector to null | |
vec->vector = NULL; | |
// and deleted to true | |
vec->deleted = true; | |
} | |
void IntVectorGetDistance(IntVector* resultvector, IntVector* vec0, IntVector* vec1) { | |
// make sure that both elements are of the same size | |
if (vec0->element_count != vec1->element_count) { | |
printf("ERROR: vec0 and vec1 do not have identical dimensions."); | |
exit(-1); | |
} | |
// if they are, loop until i == element count | |
for (int i = 0; i < vec0->element_count; i++) { | |
// push back vec0[i]-vec1[i] into resultvector | |
IntVectorPushBack(resultvector, IntVectorGetValue(vec0, i)-IntVectorGetValue(vec1, i)); | |
} | |
} |
This file contains 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 VECTORS_H | |
#define VECTORS_H | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
// A dynamic vector of intagers | |
typedef struct { | |
int* vector; | |
int element_count; | |
bool deleted; | |
} IntVector; | |
// Initialises an IntVector (must be used before doing any other operation on the vector) | |
// @param vec A pointer to an IntVector | |
void IntVectorInit(IntVector* vec); | |
// 'Pushes back' a value onto a vector | |
// @param vec A pointer to an IntVector | |
// @param value The value to push back on the IntVector | |
void IntVectorPushBack(IntVector* vec, int value); | |
// Pops any amount of values off a vector | |
// @param vec A pointer to an IntVector | |
// @param amount The amount of elements to pop off the vector | |
void IntVectorPop(IntVector* vec, int amount); | |
// Deletes a vector (should be used when finished with the IntVector) | |
// @param vec A pointer to an IntVector | |
void IntVectorDelete(IntVector* vec); | |
// Gets the distance between two IntVectors and stores the result in another | |
// @param resultvector A pointer to the vector where the result should be stored | |
// @param vec0 The vector to get the distance from | |
// @param vec1 The vector to get the distance to | |
void IntVectorGetDistance(IntVector* resultvector, IntVector* vec0, IntVector* vec1); | |
// Gets the value from an IntVector at a specific index | |
// @param vec A pointer to an IntVector | |
// @param idx The index that the requested value is at | |
// @return The value in IntVector vec at idx | |
int IntVectorGetValue(IntVector* vec, int idx); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment