ezSlice is a class to parse array slice strings like those used in Python or MATLAB. Examples are in the unit test test_ezSlice.cpp.
This is a templated, self-contained, single header implementation C++ class for parsing array slice operator strings that specify a min, max, and stride, such as "1:10:2", "::3" or "-1,-5,-2". There is an option to use any offset based indexing desired, such as 0-based for C/Python arrays, or 1-based for Fortran/MATLAB indexing. Reverse indexing is allowed, as well as relative to an array end. This class won't do the actual slice/sampling of an array; instead it only parses a string and returns the index limits and stride, or an STL vector of indices if desired.
// Some excerpts from test/test_ezSlice.cpp.
#include "ezSlice.hpp"
int main() {
ez::ezSlice<int> slice;
int min, max, stride, n = 10;
std::vector<int> values;
// Set min,max,stride without a delimiter.
slice.set("2",0,0);
slice.get(min,max,stride,n);
slice.get(values,n); // Returns [ 2 3 4 5 6 7 8 9 ]
slice.set("2","7","2");
slice.get(min,max,stride,n);
slice.get(values,n); // Returns [ 2 4 6 ]
// Set a string to parse.
std::string str = "2:5";
slice.set(str,':');
slice.get(min,max,stride,n);
slice.get(values,n); // Returns [ 2 3 4 5 ]
// Reverse stride.
str = "::-3";
slice.set(str,':');
slice.get(min,max,stride,n);
slice.get(values,n); // Returns [ 9 6 3 0 ]
// Get all along dimension.
// ":" and "::" will return same result in this context.
str = "...";
slice.set(str,':');
slice.get(min,max,stride,n);
slice.get(values,n); // Returns [ 0 1 2 3 4 5 6 7 8 9 ]
// Of cource single value works too, but use an offset.
str = "3";
slice.set(str,':', -1);
slice.get(min,max,stride,n);
slice.get(values,n); // Returns [ 2 ]
// Without offset, we get different result.
slice.set(str,':');
slice.get(min,max,stride,n);
slice.get(values,n); // Returns [ 3 ]
return 0;
}
make test
make memtest
make clean
sudo make install PREFIX=/usr/local
make html
make clean
make dist VER=0.1.1
ssh -t rsz,ezslice@shell.sourceforge.net create
scp html/* rsz,ezslice@shell.sourceforge.net:/home/project-web/ezslice/htdocs
scp ../ezSlice-0.1.1.tar.gz rsz,ezslice@shell.sourceforge.net:/home/frs/project/e/ez/ezslice
Copyright 2011, 2012 Remik Ziemlinski (see LICENSE.MIT)