Move CompactifyArray to array_utils.

1. Rename it to MapValuesToContiguousRange.
2. Improve implementation to just use a vector.

Change-Id: I2a5e73e3a6fb81694c6f452a00d60df16c0149e6
This commit is contained in:
Sameer Agarwal
2014-05-29 13:50:43 -07:00
parent 9a022f601b
commit 041f27516a
6 changed files with 73 additions and 71 deletions
+16 -1
View File
@@ -30,10 +30,11 @@
#include "ceres/array_utils.h"
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <string>
#include <vector>
#include "ceres/fpclassify.h"
#include "ceres/stringprintf.h"
@@ -94,5 +95,19 @@ void AppendArrayToString(const int size, const double* x, string* result) {
}
}
void MapValuesToContiguousRange(const int size, int* array) {
std::vector<int> unique_values(array, array + size);
std::sort(unique_values.begin(), unique_values.end());
unique_values.erase(std::unique(unique_values.begin(),
unique_values.end()),
unique_values.end());
for (int i = 0; i < size; ++i) {
array[i] = std::lower_bound(unique_values.begin(),
unique_values.end(),
array[i]) - unique_values.begin();
}
}
} // namespace internal
} // namespace ceres