Horizon
pns_itemset.h
1/*
2 * KiRouter - a push-and-(sometimes-)shove PCB router
3 *
4 * Copyright (C) 2013-2014 CERN
5 * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
6 * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
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
27#include "pns_item.h"
28
29namespace PNS {
30
37class LINE;
38
40{
41public:
42 struct ENTRY {
43
44 ENTRY( ITEM* aItem, bool aOwned = false ) :
45 item( aItem ),
46 owned( aOwned )
47 {}
48
49 ENTRY( const ENTRY& aOther )
50 {
51 owned = aOther.owned;
52
53 if( aOther.owned )
54 item = aOther.item->Clone();
55 else
56 item = aOther.item;
57 }
58
59 ~ENTRY()
60 {
61 if( owned )
62 delete item;
63 }
64
65 bool operator== ( const ENTRY& b ) const
66 {
67 return item == b.item;
68 }
69
70 bool operator< ( const ENTRY& b ) const
71 {
72 return item < b.item;
73 }
74
75 ENTRY& operator= ( const ENTRY& aOther )
76 {
77 owned = aOther.owned;
78
79 if( aOther.owned )
80 item = aOther.item->Clone();
81 else
82 item = aOther.item;
83
84 return *this;
85 }
86
87 operator ITEM* () const
88 {
89 return item;
90 }
91
92 ITEM *item;
93 bool owned;
94 };
95
96 typedef std::vector<ENTRY> ENTRIES;
97
98 ITEM_SET( ITEM* aInitialItem = NULL, bool aBecomeOwner = false )
99 {
100 if( aInitialItem )
101 {
102 m_items.push_back( ENTRY( aInitialItem, aBecomeOwner ) );
103 }
104 }
105
106 ITEM_SET( const ITEM_SET& aOther )
107 {
108 m_items = aOther.m_items;
109 }
110
111 ~ITEM_SET();
112
113 ITEM_SET& operator=( const ITEM_SET& aOther )
114 {
115 m_items = aOther.m_items;
116 return *this;
117 }
118
119 int Count( int aKindMask = -1 ) const
120 {
121 int n = 0;
122
123 if( aKindMask == -1 || aKindMask == ITEM::ANY_T )
124 return m_items.size();
125
126 for( ITEM* item : m_items )
127 {
128 if( item->Kind() & aKindMask )
129 n++;
130 }
131
132 return n;
133 }
134
135 bool Empty() const
136 {
137 return m_items.empty();
138 }
139
140 ENTRIES& Items() { return m_items; }
141 const ENTRIES& CItems() const { return m_items; }
142
143 ITEM_SET& FilterLayers( int aStart, int aEnd = -1, bool aInvert = false );
144 ITEM_SET& FilterKinds( int aKindMask, bool aInvert = false );
145 ITEM_SET& FilterNet( int aNet, bool aInvert = false );
146 ITEM_SET& FilterMarker( int aMarker, bool aInvert = false );
147
148 ITEM_SET& ExcludeLayers( int aStart, int aEnd = -1 )
149 {
150 return FilterLayers( aStart, aEnd, true );
151 }
152
153 ITEM_SET& ExcludeKinds( int aKindMask )
154 {
155 return FilterKinds( aKindMask, true );
156 }
157
158 ITEM_SET& ExcludeNet( int aNet )
159 {
160 return FilterNet( aNet, true );
161 }
162
163 ITEM_SET& ExcludeItem( const ITEM* aItem );
164
165 int Size() const
166 {
167 return m_items.size();
168 }
169
170 void Add( const LINE& aLine );
171 void Prepend( const LINE& aLine );
172
173 ITEM* operator[] ( int index ) const
174 {
175 return m_items[index].item;
176 }
177
178 void Add( ITEM* aItem, bool aBecomeOwner = false )
179 {
180 m_items.push_back( ENTRY( aItem, aBecomeOwner ) );
181 }
182
183 void Prepend( ITEM* aItem, bool aBecomeOwner = false )
184 {
185 m_items.insert( m_items.begin(), ENTRY( aItem, aBecomeOwner ) );
186 }
187
188 void Clear()
189 {
190 m_items.clear();
191 }
192
193 bool Contains( ITEM* aItem ) const
194 {
195 const ENTRY ent( aItem );
196 return std::find( m_items.begin(), m_items.end(), ent ) != m_items.end();
197 }
198
199 void Erase( ITEM* aItem )
200 {
201 ENTRY ent( aItem );
202 ENTRIES::iterator f = std::find( m_items.begin(), m_items.end(), ent );
203
204 if( f != m_items.end() )
205 m_items.erase( f );
206 }
207
208 template<class T>
209 T* FindByKind( ITEM::PnsKind kind, int index = 0 )
210 {
211 int n = 0;
212
213 for( const ITEM* item : m_items )
214 {
215 if( item->OfKind( kind ) )
216 {
217 if( index == n )
218 return static_cast<T*>( item );
219 else
220 n++;
221 }
222 }
223
224 return NULL;
225 }
226
227private:
228
229 ENTRIES m_items;
230};
231
232}
233
234#endif
Definition: pns_itemset.h:40
Class ITEM.
Definition: pns_item.h:55
virtual ITEM * Clone() const =0
Function Clone()
PnsKind
‍Supported item types
Definition: pns_item.h:61
Definition: pns_itemset.h:42