AoC code coverage
Current view: top level - aoclib - LinewiseInput.cpp (source / functions) Coverage Total Hit
Test: master Lines: 98.4 % 64 63
Test Date: 2025-07-28 10:53:57 Functions: 100.0 % 11 11

            Line data    Source code
       1              : #include "LinewiseInput.h"
       2              : #include "Grid2d.h"
       3              : 
       4              : #include <algorithm>
       5              : #include <ranges>
       6              : 
       7           77 : LinewiseInput::LinewiseInput(std::string_view const text) {
       8        44271 :   for (auto it = text.begin(); it != text.end();) {
       9        44194 :     auto lineStart = it;
      10        44194 :     it = std::find(it, text.end(), '\n');
      11        44194 :     _lines.emplace_back(lineStart, it);
      12        44194 :     if (it != text.end())
      13        44118 :       ++it;
      14        44194 :   }
      15           77 : }
      16              : 
      17            6 : std::vector<std::span<std::string_view const>> LinewiseInput::splitOnEmptyLines() const {
      18            6 :   std::vector<std::span<std::string_view const>> result;
      19              : 
      20          704 :   for (auto it = _lines.begin(); it != _lines.end();) {
      21         1390 :     while (it->empty() && it != _lines.end())
      22          692 :       ++it;
      23          698 :     auto startIt = it;
      24         8648 :     it = std::find_if(it, _lines.end(), [](std::string_view const s) { return s.empty(); });
      25          698 :     result.emplace_back(startIt, it);
      26          698 :   }
      27            6 :   return result;
      28            6 : }
      29              : 
      30           15 : bool LinewiseInput::allLinesOfSameSize() const {
      31           15 :   if (_lines.empty())
      32            0 :     return true;
      33         1703 :   return std::all_of(std::next(_lines.begin()), _lines.end(), [this](std::string_view const s) {
      34         1703 :     return s.size() == _lines.front().size();
      35         1703 :   });
      36           15 : }
      37              : 
      38          217 : Grid2d<char> makeCharGrid2d(std::span<std::string_view const> const lines) {
      39          217 :   size_t const xSize = lines.front().size();
      40          217 :   size_t const ySize = lines.size();
      41          217 :   Grid2d<char> result(xSize, ySize);
      42              : 
      43          217 :   Grid2d<char>::IndexType y = 0;
      44         4384 :   std::ranges::for_each(std::ranges::reverse_view(lines), [&](std::string_view line) {
      45         4384 :     DEBUG_ASSERT(line.size() == xSize);
      46       267504 :     for (size_t x = 0; x < xSize; ++x)
      47       263120 :       result[x, y] = line[x];
      48         4384 :     ++y;
      49         4384 :   });
      50              : 
      51          217 :   return result;
      52          217 : }
      53              : 
      54           17 : Grid2d<char> LinewiseInput::makeCharGrid2d() const { return ::makeCharGrid2d(_lines); }
      55              : 
      56              : Grid2d<char> LinewiseInput::makeCharGrid2dWithGhostLayers(int const numGhostLayers,
      57           15 :                                                           char const fillElement) const {
      58           15 :   DEBUG_ASSERT(allLinesOfSameSize());
      59           15 :   using IndexType = Grid2d<char>::IndexType;
      60           15 :   size_t const xSize = _lines.front().size();
      61           15 :   size_t const ySize = _lines.size();
      62              : 
      63           15 :   Grid2d<char> result(xSize, ySize, fillElement, numGhostLayers);
      64              : 
      65           15 :   IndexType y = 0;
      66         1718 :   std::ranges::for_each(std::ranges::reverse_view(_lines), [&](std::string_view line) {
      67       217106 :     for (IndexType x = 0; x < result.xSize(); ++x)
      68       215388 :       result[x, y] = line[x];
      69         1718 :     ++y;
      70         1718 :   });
      71              : 
      72           15 :   return result;
      73           15 : }
      74              : 
      75            6 : std::generator<std::string_view> getLineWise(std::string_view const text) {
      76         4306 :   for (auto it = text.begin(); it != text.end();) {
      77         4300 :     auto lineStart = it;
      78         4300 :     it = std::find(it, text.end(), '\n');
      79         4300 :     co_yield std::string_view{lineStart, it};
      80         4300 :     if (it != text.end())
      81         4294 :       ++it;
      82         4300 :   }
      83            6 : }
        

Generated by: LCOV version 2.0-1