LCOV - code coverage report
Current view: top level - core - slice.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 27 27 100.0 %
Date: 2024-11-17 01:47:58 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: BSD-3-Clause
       2             : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
       3             : /// @file
       4             : /// @author Owen Arnold
       5             : #include <string>
       6             : 
       7             : #include "scipp/core/except.h"
       8             : #include "scipp/core/slice.h"
       9             : 
      10             : namespace scipp::core {
      11             : 
      12             : namespace {
      13      765543 : void validate_begin(const scipp::index begin) {
      14      765543 :   if (begin < 0)
      15         131 :     throw except::SliceError("begin must be >= 0. Given " +
      16         262 :                              std::to_string(begin));
      17      765412 : }
      18             : } // namespace
      19             : 
      20             : /// Constructor for range slice
      21             : /// \param dim_ Slice Dimension
      22             : /// \param begin_ start index or single index of the slice
      23             : /// \param end_ end index for the range. Note that -1 indicates a point slice,
      24             : /// not before-end.
      25             : ///
      26      652641 : Slice::Slice(const Dim dim_, const scipp::index begin_, const scipp::index end_,
      27      652641 :              const scipp::index stride_)
      28      652641 :     : m_dim(dim_), m_begin(begin_), m_end(end_), m_stride(stride_) {
      29      652641 :   validate_begin(begin_);
      30      652549 :   if (end_ != -1 && begin_ > end_)
      31           2 :     throw except::SliceError("end must be >= begin. Given begin " +
      32           3 :                              std::to_string(begin_) + " end " +
      33           2 :                              std::to_string(end_));
      34      652548 :   if (stride_ == 0)
      35           1 :     throw except::SliceError("slice step cannot be zero");
      36      652547 :   if (stride_ < 0)
      37           2 :     throw except::SliceError("negative slice step support not implemented");
      38      652545 : }
      39             : 
      40             : /// Constructor for point slice
      41             : /// \param dim_ Slice Dimension
      42             : /// \param begin_ start index or single index of the slice
      43             : ///
      44      112902 : Slice::Slice(const Dim dim_, const index begin_)
      45      112902 :     : m_dim(dim_), m_begin(begin_), m_end(-1), m_stride(1) {
      46      112902 :   validate_begin(begin_);
      47      112863 : }
      48             : 
      49     3088356 : bool Slice::operator==(const Slice &other) const noexcept {
      50     3099624 :   return m_dim == other.dim() && m_begin == other.m_begin &&
      51     3099624 :          m_end == other.m_end && m_stride == other.m_stride;
      52             : }
      53           3 : bool Slice::operator!=(const Slice &other) const noexcept {
      54           3 :   return !(*this == other);
      55             : }
      56             : 
      57             : } // namespace scipp::core

Generated by: LCOV version 1.14