MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / basic_look_up_table

Class basic_look_up_table

include/stringzilla/stringzilla.hpp:345–384  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

343 */
344template <typename char_type_ = char>
345class basic_look_up_table {
346 static_assert(sizeof(char_type_) == 1 || sizeof(char_type_) == 2 || sizeof(char_type_) == 4,
347 "Character type must be 1, 2, or 4 bytes long");
348 static constexpr std::size_t size_k = sizeof(char_type_) == 1 ? 256ul
349 : sizeof(char_type_) == 2 ? 65536ul
350 : 4294967296ul;
351 static constexpr std::size_t bytes_k = size_k * sizeof(char_type_);
352 using unsigned_type_ = typename std::make_unsigned<char_type_>::type;
353
354 char_type_ lut_[size_k];
355
356 public:
357 using char_type = char_type_;
358
359 basic_look_up_table() noexcept { memset(&lut_[0], 0, bytes_k); }
360 explicit basic_look_up_table(char_type const (&chars)[size_k]) noexcept { memcpy(&lut_[0], chars, bytes_k); }
361 basic_look_up_table(std::array<char_type, size_k> const &chars) noexcept {
362 memcpy(&lut_[0], chars.data(), bytes_k);
363 }
364
365 basic_look_up_table(basic_look_up_table const &other) noexcept { memcpy(&lut_[0], other.lut_, bytes_k); }
366 basic_look_up_table &operator=(basic_look_up_table const &other) noexcept {
367 memcpy(&lut_[0], other.lut_, bytes_k);
368 return *this;
369 }
370
371 /**
372 * @brief Creates a look-up table with a one-to-one mapping of characters to themselves.
373 * @see Similar to `std::iota` filling, but properly handles signed integer casts.
374 */
375 static basic_look_up_table identity() noexcept {
376 basic_look_up_table result;
377 for (std::size_t i = 0; i < size_k; ++i) { result.lut_[i] = static_cast<unsigned_type_>(i); }
378 return result;
379 }
380
381 inline sz_cptr_t raw() const noexcept { return reinterpret_cast<sz_cptr_t>(&lut_[0]); }
382 inline char_type &operator[](char_type c) noexcept { return lut_[sz_bitcast_(unsigned_type_, c)]; }
383 inline char_type const &operator[](char_type c) const noexcept { return lut_[sz_bitcast_(unsigned_type_, c)]; }
384};
385
386using look_up_table = basic_look_up_table<char>;
387

Callers

nothing calls this directly

Calls 1

memcpyFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…