Download src/core/id.cpp from fava21/hf_repo4: direct link, hf CLI and curl.
- Browser
- Download file 3.22 kB
-
https://huggingface.co/datasets/fava21/hf_repo4/resolve/main/src/core/id.cpp
- Command line
-
hf download hf://datasets/fava21/hf_repo4/src/core/id.cpp
-
curl -L -o id.cpp https://huggingface.co/datasets/fava21/hf_repo4/resolve/main/src/core/id.cpp
3.22 kB
| // 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. | |
| 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 | |