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-12-01 01:56:34 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      765998 : void validate_begin(const scipp::index begin) {
      14      765998 :   if (begin < 0)
      15         131 :     throw except::SliceError("begin must be >= 0. Given " +
      16         262 :                              std::to_string(begin));
      17      765867 : }
      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      653017 : Slice::Slice(const Dim dim_, const scipp::index begin_, const scipp::index end_,
      27      653017 :              const scipp::index stride_)
      28      653017 :     : m_dim(dim_), m_begin(begin_), m_end(end_), m_stride(stride_) {
      29      653017 :   validate_begin(begin_);
      30      652925 :   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      652924 :   if (stride_ == 0)
      35           1 :     throw except::SliceError("slice step cannot be zero");
      36      652923 :   if (stride_ < 0)
      37           2 :     throw except::SliceError("negative slice step support not implemented");
      38      652921 : }
      39             : 
      40             : /// Constructor for point slice
      41             : /// \param dim_ Slice Dimension
      42             : /// \param begin_ start index or single index of the slice
      43             : ///
      44      112981 : Slice::Slice(const Dim dim_, const index begin_)
      45      112981 :     : m_dim(dim_), m_begin(begin_), m_end(-1), m_stride(1) {
      46      112981 :   validate_begin(begin_);
      47      112942 : }
      48             : 
      49     3091016 : bool Slice::operator==(const Slice &other) const noexcept {
      50     3102284 :   return m_dim == other.dim() && m_begin == other.m_begin &&
      51     3102284 :          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