Bug 234683

Summary: Enhance Vector::map to allow specifying what kind of Vector to return (e.g. inline capacity, overflow, etc.)
Product: WebKit Reporter: Sam Weinig <sam>
Component: Web Template FrameworkAssignee: Sam Weinig <sam>
Status: RESOLVED FIXED    
Severity: Normal CC: benjamin, cdumez, cmarcelo, darin, ews-watchlist, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: Other   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
darin: review+
Patch with tests
sam: commit-queue+
Patch with tests none

Description Sam Weinig 2021-12-25 17:43:45 PST
Enhance Vector::map to allow specifying what kind of Vector to return (e.g. inline capacity, overflow, etc.).
Comment 1 Sam Weinig 2021-12-25 18:46:17 PST
This is a bit tricky to tack onto the existing map due to the order of template arguments:

    template<typename MapFunction, typename R = typename std::invoke_result<MapFunction, const T&>::type> Vector<R> map(MapFunction) const;

The naive approach, to make R an overridable parameter like the following:

    template<typename MapFunction, typename R = Vector<typename std::invoke_result<MapFunction, const T&>::type>> R map(MapFunction) const;

has the problem that now, to specify the Vector type, you also need to specify the MapFunction type, which can be hard / impossible when used with lambdas.


My current idea is to use overloading + SFINAE to have another version that looks like:

    template<typename R, typename MapFunction, typename SOME_SFINAE_TEST_HERE> R map(MapFunction) const;
Comment 2 Sam Weinig 2021-12-25 19:51:11 PST
Created attachment 447965 [details]
Patch
Comment 3 Darin Adler 2021-12-27 10:11:17 PST
Comment on attachment 447965 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=447965&action=review

Seems like we should include at least one use of the new template, maybe in the tests directory?

> Source/WTF/ChangeLog:15
> +        (WTF::Malloc>::map const const):
> +        (WTF::Malloc>::map const const const):
> +        (WTF::Malloc>::map const): Deleted.

Garbled function names.
Comment 4 Sam Weinig 2021-12-27 11:49:58 PST
(In reply to Darin Adler from comment #3)
> Comment on attachment 447965 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=447965&action=review
> 
> Seems like we should include at least one use of the new template, maybe in
> the tests directory?

Yeah, I also meant to actually use this as well, but failed to upload the whole patch :(.

> 
> > Source/WTF/ChangeLog:15
> > +        (WTF::Malloc>::map const const):
> > +        (WTF::Malloc>::map const const const):
> > +        (WTF::Malloc>::map const): Deleted.
> 
> Garbled function names.

So close little script.
Comment 5 Sam Weinig 2021-12-28 09:02:34 PST Comment hidden (obsolete)
Comment 6 Sam Weinig 2021-12-28 09:03:41 PST
Created attachment 448047 [details]
Patch with tests
Comment 7 EWS 2021-12-28 10:02:51 PST
Committed r287472 (245607@main): <https://commits.webkit.org/245607@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 448047 [details].
Comment 8 Radar WebKit Bug Importer 2021-12-28 10:03:19 PST
<rdar://problem/86959641>