KiCad PCB EDA Suite
pns_index.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-2020 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_INDEX_H
23 #define __PNS_INDEX_H
24 
25 #include <deque>
26 #include <list>
27 #include <map>
28 #include <unordered_set>
29 
31 #include <geometry/shape_index.h>
32 
33 #include "pns_item.h"
34 
35 namespace PNS {
36 
37 
45 class INDEX
46 {
47 public:
48  typedef std::list<ITEM*> NET_ITEMS_LIST;
50  typedef std::unordered_set<ITEM*> ITEM_SET;
51 
52  INDEX(){};
53 
57  void Add( ITEM* aItem );
58 
62  void Remove( ITEM* aItem );
63 
67  void Replace( ITEM* aOldItem, ITEM* aNewItem );
68 
80  template<class Visitor>
81  int Query( const ITEM* aItem, int aMinDistance, Visitor& aVisitor );
82 
94  template<class Visitor>
95  int Query( const SHAPE* aShape, int aMinDistance, Visitor& aVisitor );
96 
100  NET_ITEMS_LIST* GetItemsForNet( int aNet );
101 
107  bool Contains( ITEM* aItem ) const
108  {
109  return m_allItems.find( aItem ) != m_allItems.end();
110  }
111 
115  int Size() const { return m_allItems.size(); }
116 
117  ITEM_SET::iterator begin() { return m_allItems.begin(); }
118  ITEM_SET::iterator end() { return m_allItems.end(); }
119 
120 private:
121 
122  template <class Visitor>
123  int querySingle( std::size_t aIndex, const SHAPE* aShape, int aMinDistance, Visitor& aVisitor );
124 
125  std::deque<ITEM_SHAPE_INDEX> m_subIndices;
126  std::map<int, NET_ITEMS_LIST> m_netMap;
128 };
129 
130 
131 template<class Visitor>
132 int INDEX::querySingle( std::size_t aIndex, const SHAPE* aShape, int aMinDistance, Visitor& aVisitor )
133 {
134  if( aIndex >= m_subIndices.size() )
135  return 0;
136 
137  return m_subIndices[aIndex].Query( aShape, aMinDistance, aVisitor, false );
138 }
139 
140 template<class Visitor>
141 int INDEX::Query( const ITEM* aItem, int aMinDistance, Visitor& aVisitor )
142 {
143  int total = 0;
144 
145  const LAYER_RANGE& layers = aItem->Layers();
146 
147  for( int i = layers.Start(); i <= layers.End(); ++i )
148  total += querySingle( i, aItem->Shape(), aMinDistance, aVisitor );
149 
150  return total;
151 }
152 
153 template<class Visitor>
154 int INDEX::Query( const SHAPE* aShape, int aMinDistance, Visitor& aVisitor )
155 {
156  int total = 0;
157 
158  for( std::size_t i = 0; i < m_subIndices.size(); ++i )
159  total += querySingle( i, aShape, aMinDistance, aVisitor );
160 
161  return total;
162 }
163 
164 };
165 
166 #endif
ITEM.
Definition: pns_item.h:53
std::list< ITEM * > NET_ITEMS_LIST
Definition: pns_index.h:48
std::deque< ITEM_SHAPE_INDEX > m_subIndices
Definition: pns_index.h:125
int Start() const
Definition: pns_layerset.h:83
int End() const
Definition: pns_layerset.h:88
SHAPE_INDEX< ITEM * > ITEM_SHAPE_INDEX
Definition: pns_index.h:49
virtual const SHAPE * Shape() const
Function Shape()
Definition: pns_item.h:214
SHAPE.
Definition: shape.h:74
ITEM_SET m_allItems
Definition: pns_index.h:127
NET_ITEMS_LIST * GetItemsForNet(int aNet)
Returns list of all items in a given net.
Definition: pns_index.cpp:70
int Query(const ITEM *aItem, int aMinDistance, Visitor &aVisitor)
Searches items in the index that are in proximity of aItem.
Definition: pns_index.h:141
bool Contains(ITEM *aItem) const
Function Contains()
Definition: pns_index.h:107
void Replace(ITEM *aOldItem, ITEM *aNewItem)
Replaces one item with another.
Definition: pns_index.cpp:63
ITEM_SET::iterator end()
Definition: pns_index.h:118
ITEM_SET::iterator begin()
Definition: pns_index.h:117
Board layer functions and definitions.
std::map< int, NET_ITEMS_LIST > m_netMap
Definition: pns_index.h:126
int Size() const
Returns number of items stored in the index.
Definition: pns_index.h:115
void Add(ITEM *aItem)
Adds item to the spatial index.
Definition: pns_index.cpp:27
std::unordered_set< ITEM * > ITEM_SET
Definition: pns_index.h:50
Push and Shove diff pair dimensions (gap) settings dialog.
void Remove(ITEM *aItem)
Removes an item from the spatial index.
Definition: pns_index.cpp:45
int querySingle(std::size_t aIndex, const SHAPE *aShape, int aMinDistance, Visitor &aVisitor)
Definition: pns_index.h:132
INDEX.
Definition: pns_index.h:45
LAYER_RANGE.
Definition: pns_layerset.h:32
const LAYER_RANGE & Layers() const
Definition: pns_item.h:151