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-04-28 01:25:40 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      761632 : void validate_begin(const scipp::index begin) {
      14      761632 :   if (begin < 0)
      15         131 :     throw except::SliceError("begin must be >= 0. Given " +
      16         262 :                              std::to_string(begin));
      17      761501 : }
      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      652041 : Slice::Slice(const Dim dim_, const scipp::index begin_, const scipp::index end_,
      27      652041 :              const scipp::index stride_)
      28      652041 :     : m_dim(dim_), m_begin(begin_), m_end(end_), m_stride(stride_) {
      29      652041 :   validate_begin(begin_);
      30      651949 :   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      651948 :   if (stride_ == 0)
      35           1 :     throw except::SliceError("slice step cannot be zero");
      36      651947 :   if (stride_ < 0)
      37           2 :     throw except::SliceError("negative slice step support not implemented");
      38      651945 : }
      39             : 
      40             : /// Constructor for point slice
      41             : /// \param dim_ Slice Dimension
      42             : /// \param begin_ start index or single index of the slice
      43             : ///
      44      109591 : Slice::Slice(const Dim dim_, const index begin_)
      45      109591 :     : m_dim(dim_), m_begin(begin_), m_end(-1), m_stride(1) {
      46      109591 :   validate_begin(begin_);
      47      109552 : }
      48             : 
      49     3077274 : bool Slice::operator==(const Slice &other) const noexcept {
      50     3088542 :   return m_dim == other.dim() && m_begin == other.m_begin &&
      51     3088542 :          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