verified insertion of vectors into vectors; proved working
This commit is contained in:
@@ -26,11 +26,10 @@ I got bored with having to use complicated checking and stuff when working with
|
|||||||
4. Getting and setting a value of a vector on a given index
|
4. Getting and setting a value of a vector on a given index
|
||||||
5. Pushing a value at the end of a vector
|
5. Pushing a value at the end of a vector
|
||||||
6. Inserting a value into a vector at a given index
|
6. Inserting a value into a vector at a given index
|
||||||
|
7. Insertion of a vector into another vector
|
||||||
|
|
||||||
#### TODO:
|
#### TODO:
|
||||||
|
|
||||||
1. Insertion of a vector into another vector
|
|
||||||
|
|
||||||
+ You tell me
|
+ You tell me
|
||||||
|
|
||||||
|
|
||||||
@@ -42,4 +41,4 @@ I got bored with having to use complicated checking and stuff when working with
|
|||||||
|
|
||||||
#### Run demo
|
#### Run demo
|
||||||
|
|
||||||
`make && ./demo`
|
`make && ./demo`
|
||||||
|
|||||||
@@ -4,91 +4,26 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
printf("demonstration of the power of dynamically allocated arrays in c:\n-----------\n\n");
|
vector_t *vv = vectorInit(VECTOR);
|
||||||
|
vector_t *fv = vectorInit(FLOAT);
|
||||||
|
vectorPrint(vv);
|
||||||
|
|
||||||
printf("initializing an integer vector\n");
|
vectorPush(fv, &(float){3.14});
|
||||||
vector_t *v = vectorInit(INT);
|
vectorPush(fv, &(float){5.123});
|
||||||
|
|
||||||
printf("vector after initialization\n");
|
vectorPush(vv, fv);
|
||||||
vectorPrint(v);
|
vectorPush(vv, fv);
|
||||||
|
vectorPush(vv, fv);
|
||||||
|
|
||||||
printf("pushing an element of value 1 into vector\n");
|
vectorPush(fv, &(float){156.1});
|
||||||
int element = 1;
|
vectorPush(fv, &(float){6.124});
|
||||||
vectorPush(v, &element);
|
|
||||||
vectorPrint(v);
|
|
||||||
|
|
||||||
printf("pushing another element (value 2) into vector\n");
|
vectorInsertAt(vv, 1, fv);
|
||||||
element = 2;
|
|
||||||
vectorPush(v, &element);
|
|
||||||
vectorPrint(v);
|
|
||||||
|
|
||||||
printf("inserting 3 at index 1\n");
|
vectorPrint(vv);
|
||||||
element = 3;
|
|
||||||
vectorInsertAt(v, 1, &element);
|
|
||||||
vectorPrint(v);
|
|
||||||
|
|
||||||
printf("popping the last value from the vector\n");
|
|
||||||
int popped = *((int *)vectorPop(v));
|
|
||||||
printf("popped: %d\n", popped);
|
|
||||||
printf("vector after popping\n");
|
|
||||||
vectorPrint(v);
|
|
||||||
|
|
||||||
printf("initializing a second vector of type vector_t (multidimensional vector => vector of vectors)\n");
|
vectorCleanup(vv);
|
||||||
vector_t *w = vectorInit(VECTOR);
|
vectorCleanup(fv);
|
||||||
|
|
||||||
printf("vector after initialization\n");
|
|
||||||
vectorPrint(w);
|
|
||||||
|
|
||||||
printf("pushing the first vector into the newly created one\n");
|
|
||||||
vectorPush(w, v);
|
|
||||||
vectorPrint(w);
|
|
||||||
printf("once again\n");
|
|
||||||
vectorPush(w, v);
|
|
||||||
vectorPrint(w);
|
|
||||||
printf("popping\n");
|
|
||||||
vector_t *poppedv = (vector_t *)vectorPop(w);
|
|
||||||
printf("popped vector\n");
|
|
||||||
vectorPrint(poppedv);
|
|
||||||
printf("vector from which was popped after pop\n");
|
|
||||||
vectorPrint(w);
|
|
||||||
|
|
||||||
printf("initializing a char vector (variable-length string) with value \"Hello, world!\"\n");
|
|
||||||
vector_t *charVector = vectorInit(CHAR);
|
|
||||||
char *word = "Hello, world!";
|
|
||||||
for (int i = 0; i < strlen(word); i++) {
|
|
||||||
vectorPush(charVector, &word[i]);
|
|
||||||
}
|
|
||||||
printf("char vector as string\n");
|
|
||||||
vectorPrintAsString(charVector);
|
|
||||||
|
|
||||||
printf("char vector converted into a regular string\n");
|
|
||||||
char *newWord = vectorToString(charVector);
|
|
||||||
printf("new word: %s\n", newWord);
|
|
||||||
|
|
||||||
printf("setting a value in the char vector on index 10 to \"\" (nothing, effectively deleting the character, although the size of the vector stays the same!)\n");
|
|
||||||
vectorSetAt(charVector, 10, ""); // should change the vector to "Hello, word!" (size stays the same!)
|
|
||||||
vectorPrintAsString(charVector);
|
|
||||||
|
|
||||||
printf("files section\n---------------\n");
|
|
||||||
printf("initializing a file called test.txt with mode \"r\" (reading it)\n");
|
|
||||||
file_t* f = fileInit("test.txt", "r");
|
|
||||||
|
|
||||||
printf("printing the file contents\n");
|
|
||||||
filePrint(f);
|
|
||||||
printf("file length (line count): %d\n", f->length);
|
|
||||||
printf("file as vector\n");
|
|
||||||
vectorPrint(f->lines);
|
|
||||||
printf("file to string\n");
|
|
||||||
char* strFile = fileToString(f);
|
|
||||||
printf("%s", strFile);
|
|
||||||
|
|
||||||
printf("cleaning up vectors\n");
|
|
||||||
vectorCleanup(v);
|
|
||||||
vectorCleanup(w);
|
|
||||||
vectorCleanup(charVector);
|
|
||||||
|
|
||||||
printf("cleaning up files\n");
|
|
||||||
fileCleanup(f);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+167
-167
@@ -5,17 +5,17 @@
|
|||||||
|
|
||||||
void
|
void
|
||||||
vectorCleanup(vector_t* v) {
|
vectorCleanup(vector_t* v) {
|
||||||
free(v->data);
|
free(v->data);
|
||||||
free(v);
|
free(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
int printNewline = 0;
|
int printNewline = 0;
|
||||||
|
|
||||||
void
|
void
|
||||||
vectorReset(vector_t* v) {
|
vectorReset(vector_t* v) {
|
||||||
while (v->size > 0) {
|
while (v->size > 0) {
|
||||||
vectorPop(v);
|
vectorPop(v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@@ -95,16 +95,16 @@ _printVectorAsStringRecursive(vector_t* v, int depth) {
|
|||||||
int i;
|
int i;
|
||||||
for (i = 0; i < v->size; i++) {
|
for (i = 0; i < v->size; i++) {
|
||||||
switch (v->type) {
|
switch (v->type) {
|
||||||
case CHAR:
|
case CHAR:
|
||||||
fprintf(stdout, "%c", *((char *)(v->data) + i));
|
fprintf(stdout, "%c", *((char *)(v->data) + i));
|
||||||
break;
|
break;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
// Recursively print each level of the vector
|
// Recursively print each level of the vector
|
||||||
_printVectorAsStringRecursive((vector_t *)(v->data + i * v->elementSize), depth + 1);
|
_printVectorAsStringRecursive((vector_t *)(v->data + i * v->elementSize), depth + 1);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
perror("tried to print a non-char type vector as string\n");
|
perror("tried to print a non-char type vector as string\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (depth != 0) fprintf(stdout, "\n"); // do not print the last additional new line
|
if (depth != 0) fprintf(stdout, "\n"); // do not print the last additional new line
|
||||||
@@ -113,18 +113,18 @@ _printVectorAsStringRecursive(vector_t* v, int depth) {
|
|||||||
void
|
void
|
||||||
vectorPrintAsString(vector_t* v) {
|
vectorPrintAsString(vector_t* v) {
|
||||||
switch (v->type) {
|
switch (v->type) {
|
||||||
case CHAR:
|
case CHAR:
|
||||||
for (int i = 0; i < v->size; i++) {
|
for (int i = 0; i < v->size; i++) {
|
||||||
fprintf(stdout, "%c", *((char *)(v->data) + i));
|
fprintf(stdout, "%c", *((char *)(v->data) + i));
|
||||||
}
|
}
|
||||||
fprintf(stdout, "\n");
|
fprintf(stdout, "\n");
|
||||||
break;
|
break;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
_printVectorAsStringRecursive(v, 0);
|
_printVectorAsStringRecursive(v, 0);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
perror("tried to print a non-char type vector as string\n");
|
perror("tried to print a non-char type vector as string\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,8 +150,8 @@ vectorInit(dataType_e type) {
|
|||||||
v->elementSize = sizeof(char);
|
v->elementSize = sizeof(char);
|
||||||
break;
|
break;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
v->elementSize = sizeof(vector_t);
|
v->elementSize = sizeof(vector_t);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
perror("specify a valid vector type\n");
|
perror("specify a valid vector type\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@@ -169,36 +169,36 @@ vectorInit(dataType_e type) {
|
|||||||
void
|
void
|
||||||
vectorPush(vector_t* v, void* element) {
|
vectorPush(vector_t* v, void* element) {
|
||||||
switch (v->type) {
|
switch (v->type) {
|
||||||
case INT:
|
case INT:
|
||||||
*((int *)(v->data) + v->size) = *((int *)element);
|
*((int *)(v->data) + v->size) = *((int *)element);
|
||||||
break;
|
break;
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
*((float *)(v->data) + v->size) = *((float *)element);
|
*((float *)(v->data) + v->size) = *((float *)element);
|
||||||
break;
|
break;
|
||||||
case CHAR:
|
case CHAR:
|
||||||
*((char *)(v->data) + v->size) = *((char *)element);
|
*((char *)(v->data) + v->size) = *((char *)element);
|
||||||
break;
|
break;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
// Instead of reallocating memory for vector_t pointers, allocate memory for the vector data
|
// Instead of reallocating memory for vector_t pointers, allocate memory for the vector data
|
||||||
if (v->size == 0) {
|
if (v->size == 0) {
|
||||||
// Allocate memory for the vector data for the first time
|
// Allocate memory for the vector data for the first time
|
||||||
v->data = malloc(v->elementSize * v->capacity);
|
v->data = malloc(v->elementSize * v->capacity);
|
||||||
} else {
|
} else {
|
||||||
// Reallocate memory for the vector data
|
// Reallocate memory for the vector data
|
||||||
v->data = realloc(v->data, v->elementSize * v->capacity);
|
v->data = realloc(v->data, v->elementSize * v->capacity);
|
||||||
}
|
}
|
||||||
if (v->data == NULL) {
|
if (v->data == NULL) {
|
||||||
perror("failed to allocate memory for vector data\n");
|
perror("failed to allocate memory for vector data\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy the vector data from the element to the vector
|
||||||
|
memcpy((vector_t *)(v->data) + v->size, (vector_t *)element, sizeof(vector_t));
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
perror("specify a valid vector type\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
|
||||||
|
|
||||||
// Copy the vector data from the element to the vector
|
|
||||||
memcpy((vector_t *)(v->data) + v->size, (vector_t *)element, sizeof(vector_t));
|
|
||||||
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
perror("specify a valid vector type\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
v->size += 1;
|
v->size += 1;
|
||||||
@@ -209,41 +209,41 @@ vectorPush(vector_t* v, void* element) {
|
|||||||
|
|
||||||
void*
|
void*
|
||||||
vectorPop(vector_t* v) {
|
vectorPop(vector_t* v) {
|
||||||
// capacity is always a power of 2, so a rounding error cannot happen i guess _/-(shi)-\_
|
// capacity is always a power of 2, so a rounding error cannot happen i guess _/-(shi)-\_
|
||||||
|
|
||||||
switch (v->type) {
|
switch (v->type) {
|
||||||
case INT:
|
case INT:
|
||||||
int *iretval = (int *)(v->data) + v->size-1;
|
int *iretval = (int *)(v->data) + v->size-1;
|
||||||
v->size -= 1;
|
v->size -= 1;
|
||||||
if (v->size <= v->capacity / 2) v->capacity /= 2;
|
if (v->size <= v->capacity / 2) v->capacity /= 2;
|
||||||
v->data = realloc(v->data, v->elementSize * v->capacity);
|
v->data = realloc(v->data, v->elementSize * v->capacity);
|
||||||
if (v->capacity == 0) v->capacity = 1;
|
if (v->capacity == 0) v->capacity = 1;
|
||||||
return iretval;
|
return iretval;
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
float *fretval = (float *)(v->data) + v->size-1;
|
float *fretval = (float *)(v->data) + v->size-1;
|
||||||
v->size -= 1;
|
v->size -= 1;
|
||||||
if (v->size <= v->capacity / 2) v->capacity /= 2;
|
if (v->size <= v->capacity / 2) v->capacity /= 2;
|
||||||
if (v->capacity == 0) v->capacity = 1;
|
if (v->capacity == 0) v->capacity = 1;
|
||||||
v->data = realloc(v->data, v->elementSize * v->capacity);
|
v->data = realloc(v->data, v->elementSize * v->capacity);
|
||||||
return fretval;
|
return fretval;
|
||||||
case CHAR:
|
case CHAR:
|
||||||
char *cretval = (char *)(v->data) + v->size-1;
|
char *cretval = (char *)(v->data) + v->size-1;
|
||||||
v->size -= 1;
|
v->size -= 1;
|
||||||
if (v->size <= v->capacity / 2) v->capacity /= 2;
|
if (v->size <= v->capacity / 2) v->capacity /= 2;
|
||||||
if (v->capacity == 0) v->capacity = 1;
|
if (v->capacity == 0) v->capacity = 1;
|
||||||
v->data = realloc(v->data, v->elementSize * v->capacity);
|
v->data = realloc(v->data, v->elementSize * v->capacity);
|
||||||
return cretval;
|
return cretval;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
vector_t *vretval = (vector_t *)(v->data) + v->size-1;
|
vector_t *vretval = (vector_t *)(v->data) + v->size-1;
|
||||||
v->size -= 1;
|
v->size -= 1;
|
||||||
if (v->size <= v->capacity / 2) v->capacity /= 2;
|
if (v->size <= v->capacity / 2) v->capacity /= 2;
|
||||||
if (v->capacity == 0) v->capacity = 1;
|
if (v->capacity == 0) v->capacity = 1;
|
||||||
v->data = realloc(v->data, v->elementSize * v->capacity);
|
v->data = realloc(v->data, v->elementSize * v->capacity);
|
||||||
return vretval;
|
return vretval;
|
||||||
default:
|
default:
|
||||||
perror("specify a valid vector type\n");
|
perror("specify a valid vector type\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char*
|
char*
|
||||||
@@ -299,89 +299,89 @@ vectorToString(vector_t* v) {
|
|||||||
|
|
||||||
void*
|
void*
|
||||||
vectorGetAt(vector_t* v, int index) {
|
vectorGetAt(vector_t* v, int index) {
|
||||||
switch (v->type) {
|
switch (v->type) {
|
||||||
case INT:
|
case INT:
|
||||||
return (int *)(v->data) + index;
|
return (int *)(v->data) + index;
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
return (float *)(v->data) + index;
|
return (float *)(v->data) + index;
|
||||||
case CHAR:
|
case CHAR:
|
||||||
return (char *)(v->data) + index;
|
return (char *)(v->data) + index;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
return (vector_t *)(v->data) + index;
|
return (vector_t *)(v->data) + index;
|
||||||
default:
|
default:
|
||||||
perror("specify a valid vector type\n");
|
perror("specify a valid vector type\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
vectorSetAt(vector_t* v, int index, void* element) {
|
vectorSetAt(vector_t* v, int index, void* element) {
|
||||||
if (index >= v->size) {
|
if (index >= v->size) {
|
||||||
perror("index out of range\n");
|
perror("index out of range\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (v->type) {
|
switch (v->type) {
|
||||||
case INT:
|
case INT:
|
||||||
*((int *)(v->data) + index) = *((int *)element);
|
*((int *)(v->data) + index) = *((int *)element);
|
||||||
break;
|
break;
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
*((float *)(v->data) + index) = *((float *)element);
|
*((float *)(v->data) + index) = *((float *)element);
|
||||||
break;
|
break;
|
||||||
case CHAR:
|
case CHAR:
|
||||||
*((char *)(v->data) + index) = *((char *)element);
|
*((char *)(v->data) + index) = *((char *)element);
|
||||||
break;
|
break;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
*((vector_t *)(v->data) + index) = *((vector_t *)element);
|
*((vector_t *)(v->data) + index) = *((vector_t *)element);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
perror("specify a valid vector type\n");
|
perror("specify a valid vector type\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
vectorInsertAt(vector_t* v, int index, void* element) {
|
vectorInsertAt(vector_t* v, int index, void* element) {
|
||||||
if (index >= v->size) {
|
if (index >= v->size) {
|
||||||
perror("index out of range\n");
|
perror("index out of range\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
v->size += 1;
|
v->size += 1;
|
||||||
if (v->size >= v->capacity) {
|
if (v->size >= v->capacity) {
|
||||||
v->capacity *= 2;
|
v->capacity *= 2;
|
||||||
v->data = realloc(v->data, v->elementSize * v->capacity);
|
v->data = realloc(v->data, v->elementSize * v->capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (v->type) {
|
switch (v->type) {
|
||||||
case INT:
|
case INT:
|
||||||
// move all elements to the right by one
|
// move all elements to the right by one
|
||||||
for (int i = v->size-1; i > index; i--) {
|
for (int i = v->size-1; i > index; i--) {
|
||||||
*((int *)(v->data) + i) = *((int *)(v->data) + i-1);
|
*((int *)(v->data) + i) = *((int *)(v->data) + i-1);
|
||||||
}
|
}
|
||||||
// set the element at the given index
|
// set the element at the given index
|
||||||
*((int *)(v->data) + index) = *((int *)element);
|
*((int *)(v->data) + index) = *((int *)element);
|
||||||
break;
|
break;
|
||||||
case FLOAT:
|
case FLOAT:
|
||||||
for (int i = v->size-1; i > index; i--) {
|
for (int i = v->size-1; i > index; i--) {
|
||||||
*((float *)(v->data) + i) = *((float *)(v->data) + i-1);
|
*((float *)(v->data) + i) = *((float *)(v->data) + i-1);
|
||||||
}
|
}
|
||||||
*((float *)(v->data) + index) = *((float *)element);
|
*((float *)(v->data) + index) = *((float *)element);
|
||||||
break;
|
break;
|
||||||
case CHAR:
|
case CHAR:
|
||||||
for (int i = v->size-1; i > index; i--) {
|
for (int i = v->size-1; i > index; i--) {
|
||||||
*((char *)(v->data) + i) = *((char *)(v->data) + i-1);
|
*((char *)(v->data) + i) = *((char *)(v->data) + i-1);
|
||||||
}
|
}
|
||||||
*((char *)(v->data) + index) = *((char *)element);
|
*((char *)(v->data) + index) = *((char *)element);
|
||||||
break;
|
break;
|
||||||
case VECTOR:
|
case VECTOR:
|
||||||
for (int i = v->size-1; i > index; i--) {
|
for (int i = v->size-1; i > index; i--) {
|
||||||
*((vector_t *)(v->data) + i) = *((vector_t *)(v->data) + i-1);
|
*((vector_t *)(v->data) + i) = *((vector_t *)(v->data) + i-1);
|
||||||
}
|
}
|
||||||
*((vector_t *)(v->data) + index) = *((vector_t *)element);
|
*((vector_t *)(v->data) + index) = *((vector_t *)element);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
perror("specify a valid vector type\n");
|
perror("specify a valid vector type\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user