From fd236cf9cbfddf47e8edbb17d56255de25763699 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= <oberhuber.tomas@gmail.com>
Date: Sun, 18 Apr 2021 13:03:20 +0200
Subject: [PATCH] Refactoring segments printing.

---
 .../Algorithms/Segments/SegmentsPrinting.h    | 76 ++++++-------------
 1 file changed, 25 insertions(+), 51 deletions(-)

diff --git a/src/TNL/Algorithms/Segments/SegmentsPrinting.h b/src/TNL/Algorithms/Segments/SegmentsPrinting.h
index 260ad71e8d..7695cc6939 100644
--- a/src/TNL/Algorithms/Segments/SegmentsPrinting.h
+++ b/src/TNL/Algorithms/Segments/SegmentsPrinting.h
@@ -49,65 +49,39 @@ std::ostream& printSegments( const Segments& segments, std::ostream& str )
    return str;
 }
 
-
-/**
- * \brief Print segments with related content.
- *
- * \tparam Segments is type of segments.
- * \tparam Fetch is a lambda function for reading related data.
- * \param segments is an instance of segments.
- * \param fetch is an instance of lambda function reading related data. It is supposed to defined as
- *
- * ```
- * auto fetch = [=] __cuda_callable__ ( IndexType globalIdx ) -> ValueType { return data_view[ globalIdx ]; };
- * ```
- *
- * \param str is output stream.
- * \return reference to the output stream.
- *
- * \par Example
- * \include Algorithms/Segments/SegmentsPrintingExample-2.cpp
- * \par Output
- * \include SegmentsPrintingExample-2.out
- */
 template< typename Segments,
           typename Fetch >
-std::ostream& printSegments( const Segments& segments, Fetch&& fetch, std::ostream& str )
+struct SegmentsPrinter
 {
-   using IndexType = typename Segments::IndexType;
-   using DeviceType = typename Segments::DeviceType;
-   using ValueType = decltype( fetch( IndexType() ) );
+   SegmentsPrinter( const Segments& segments, Fetch& fetch )
+   : segments( segments ), fetch( fetch ) {}
 
-   TNL::Containers::Array< ValueType, DeviceType, IndexType > aux( 1 );
-   auto view = segments.getConstView();
-   for( IndexType segmentIdx = 0; segmentIdx < segments.getSegmentsCount(); segmentIdx++ )
+   std::ostream& print( std::ostream& str ) const
    {
-      str << "Seg. " << segmentIdx << ": [ ";
-      auto segmentSize = segments.getSegmentSize( segmentIdx );
-      for( IndexType localIdx = 0; localIdx < segmentSize; localIdx++ )
+      using IndexType = typename Segments::IndexType;
+      using DeviceType = typename Segments::DeviceType;
+      using ValueType = decltype( fetch( IndexType() ) );
+
+      TNL::Containers::Array< ValueType, DeviceType, IndexType > aux( 1 );
+      auto view = segments.getConstView();
+      for( IndexType segmentIdx = 0; segmentIdx < segments.getSegmentsCount(); segmentIdx++ )
       {
-         aux.forAllElements( [=] __cuda_callable__ ( IndexType elementIdx, double& v ) mutable {
-            v = fetch( view.getGlobalIndex( segmentIdx, localIdx ) );
-         } );
-         auto value = aux.getElement( 0 );
-         str << value;
-         if( localIdx < segmentSize - 1 )
-            str << ", ";
+         str << "Seg. " << segmentIdx << ": [ ";
+         auto segmentSize = segments.getSegmentSize( segmentIdx );
+         for( IndexType localIdx = 0; localIdx < segmentSize; localIdx++ )
+         {
+            aux.forAllElements( [=] __cuda_callable__ ( IndexType elementIdx, double& v ) mutable {
+               v = fetch( view.getGlobalIndex( segmentIdx, localIdx ) );
+            } );
+            auto value = aux.getElement( 0 );
+            str << value;
+            if( localIdx < segmentSize - 1 )
+               str << ", ";
+         }
+         str << " ] " << std::endl;
       }
-      str << " ] " << std::endl;
+      return str;
    }
-   return str;
-}
-
-
-template< typename Segments,
-          typename Fetch >
-struct SegmentsPrinter
-{
-   SegmentsPrinter( const Segments& segments, Fetch& fetch )
-   : segments( segments ), fetch( fetch ) {}
-
-   std::ostream& print( std::ostream& str ) const { return printSegments( segments, fetch, str ); }
 
    protected:
 
-- 
GitLab