Added copy and compare functions

This commit is contained in:
2019-03-11 00:37:18 -04:00
parent 5eb4931002
commit d9da762bd9
2 changed files with 77 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
//myHelper/DynamicInt64Array.h
//Matthew Ellison
// Created: 03-08-19
//Modified: 03-09-19
//Modified: 03-10-19
//This is the implementation for a dynamic array in c
/*
Copyright (C) 2019 Matthew Ellison
@@ -298,6 +298,47 @@ bool isSortedDynamicInt64Array(struct DynamicInt64Array* ary){
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
int64_t getSumDynamicInt64Array(struct DynamicInt64Array* ary){
int64_t sum = 0;