Branch data Line data Source code
1 : : // SPDX-License-Identifier: MIT
2 : : // Copyright (c) 2026 Dim Himro
3 : :
4 : : #ifndef DEFINITION_KEY_H
5 : : #define DEFINITION_KEY_H
6 : :
7 : : #include <cstdint>
8 : : #include <functional>
9 : : #include <string>
10 : : #include <utility>
11 : :
12 : : /**
13 : : * @brief Stable frame identifier: normalized CAN ID and frame type.
14 : : */
15 : : struct FrameKey {
16 : : std::uint32_t canId = 0U; /**< Normalized CAN identifier. */
17 : : bool isExtended = false; /**< True for extended CAN frames. */
18 : :
19 : 12789 : FrameKey() = default;
20 : :
21 : : /** @brief Create a key from a normalized identifier and frame type. */
22 : 23974 : FrameKey(std::uint32_t id, bool extended)
23 : 23974 : : canId(id), isExtended(extended) {
24 : 23974 : }
25 : :
26 : : /** @brief Compare all logical identity fields. */
27 : 6502 : bool operator==(const FrameKey &other) const {
28 [ + + + - ]: 6502 : return (canId == other.canId) && (isExtended == other.isExtended);
29 : : }
30 : : };
31 : :
32 : : /**
33 : : * @brief Hasher for frame key.
34 : : */
35 : : struct FrameKeyHasher {
36 : : /** @brief Hash the logical identity fields. */
37 : 11161 : std::size_t operator()(const FrameKey &key) const {
38 : 11161 : const std::size_t a = static_cast<std::size_t>(key.canId);
39 [ + + ]: 11161 : const std::size_t b = key.isExtended ? 1U : 0U;
40 : 11161 : return (a * 1315423911U) ^ b;
41 : : }
42 : : };
43 : :
44 : : /** Stable signal identifier within one logical DBC database. */
45 : : struct SignalKey {
46 : : FrameKey frame; /**< Owning frame identity. */
47 : : std::string name; /**< Signal name within the frame. */
48 : :
49 : 6380 : SignalKey() = default;
50 : : /** @brief Create a signal key from its frame identity and name. */
51 : 6454 : SignalKey(FrameKey frameKey, std::string signalName)
52 : 6454 : : frame(frameKey), name(std::move(signalName)) {
53 : 6454 : }
54 : :
55 : : /** @brief Compare all logical identity fields. */
56 : 22 : bool operator==(const SignalKey &other) const {
57 [ + - + - ]: 22 : return frame == other.frame && name == other.name;
58 : : }
59 : : };
60 : :
61 : : /** @brief Hasher for stable signal keys. */
62 : : struct SignalKeyHasher {
63 : : /** @brief Hash the logical identity fields. */
64 : 9 : std::size_t operator()(const SignalKey &key) const {
65 : 9 : return FrameKeyHasher()(key.frame) ^ (std::hash<std::string>()(key.name) << 1U);
66 : : }
67 : : };
68 : :
69 : : #endif /* DEFINITION_KEY_H */
|