Advanced find() for associative containers

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Advanced find() for associative containers

Boost - Dev mailing list
I've written an improved version of find() for associative containers
(in the form of a free function), and I need your help answering these
two questions:

1. Will it be a useful addition to boost?
2. Where exactly should it fit in the boost?

Public interface consists of a single free function template:

template <typename C>
auto find(C& c, const typename C::key_type& key);

If element is found, return value evaluates to true and contains
iterator to found element. If element is not found, return value
evaluates to false and contains hint iterator for insert/emplace.

Allowing one to do this:

if (auto i = find(container, key))
{
     i->value_member();
     container.erase(i);
}

And this:

if (auto i = !find(container, key))
{
     container.insert(i, expensive_value_construction());
}

The code is here:
https://github.com/Boris-Rasin/find

I've tested it with the following containers:

std::map
std::multimap
std::unordered_map
std::set
std::multiset
std::unordered_set
boost::container::map
boost::container::set
boost::container::flat_map
boost::container::flat_set
boost::multi_index::ordered_unique
boost::multi_index::ordered_non_unique
boost::multi_index::hashed_unique
boost::multi_index::hashed_non_unique
boost::multi_index::ranked_unique
boost::multi_index::ranked_non_unique

Thanks for your comments and help.

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Advanced find() for associative containers

Boost - Dev mailing list
On Thu, Mar 16, 2017 at 6:18 PM, Boris Rasin via Boost
<[hidden email]> wrote:
> I've written an improved version of find() for associative containers (in
> the form of a free function), and I need your help answering these two
> questions:
>
> 1. Will it be a useful addition to boost?
> 2. Where exactly should it fit in the boost?

Nice!
I've got something similar:
https://github.com/OlafvdSpek/xbt/blob/master/misc/xbt/find_ptr.h

Most variants return a pointer rather than a (conditional) iterator.

Gr,
Olaf

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Advanced find() for associative containers

Boost - Dev mailing list
In reply to this post by Boost - Dev mailing list
On 03/16/2017 06:18 PM, Boris Rasin via Boost wrote:

> 2. Where exactly should it fit in the boost?

The most appropriate place would be Boost.Algorithm, so that is
Marshall's domain.

> Public interface consists of a single free function template:
>
> template <typename C>
> auto find(C& c, const typename C::key_type& key);

Your find() function deviates on several accounts from std::find()
so it should probably be renamed, e.g. to find_key.

Can you elaborate on why you have a specialization that uses
lower_bound()?

What is the minimal C++ version required? Looks like C++14 from
the auto return type.


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Advanced find() for associative containers

Boost - Dev mailing list
> Can you elaborate on why you have a specialization that uses
> lower_bound()?

This provides hint for insert/emplace in case element is not found. In
fact this is the default implementation, and the find() member is only
used for associative containers without lower_bound().

> What is the minimal C++ version required? Looks like C++14 from
> the auto return type.

Yes, right now C++14 is required. It is probably possible to back port
this code to C++11 and maybe even C++98.

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Loading...