-
Notifications
You must be signed in to change notification settings - Fork 25
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
Implementing setUnion
in C++.
#3970
Comments
I don't think it is a strong requirement to do set union in-place. I think |
Aside: out of curiosity, why does the definition of C++
Some comments on each:
I think that (2) is a fundamental conflict with how we generate code. To generate code for template <typename T>
std::set<T> union_sets(const std::set<T>& s1, const std::set<T>& s2) {
std::set<T> result;
std::set_union(s1.begin(), s1.end(), s2.begin(), s2.end(),
std::inserter(result, result.begin()));
return result;
} This function will allow us to get rid of some of the syntactic noise of the generated code. Furthermore, I noticed that we we use iterators for set containment as well (i.e., not very human-readable for our purposes), I think we can do the same for that as well, e.g.,: template <typename T>
bool set_contains(const std::set<T>& s, T e) {
return s.find(e) != s.end();
} Wrapping everything together into a complete snippet: #include <iostream>
#include <set>
#include <algorithm>
#include <iterator>
template <typename T>
std::set<T> union_sets(const std::set<T>& s1, const std::set<T>& s2) {
std::set<T> result;
std::set_union(s1.begin(), s1.end(), s2.begin(), s2.end(),
std::inserter(result, result.begin()));
return result;
}
template <typename T>
bool set_contains(const std::set<T>& s, T e) {
return s.find(e) != s.end();
}
int main() {
std::set<int> s1 = {1, 2, 3, 4, 5};
std::set<int> s2 = {4, 5, 6, 7, 8};
int e = 5;
if (set_contains(union_sets(s1, s2), e)) {
std::cout << "Yes" << std::endl;
} else {
std::cout << "No" << std::endl;
}
return 0;
} I think that this is more representative of the kind of code we want to generate. That being said, I'm taking a very biased stance in what kind of semantics I'm assuming we want out of the set API in GOOL. So, my question is: what do we want out of the set API in GOOL? What should these functions translate to in each of the languages, semantically? For example, "set addition" is a rather uncommon thing (at least in my experience...) -- I'm not quite sure how we would even go about writing that, we usually would see something like We have a pipeline of expressions: |
The function Drasil/code/drasil-gool/lib/Drasil/GOOL/LanguageRenderer/CommonPseudoOO.hs Lines 148 to 149 in 76ce309
Currently, our basic set tests do not include functionality to test union operations. However, to ensure consistency across different languages, the union function in C++ should be translated in a way that aligns with its implementation in other languages.
Given this, to maintain semantic consistency across different languages, I think we need to implement a similar imperative-style If we implement custom functions for
|
Great! Thank you! This clarifies a lot for me! In that case, I think that Java deviates from the rest because I think that generating these polyfill functions is the right way to go to keep the code looking the same across generated code bases. @JacquesCarette @smiths Do you have any thoughts on this? |
Drasil/code/drasil-gool/lib/Drasil/GOOL/LanguageRenderer/CppRenderer.hs
Lines 1390 to 1394 in 42e4ca1
At the moment,
setUnion
is not implemented in C++ yet. The standard way to compute a set union is viastd::set_union
, but this function does not modify sets in-place. Instead, it computes the union of two sets and stores the result in a third set.Is there a recommended way in C++ to perform an in-place set union similar to Julia's
union!
or Java'saddAll
?I found that using the
insert
method (e.g., a.insert(b.begin(), b.end());) can achieve an in-place union by adding elements from one set to another directly. To implement this, I need to iterate over the second set and insert each element into the first set. How can I achieve this iteration and insertion using a loop, such asForEach
, to go fromb.begin()
tob.end()
and add the elements intoset a
?The text was updated successfully, but these errors were encountered: