Skip to content
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

Feature/map functions #350

Merged
merged 6 commits into from
Jun 24, 2017
Merged

Conversation

shreyans800755
Copy link
Contributor

@shreyans800755 shreyans800755 commented May 25, 2017

Added intersection and difference functions for map along with docs.
Closes #341

@shreyans800755 shreyans800755 force-pushed the feature/map_functions branch from 68f5bc4 to 6f9d96e Compare May 25, 2017 19:59
@shreyans800755
Copy link
Contributor Author

shreyans800755 commented May 25, 2017

@ldionne I think I should also add symmetric_difference implementation for maps. I'll try to update the PR with its commits soon.
EDIT: I think same implementation of sym_diff should work for both maps and sets.

One doubt:
Current implementation:
symmetric_difference(x, y) = union(difference(x, y), difference(y, x))

As you must be aware of the other implementation:
symmetric_difference(x, y) = difference(union(x, y), intersection(x, y))

Is former implementation is chosen randomly, or is it faster than later one ?

@shreyans800755 shreyans800755 force-pushed the feature/map_functions branch from 6f9d96e to 42944a8 Compare May 27, 2017 12:52
Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall great, with a few nitpicks! Thanks a lot for the great PR!

As for symmetric_difference, I think it would be quite nice to add it in this PR. I'm not sure why I chose one implementation over the other when I wrote it for hana::set.

// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#include <boost/hana/assert.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please order includes.

// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#include <boost/hana/assert.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x2

//! is present in `ys`.
//! In other words, the following holds for any object `pair(k, v)`:
//! @code
//! pair(k, v) ^in^ intersection(xs, ys) if and only if (k, v) ^in^ xs && k ^in^ ys.keys()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ys.keys() does not exist; consider using keys(ys) instead

//!
//!
//! @note
//! This function is not commutative, i.e. `intersection(xs, ys)` is
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reword to

//! This function is not commutative, i.e. `intersection(xs, ys)` is not
//! necessarily the same as `intersection(ys, xs)`. Indeed, the set of keys
//! in `intersection(xs, ys)` is always the same as the set of keys in
//! `intersection(ys, xs)`, but the value associated to each key may be
//! different. `intersection(xs, ys)` contains values present in `xs`, and
//! `intersection(ys, xs)` contains values present in `ys`.

//! is not present in `keys(ys)`.
//! In other words, the following holds for any object `pair(k, v)`:
//! @code
//! pair(k, v) ^in^ difference(xs, ys) if and only if (k, v) ^in^ xs && k ^not in^ ys.keys()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment for ys.keys().


// Second template param will be pair
// Get its key and check if it exists, if it does, insert key, value pair.
template<typename Result, typename Pair>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space between template and <typename.

return hana::insert(static_cast<Result&&>(result), static_cast<Pair&&>(pair));
}

template<typename Result, typename Pair>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x2

#include <laws/base.hpp>
#include <support/minimal_product.hpp>
namespace hana = boost::hana;
using hana::test::ct_eq;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this.

p<1, 4>())
),
hana::make_map()
));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a bigger test case with something like 10 elements in the maps?

#include <laws/base.hpp>
#include <support/minimal_product.hpp>
namespace hana = boost::hana;
using hana::test::ct_eq;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed.

@ricejasonf
Copy link
Collaborator

What is the reason for intersection not being commutative. Is it a performance thing?

@shreyans800755
Copy link
Contributor Author

@ricejasonf intersection is done on the base of keys in case of hana::map, and not based on key and values, both. Frankly, I only thought this use case, and it didn't really struck into my head about intersecting two maps based on (key, value) pairs.

Is it a performance thing?

Performance based on asymptotic analysis should be same if want to intersect based on (key, value), although the overhead of comparison of value will be increased.

@ricejasonf
Copy link
Collaborator

ricejasonf commented Jun 6, 2017

@shreyans800755 Ah, right, it compares the keys. I wasn't even thinking about that. lol 😅

@shreyans800755 shreyans800755 force-pushed the feature/map_functions branch 2 times, most recently from f8bc26f to a697773 Compare June 8, 2017 16:59
@ldionne
Copy link
Member

ldionne commented Jun 8, 2017

One of the jobs failed with

/home/travis/build/boostorg/hana/include/boost/hana/fwd/set.hpp:263: warning: included file example/set/difference.cpp is not found. Check your EXAMPLE_PATH

@shreyans800755 shreyans800755 force-pushed the feature/map_functions branch from a697773 to c7e2b48 Compare June 8, 2017 20:19
@@ -51,9 +53,11 @@ Distributed under the Boost Software License, Version 1.0.
#include <boost/hana/optional.hpp>
#include <boost/hana/remove_if.hpp>
#include <boost/hana/second.hpp>
#include <boost/hana/tuple.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need #include <boost/hana/tuple.hpp> here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected.
I'll push symmetric_difference soon.

@shreyans800755 shreyans800755 force-pushed the feature/map_functions branch from c7e2b48 to 71858c6 Compare June 9, 2017 17:12
@@ -5,10 +5,14 @@
#include <boost/hana/assert.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just like we did for the other functions, would it be possible to have an example/set/symmetric_difference.cpp and an example/map/symmetric_difference.cpp file? This would be consistent with the rest of the lib, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of that, too. I chose this way because we don't have different implementations for set and map.

@@ -0,0 +1,134 @@
// Copyright Louis Dionne 2013-2017
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as for examples; it would be nice to have a test for map and a test for sets in different files.

Copy link
Contributor Author

@shreyans800755 shreyans800755 Jun 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thought as example. So, I wasn't sure if we need two different files.

@shreyans800755
Copy link
Contributor Author

Regarding documentation of symmetric_difference, currently it exists only for set. Should I move it to include/boost/hana/fwd/set.hpp and add similar for map in include/boost/hana/fwd/map.hpp ?
Or may be just adding a @relatealso tag with map in the same file include/boost/hana/fwd/symmetric_difference.hpp should do ?

@ldionne
Copy link
Member

ldionne commented Jun 11, 2017

I would duplicate the documentation for set and map. While duplication is generally bad, the rationale here is that the two implementations for set and map are conceptually unrelated, since they don't share a common concept. If we were to create a new concept and move symmetric_difference under it (and also probably others), then this is how we would de-duplicate the documentation. Does that make sense?

@shreyans800755 shreyans800755 force-pushed the feature/map_functions branch from 5a7a216 to 311b052 Compare June 11, 2017 20:51
@shreyans800755
Copy link
Contributor Author

That makes sense. I'll push the changes.

@shreyans800755 shreyans800755 force-pushed the feature/map_functions branch from 311b052 to 3200dce Compare June 11, 2017 21:41
@@ -2,8 +2,6 @@
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

#include <boost/hana/assert.hpp>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those should stay, no?

Copy link
Contributor Author

@shreyans800755 shreyans800755 Jun 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is already coming from:
test/set/symmetric_difference.cpp:10
test/_include/laws/base.hpp:25
test/_include/support/tracked.hpp:11

And that's why tests are also passing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but you should always include what you use to avoid relying on such dependencies, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Will update soon.

@shreyans800755 shreyans800755 force-pushed the feature/map_functions branch from 3200dce to a88133a Compare June 18, 2017 19:39
@ldionne ldionne merged commit 7533611 into boostorg:develop Jun 24, 2017
@ricejasonf
Copy link
Collaborator

:shipit: 👍

@ldionne
Copy link
Member

ldionne commented Jun 24, 2017

Thanks a lot @shreyans800755, this is a great PR!

@shreyans800755 shreyans800755 deleted the feature/map_functions branch June 24, 2017 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants