Menu

Tree [6b26f8] master v0.1.1 /
 History

HTTPS access


File Date Author Commit
 html 2012-07-31 rsz rsz [6b26f8] Restructured dirs and expanded readme.
 libs 2012-07-31 rsz rsz [6b26f8] Restructured dirs and expanded readme.
 src 2012-07-31 rsz rsz [6b26f8] Restructured dirs and expanded readme.
 test 2012-07-31 rsz rsz [6b26f8] Restructured dirs and expanded readme.
 LICENSE.MIT 2012-07-31 rsz rsz [6b26f8] Restructured dirs and expanded readme.
 Makefile 2012-07-31 rsz rsz [6b26f8] Restructured dirs and expanded readme.
 README.md 2012-07-31 rsz rsz [6b26f8] Restructured dirs and expanded readme.

Read Me

Overview

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.

Features

  • Single C++ header implementation.
  • Only depends on STL.
  • Uses template for slice index type, so you can choose what's best for
    you: int, size_t, unsigned int, char, etc.
  • Custom delimiter instead of common ":" or ",".
  • Optional min, max, stride slice parameters. Some examples:
    "..." denotes entire index range.
    "2::3" will start with second (or third) index and skip by 3.
    "::-1" denotes entire range but in reverse.
    "-1" is the last element.
    "-5:" is from fifth from last until end.

Download

Source code

Git

C++ Usage

// 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;
}

Installation

make test
make memtest
make clean
sudo make install PREFIX=/usr/local

Distribution

make html
make clean
make dist VER=0.1.1

Publishing

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

License

Copyright 2011, 2012 Remik Ziemlinski (see LICENSE.MIT)

<link rel="stylesheet" href="http://yandex.st/highlightjs/7.0/styles/default.min.css">

<script src="http://yandex.st/highlightjs/7.0/highlight.min.js"></script> <script>hljs.initHighlightingOnLoad();</script>