From d7e80f800884f8eae6e6e2cf049f8ac4100e35c4 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Sat, 26 Sep 2020 12:40:32 +0100 Subject: [PATCH] Add std::contains() to simplify a lot of code. --- common/tool/tool_manager.cpp | 2 +- eeschema/bus_alias.cpp | 5 ++--- eeschema/dialogs/dialog_symbol_remap.cpp | 2 +- eeschema/libedit/lib_edit_frame.cpp | 2 +- eeschema/libedit/lib_manager.cpp | 4 +++- include/common.h | 10 ++++++++++ pcbnew/class_board.cpp | 2 +- pcbnew/connectivity/connectivity_items.cpp | 2 +- pcbnew/dialogs/dialog_export_svg.cpp | 3 +-- pcbnew/dialogs/panel_setup_layers.cpp | 6 +++--- pcbnew/import_gfx/graphics_import_mgr.cpp | 8 ++++---- pcbnew/router/pns_itemset.h | 4 ++-- pcbnew/router/pns_link_holder.h | 3 +-- pcbnew/router/pns_tool_base.cpp | 2 +- pcbnew/tools/selection_tool.cpp | 17 ++++++----------- 15 files changed, 38 insertions(+), 34 deletions(-) diff --git a/common/tool/tool_manager.cpp b/common/tool/tool_manager.cpp index c0d4b43441..58ac791c0b 100644 --- a/common/tool/tool_manager.cpp +++ b/common/tool/tool_manager.cpp @@ -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() ); } diff --git a/eeschema/bus_alias.cpp b/eeschema/bus_alias.cpp index b7eb0f5d45..56cc50c2b3 100644 --- a/eeschema/bus_alias.cpp +++ b/eeschema/bus_alias.cpp @@ -19,7 +19,7 @@ */ #include - +#include #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 ); } diff --git a/eeschema/dialogs/dialog_symbol_remap.cpp b/eeschema/dialogs/dialog_symbol_remap.cpp index 870244d0e5..4c3a907b40 100644 --- a/eeschema/dialogs/dialog_symbol_remap.cpp +++ b/eeschema/dialogs/dialog_symbol_remap.cpp @@ -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; diff --git a/eeschema/libedit/lib_edit_frame.cpp b/eeschema/libedit/lib_edit_frame.cpp index 06c78cab0e..4384053227 100644 --- a/eeschema/libedit/lib_edit_frame.cpp +++ b/eeschema/libedit/lib_edit_frame.cpp @@ -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(); diff --git a/eeschema/libedit/lib_manager.cpp b/eeschema/libedit/lib_manager.cpp index 0992915ed4..4338ad8416 100644 --- a/eeschema/libedit/lib_manager.cpp +++ b/eeschema/libedit/lib_manager.cpp @@ -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 ); diff --git a/include/common.h b/include/common.h index 9c2a55ed6f..a75c96ae18 100644 --- a/include/common.h +++ b/include/common.h @@ -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 + 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. */ diff --git a/pcbnew/class_board.cpp b/pcbnew/class_board.cpp index 9d824583e0..75506108ad 100644 --- a/pcbnew/class_board.cpp +++ b/pcbnew/class_board.cpp @@ -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 ); } diff --git a/pcbnew/connectivity/connectivity_items.cpp b/pcbnew/connectivity/connectivity_items.cpp index 1a66824d30..d7fd57e8dd 100644 --- a/pcbnew/connectivity/connectivity_items.cpp +++ b/pcbnew/connectivity/connectivity_items.cpp @@ -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 ); } diff --git a/pcbnew/dialogs/dialog_export_svg.cpp b/pcbnew/dialogs/dialog_export_svg.cpp index ef890951dc..d2dc7bd67f 100644 --- a/pcbnew/dialogs/dialog_export_svg.cpp +++ b/pcbnew/dialogs/dialog_export_svg.cpp @@ -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 ); } } diff --git a/pcbnew/dialogs/panel_setup_layers.cpp b/pcbnew/dialogs/panel_setup_layers.cpp index 37935a0441..3f1a99f503 100644 --- a/pcbnew/dialogs/panel_setup_layers.cpp +++ b/pcbnew/dialogs/panel_setup_layers.cpp @@ -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 ); diff --git a/pcbnew/import_gfx/graphics_import_mgr.cpp b/pcbnew/import_gfx/graphics_import_mgr.cpp index 2d969fdc35..5f994e5ed4 100644 --- a/pcbnew/import_gfx/graphics_import_mgr.cpp +++ b/pcbnew/import_gfx/graphics_import_mgr.cpp @@ -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 ); + } ); } diff --git a/pcbnew/router/pns_itemset.h b/pcbnew/router/pns_itemset.h index 118a9541a4..72a264b5d4 100644 --- a/pcbnew/router/pns_itemset.h +++ b/pcbnew/router/pns_itemset.h @@ -23,7 +23,7 @@ #define __PNS_ITEMSET_H #include - +#include #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 ) diff --git a/pcbnew/router/pns_link_holder.h b/pcbnew/router/pns_link_holder.h index 1d37ba601c..de534b267c 100644 --- a/pcbnew/router/pns_link_holder.h +++ b/pcbnew/router/pns_link_holder.h @@ -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 diff --git a/pcbnew/router/pns_tool_base.cpp b/pcbnew/router/pns_tool_base.cpp index f305518fa1..59e256f83d 100644 --- a/pcbnew/router/pns_tool_base.cpp +++ b/pcbnew/router/pns_tool_base.cpp @@ -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... diff --git a/pcbnew/tools/selection_tool.cpp b/pcbnew/tools/selection_tool.cpp index 807f403a4e..d6ede85a62 100644 --- a/pcbnew/tools/selection_tool.cpp +++ b/pcbnew/tools/selection_tool.cpp @@ -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; } } }