I was working with my own "Vector"-like class and thought about something.
If I use the [] operator and return a reference, is there any way to mark that indice when a value is assigned to it?
For example.. if I want to make a trim function that removes the "garbage" information when I allow the user to specify a size for the Array Class and call trim.
Here's the code in case you need to see it.
#pragma once
/*
MyLibrary.h
Meant to be used with stack-objects only. Namely primitive types (hence the name).
*/
template<typename P>
class PrimArray{
private:
P *value;
int currentSize;
public:
PrimArray(){
int temp = 1;
value = (P*)malloc(temp * sizeof(P));
currentSize = temp;
};
P *copyContents(){//returns a deep copy of this array-class
P *copy = (P*)malloc(currentSize * sizeof(P));
for(int i = 0; i < currentSize; i++)
copy[i] = (*this)[i];
return copy;
}
~PrimArray(){free((void*)value);};
int length(){return currentSize;};
bool storeValue(P arg, int location){
if(location >= 0 && location < currentSize){
(*this)[location] = arg;
return true;
}
else if(location == currentSize){
P *temp = (P*)malloc(currentSize * sizeof(P));
for(int i = 0; i < currentSize; i++)
temp[i] = (*this)[i];
currentSize++;
free((void*)value);
value = (P*)malloc(currentSize * sizeof(P));
for(int i = 0; i < currentSize; i++)
{
if(i != (currentSize - 1))
(*this)[i] = temp[i];
else (*this)[i] = arg;
}
free((void*)temp);
return true;
}
else{
std::cout << "Could not store value at indice: "<< location;
std::cout << ". " << "\nCurrent size is: "<< currentSize << std::endl;
return false;
}
};
bool storeValue(P arg){//appends the value to the end of this prim array
P *temp = (P*)malloc(currentSize * sizeof(P));
for(int i = 0; i < currentSize; i++)
temp[i] = (*this)[i];
currentSize++;
free((void*)value);
value = (P*)malloc(currentSize * sizeof(P));
for(int i = 0; i < currentSize; i++){
if(i != (currentSize - 1))
(*this)[i] = temp[i];
else (*this)[i] = arg;
}
free((void*)temp);
return true;
}
void trimToSize(int startRange = 0, int endRange){//trims this array
if(startRange <= endRange && startRange >= 0
&& endRange < currentSize){
if(endRange <= 0 || startRange >= endRange){
clear();
}
else{
P *temp = (P*)malloc((endRange - startRange) * sizeof(P));
for(int i = startRange; i < endRange; i++)
temp[i - startRange] = (*this)[i];
free((void*)value);
currentSize = (endRange - startRange);
value = (P*)malloc(currentSize * sizeof(P));
for(int i = 0; i < currentSize; i++)
(*this)[i] = temp[i];
free((void*)temp);
}
}
else{
std::cout << "Trim failed! ";
std::cout << "Current size is: "<< currentSize;
std::cout << std::endl;
}
}
void clear(){
free((void*)value);
value = (P*)malloc(1 * sizeof(P));
currentSize = 1;
}
void displayContents(){
for(int i = 0; i < currentSize; i++){
if(i != (currentSize - 1))
std::cout << (*this)[i] << ", ";
else std::cout << (*this)[i] << std::endl;
}
};
P &operator[](int arg){
P *temp = 0;
if(arg >= 0 && arg < currentSize)
return value[arg];
else
{
std::cout << "Index out of bounds!";
std::cout << "Could not access reference at location: ";
std::cout << arg << "!" << std::endl;
return *temp;
}
};
};