List of all members | Public Member Functions | Static Public Member Functions | Public Attributes
casadi::Slice Class Reference

Class representing a Slice. More...

#include <slice.hpp>

Detailed Description

Note that Python or Octave do not need to use this class. They can just use slicing utility from the host language ( M[0:6] in Python, M(1:7) )

Extra doc: https://github.com/casadi/casadi/wiki/L_13

Definition at line 47 of file slice.hpp.

Inheritance diagram for casadi::Slice:
Inheritance graph
[legend]
Collaboration diagram for casadi::Slice:
Collaboration graph
[legend]

Public Member Functions

 Slice ()
 Default constructor - all elements. More...
 
 Slice (casadi_int i, bool ind1=false)
 A single element (explicit to avoid ambiguity with IM overload. More...
 
 Slice (casadi_int start, casadi_int stop, casadi_int step=1)
 A slice. More...
 
 Slice (int start, int stop, int step=1)
 
 Slice (int start, casadi_int stop, int step=1)
 
 Slice (casadi_int start, int stop, int step=1)
 
std::vector< casadi_int > all () const
 Get a vector of indices. More...
 
std::vector< casadi_int > all (casadi_int len, bool ind1=false) const
 Get a vector of indices. More...
 
std::vector< casadi_int > all (const Slice &outer, casadi_int len) const
 Get a vector of indices (nested slice) More...
 
size_t size () const
 Get number of elements. More...
 
bool is_empty () const
 Check if slice is empty. More...
 
bool is_scalar (casadi_int len) const
 Is the slice a scalar. More...
 
casadi_int scalar (casadi_int len) const
 Get scalar (if is_scalar) More...
 
bool operator== (const Slice &other) const
 Check equality. More...
 
bool operator!= (const Slice &other) const
 Check inequality. More...
 
Slice apply (casadi_int len, bool ind1=false) const
 Apply concrete length. More...
 
Slice operator- (casadi_int i) const
 Substract. More...
 
Slice operator* (casadi_int i) const
 
std::string type_name () const
 Get name of the class. More...
 
void disp (std::ostream &stream, bool more=false) const
 Print a description of the object. More...
 
std::string get_str (bool more=false) const
 Get string representation. More...
 
Dict info () const
 
void serialize (SerializingStream &s) const
 Serialize an object. More...
 

Static Public Member Functions

static Slice deserialize (DeserializingStream &s)
 Deserialize without type information. More...
 

Public Attributes

casadi_int start
 start value: negative values will get added to length More...
 
casadi_int stop
 stop value: use std::numeric_limits<casadi_int>::max() to indicate unboundedness More...
 
casadi_int step
 

Constructor & Destructor Documentation

◆ Slice() [1/6]

casadi::Slice::Slice ( )

Definition at line 32 of file slice.cpp.

32  : start(0), stop(std::numeric_limits<casadi_int>::max()), step(1) {
33  }
casadi_int step
Definition: slice.hpp:54
casadi_int stop
stop value: use std::numeric_limits<casadi_int>::max() to indicate unboundedness
Definition: slice.hpp:53
casadi_int start
start value: negative values will get added to length
Definition: slice.hpp:51

Referenced by apply(), deserialize(), operator*(), and operator-().

◆ Slice() [2/6]

casadi::Slice::Slice ( casadi_int  i,
bool  ind1 = false 
)
explicit

Definition at line 35 of file slice.cpp.

35  : start(i-ind1), stop(i-ind1+1), step(1) {
36  casadi_assert(!(ind1 && i<=0),
37  "Matlab is 1-based, but requested index " +
38  str(i) + ". Note that negative slices are"
39  " disabled in the Matlab interface. "
40  "Possibly you may want to use 'end'.");
41  if (i==-1) stop = std::numeric_limits<casadi_int>::max();
42  }
std::string str(const T &v)
String representation, any type.

References stop, and casadi::str().

◆ Slice() [3/6]

casadi::Slice::Slice ( casadi_int  start,
casadi_int  stop,
casadi_int  step = 1 
)

Definition at line 44 of file slice.cpp.

44  :
45  start(start), stop(stop), step(step) { }

◆ Slice() [4/6]

casadi::Slice::Slice ( int  start,
int  stop,
int  step = 1 
)

Definition at line 47 of file slice.cpp.

47  : start(start), stop(stop), step(step) {
48  }

◆ Slice() [5/6]

casadi::Slice::Slice ( int  start,
casadi_int  stop,
int  step = 1 
)

Definition at line 49 of file slice.cpp.

49  : start(start), stop(stop), step(step) {
50  }

◆ Slice() [6/6]

casadi::Slice::Slice ( casadi_int  start,
int  stop,
int  step = 1 
)

Definition at line 51 of file slice.cpp.

51  : start(start), stop(stop), step(step) {
52  }

Member Function Documentation

◆ all() [1/3]

std::vector< casadi_int > casadi::Slice::all ( ) const

Definition at line 90 of file slice.cpp.

90  {
91  casadi_assert(start!=std::numeric_limits<casadi_int>::min(), "Need a length");
92  casadi_assert(stop!=std::numeric_limits<casadi_int>::max(), "Need a length");
93 
94  if ((stop>=start && step<0) ||
95  (stop<=start && step>0)) return std::vector<casadi_int>();
96 
97  return range(start, stop, step);
98  }
std::vector< casadi_int > range(casadi_int start, casadi_int stop, casadi_int step, casadi_int len)
Range function.

References casadi::range(), start, step, and stop.

Referenced by all(), casadi::Matrix< Scalar >::get(), casadi::MX::get(), casadi::MX::get_nz(), casadi::MXNode::get_nz_ref(), casadi::Matrix< Scalar >::set(), casadi::MX::set(), casadi::Matrix< Scalar >::set_nz(), casadi::MX::set_nz(), and size().

◆ all() [2/3]

std::vector< casadi_int > casadi::Slice::all ( casadi_int  len,
bool  ind1 = false 
) const

Definition at line 100 of file slice.cpp.

100  {
101  return apply(len, ind1).all();
102  }
Slice apply(casadi_int len, bool ind1=false) const
Apply concrete length.
Definition: slice.cpp:66
std::vector< casadi_int > all() const
Get a vector of indices.
Definition: slice.cpp:90

References all(), and apply().

◆ all() [3/3]

std::vector< casadi_int > casadi::Slice::all ( const Slice outer,
casadi_int  len 
) const

Definition at line 129 of file slice.cpp.

129  {
130  std::vector<casadi_int> ret;
131  for (casadi_int i=outer.start; i!=outer.stop; i+=outer.step) {
132  for (casadi_int j=i+start; j!=i+stop; j+=step) {
133  ret.push_back(j);
134  }
135  }
136  return ret;
137  }

References start, step, and stop.

◆ apply()

Slice casadi::Slice::apply ( casadi_int  len,
bool  ind1 = false 
) const

Definition at line 66 of file slice.cpp.

66  {
67  casadi_int start = this->start;
68  if (start==std::numeric_limits<casadi_int>::min()) {
69  start = (step < 0) ? len - 1 : 0;
70  } else if (start<0) {
71  start+=len;
72  }
73  casadi_int stop = this->stop;
74  if (stop==std::numeric_limits<casadi_int>::max()) {
75  stop = (step < 0) ? -1 : len;
76  } else if (stop<0) {
77  stop+=len;
78  }
79 
80  casadi_assert(stop<=len,
81  "Slice (start=" + str(start) + ", stop=" + str(stop) + ", step=" + str(step)
82  + ") out of bounds with supplied length of " + str(len));
83  casadi_assert(start>=0,
84  "Slice (start=" + str(start) + ", stop=" + str(stop) + ", step=" + str(step)
85  + ") out of bounds with start<0.");
86 
87  return Slice(start+ind1, stop+ind1, step);
88  }
Slice()
Default constructor - all elements.
Definition: slice.cpp:32

References Slice(), start, step, stop, and casadi::str().

Referenced by all(), and casadi::MX::get().

◆ deserialize()

Slice casadi::Slice::deserialize ( DeserializingStream s)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_15

Definition at line 298 of file slice.cpp.

298  {
299  casadi_int start, stop, step;
300  s.unpack("Slice::start", start);
301  s.unpack("Slice::stop", stop);
302  s.unpack("Slice::step", step);
303  return Slice(start, stop, step);
304  }

References Slice(), start, step, stop, and casadi::DeserializingStream::unpack().

Referenced by casadi::DeserializingStream::unpack().

◆ disp()

void casadi::Slice::disp ( std::ostream &  stream,
bool  more = false 
) const

Definition at line 115 of file slice.cpp.

115  {
116  bool from_beginning = start == 0;
117  bool till_end = stop == std::numeric_limits<casadi_int>::max();
118  bool skip_none = step==1;
119  if (stop==start+1) {
120  stream << start;
121  } else {
122  if (!from_beginning) stream << start;
123  stream << ":";
124  if (!till_end) stream << stop;
125  if (!skip_none) stream << ":" << step;
126  }
127  }

References start, step, and stop.

◆ get_str()

std::string casadi::Slice::get_str ( bool  more = false) const
inline

Definition at line 113 of file slice.hpp.

113  {
114  std::stringstream ss;
115  disp(ss, more);
116  return ss.str();
117  }
void disp(std::ostream &stream, bool more=false) const
Print a description of the object.
Definition: slice.cpp:115

◆ info()

Dict casadi::Slice::info ( ) const
inline

Obtain information

Definition at line 120 of file slice.hpp.

120  {
121  return {{"start", start}, {"stop", stop}, {"step", step}};
122  }

Referenced by casadi::SubAssign::info(), and casadi::SubRef::info().

◆ is_empty()

bool casadi::Slice::is_empty ( ) const

Definition at line 111 of file slice.cpp.

111  {
112  return size()==0;
113  }
size_t size() const
Get number of elements.
Definition: slice.cpp:104

References size().

Referenced by casadi::MXNode::get_nzadd().

◆ is_scalar()

bool casadi::Slice::is_scalar ( casadi_int  len) const

Definition at line 139 of file slice.cpp.

139  {
140  casadi_int start = std::min(this->start, len);
141  casadi_int stop = std::min(this->stop, len);
142  casadi_int nret = (stop-start)/step + ((stop-start)%step!=0);
143  return nret==1;
144  }

References start, step, and stop.

Referenced by casadi::Matrix< Scalar >::get(), scalar(), casadi::Matrix< Scalar >::set(), and casadi::Matrix< Scalar >::set_nz().

◆ operator!=()

bool casadi::Slice::operator!= ( const Slice other) const
inline

Definition at line 95 of file slice.hpp.

95 { return !(*this == other);}

◆ operator*()

Slice casadi::Slice::operator* ( casadi_int  i) const

Definition at line 60 of file slice.cpp.

60  {
61  return Slice(start==std::numeric_limits<casadi_int>::min() ? start : start*i,
62  stop==std::numeric_limits<casadi_int>::max() ? stop : stop*i,
63  step*i);
64  }

References Slice(), start, step, and stop.

◆ operator-()

Slice casadi::Slice::operator- ( casadi_int  i) const

Definition at line 54 of file slice.cpp.

54  {
55  return Slice(start==std::numeric_limits<casadi_int>::min() ? start : start-i,
56  stop==std::numeric_limits<casadi_int>::max() ? stop : stop-i,
57  step);
58  }

References Slice(), start, step, and stop.

◆ operator==()

bool casadi::Slice::operator== ( const Slice other) const
inline

Definition at line 90 of file slice.hpp.

90  {
91  return start==other.start && stop==other.stop && step==other.step;
92  }

References start, step, and stop.

◆ scalar()

casadi_int casadi::Slice::scalar ( casadi_int  len) const

Definition at line 146 of file slice.cpp.

146  {
147  casadi_assert_dev(is_scalar(len));
148  casadi_assert(start >= -len && start < len, "Slice::getScalar: out of bounds");
149  return start >= 0 ? start : start+len;
150  }
bool is_scalar(casadi_int len) const
Is the slice a scalar.
Definition: slice.cpp:139

References is_scalar(), and start.

Referenced by casadi::Matrix< double >::einstein(), casadi::Matrix< Scalar >::get(), casadi::Matrix< double >::operator->(), casadi::Matrix< Scalar >::set(), and casadi::Matrix< Scalar >::set_nz().

◆ serialize()

void casadi::Slice::serialize ( SerializingStream s) const

Extra doc: https://github.com/casadi/casadi/wiki/L_14

Definition at line 292 of file slice.cpp.

292  {
293  s.pack("Slice::start", start);
294  s.pack("Slice::stop", stop);
295  s.pack("Slice::step", step);
296  }

References casadi::SerializingStream::pack(), start, step, and stop.

Referenced by casadi::SerializingStream::pack().

◆ size()

size_t casadi::Slice::size ( ) const

Definition at line 104 of file slice.cpp.

104  {
105  casadi_assert(start!=std::numeric_limits<casadi_int>::min() &&
106  stop!=std::numeric_limits<casadi_int>::max(),
107  "Cannot determine numel of slice.");
108  return all(std::numeric_limits<casadi_int>::max()).size();
109  }

References all(), start, and stop.

Referenced by casadi::GetNonzerosParam::create(), and is_empty().

◆ type_name()

std::string casadi::Slice::type_name ( ) const
inline

Definition at line 107 of file slice.hpp.

107 {return "Slice";}

Member Data Documentation

◆ start

casadi_int casadi::Slice::start

◆ step

casadi_int casadi::Slice::step

◆ stop

casadi_int casadi::Slice::stop

The documentation for this class was generated from the following files: