libpnicore
slice.hpp
1 //
2 // (c) Copyright 2012 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpnicore.
5 //
6 // libpnicore is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // libpnicore is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with libpnicore. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // ===========================================================================
20 //
21 // Created on: May 14, 2012
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 #pragma once
25 
26 #include <iostream>
27 #include <utility>
28 
29 #include "../error/exceptions.hpp"
30 
31 namespace pni{
32 namespace core{
33 
49  class slice
50  {
51  private:
52  size_t _first;
53  size_t _last;
54  size_t _stride;
55 
65  void _check_start_stop(const exception_record &o) const;
66 
67  //-----------------------------------------------------------------
77  void _check_stride(const exception_record &o) const;
78  public:
79  //==============constructors and destructor========================
81  slice() {}
82 
83  //-----------------------------------------------------------------
94  explicit slice(size_t first,size_t last,size_t stride=1);
95 
96  //-----------------------------------------------------------------
102 
113  explicit slice(const std::initializer_list<size_t> &l);
122 
123  //-----------------------------------------------------------------
132  explicit slice(size_t index);
133 
134  //-----------------------------------------------------------------
136  ~slice(){}
137 
138  //===============assignment operators==============================
140  slice &operator=(const slice &s);
141 
142  //=================public member methods===========================
148  size_t first() const { return _first; }
149 
150  //-----------------------------------------------------------------
156  size_t last() const { return _last; }
157 
158  //-----------------------------------------------------------------
164  size_t stride() const { return _stride; }
165  };
166 
168  std::ostream &operator<<(std::ostream &o,const slice &s);
169 
170  bool operator==(const slice &lhs,const slice &rhs);
171 
172  bool operator!=(const slice &lhs,const slice &rhs);
173 
174  //-------------------------------------------------------------------------
181 
186  size_t size(const slice &s);
190 
191  //-------------------------------------------------------------------------
197 
202  size_t span(const slice &s);
206 
207 //end of namespace
208 }
209 }
size_t first() const
return the first element
Definition: slice.hpp:148
slice()
default constructor
Definition: slice.hpp:81
void _check_stride(const exception_record &o) const
check stride
exception record
Definition: exceptions.hpp:90
slice & operator=(const slice &s)
copy assignment operator
bool operator==(const array_view< ATYPE > &a, const array_view< ATYPE > &b)
compare two array views
Definition: array_view.hpp:862
size_t last() const
return last element
Definition: slice.hpp:156
void _check_start_stop(const exception_record &o) const
check first and last index
Definition: add_op.hpp:29
~slice()
destructor
Definition: slice.hpp:136
bool operator!=(const array_view< ATYPE > &a, const array_view< ATYPE > &b)
compare two array views
Definition: array_view.hpp:885
size_t span(const slice &s)
compute total elements spanned
size_t _stride
distance between indices
Definition: slice.hpp:54
index slice
Definition: slice.hpp:49
std::ostream & operator<<(std::ostream &stream, const array_view< ATYPE > &v)
output operator for view
Definition: array_view.hpp:903
size_t _first
first index in the slice
Definition: slice.hpp:52
size_t size(const slice &s)
compute slice size
size_t stride() const
return stride
Definition: slice.hpp:164
size_t _last
last index in the slice
Definition: slice.hpp:53