# include # include # include # include using namespace std; # include "components.hpp" int main ( ); void components_1d_test ( ); void components_2d_test ( ); void components_3d_test ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // components_test() tests components(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 17 July 2022 // // Author: // // John Burkardt // { timestamp ( ); cout << "\n"; cout << "components_test():\n"; cout << " C++ version\n"; cout << " Test components().\n"; components_1d_test ( ); components_2d_test ( ); components_3d_test ( ); // // Terminate. // cout << "\n"; cout << "components_test()\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 void components_1d_test ( ) //****************************************************************************80 // // Purpose: // // components_1d_test() tests components_1d(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 17 July 2022 // // Author: // // John Burkardt // { # define N 28 int a[N] = { 0, 0, 1, 2, 4, 0, 0, 4, 0, 0, 0, 8, 9, 9, 1, 2, 3, 0, 0, 5, 0, 1, 6, 0, 0, 0, 4, 0 }; int c[N]; int component_num; int j; int n = N; cout << "\n"; cout << "components_1d_test():\n"; cout << " components_1d() finds and labels connected\n"; cout << " components in a 1D integer vector.\n"; cout << "\n"; cout << " A:\n"; cout << "\n"; cout << " "; for ( j = 0; j < n; j++ ) { cout << a[j]; } cout << "\n"; component_num = components_1d ( n, a, c ); cout << "\n"; cout << " Number of components = " << component_num << "\n"; cout << "\n"; cout << " C:\n"; cout << "\n"; cout << " "; for ( j = 0; j < n; j++ ) { cout << c[j]; } cout << "\n"; return; # undef N } //****************************************************************************80 void components_2d_test ( ) //****************************************************************************80 // // Purpose: // // components_2d_test() tests components_2d(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 17 July 2022 // // Author: // // John Burkardt // { # define M 9 # define N 17 int a[M*N] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; int *c; int component_num; int i; int j; int m = M; int n = N; cout << "\n"; cout << "components_2d_test():\n"; cout << " components_2d() finds and labels connected\n"; cout << " components in a 2D integer array.\n"; cout << "\n"; cout << " A:\n"; cout << "\n"; for ( i = 0; i < m; i++ ) { cout << " "; for ( j = 0; j < n; j++ ) { cout << a[i+j*m]; } cout << "\n"; } c = new int[m*n]; component_num = components_2d ( m, n, a, c ); cout << "\n"; cout << " Number of components = " << component_num << "\n"; cout << "\n"; cout << " C:\n"; cout << "\n"; for ( i = 0; i < m; i++ ) { cout << " "; for ( j = 0; j < n; j++ ) { cout << c[i+j*m]; } cout << "\n"; } delete [] c; return; # undef M # undef N } //****************************************************************************80 void components_3d_test ( ) //****************************************************************************80 // // Purpose: // // components_3d_test() tests components_3d(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 17 July 2022 // // Author: // // John Burkardt // { # define L 64 # define M 64 # define N 26 int a[L*M*N]; int c[L*M*N]; int component_num; string filename; int i; int *indices; int j; int j1; int k; int l = L; int m = M; int m1; int n = N; int n1; int *s; int s_total; cout << "\n"; cout << "components_3d_test()\n"; cout << " components_3d() finds and labels connected\n"; cout << " components in a 3D integer block.\n"; cout << "\n"; cout << " A is a 3D block of order " << l << " * " << m << " * " << n << "\n"; for ( k = 0; k < n; k++ ) { for ( j = 0; j < m; j++ ) { for ( i = 0; i < l; i++ ) { a[i+j*l+k*l*m] = 0; } } } // // Retrieve the indices of nonzero data in A by reading a file. // filename = "indices.txt"; i4mat_header_read ( filename, &m1, &n1 ); indices = i4mat_data_read ( filename, m1, n1 ); for ( j1 = 0; j1 < n1; j1++ ) { i = indices[0+j1*3] - 1; j = indices[1+j1*3] - 1; k = indices[2+j1*3] - 1; a[i+j*l+k*l*m] = 1; } delete [] indices; cout << "\n"; cout << " Number of nonzero A values is " << n1 << "\n"; // // Determine the components. // component_num = components_3d ( l, m, n, a, c ); s = new int[component_num]; for ( i = 0; i < component_num; i++ ) { s[i] = 0; } cout << "\n"; cout << " Number of components = " << component_num << "\n"; for ( k = 0; k < n; k++ ) { for ( j = 0; j < m; j++ ) { for ( i = 0; i < l; i++ ) { if ( c[i+j*l+k*l*m] != 0 ) { s[c[i+j*l+k*l*m]-1] = s[c[i+j*l+k*l*m]-1] + 1; } } } } cout << "\n"; cout << " Component Size\n"; cout << "\n"; s_total = 0; for ( i = 0; i < component_num; i++ ) { cout << " " << setw(4) << i + 1 << " " << setw(8) << s[i] << "\n"; s_total = s_total + s[i]; } cout << "------ --------\n"; cout << " Total " << setw(8) << s_total << "\n"; delete [] s; return; # undef L # undef M # undef N }