Add std::contains() to simplify a lot of code.

This commit is contained in:
Jeff Young 2020-09-26 12:40:32 +01:00
parent e32c113249
commit d7e80f8008
15 changed files with 38 additions and 34 deletions

View File

@ -1037,7 +1037,7 @@ bool TOOL_MANAGER::isActive( TOOL_BASE* aTool )
return false;
// Just check if the tool is on the active tools stack
return std::find( m_activeTools.begin(), m_activeTools.end(), aTool->GetId() ) != m_activeTools.end();
return std::contains( m_activeTools, aTool->GetId() );
}

View File

@ -19,7 +19,7 @@
*/
#include <algorithm>
#include <common.h>
#include "bus_alias.h"
@ -36,6 +36,5 @@ BUS_ALIAS::~BUS_ALIAS()
bool BUS_ALIAS::Contains( const wxString& aName )
{
return ( std::find( m_members.begin(), m_members.end(), aName )
!= m_members.end() );
return std::contains( m_members, aName );
}

View File

@ -180,7 +180,7 @@ void DIALOG_SYMBOL_REMAP::createProjectSymbolLibTable( REPORTER& aReporter )
libName.Replace( " ", "-" );
// Don't create duplicate table entries.
while( std::find( libNames.begin(), libNames.end(), libName ) != libNames.end() )
while( std::contains( libNames, libName ) )
{
libName = libName.Left( libNameLen );
libName << libNameInc;

View File

@ -1001,7 +1001,7 @@ void LIB_EDIT_FRAME::HardRedraw()
for( LIB_ITEM& item : m_my_part->GetDrawItems() )
{
if( std::find( selection.begin(), selection.end(), &item ) == selection.end() )
if( !std::contains( selection, &item ) )
item.ClearSelected();
else
item.SetSelected();

View File

@ -873,8 +873,10 @@ bool LIB_MANAGER::LIB_BUFFER::DeleteBuffer( LIB_MANAGER::PART_BUFFER::PTR aPartB
// Remove all derived symbols to prevent broken inheritance.
if( aPartBuf->GetPart()->IsRoot() && HasDerivedSymbols( aPartBuf->GetPart()->GetName() )
&& removeChildSymbols( aPartBuf ) == 0 )
&& removeChildSymbols( aPartBuf ) == 0 )
{
retv = false;
}
m_deleted.emplace_back( *partBufIt );
m_parts.erase( partBufIt );

View File

@ -409,6 +409,16 @@ std::ostream& operator<<( std::ostream& out, const wxSize& size );
std::ostream& operator<<( std::ostream& out, const wxPoint& pt );
namespace std
{
template<class Container, class Value>
bool contains( Container aContainer, Value aValue )
{
return std::find( aContainer.begin(), aContainer.end(), aValue ) != aContainer.end();
}
}
/**
* A wrapper around a wxFileName which is much more performant with a subset of the API.
*/

View File

@ -1912,7 +1912,7 @@ void BOARD::SanitizeNetcodes()
void BOARD::AddListener( BOARD_LISTENER* aListener )
{
if( std::find( m_listeners.begin(), m_listeners.end(), aListener ) == m_listeners.end() )
if( !std::contains( m_listeners, aListener ) )
m_listeners.push_back( aListener );
}

View File

@ -432,7 +432,7 @@ wxString CN_CLUSTER::OriginNetName() const
bool CN_CLUSTER::Contains( const CN_ITEM* aItem )
{
return std::find( m_items.begin(), m_items.end(), aItem ) != m_items.end();
return std::contains( m_items, aItem );
}

View File

@ -163,8 +163,7 @@ void DIALOG_EXPORT_SVG::initDialog()
m_boxSelectLayer[layer] = std::make_pair( m_TechnicalLayersList, checkIndex );
}
if( std::find( cfg->m_ExportSvg.layers.begin(), cfg->m_ExportSvg.layers.end(), layer ) !=
cfg->m_ExportSvg.layers.end() )
if( std::contains( cfg->m_ExportSvg.layers, layer ) )
m_boxSelectLayer[layer].first->Check( checkIndex, true );
}
}

