slice.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_SLICE_HPP
27 #define CASADI_SLICE_HPP
28 
29 #include <vector>
30 #include "exception.hpp"
31 #include "printable.hpp"
32 #include <limits>
33 #include <iostream>
34 #include "generic_type.hpp"
35 
36 namespace casadi {
37  class SerializingStream;
38  class DeserializingStream;
39 
40 
47  class CASADI_EXPORT Slice
48  : public SWIG_IF_ELSE(PrintableCommon, Printable<Slice>) {
49  public:
51  casadi_int start;
53  casadi_int stop;
54  casadi_int step;
55 
57  Slice();
58 
60  explicit Slice(casadi_int i, bool ind1=false);
61 
63  Slice(casadi_int start, casadi_int stop, casadi_int step=1);
64  Slice(int start, int stop, int step=1);
65  Slice(int start, casadi_int stop, int step=1);
66  Slice(casadi_int start, int stop, int step=1);
67 
69  std::vector<casadi_int> all() const;
70 
72  std::vector<casadi_int> all(casadi_int len, bool ind1=false) const;
73 
75  std::vector<casadi_int> all(const Slice& outer, casadi_int len) const;
76 
78  size_t size() const;
79 
81  bool is_empty() const;
82 
84  bool is_scalar(casadi_int len) const;
85 
87  casadi_int scalar(casadi_int len) const;
88 
90  bool operator==(const Slice& other) const {
91  return start==other.start && stop==other.stop && step==other.step;
92  }
93 
95  bool operator!=(const Slice& other) const { return !(*this == other);}
96 
98  Slice apply(casadi_int len, bool ind1=false) const;
99 
101  Slice operator-(casadi_int i) const;
102 
103  // Multiply
104  Slice operator*(casadi_int i) const;
105 
107  std::string type_name() const {return "Slice";}
108 
110  void disp(std::ostream& stream, bool more=false) const;
111 
113  std::string get_str(bool more=false) const {
114  std::stringstream ss;
115  disp(ss, more);
116  return ss.str();
117  }
118 
120  Dict info() const {
121  return {{"start", start}, {"stop", stop}, {"step", step}};
122  }
123 
127  void serialize(SerializingStream& s) const;
128 
133  };
134 
136  Slice CASADI_EXPORT to_slice(const std::vector<casadi_int>& v, bool ind1=false);
137 
139  std::pair<Slice, Slice> CASADI_EXPORT to_slice2(const std::vector<casadi_int>& v);
140 
142  bool CASADI_EXPORT is_slice(const std::vector<casadi_int>& v, bool ind1=false);
143 
145  bool CASADI_EXPORT is_slice2(const std::vector<casadi_int>& v);
146 
147 } // namespace casadi
148 
149 #endif // CASADI_SLICE_HPP
Helper class for Serialization.
Helper class for Serialization.
Class representing a Slice.
Definition: slice.hpp:48
std::string type_name() const
Get name of the class.
Definition: slice.hpp:107
void serialize(SerializingStream &s) const
Serialize an object.
Slice operator-(casadi_int i) const
Substract.
std::vector< casadi_int > all() const
Get a vector of indices.
size_t size() const
Get number of elements.
static Slice deserialize(DeserializingStream &s)
Deserialize without type information.
casadi_int scalar(casadi_int len) const
Get scalar (if is_scalar)
bool is_empty() const
Check if slice is empty.
casadi_int step
Definition: slice.hpp:54
Slice apply(casadi_int len, bool ind1=false) const
Apply concrete length.
Slice(casadi_int i, bool ind1=false)
A single element (explicit to avoid ambiguity with IM overload.
casadi_int stop
stop value: use std::numeric_limits<casadi_int>::max() to indicate unboundedness
Definition: slice.hpp:53
bool operator==(const Slice &other) const
Check equality.
Definition: slice.hpp:90
Slice()
Default constructor - all elements.
std::vector< casadi_int > all(casadi_int len, bool ind1=false) const
Get a vector of indices.
Dict info() const
Definition: slice.hpp:120
Slice(casadi_int start, int stop, int step=1)
void disp(std::ostream &stream, bool more=false) const
Print a description of the object.
std::string get_str(bool more=false) const
Get string representation.
Definition: slice.hpp:113
Slice operator*(casadi_int i) const
Slice(casadi_int start, casadi_int stop, casadi_int step=1)
A slice.
std::vector< casadi_int > all(const Slice &outer, casadi_int len) const
Get a vector of indices (nested slice)
Slice(int start, int stop, int step=1)
bool operator!=(const Slice &other) const
Check inequality.
Definition: slice.hpp:95
bool is_scalar(casadi_int len) const
Is the slice a scalar.
casadi_int start
start value: negative values will get added to length
Definition: slice.hpp:51
Slice(int start, casadi_int stop, int step=1)
The casadi namespace.
bool CASADI_EXPORT is_slice(const IM &x, bool ind1=false)
Is the IM a Slice.
std::pair< Slice, Slice > CASADI_EXPORT to_slice2(const std::vector< casadi_int > &v)
Construct nested slices from an index vector (requires is_slice2(v) to be true)
bool CASADI_EXPORT is_slice2(const std::vector< casadi_int > &v)
Check if an index vector can be represented more efficiently as two nested slices.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
Slice CASADI_EXPORT to_slice(const IM &x, bool ind1=false)
Convert IM to Slice.