当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Maintainability issue with this vector implementation

问题描述:

I have a question concerning the right/wrong (design) approach to tackle a problem. First I clarify the situation, then my solution and finally the problem with my solution. I'm implementing it in c++.

The situation:

  • A class of type y needs to hold and process a collection of information of a certain type x.
  • Each instance of this type is only relevant to a specific object.
  • An object should be able to do 3 things: look up information about the instance that is relevant to it, delete the instance and add a new instance.

I implemented the following:

  • All the information is kept in a vector 1.
  • Other objects have the index of the element in the vector to find the correct instance.

    However what if I delete an element? indexes should still be valid so I did this:

  • Another vector 2 is kept in parallel which holds boolean values, true if an index is available for a new instance, false if it is taken. Instead of deleting the instance in vector 1 I set vector 2 to true. Now if a new instance is added it will be added to the first available position (or at the back). This way no element is ever deleted but the vector doesn't grow each time an instance is added.

The problem with this solution is:

The correct behaviour of this system is completely dependant on every object that has an index. If one handles their index bad problems will occur and it is very difficult to track where the index is badly handled.

For example: object A releases it's instance (notifies class y) but doesn't delete the index, and it continues to apply operations on the element while that index has now been assigned to another object and holds a different instance!

So I am looking for a more reliable solution, possible a better data structure. Thank you.

EDIT: I thought a description would be the most helpful but here is the code from that class that holds the data.

these are the vectors.

std::vector<bool> availability;

std::vector<X> data;

and here are the methods

Y::Y() : data(1, X), availability(1, false) {

}

int Y::createNewInstance() {

if(data.size() > 1) {

bool indexFound = false;

std::vector<X>::size_type chosenIndex;

for(std::vector<X>::size_type i = 1; i < availability.size(); ++i) {

//if true, the index is not taken, so use it!

if (availability.at(i) == true) {

chosenIndex = i;

availability.at(chosenIndex) = false;

indexFound = true;

break;

}

}

if (indexFound) {

data.at(chosenIndex) = X;

return chosenIndex;

} else {

data.push_back(X);

availability.push_back(false);

return availability.size() - 1;

}

} else {

startTimes.push_back(X);

availability.push_back(false);

return availability.size() - 1;

}

}

X Y::getDataElement(int index, bool finish) {

if (data.size() > index && index != 0) {

//if finish is true, make element available for other users

if(finish) {

availability.at(index) = true;

}

return data.at(index);

}

}

网友答案:

What about using std::map as your container, where keys will be your indices. You won't have problems with changing indices while removing elements. You will need to write one function like findFreeIndex to manage free indices. If you don't need to add elements in "free" space you don't even need this. You can simply always add new elements in maximal index plus one.

This solution has minor performance penalty, as accessing map is little slower than accessing vector, but usually this is not a problem.

分享给朋友:
您可能感兴趣的文章:
随机阅读: