-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow selecting all variables as payload in spatial search SERVICE #1656
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a0b39d2
Allow selecting all variables as payload in spatial search SERVICE
000fb9c
add one more test to get coverage to 100%
fcf4cf5
Separate PayloadVariables into own class and module, add tests and te…
7eaf7cd
apply feedback
03b1ea4
add edge case test and explanatory comments
33e3c7b
smaller include
218b109
move payloadvariables to parser
50fee8c
also move the unit test
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2024, University of Freiburg, | ||
// Chair of Algorithms and Data Structures. | ||
// Author: Christoph Ullinger <[email protected]> | ||
|
||
#include "parser/PayloadVariables.h" | ||
|
||
// ____________________________________________________________________________ | ||
PayloadVariables::PayloadVariables(std::vector<Variable> variables) | ||
: variables_{std::move(variables)} {}; | ||
|
||
// ____________________________________________________________________________ | ||
PayloadVariables PayloadVariables::all() { | ||
PayloadVariables pv{}; | ||
pv.setToAll(); | ||
return pv; | ||
}; | ||
|
||
// ____________________________________________________________________________ | ||
void PayloadVariables::addVariable(const Variable& variable) { | ||
// Helper visitor to check if the payload variables has not been set to all | ||
// and a variable can be added. If yes, add it. | ||
auto addVarVisitor = [&]<typename T>(T& value) { | ||
if constexpr (std::is_same_v<T, std::vector<Variable>>) { | ||
value.push_back(variable); | ||
} | ||
}; | ||
|
||
std::visit(addVarVisitor, variables_); | ||
}; | ||
|
||
// ____________________________________________________________________________ | ||
void PayloadVariables::setToAll() { | ||
variables_ = detail::PayloadAllVariables{}; | ||
}; | ||
|
||
// ____________________________________________________________________________ | ||
bool PayloadVariables::empty() const { | ||
// Helper visitor to check if the payload variables are empty | ||
auto emptyVisitor = []<typename T>(const T& value) -> bool { | ||
if constexpr (std::is_same_v<T, detail::PayloadAllVariables>) { | ||
return false; | ||
} else { | ||
static_assert(std::is_same_v<T, std::vector<Variable>>); | ||
return value.empty(); | ||
} | ||
}; | ||
|
||
return std::visit(emptyVisitor, variables_); | ||
}; | ||
|
||
// ____________________________________________________________________________ | ||
bool PayloadVariables::isAll() const { | ||
return std::holds_alternative<detail::PayloadAllVariables>(variables_); | ||
}; | ||
|
||
// ____________________________________________________________________________ | ||
const std::vector<Variable>& PayloadVariables::getVariables() const { | ||
// Helper visitor to check if the payload variables has been set to all: then | ||
// throw, otherwise return the vector | ||
auto getVarVisitor = | ||
[]<typename T>(const T& value) -> const std::vector<Variable>& { | ||
if constexpr (std::is_same_v<T, std::vector<Variable>>) { | ||
return value; | ||
} else { | ||
AD_THROW( | ||
"getVariables may only be called on a non-all PayloadVariables " | ||
"object."); | ||
} | ||
}; | ||
|
||
return std::visit(getVarVisitor, variables_); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2024, University of Freiburg, | ||
// Chair of Algorithms and Data Structures. | ||
// Author: Christoph Ullinger <[email protected]> | ||
|
||
#pragma once | ||
|
||
#include <vector> | ||
|
||
#include "parser/data/Variable.h" | ||
|
||
namespace detail { | ||
// Represents the selection of all variables as payload | ||
struct PayloadAllVariables : std::monostate { | ||
bool operator==([[maybe_unused]] const std::vector<Variable>& other) const { | ||
return false; | ||
}; | ||
}; | ||
} // namespace detail | ||
|
||
// This class represents a list of variables to be included in the result of | ||
// an operation. This is currently used in the spatial search. | ||
class PayloadVariables { | ||
public: | ||
// Construct an empty payload variables object | ||
PayloadVariables() = default; | ||
|
||
// Construct a payload variables object from a vector of variables | ||
PayloadVariables(std::vector<Variable> variables); | ||
|
||
// Construct a payload variables object that is set to all | ||
static PayloadVariables all(); | ||
|
||
// Add a variable to the payload variables or do nothing if all variables are | ||
// already selected | ||
void addVariable(const Variable& variable); | ||
|
||
// Select all variables. | ||
void setToAll(); | ||
|
||
// Returns whether the payload variables object is empty. | ||
bool empty() const; | ||
|
||
// Returns whether all variables have been selected. | ||
bool isAll() const; | ||
|
||
// Returns a vector of variables if all has not been set. Otherwise throws. | ||
const std::vector<Variable>& getVariables() const; | ||
|
||
// For testing: equality operator | ||
bool operator==(const PayloadVariables& other) const { | ||
return variables_ == other.variables_; | ||
}; | ||
|
||
private: | ||
std::variant<detail::PayloadAllVariables, std::vector<Variable>> variables_ = | ||
std::vector<Variable>{}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the
!isAll
call redundant, becauseisAll()
directly implies!empty()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I disagree. If I'm not totally mistaken, this is neither logically nor semantically redundant.
<payload> <all>
. This is the default behavior anyway. But without the!isAll()
here we would throw despite the user's configuration just stating the default value.Edit: I will add a unit test for the edge case.