1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-07-04 18:29:40 +00:00

Ensure optionals appear at the end of any ROM request list.

This commit is contained in:
Thomas Harte 2021-06-06 22:36:26 -04:00
parent 6c559d7556
commit 54e3332673
2 changed files with 17 additions and 0 deletions

View File

@ -32,6 +32,7 @@ Request Request::append(Node::Type type, const Request &rhs) {
if(node.type == type && rhs.node.type == type) {
Request new_request = *this;
new_request.node.children.insert(new_request.node.children.end(), rhs.node.children.begin(), rhs.node.children.end());
new_request.node.sort();
return new_request;
}
@ -39,6 +40,7 @@ Request Request::append(Node::Type type, const Request &rhs) {
if(node.type == type && rhs.node.type == Node::Type::One) {
Request new_request = *this;
new_request.node.children.push_back(rhs.node);
new_request.node.sort();
return new_request;
}
@ -46,6 +48,7 @@ Request Request::append(Node::Type type, const Request &rhs) {
if(rhs.node.type == type && node.type == Node::Type::One) {
Request new_request = rhs;
new_request.node.children.push_back(node);
new_request.node.sort();
return new_request;
}

View File

@ -9,6 +9,7 @@
#ifndef ROMCatalogue_hpp
#define ROMCatalogue_hpp
#include <algorithm>
#include <functional>
#include <map>
#include <optional>
@ -244,6 +245,19 @@ struct Request {
const std::function<void(ROM::Request::ListType type, const ROM::Description &, bool is_optional, size_t remaining)> &add_item
) const;
bool subtract(const ROM::Map &map);
void sort() {
// Don't do a full sort, but move anything optional to the back.
// This makes them print more nicely; it's a human-facing tweak only.
ssize_t index = ssize_t(children.size() - 1);
bool has_seen_non_optional = false;
while(index >= 0) {
has_seen_non_optional |= !children[size_t(index)].is_optional;
if(children[size_t(index)].is_optional && has_seen_non_optional) {
std::rotate(children.begin() + index, children.begin() + index + 1, children.end());
}
--index;
}
}
};
Node node;