mirror of
https://bitbucket.org/Mattrixwv/myhelpers.git
synced 2025-12-06 18:43:59 -05:00
Added copy and compare functions
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
//myHelper/DynamicInt64Array.h
|
//myHelper/DynamicInt64Array.h
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-08-19
|
// Created: 03-08-19
|
||||||
//Modified: 03-09-19
|
//Modified: 03-10-19
|
||||||
//This is the implementation for a dynamic array in c
|
//This is the implementation for a dynamic array in c
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2019 Matthew Ellison
|
Copyright (C) 2019 Matthew Ellison
|
||||||
@@ -298,6 +298,47 @@ bool isSortedDynamicInt64Array(struct DynamicInt64Array* ary){
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Compares 2 DynamicInt64Array. Returns 0 if equal, -1 if ary1 is < ary2, and 1 if ary1 > ary2
|
||||||
|
int compareDynamicInt64Array(struct DynamicInt64Array* ary1, struct DynamicInt64Array* ary2){
|
||||||
|
//Make sure they are the same size
|
||||||
|
if(ary1->size < ary2->size){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Step through every element in each array, checking that they are the same
|
||||||
|
for(uint64_t location = 0;location < ary1->size;++location){
|
||||||
|
//If an element is not the same see if ary1 is larger or smaller than ary2
|
||||||
|
if(ary1->ptr[location] != ary2->ptr[location]){
|
||||||
|
if(ary1->ptr[location] < ary2->ptr[location]){
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//If it made it all the way through the loop without triggering an inequality they are equal
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Performs a deep copy on a DynamicInt64Array
|
||||||
|
void copyDynamicInt64Array(struct DynamicInt64Array* ary1, struct DynamicInt64Array* ary2){
|
||||||
|
//Make sure the second array is empty
|
||||||
|
if(ary2->size != 0){
|
||||||
|
destroyDynamicInt64Array(ary2);
|
||||||
|
initDynamicInt64Array(ary2);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Reserve the propper amount of space
|
||||||
|
reserveDynamicInt64Array(ary2, ary1->size);
|
||||||
|
|
||||||
|
//Look through every element in the first array and copy it to the second
|
||||||
|
for(uint64_t location = 0;location < ary1->size;++location){
|
||||||
|
pushBackDynamicInt64Array(ary2, ary1->ptr[location]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//This function returns the sum of all elements in a DynamicInt64Array
|
//This function returns the sum of all elements in a DynamicInt64Array
|
||||||
int64_t getSumDynamicInt64Array(struct DynamicInt64Array* ary){
|
int64_t getSumDynamicInt64Array(struct DynamicInt64Array* ary){
|
||||||
int64_t sum = 0;
|
int64_t sum = 0;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//myHelper/DynamicInt64Array.h
|
//myHelper/DynamicInt64Array.h
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-08-19
|
// Created: 03-08-19
|
||||||
//Modified: 03-09-19
|
//Modified: 03-10-19
|
||||||
//This is the test for my dynamic array in c
|
//This is the test for my dynamic array in c
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2019 Matthew Ellison
|
Copyright (C) 2019 Matthew Ellison
|
||||||
@@ -178,8 +178,42 @@ int main(){
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Test copy function
|
||||||
|
struct DynamicInt64Array testAry2;
|
||||||
|
initDynamicInt64Array(&testAry2);
|
||||||
|
copyDynamicInt64Array(&testAry, &testAry2);
|
||||||
|
if((testAry.ptr[0] == testAry2.ptr[0]) && (testAry.ptr[1] == testAry2.ptr[1]) && (testAry.ptr[2] == testAry2.ptr[2]) && (testAry.ptr[3] == testAry2.ptr[3]) && (testAry.ptr[4] == testAry2.ptr[4]) && (testAry.ptr[5] == testAry2.ptr[5])){
|
||||||
|
printf("copy is working correctly\n");
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
printf("There is something wrong with copy\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Test compare function
|
||||||
|
//Test equals
|
||||||
|
if(compareDynamicInt64Array(&testAry, &testAry2) != 0){
|
||||||
|
printf("There is a problem with compare equals\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
//Test lessThan
|
||||||
|
pushBackDynamicInt64Array(&testAry2, 7);
|
||||||
|
if(compareDynamicInt64Array(&testAry, &testAry2) != -1){
|
||||||
|
printf("There is a problem with compare less than\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
//Test greaterThan
|
||||||
|
if(compareDynamicInt64Array(&testAry2, &testAry) != 1){
|
||||||
|
printf("There is a problem with compare greater than\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
printf("compare is working correctly\n");
|
||||||
|
}
|
||||||
|
|
||||||
//Release all of the memory allocated by the array
|
//Release all of the memory allocated by the array
|
||||||
destroyDynamicInt64Array(&testAry);
|
destroyDynamicInt64Array(&testAry);
|
||||||
|
destroyDynamicInt64Array(&testAry2);
|
||||||
|
|
||||||
printf("\n\nEND OF TESTS\n");
|
printf("\n\nEND OF TESTS\n");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user