| 799 | } |
| 800 | |
| 801 | void Encoder::sortDict(valueArray &items) { |
| 802 | auto &keys = items.keys; |
| 803 | size_t n = keys.size(); |
| 804 | if (n < 2) |
| 805 | return; |
| 806 | |
| 807 | // Fill in the pointers of any keys that refer to inline strings: |
| 808 | for (unsigned i = 0; i < n; i++) { |
| 809 | if (keys[i].buf == nullptr) { |
| 810 | const Value *item = &items[2*i]; |
| 811 | if (item->tag() == kStringTag) { |
| 812 | keys[i].buf = offsetby(item, 1); // inline string |
| 813 | } else { |
| 814 | assert(item->tag() == kShortIntTag); |
| 815 | keys[i] = {nullptr, (size_t)item->asUnsigned()}; // integer |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | // Construct an array that describes the permutation of item indices: |
| 821 | TempArray(indices, const FLSlice*, n); |
| 822 | const FLSlice* base = &keys[0]; |
| 823 | for (unsigned i = 0; i < n; i++) |
| 824 | indices[i] = base + i; |
| 825 | std::sort(&indices[0], &indices[n], &compareKeysByIndex); |
| 826 | // indices[i] is now a pointer to the Value that should go at index i |
| 827 | |
| 828 | // Now rewrite items according to the permutation in indices: |
| 829 | TempArray(oldBuf, char, 2*n * sizeof(Value)); |
| 830 | auto old = (Value*)oldBuf; |
| 831 | memcpy(old, &items[0], 2*n * sizeof(Value)); |
| 832 | for (size_t i = 0; i < n; i++) { |
| 833 | auto j = indices[i] - base; |
| 834 | if ((ssize_t)i != j) { |
| 835 | items[2*i] = old[2*j]; |
| 836 | items[2*i+1] = old[2*j+1]; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | } } |
nothing calls this directly
no test coverage detected