ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Csr.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------- File Csr.h -------------------------------*/
3/*--------------------------------------------------------------------------*/
38/*--------------------------------------------------------------------------*/
39/*----------------------------- DEFINITIONS --------------------------------*/
40/*--------------------------------------------------------------------------*/
41
42#ifndef __ADS_interop_Csr
43 #define __ADS_interop_Csr
44/* self-identification: #endif constant */
45
46/*--------------------------------------------------------------------------*/
47/*------------------------------ INCLUDES ----------------------------------*/
48/*--------------------------------------------------------------------------*/
49
50#include "ADS/Contract.h"
52
53#include <cstddef>
54#include <type_traits>
55#include <vector>
56
57/*--------------------------------------------------------------------------*/
58/*--------------------------- NAMESPACE ------------------------------------*/
59/*--------------------------------------------------------------------------*/
60
61namespace ADS
62{
63
64/*--------------------------------------------------------------------------*/
65/*----------------------------- CLASS CsrInput -----------------------------*/
66/*--------------------------------------------------------------------------*/
67
70template< class X , class Y = X >
72{
73 public:
75 struct Copies
76 {
77 bool offsets = false;
78 bool heads = false;
79 bool weights = false;
80 };
81
84 template< class G >
85 explicit CsrInput( const G & g , bool weights = true ) {
86 const std::size_t n = std::size_t( g.n() ) , a = std::size_t( g.arcs() );
87 if constexpr( std::is_same_v< X , int > )
88 f_off = g.offsets();
89 else {
90 f_offCopy.assign( g.offsets() , g.offsets() + n + 1 );
91 f_off = f_offCopy.data();
92 f_copies.offsets = true;
93 }
94 if constexpr( std::is_same_v< X , int > && CsrArrays< G > )
95 f_head = g.heads();
96 else {
97 const auto h = g.arcHeads();
98 f_headCopy.resize( a );
99 for( std::size_t k = 0 ; k < a ; ++k )
100 f_headCopy[ k ] = X( h[ std::ptrdiff_t( k ) ] );
101 f_head = f_headCopy.data();
102 f_copies.heads = true;
103 }
104 if constexpr( G::weighted ) {
105 if( ! weights )
106 return;
107 using W = typename G::Weight;
108 if constexpr( std::is_same_v< Y , W > && CsrArrays< G > )
109 f_w = g.weightData();
110 else {
111 const auto w = g.arcWeights();
112 f_wCopy.resize( a );
113 for( std::size_t k = 0 ; k < a ; ++k )
114 f_wCopy[ k ] = Y( w[ std::ptrdiff_t( k ) ] );
115 f_w = f_wCopy.data();
116 f_copies.weights = true;
117 }
118 }
119 }
120
121 CsrInput( const CsrInput & ) = delete;
122 CsrInput & operator=( const CsrInput & ) = delete;
123
125 [[nodiscard]] const X * offsets( void ) const { return( f_off ); }
127 [[nodiscard]] const X * heads( void ) const { return( f_head ); }
130 [[nodiscard]] const Y * weights( void ) const { return( f_w ); }
132 [[nodiscard]] const Copies & copied( void ) const { return( f_copies ); }
133
134 private:
135 const X * f_off = nullptr;
136 const X * f_head = nullptr;
137 const Y * f_w = nullptr;
138 std::vector< X > f_offCopy;
139 std::vector< X > f_headCopy;
140 std::vector< Y > f_wCopy;
141 Copies f_copies;
142 }; // end( class CsrInput )
143
144/*--------------------------------------------------------------------------*/
145/*---------------------------- STRUCT Partition ----------------------------*/
146/*--------------------------------------------------------------------------*/
147
150{
151 std::vector< int > part;
152 long long cut = 0;
153 int k = 0;
154
158 [[nodiscard]] std::vector< int > order( void ) const {
159 std::vector< int > start( std::size_t( k ) + 1 , 0 );
160 for( const int p : part ) {
161 ADS_PRE( ( p >= 0 ) && ( p < k ) );
162 ++start[ std::size_t( p ) + 1 ];
163 }
164 for( std::size_t q = 1 ; q < start.size() ; ++q )
165 start[ q ] += start[ q - 1 ];
166 std::vector< int > p( part.size() );
167 for( std::size_t u = 0 ; u < part.size() ; ++u )
168 p[ u ] = start[ std::size_t( part[ u ] ) ]++;
169 return( p );
170 }
171
173 [[nodiscard]] std::vector< int > sizes( void ) const {
174 std::vector< int > s( std::size_t( k ) , 0 );
175 for( const int p : part )
176 ++s[ std::size_t( p ) ];
177 return( s );
178 }
179 }; // end( struct Partition )
180
181/*--------------------------------------------------------------------------*/
182
183} // end( namespace ADS )
184
185/*--------------------------------------------------------------------------*/
186
187#endif /* Csr.h included */
188
189/*--------------------------------------------------------------------------*/
190/*----------------------------- End File Csr.h -----------------------------*/
191/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A static GRAPH for the engineered algorithms, beyond the book [see Graph.h]: the compressed sparse ro...
CsrInput(const G &g, bool weights=true)
the arrays of , with its weights if weights and if the graph is weighted; must outlive the input and...
Definition Csr.h:85
const X * offsets(void) const
the offsets of the rows
Definition Csr.h:125
const X * heads(void) const
the heads of the arcs, row by row
Definition Csr.h:127
CsrInput & operator=(const CsrInput &)=delete
const kahip_idx * weights(void) const
Definition Csr.h:130
const Copies & copied(void) const
which arrays were copied
Definition Csr.h:132
CsrInput(const CsrInput &)=delete
a graph whose compressed rows are plain arrays, offsets(), heads() and, if weighted,...
Definition StaticGraph.h:585
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96
which arrays were copied
Definition Csr.h:76
bool weights
the weights
Definition Csr.h:79
bool heads
the heads
Definition Csr.h:78
bool offsets
the offsets
Definition Csr.h:77
a partition of the nodes of a graph into parts
Definition Csr.h:150
int k
the number of the parts
Definition Csr.h:153
std::vector< int > sizes(void) const
the number of the nodes of each part
Definition Csr.h:173
long long cut
the weight of the edges between two parts
Definition Csr.h:152
std::vector< int > part
the part of each node, in
Definition Csr.h:151
std::vector< int > order(void) const
the name of each node when the nodes are numbered part by part, in the order of their names within a ...
Definition Csr.h:158