ZVISION: Update removeDuplicateEntries to preserve order

The new algorithm is potentially slower, but it doesn't need
to use sort. Speed shouldn't be a problem because the function
isn't used that often and in each case the size of the container
is small.
This commit is contained in:
RichieSams 2013-10-01 18:46:57 -05:00
parent 1f9ba897b8
commit b913e9f8a3

View File

@ -61,19 +61,33 @@ void trimCommentsAndWhiteSpace(Common::String *string);
void dumpEveryResultAction(const Common::String &destFile);
/**
* Removes all duplicate entries from container.
* Removes all duplicate entries from container. Relative order will be preserved.
*
* @param container
* @return
* @param container The Array to remove duplicate entries from
*/
template<class T>
void removeDuplicateEntries(Common::Array<T> &container) {
Common::sort(container.begin(), container.end());
// Length of modified array
int newLength = 1;
int j;
for (uint i = 0; i + 1 < container.size(); ++i) {
while (i + 1 < container.size() && container[i] == container[i + 1]) {
container.remove_at(i + 1);
for(int i = 1; i < container.size(); i++) {
for(j = 0; j < newLength; j++) {
if (container[i] == container[j]) {
break;
}
}
// If none of the values in index[0..j] of container are the same as array[i],
// then copy the current value to corresponding new position in array
if (j == newLength) {
container[newLength++] = container[i];
}
}
// Actually remove the unneeded space
while (container.size() < newLength) {
container.pop_back();
}
}