I am getting an out of range exception with my while loop in my code.
I have tried to use an Array instead and the error does not occur with the same loop structure. I am confused why this would happen. I believe that the savedValue <= value[rank.at(current-1)]) statement is what is causing the error.
int suffixArray::size;
std::vector<int> suffixArray::rank;
int main( int argc, char* argv[]) {
std:: string test = "BANANA$";
suffixArray testString (test);
return 0;
}
#include <iostream>
#include <vector>
class suffixArray{
public: static int size;
public: static std::vector<int> rank;
public: suffixArray(std:: string concatenated ){
size =(int) concatenated.length();
std:: cout << size << std:: endl;
rank.resize(7);
char *suffixPointers[concatenated.length()];
int value[concatenated.length()];
for(int i =0; i <= size-1; i++){
suffixPointers[i] = &concatenated[i];
value[i] = (int)concatenated[i];
}
std::cout << "[";
for(int i = 0; i<= size-1; i++){
std::cout <<value[i] << " ";
}
std::cout << "]"<< std:: endl;
for(int i = 0; i<=size -1; i++){
if(i == 0){
rank.assign(i,i);
}
else if(value[i] > value[i-1]){
rank.assign(i,i);
}else{
int current =i;
int savedValue = value[i];
int prevSavedRank;
while(current-1 >= 0 && savedValue <= value[rank.at(current-1)]){
prevSavedRank= rank.at(current-1);
rank.assign(current-1, i);
rank.assign(current, prevSavedRank);
current--;
}
}
}
}
};