stage/structure/tokens.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | #include "chrono_batch_render/stage/structure/tokens.h" | ||
| 3 | |||
| 4 | #include "chrono_batch_render/util/error.h" | ||
| 5 | |||
| 6 | namespace chrono::batch_render { | ||
| 7 | |||
| 8 | 511 | Tokens::Tokens() { | |
| 9 | 511 | Intern(""); | |
| 10 | 511 | } | |
| 11 | |||
| 12 | 51284 | Token Tokens::Intern(std::string_view text) { | |
| 13 | 51284 | if (const auto found = m_ids.find(text); found != m_ids.end()) { | |
| 14 | 12708 | return {found->second}; | |
| 15 | } | ||
| 16 | 38576 | const uint32_t id = uint32_t(m_texts.size()); | |
| 17 | 38576 | const std::string& stored = m_texts.emplace_back(text); | |
| 18 | 38576 | m_ids.emplace(stored, id); | |
| 19 | 38576 | return {id}; | |
| 20 | } | ||
| 21 | |||
| 22 | 152678 | Token Tokens::Find(std::string_view text) const { | |
| 23 | 152678 | const auto found = m_ids.find(text); | |
| 24 | 255546 | return found == m_ids.end() ? Token{} : Token{found->second}; | |
| 25 | } | ||
| 26 | |||
| 27 | 20689 | std::string_view Tokens::Text(Token token) const { | |
| 28 | 20689 | if (token.id >= m_texts.size()) { | |
| 29 | 1 | throw Error("No token {}", token.id); | |
| 30 | } | ||
| 31 | 20688 | return m_texts[token.id]; | |
| 32 | } | ||
| 33 | |||
| 34 | } | ||
| 35 |