Define and use utilities transform_range, transform_container

This commit is contained in:
Paul Licameli 2019-02-25 12:50:33 -05:00
parent b0738f6e09
commit 52642e49a5
4 changed files with 29 additions and 25 deletions

View File

@ -303,8 +303,8 @@ wxString Internat::StripAccelerators(const wxString &s)
wxArrayString LocalizedStrings(
const ComponentInterfaceSymbol strings[], size_t nStrings)
{
wxArrayString results;
std::transform( strings, strings + nStrings, std::back_inserter(results),
std::mem_fn( &ComponentInterfaceSymbol::Translation ) );
return results;
return transform_range<wxArrayString>(
strings, strings + nStrings,
std::mem_fn( &ComponentInterfaceSymbol::Translation )
);
}

View File

@ -640,4 +640,20 @@ namespace std
}
#endif
// A utility function building a container of results
template< typename Container, typename Iterator, typename Function >
Container transform_range( Iterator first, Iterator last, Function &&fn )
{
Container result;
std::transform( first, last, std::back_inserter( result ), fn );
return result;
}
// A utility function, often constructing a vector from another vector
template< typename OutContainer, typename InContainer, typename Function >
OutContainer transform_container( InContainer &inContainer, Function &&fn )
{
return transform_range<OutContainer>(
inContainer.begin(), inContainer.end(), fn );
}
#endif // __AUDACITY_MEMORY_X_H__

View File

@ -1152,22 +1152,17 @@ namespace {
Array GetTypedTracks(const TrackRange &trackRange,
bool selectionOnly, bool includeMuted)
{
Array array;
using Type = typename
std::remove_reference< decltype( *array[0] ) >::type;
using Type = typename std::remove_reference<
decltype( *std::declval<Array>()[0] )
>::type;
auto subRange =
trackRange.template Filter<Type>();
if ( selectionOnly )
subRange = subRange + &Track::IsSelected;
if ( ! includeMuted )
subRange = subRange - &Type::GetMute;
std::transform(
subRange.begin(), subRange.end(), std::back_inserter( array ),
[]( Type *t ){ return Track::Pointer<Type>( t ); }
);
return array;
return transform_range<Array>( subRange.begin(), subRange.end(),
[]( Type *t ){ return Track::Pointer<Type>( t ); } );
}
}

View File

@ -111,20 +111,13 @@ private:
auto ODDecodeFFmpegTask::FromList( const TrackHolders &channels ) -> Streams
{
Streams streams;
streams.reserve(channels.size());
using namespace std;
transform(channels.begin(), channels.end(), back_inserter(streams),
// Convert array of array of unique_ptr to array of array of bare pointers
return transform_container<Streams>( channels,
[](const NewChannelGroup &holders) {
Channels channels;
channels.reserve(holders.size());
transform(holders.begin(), holders.end(), back_inserter(channels),
mem_fn(&NewChannelGroup::value_type::get)
);
return channels;
return transform_container<Channels>( holders,
std::mem_fn(&NewChannelGroup::value_type::get) );
}
);
return streams;
}
//------ ODDecodeFFmpegTask definitions