File size: 3,223 Bytes
2edf277 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | // FCS Glass - src/core/id.cpp
//
// Implements: C2. See src/core/id.h for the design decisions.
//
// REQ C2 - the id path. FNV-1a 64-bit. The constants are the published
// offset basis and prime for 64-bit FNV; they are named rather than inlined so
// the algorithm is recognisable at a glance.
#include "core/id.h"
#include <cstddef> // std::size_t; <cstdint> does not guarantee it
namespace fcs {
namespace core {
namespace {
constexpr Id kFnvOffsetBasis = 1469598103934665603ULL; // 0xcbf29ce484222325
constexpr Id kFnvPrime = 1099511628211ULL; // 0x100000001b3
// Design note 3: keep 0 out of the hash range so kNullId is unambiguous.
Id NonZero(Id h) { return (h == kNullId) ? Id{1} : h; }
// Length without <cstring>: this file is lifted verbatim into the portable
// harness, and one more project header dependency is one more thing to keep in
// sync for no benefit.
std::size_t NameLength(const char* name) {
std::size_t n = 0;
while (name[n] != '\0') ++n;
return n;
}
} // namespace
// REQ C2
Id HashName(Id parent, const char* name) {
Id h = (parent == kNullId) ? kFnvOffsetBasis : parent;
// The LENGTH is folded in first, and that is load-bearing rather than
// tidiness. FNV-1a is a plain sequential fold, so without a separator a PATH
// and a flat name are the same computation: HashName(p, "ab") came out equal
// to HashName(HashName(p, "a"), "b"), and the harness caught it. Folding the
// depth instead would not help - "a" then "bc" and "ab" then "c" share a depth
// sequence and the same byte stream. Both collisions are silent: no crash,
// just two different widgets inheriting each other's hover state.
const std::size_t n = NameLength(name);
h ^= static_cast<Id>(n);
h *= kFnvPrime;
for (std::size_t i = 0; i < n; ++i) {
h ^= static_cast<Id>(static_cast<unsigned char>(name[i]));
h *= kFnvPrime;
}
return NonZero(h);
}
// REQ C2 - folded a byte at a time for the same reason HashName is: a single
// multiply would let 0..255 and 256..511 share an id.
Id HashIndex(Id parent, int index) {
Id h = (parent == kNullId) ? kFnvOffsetBasis : parent;
const unsigned int v = static_cast<unsigned int>(index);
for (int b = 0; b < 4; ++b) {
h ^= static_cast<Id>((v >> (8 * b)) & 0xFFu);
h *= kFnvPrime;
}
return NonZero(h);
}
// REQ C2
void IdStack::Push(Id id) {
// Design note 3 of the header: a no-op beats an overflow, because a dropped
// scope costs two widgets a shared id and an overflow costs memory safety.
if (depth_ >= kMaxIdDepth) return;
stack_[depth_++] = id;
}
void IdStack::PushName(const char* name) { Push(HashName(Current(), name)); }
void IdStack::PushIndex(int index) { Push(HashIndex(Current(), index)); }
Id IdStack::Current() const { return (depth_ > 0) ? stack_[depth_ - 1] : kNullId; }
Id IdStack::Parent() const { return (depth_ > 1) ? stack_[depth_ - 2] : kNullId; }
// REQ C2 - at depth 0 this is a no-op, so depth can never go negative and
// Current() can never be read from a popped stack.
void IdStack::Pop() {
if (depth_ > 0) --depth_;
}
} // namespace core
} // namespace fcs
|