| 223 | } |
| 224 | |
| 225 | void StringPool::sortByConfig() |
| 226 | { |
| 227 | LOG_ALWAYS_FATAL_IF(mOriginalPosToNewPos.size() > 0, "Can't sort string pool after already sorted."); |
| 228 | |
| 229 | const size_t N = mEntryArray.size(); |
| 230 | |
| 231 | // This is a vector that starts out with a 1:1 mapping to entries |
| 232 | // in the array, which we will sort to come up with the desired order. |
| 233 | // At that point it maps from the new position in the array to the |
| 234 | // original position the entry appeared. |
| 235 | Vector<size_t> newPosToOriginalPos; |
| 236 | newPosToOriginalPos.setCapacity(N); |
| 237 | for (size_t i=0; i < N; i++) { |
| 238 | newPosToOriginalPos.add(i); |
| 239 | } |
| 240 | |
| 241 | // Sort the array. |
| 242 | NOISY(printf("SORTING STRINGS BY CONFIGURATION...\n")); |
| 243 | // Vector::sort uses insertion sort, which is very slow for this data set. |
| 244 | // Use quicksort instead because we don't need a stable sort here. |
| 245 | qsort_r_compat(newPosToOriginalPos.editArray(), N, sizeof(size_t), this, config_sort); |
| 246 | //newPosToOriginalPos.sort(config_sort, this); |
| 247 | NOISY(printf("DONE SORTING STRINGS BY CONFIGURATION.\n")); |
| 248 | |
| 249 | // Create the reverse mapping from the original position in the array |
| 250 | // to the new position where it appears in the sorted array. This is |
| 251 | // so that clients can re-map any positions they had previously stored. |
| 252 | mOriginalPosToNewPos = newPosToOriginalPos; |
| 253 | for (size_t i=0; i<N; i++) { |
| 254 | mOriginalPosToNewPos.editItemAt(newPosToOriginalPos[i]) = i; |
| 255 | } |
| 256 | |
| 257 | #if 0 |
| 258 | SortedVector<entry> entries; |
| 259 | |
| 260 | for (size_t i=0; i<N; i++) { |
| 261 | printf("#%d was %d: %s\n", i, newPosToOriginalPos[i], |
| 262 | mEntries[mEntryArray[newPosToOriginalPos[i]]].makeConfigsString().string()); |
| 263 | entries.add(mEntries[mEntryArray[i]]); |
| 264 | } |
| 265 | |
| 266 | for (size_t i=0; i<entries.size(); i++) { |
| 267 | printf("Sorted config #%d: %s\n", i, |
| 268 | entries[i].makeConfigsString().string()); |
| 269 | } |
| 270 | #endif |
| 271 | |
| 272 | // Now we rebuild the arrays. |
| 273 | Vector<entry> newEntries; |
| 274 | Vector<size_t> newEntryArray; |
| 275 | Vector<entry_style> newEntryStyleArray; |
| 276 | DefaultKeyedVector<size_t, size_t> origOffsetToNewOffset; |
| 277 | |
| 278 | for (size_t i=0; i<N; i++) { |
| 279 | // We are filling in new offset 'i'; oldI is where we can find it |
| 280 | // in the original data structure. |
| 281 | size_t oldI = newPosToOriginalPos[i]; |
| 282 | // This is the actual entry associated with the old offset. |
no test coverage detected