Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-30 08:37:55

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file corecel/cont/SpanIO.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <iomanip>
0010 #include <ostream>
0011 
0012 #include "Span.hh"
0013 
0014 namespace celeritas
0015 {
0016 //---------------------------------------------------------------------------//
0017 /*!
0018  * Write the elements of span \a s to stream \a os.
0019  */
0020 template<class T, std::size_t E>
0021 std::ostream& operator<<(std::ostream& os, Span<T, E> const& s)
0022 {
0023     std::streamsize size = s.size();
0024     std::streamsize width = os.width();
0025     std::streamsize remainder = 0;
0026 
0027     os.width(0);
0028     os << '{';
0029     if (width > 2 + (size - 1))
0030     {
0031         // Subtract width for spaces and braces
0032         width -= 2 + (size - 1);
0033         // Individual width is 1/N of that, rounded down, keep remainder
0034         // separate
0035         remainder = width % size;
0036         width = width / size;
0037     }
0038     else
0039     {
0040         width = 0;
0041     }
0042 
0043     // First element gets the remainder
0044     os.width(width + remainder);
0045     if (!s.empty())
0046     {
0047         os << s[0];
0048     }
0049 
0050     for (std::streamsize i = 1; i < size; ++i)
0051     {
0052         os << ',';
0053         os.width(width);
0054         os << s[i];
0055     }
0056     os << '}';
0057 
0058     return os;
0059 }
0060 
0061 //---------------------------------------------------------------------------//
0062 }  // namespace celeritas