KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pns_itemset.h
Go to the documentation of this file.
1/*
2 * KiRouter - a push-and-(sometimes-)shove PCB router
3 *
4 * Copyright (C) 2013-2014 CERN
5 * Copyright (C) 2016-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 * Author: Tomasz Wlostowski <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#ifndef __PNS_ITEMSET_H
23#define __PNS_ITEMSET_H
24
25#include <vector>
26#include <core/kicad_algo.h>
27#include "pns_item.h"
28
29namespace PNS {
30
34class LINE;
35
36class ITEM_SET : public ITEM_OWNER
37{
38public:
39 ITEM_SET( ITEM* aInitialItem = nullptr, bool aBecomeOwner = false )
40 {
41 if( aInitialItem )
42 m_items.emplace_back( aInitialItem );
43
44 if( aBecomeOwner && aInitialItem )
45 aInitialItem->SetOwner( this );
46 }
47
48 ITEM_SET( const ITEM_SET& aOther )
49 {
50 m_items = aOther.m_items;
51 }
52
53 ~ITEM_SET();
54
55 ITEM_SET& operator=( const ITEM_SET& aOther )
56 {
57 m_items.clear();
58 m_items.reserve( aOther.m_items.size() );
59
60 for( ITEM* item : aOther.m_items )
61 m_items.push_back( item );
62
63 return *this;
64 }
65
66 int Count( int aKindMask = -1 ) const
67 {
68 int n = 0;
69
70 if( aKindMask == -1 || aKindMask == ITEM::ANY_T )
71 return static_cast<int>( m_items.size() );
72
73 for( ITEM* item : m_items )
74 {
75 if( item->Kind() & aKindMask )
76 n++;
77 }
78
79 return n;
80 }
81
82 bool Empty() const
83 {
84 return m_items.empty();
85 }
86
87 std::vector<ITEM*>& Items() { return m_items; }
88 const std::vector<ITEM*>& CItems() const { return m_items; }
89
90 ITEM_SET& FilterLayers( int aStart, int aEnd = -1, bool aInvert = false );
91 ITEM_SET& FilterKinds( int aKindMask, bool aInvert = false );
92 ITEM_SET& FilterNet( NET_HANDLE aNet, bool aInvert = false );
93 ITEM_SET& FilterMarker( int aMarker, bool aInvert = false );
94
95 ITEM_SET& ExcludeLayers( int aStart, int aEnd = -1 )
96 {
97 return FilterLayers( aStart, aEnd, true );
98 }
99
100 ITEM_SET& ExcludeKinds( int aKindMask )
101 {
102 return FilterKinds( aKindMask, true );
103 }
104
106 {
107 return FilterNet( aNet, true );
108 }
109
110 ITEM_SET& ExcludeItem( const ITEM* aItem );
111
112 int Size() const
113 {
114 return static_cast<int>( m_items.size() );
115 }
116
117 void Add( const LINE& aLine );
118 void Prepend( const LINE& aLine );
119
120 ITEM* operator[]( size_t aIndex ) const
121 {
122 return m_items[aIndex];
123 }
124
125 std::vector<ITEM*>::iterator begin() { return m_items.begin(); }
126 std::vector<ITEM*>::iterator end() { return m_items.end(); }
127 std::vector<ITEM*>::const_iterator cbegin() const { return m_items.cbegin(); }
128 std::vector<ITEM*>::const_iterator cend() const { return m_items.cend(); }
129
130 void Add( ITEM* aItem, bool aBecomeOwner = false )
131 {
132 m_items.emplace_back( aItem );
133
134 if( aBecomeOwner )
135 aItem->SetOwner( this );
136 }
137
138 void Prepend( ITEM* aItem, bool aBecomeOwner = false )
139 {
140 m_items.emplace( m_items.begin(), aItem );
141
142 if( aBecomeOwner )
143 aItem->SetOwner( this );
144 }
145
146 void Clear()
147 {
148 m_items.clear();
149 }
150
151 bool Contains( ITEM* aItem ) const
152 {
153 return alg::contains( m_items, aItem );
154 }
155
156 void Erase( ITEM* aItem )
157 {
158 std::vector<ITEM*>::iterator f = std::find( m_items.begin(), m_items.end(), aItem );
159
160 if( f != m_items.end() )
161 m_items.erase( f );
162 }
163
164 template<class T>
165 T* FindByKind( ITEM::PnsKind kind, int index = 0 )
166 {
167 int n = 0;
168
169 for( const ITEM* item : m_items )
170 {
171 if( item->OfKind( kind ) )
172 {
173 if( index == n )
174 return static_cast<T*>( item );
175 else
176 n++;
177 }
178 }
179
180 return nullptr;
181 }
182
183private:
184 std::vector<ITEM*> m_items;
185};
186
187}
188
189#endif
Definition: line.h:36
bool Empty() const
Definition: pns_itemset.h:82
int Size() const
Definition: pns_itemset.h:112
void Prepend(ITEM *aItem, bool aBecomeOwner=false)
Definition: pns_itemset.h:138
ITEM_SET & operator=(const ITEM_SET &aOther)
Definition: pns_itemset.h:55
int Count(int aKindMask=-1) const
Definition: pns_itemset.h:66
ITEM_SET & FilterNet(NET_HANDLE aNet, bool aInvert=false)
std::vector< ITEM * > m_items
Definition: pns_itemset.h:184
void Add(const LINE &aLine)
Definition: pns_itemset.cpp:32
ITEM_SET & ExcludeItem(const ITEM *aItem)
std::vector< ITEM * >::const_iterator cbegin() const
Definition: pns_itemset.h:127
ITEM_SET & FilterMarker(int aMarker, bool aInvert=false)
Definition: pns_itemset.cpp:86
bool Contains(ITEM *aItem) const
Definition: pns_itemset.h:151
ITEM_SET(const ITEM_SET &aOther)
Definition: pns_itemset.h:48
ITEM_SET & FilterKinds(int aKindMask, bool aInvert=false)
Definition: pns_itemset.cpp:70
ITEM_SET & ExcludeNet(NET_HANDLE aNet)
Definition: pns_itemset.h:105
ITEM_SET & FilterLayers(int aStart, int aEnd=-1, bool aInvert=false)
Definition: pns_itemset.cpp:48
std::vector< ITEM * > & Items()
Definition: pns_itemset.h:87
ITEM_SET & ExcludeLayers(int aStart, int aEnd=-1)
Definition: pns_itemset.h:95
ITEM_SET(ITEM *aInitialItem=nullptr, bool aBecomeOwner=false)
Definition: pns_itemset.h:39
std::vector< ITEM * >::const_iterator cend() const
Definition: pns_itemset.h:128
ITEM_SET & ExcludeKinds(int aKindMask)
Definition: pns_itemset.h:100
std::vector< ITEM * >::iterator end()
Definition: pns_itemset.h:126
void Prepend(const LINE &aLine)
Definition: pns_itemset.cpp:40
void Erase(ITEM *aItem)
Definition: pns_itemset.h:156
std::vector< ITEM * >::iterator begin()
Definition: pns_itemset.h:125
ITEM * operator[](size_t aIndex) const
Definition: pns_itemset.h:120
T * FindByKind(ITEM::PnsKind kind, int index=0)
Definition: pns_itemset.h:165
void Add(ITEM *aItem, bool aBecomeOwner=false)
Definition: pns_itemset.h:130
const std::vector< ITEM * > & CItems() const
Definition: pns_itemset.h:88
Base class for PNS router board items.
Definition: pns_item.h:97
PnsKind
< Supported item types
Definition: pns_item.h:101
Represents a track on a PCB, connecting two non-trivial joints (that is, vias, pads,...
Definition: pns_line.h:61
void SetOwner(const ITEM_OWNER *aOwner)
Set the node that owns this item.
Definition: pns_item.h:76
Push and Shove diff pair dimensions (gap) settings dialog.
void * NET_HANDLE
Definition: pns_item.h:54
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:100