Serialbox  2.2.0
Data serialization library and tools for C/C++, Python and Fortran
Slice.h
Go to the documentation of this file.
1 //===-- serialbox/core/Slice.h ------------------------------------------------------*- C++ -*-===//
2 //
3 // S E R I A L B O X
4 //
5 // This file is distributed under terms of BSD license.
6 // See LICENSE.txt for more information
7 //
8 //===------------------------------------------------------------------------------------------===//
9 //
14 //===------------------------------------------------------------------------------------------===//
15 
16 #ifndef SERIALBOX_CORE_SLICE_H
17 #define SERIALBOX_CORE_SLICE_H
18 
19 #include <cassert>
20 #include <limits>
21 #include <vector>
22 
23 namespace serialbox {
24 
27 
29 struct SliceTriple {
30  int start;
31  int stop;
32  int step;
33 };
34 
53 class Slice {
54 public:
55  struct Empty {};
56 
67  Slice(int start = 0, int stop = -1, int step = 1) noexcept {
68  assert(start >= 0);
69  assert(stop < 0 || stop >= start);
70  assert(step > 0);
71  sliceTriples_.push_back({start, stop, step});
72  }
73 
74  Slice(Empty) {}
75  Slice(const Slice&) = default;
76  Slice(Slice&&) = default;
77  Slice& operator=(const Slice&) = default;
78  Slice& operator=(Slice&&) = default;
79 
91  Slice& operator()(int start = 0, int stop = -1, int step = 1) noexcept {
92  assert(start >= 0);
93  assert(stop < 0 || stop >= start);
94  assert(step > 0);
95  sliceTriples_.push_back({start, stop, step});
96  return *this;
97  }
98 
100  bool empty() const noexcept { return sliceTriples_.empty(); }
101 
103  void swap(Slice& other) noexcept { sliceTriples_.swap(other.sliceTriples_); }
104 
106  std::vector<SliceTriple>& sliceTriples() noexcept { return sliceTriples_; }
107  const std::vector<SliceTriple>& sliceTriples() const noexcept { return sliceTriples_; }
108 
109 private:
110  std::vector<SliceTriple> sliceTriples_;
111 };
112 
114 
115 } // namespace serialbox
116 
117 #endif
int step
Step of the slice.
Definition: Slice.h:32
Slice & operator()(int start=0, int stop=-1, int step=1) noexcept
Append a slice to the i-th dimension where i is the current size() of the sliceTriples vector...
Definition: Slice.h:91
int start
Starting index of the slice.
Definition: Slice.h:30
Namespace of the serialbox library.
Definition: Archive.h:25
void swap(Slice &other) noexcept
Swap with other
Definition: Slice.h:103
int stop
Stopping index of the slice (index stop is not included)
Definition: Slice.h:31
std::vector< SliceTriple > & sliceTriples() noexcept
Get slice triples.
Definition: Slice.h:106
Slice per dimension.
Definition: Slice.h:29
bool empty() const noexcept
Check if slice is empty.
Definition: Slice.h:100
Specification of the slice indices which is used for partial loading of serialized data...
Definition: Slice.h:53
Slice(int start=0, int stop=-1, int step=1) noexcept
Initialize the slice of the first dimension.
Definition: Slice.h:67