Platform.Data.Doublets C++
Loading...
Searching...
No Matches
ILinksExtensions.Structure.h
Go to the documentation of this file.
1#include <concepts>
2#include <ostream>
3#include <sstream>
4#include <string>
5#include <unordered_set>
6
8{
11 template <typename TStorage, typename TIsElement, typename TAppendElement>
12 void AppendStructure(TStorage& storage, std::ostream& output,
13 std::unordered_set<typename TStorage::LinkAddressType>& visited,
14 typename TStorage::LinkAddressType address, const TIsElement& isElement,
15 const TAppendElement& appendElement, bool renderIndex = false, bool renderDebug = false)
16 {
17 using Address = typename TStorage::LinkAddressType;
18 const auto& constants = storage.Constants;
19 if (address == constants.Null || address == constants.Any || address == constants.Itself)
20 return;
21 if (!Exists(storage, address))
22 {
23 if (renderDebug)
24 output << '~';
25 output << std::to_string(address);
26 return;
27 }
28 if (!visited.insert(address).second)
29 {
30 if (renderDebug)
31 output << '*';
32 output << std::to_string(address);
33 return;
34 }
35 const Link<Address> link{GetLink(storage, address)};
36 output << '(';
37 if (renderIndex)
38 output << std::to_string(address) << ':';
39 auto appendReference = [&](Address reference) {
40 if (reference == address)
41 {
42 output << std::to_string(reference);
43 return;
44 }
45 // Missing raw sequence values must not be dereferenced (issue #242).
46 if (reference != constants.Null && reference != constants.Any && reference != constants.Itself &&
47 Exists(storage, reference))
48 {
49 const Link<Address> element{GetLink(storage, reference)};
50 if (isElement(element))
51 {
52 appendElement(output, element);
53 return;
54 }
55 }
56 AppendStructure(storage, output, visited, reference, isElement, appendElement, renderIndex, renderDebug);
57 };
58 appendReference(link.Source);
59 output << ' ';
60 appendReference(link.Target);
61 output << ')';
62 }
63
65 template <typename TStorage, typename TIsElement, typename TAppendElement>
66 requires std::invocable<const TAppendElement&, std::ostream&, const Link<typename TStorage::LinkAddressType>&>
67 std::string FormatStructure(TStorage& storage, typename TStorage::LinkAddressType address,
68 const TIsElement& isElement, const TAppendElement& appendElement,
69 bool renderIndex = false, bool renderDebug = false)
70 {
71 std::ostringstream output;
72 std::unordered_set<typename TStorage::LinkAddressType> visited;
73 AppendStructure(storage, output, visited, address, isElement, appendElement, renderIndex, renderDebug);
74 return output.str();
75 }
76
78 template <typename TStorage, typename TIsElement>
79 std::string FormatStructure(TStorage& storage, typename TStorage::LinkAddressType address,
80 const TIsElement& isElement, bool renderIndex = false, bool renderDebug = false)
81 {
82 auto appendElement = [](std::ostream& output, const Link<typename TStorage::LinkAddressType>& element) {
83 output << std::to_string(element.Index);
84 };
85 return FormatStructure(storage, address, isElement, appendElement, renderIndex, renderDebug);
86 }
87} // namespace Platform::Data::Doublets
Definition TargetMatcher.h:2
std::string FormatStructure(TStorage &storage, typename TStorage::LinkAddressType address, const TIsElement &isElement, const TAppendElement &appendElement, bool renderIndex=false, bool renderDebug=false)
Formats a structure with custom rendering for links accepted by isElement.
Definition ILinksExtensions.Structure.h:67
void AppendStructure(TStorage &storage, std::ostream &output, std::unordered_set< typename TStorage::LinkAddressType > &visited, typename TStorage::LinkAddressType address, const TIsElement &isElement, const TAppendElement &appendElement, bool renderIndex=false, bool renderDebug=false)
Definition ILinksExtensions.Structure.h:12