KiCad PCB EDA Suite
Loading...
Searching...
No Matches
conn_partition.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include "conn_partition.h"
21#include "conn_tasks.h"
22
23#include <cassert>
24#include <numeric>
25#include <stdexcept>
26
27namespace SCH_CONNECTIVITY
28{
30{
31 while( m_parent[aNode] != aNode )
32 {
33 m_parent[aNode] = m_parent[m_parent[aNode]];
34 aNode = m_parent[aNode];
35 }
36
37 return aNode;
38}
39
40void PARTITIONER::Unite( NODE_ID aLeft, NODE_ID aRight )
41{
42 aLeft = Find( aLeft );
43 aRight = Find( aRight );
44
45 if( aLeft == aRight )
46 return;
47
48 if( m_rank[aLeft] < m_rank[aRight] )
49 std::swap( aLeft, aRight );
50
51 m_parent[aRight] = aLeft;
52
53 if( m_rank[aLeft] == m_rank[aRight] )
54 ++m_rank[aLeft];
55}
56
57std::vector<PARTITION> PARTITIONER::Build( std::span<const NODE_INPUT> aInputs, const SESSION_KEYS& aKeys )
58{
59 const size_t count = aKeys.NodeCount();
60 m_parent.resize( count );
61 std::iota( m_parent.begin(), m_parent.end(), NODE_ID( 0 ) );
62 m_rank.assign( count, 0 );
63 m_active.assign( count, 0 );
64 m_versions.assign( count, 0 );
65 m_groupOfRoot.assign( count, INVALID_ID );
66
67 for( const NODE_INPUT& input : aInputs )
68 {
69 if( input.node >= count )
70 throw std::invalid_argument( "Connectivity partition node outside session keys" );
71
72 assert( m_versions[input.node] == 0 );
73 m_active[input.node] = 1;
74 m_versions[input.node] = input.version;
75
76 for( NODE_ID edge : input.edges )
77 {
78 if( edge >= count )
79 throw std::invalid_argument( "Connectivity partition edge outside session keys" );
80
81 m_active[edge] = 1;
82 Unite( input.node, edge );
83 }
84 }
85
86 std::vector<PARTITION> result;
87
88 for( size_t i = 0; i < count; ++i )
89 {
90 if( !m_active[i] )
91 continue;
92
93 const NODE_ID node = static_cast<NODE_ID>( i );
94 const NODE_ID root = Find( node );
96
97 if( group == INVALID_ID )
98 {
99 group = static_cast<NODE_ID>( result.size() );
100 result.push_back( { node, {}, 14695981039346656037ULL } );
101 }
102
103 PARTITION& partition = result[group];
104 partition.identity.emplace_back( node, m_versions[node] );
105 }
106
107 ParallelFor( result.size(), [&]( size_t i )
108 {
109 PARTITION& partition = result[i];
110
111 for( const auto& [node, version] : partition.identity )
112 {
113 partition.hash = ( ( partition.hash ^ node ) * 1099511628211ULL ^ version ) * 1099511628211ULL;
114
115 if( aKeys.Less( aKeys.Node( node ), aKeys.Node( partition.anchor ) ) )
116 partition.anchor = node;
117 }
118 } );
119
120 return result;
121}
122} // namespace SCH_CONNECTIVITY
NODE_ID Find(NODE_ID aNode)
void Unite(NODE_ID aLeft, NODE_ID aRight)
std::vector< NODE_ID > m_parent
std::vector< uint64_t > m_versions
std::vector< uint8_t > m_active
std::vector< uint8_t > m_rank
std::vector< PARTITION > Build(std::span< const NODE_INPUT > aInputs, const SESSION_KEYS &aKeys)
std::vector< NODE_ID > m_groupOfRoot
Session IDs are dense handles, never a canonical ordering.
Definition conn_keys.h:146
Value keys and the key session of the schematic connectivity engine.
void ParallelFor(size_t aCount, FUNCTION &&aFunction, thread_pool &aPool=GetKiCadThreadPool())
Independent ordinal writes only; preparation and cache commits stay on the caller thread.
Definition conn_tasks.h:72
uint32_t NODE_ID
Session handle of a NODE_KEY graph node.
Definition conn_keys.h:43
constexpr uint32_t INVALID_ID
Marks an unset handle.
Definition conn_keys.h:47
Exact identity of one connected component.
std::vector< std::pair< NODE_ID, uint64_t > > identity
wxString result
Test unit parsing edge cases and error handling.