View File

@ -752,9 +752,9 @@ LSEQ PANEL_SETUP_LAYERS::getRemovedLayersWithItems()
PCB_LAYER_COLLECTOR collector;
LSEQ newLayerSeq = newLayers.Seq();
for( auto layer_id : curLayers.Seq() )
for( PCB_LAYER_ID layer_id : curLayers.Seq() )
{
if( std::find( newLayerSeq.begin(), newLayerSeq.end(), layer_id ) == newLayerSeq.end() )
if( !std::contains( newLayerSeq, layer_id ) )
{
collector.SetLayerId( layer_id );
collector.Collect( m_pcb, GENERAL_COLLECTOR::BoardLevelItems );
@ -786,7 +786,7 @@ LSEQ PANEL_SETUP_LAYERS::getNonRemovableLayers()
if( IsCopperLayer( layer_id ) ) // Copper layers are not taken into account here
continue;
if( std::find( newLayerSeq.begin(), newLayerSeq.end(), layer_id ) == newLayerSeq.end() )
if( !std::contains( newLayerSeq, layer_id ) )
{
collector.SetLayerId( layer_id );
collector.Collect( m_pcb, GENERAL_COLLECTOR::ModuleItems );

View File

@ -38,10 +38,10 @@ GRAPHICS_IMPORT_MGR::GRAPHICS_IMPORT_MGR( const TYPE_LIST& aBlacklist )
};
std::copy_if( all_types.begin(), all_types.end(), std::back_inserter( m_importableTypes ),
[&aBlacklist]( const GFX_FILE_T& arg ) {
return ( std::find( aBlacklist.begin(), aBlacklist.end(), arg )
== aBlacklist.end() );
} );
[&aBlacklist]( const GFX_FILE_T& arg )
{
return !std::contains( aBlacklist, arg );
} );
}

View File

@ -23,7 +23,7 @@
#define __PNS_ITEMSET_H
#include <vector>
#include <common.h>
#include "pns_item.h"
namespace PNS {
@ -193,7 +193,7 @@ public:
bool Contains( ITEM* aItem ) const
{
const ENTRY ent( aItem );
return std::find( m_items.begin(), m_items.end(), ent ) != m_items.end();
return std::contains( m_items, ent );
}
void Erase( ITEM* aItem )

View File

@ -56,8 +56,7 @@ public:
///> Checks if the segment aLink is a part of the line.
bool ContainsLink( const LINKED_ITEM* aItem ) const
{
return std::find( m_links.begin(), m_links.end(),
aItem ) != m_links.end();
return std::contains( m_links, aItem );
}
LINKED_ITEM* GetLink( int aIndex ) const

View File

@ -138,7 +138,7 @@ ITEM* TOOL_BASE::pickSingleItem( const VECTOR2I& aWhere, int aNet, int aLayer, b
if( !m_iface->IsAnyLayerVisible( item->Layers() ) )
continue;
if( std::find( aAvoidItems.begin(), aAvoidItems.end(), item ) != aAvoidItems.end() )
if( std::contains( aAvoidItems, item ) )
continue;
// fixme: this causes flicker with live loop removal...

View File

@ -1183,18 +1183,13 @@ void SELECTION_TOOL::selectAllItemsOnSheet( wxString& aSheetPath )
{
for( BOARD_CONNECTED_ITEM* mitem : board()->GetConnectivity()->GetNetItems( netCode, padType ) )
{
if( mitem->Type() == PCB_PAD_T)
if( mitem->Type() == PCB_PAD_T && !std::contains( modList, mitem->GetParent() ) )
{
bool found = std::find( modList.begin(), modList.end(), mitem->GetParent() ) != modList.end();
if( !found )
{
// if we cannot find the module of the pad in the modList
// then we can assume that that module is not located in the same
// schematic, therefore invalidate this netcode.
removeCodeList.push_back( netCode );
break;
}
// if we cannot find the module of the pad in the modList
// then we can assume that that module is not located in the same
// schematic, therefore invalidate this netcode.
removeCodeList.push_back( netCode );
break;
}
}
}