| 289 | // --------------------------------------------------------------------------- |
| 290 | |
| 291 | DictionaryEncoded dictionaryEncodeStrings( |
| 292 | Span<const uint8_t> offsets_data, Span<const uint8_t> values_data, std::size_t row_count) { |
| 293 | DictionaryEncoded result; |
| 294 | result.count = row_count; |
| 295 | |
| 296 | if (row_count == 0) { |
| 297 | result.index_bytes = 1; |
| 298 | return result; |
| 299 | } |
| 300 | |
| 301 | // First pass: build dictionary to determine size |
| 302 | tsl::robin_map<std::string, uint32_t> lookup; |
| 303 | std::vector<uint32_t> temp_indices; |
| 304 | temp_indices.reserve(row_count); |
| 305 | |
| 306 | for (std::size_t row = 0; row < row_count; ++row) { |
| 307 | uint32_t start_offset = 0; |
| 308 | uint32_t end_offset = 0; |
| 309 | std::memcpy(&start_offset, offsets_data.data() + row * sizeof(uint32_t), sizeof(uint32_t)); |
| 310 | std::memcpy(&end_offset, offsets_data.data() + (row + 1) * sizeof(uint32_t), sizeof(uint32_t)); |
| 311 | |
| 312 | std::string_view str_view( |
| 313 | reinterpret_cast<const char*>(values_data.data() + start_offset), end_offset - start_offset); |
| 314 | |
| 315 | std::string key_str(str_view); |
| 316 | auto it = lookup.find(key_str); |
| 317 | uint32_t index = 0; |
| 318 | if (it != lookup.end()) { |
| 319 | index = it->second; |
| 320 | } else { |
| 321 | index = static_cast<uint32_t>(result.dictionary.size()); |
| 322 | result.dictionary.push_back(key_str); |
| 323 | lookup[std::move(key_str)] = index; |
| 324 | } |
| 325 | temp_indices.push_back(index); |
| 326 | } |
| 327 | |
| 328 | // Determine narrowed index width |
| 329 | result.index_bytes = indexBytesFor(result.dictionary.size()); |
| 330 | result.indices.reserve(row_count * result.index_bytes); |
| 331 | |
| 332 | for (uint32_t idx : temp_indices) { |
| 333 | writeIndex(result.indices, idx, result.index_bytes); |
| 334 | } |
| 335 | |
| 336 | return result; |
| 337 | } |
| 338 | |
| 339 | std::string_view dictionaryLookup(const DictionaryEncoded& encoded, std::size_t row) { |
| 340 | uint32_t index = readIndex(encoded.indices.data(), row, encoded.index_bytes); |