Skip to content
Ned Bingham edited this page Mar 17, 2017 · 3 revisions

std/sparse_range.h


template <class value_type, class step_type> struct sparse_range

Stores a sparse range of values in [start,finish) traversed with a given step size.

Member Types

  • typedef value_type type so that generic wrappers can access this containers value_type
  • typedef const_iterator iterator so that algorithms dependent upon iterator work.
  • sparse_range::const_iterator

Member Variables

  • value_type start represents the starting value of the range.
  • value_type finish represents the ending value of the range.
  • step_type step represents the step size of the range.

Member Functions

Constructor

sparse_range(value_type start, value_type finish)

Set start and finish directly and set step to 1.

sparse_range(value_type start, value_type finish, step_type step)

Set start, finish, and step directly.

sparse_range(const sparse_range<value_type2, step_type2> &a)

Copy another range of any type into this one with implicit type casts.

sparse_range(const range<value_type2> &a)

Copy another range of any type into this one with implicit type casts. step is set to 1.

sparse_range(const_iterator start, const_iterator finish)

Copy another range using it's iterator bounds (this is used by the sub() functions).

sparse_range(range<value_type2>::const_iterator start, range<value_type2>::const_iterator finish)

Copy another range using it's iterator bounds (this is used by the sub() functions). step is set to 1.

Utility

int size() returns the number of values in this range: (finish-start)/step.

Iterators

const_iterator begin() returns an iterator to the first element.

const_iterator end() returns an iterator to one after the last element.

const_iterator rbegin() returns an iterator to the last element.

const_iterator rend() returns an iterator to one before the first element.

const_iterator at(int i) returns an iterator to the ith element.

Accessors

value_type front() Returns the value of the first element: start.

value_type back() Returns the value of the last element: finish-1.

value_type get(int i) Returns the value of the ith element: start+i.

value_type operator[](int i) Returns the value of the ith element: start+i.

Slicing

slice<sparse_range<value_type, step_type> > deref()

wraps this range with a slice. This is particularly useful if an algorithm gives you a container filled with iterators. You can convert it into a slice with a single call to this function.

sparse_range<int, step_type> idx()

This is orthogonal to deref(). If this container is filled with iterators, it returns the index of each iterator.

slice<range<iterator> > sub(int start, int end)
sparse_range<value_type, step_type> subcpy(int start, int end)

returns a slice or copy of the elements within the range [start,end).

slice<range<iterator> > sub(int start)
sparse_range<value_type, step_type> subcpy(int start)

returns a slice or copy of the elements after start.

slice<range<iterator> > sub()
sparse_range<value_type, step_type> subcpy()

returns a slice or copy of all the elements in this container.

static slice<range<const_iterator> > sub(const_iterator start, const_iterator end)

returns a slice of all the elements within the range [start,end) of a range container.

sparse_range<typename container::iterator, step_type> sample(container &c)
sparse_range<typename container::const_iterator, step_type> sample(const container &c)

uses the values of the range container as indices into c. Returns another range container with the indexed value from c.

Modifiers

void swap(sparse_range<value_type, step_type> &root)

swaps the contents of this range container with another.

sparse_range<value_type, step_type> &operator=(const sparse_range<value_type, step_type> &root)

sets the contents of this range container equal the contents of another.

Non-Member Functions

Comparison

bool operator==(sparse_range<value_type1, step_type1> s1, sparse_range<value_type2, step_type2> s2)
bool operator!=(sparse_range<value_type1, step_type1> s1, sparse_range<value_type2, step_type2> s2)
bool operator<(sparse_range<value_type1, step_type1> s1, sparse_range<value_type2, step_type2> s2)
bool operator>(sparse_range<value_type1, step_type1> s1, sparse_range<value_type2, step_type2> s2)
bool operator<=(sparse_range<value_type1, step_type1> s1, sparse_range<value_type2, step_type2> s2)
bool operator>=(sparse_range<value_type1, step_type1> s1, sparse_range<value_type2, step_type2> s2)

Compares two range containers by comparing start followed by finish.

bool operator==(sparse_range<value_type1, step_type1> s1, core::slice<container2> s2)
bool operator!=(sparse_range<value_type1, step_type1> s1, core::slice<container2> s2)
bool operator<(sparse_range<value_type1, step_type1> s1, core::slice<container2> s2)
bool operator>(sparse_range<value_type1, step_type1> s1, core::slice<container2> s2)
bool operator<=(sparse_range<value_type1, step_type1> s1, core::slice<container2> s2)
bool operator>=(sparse_range<value_type1, step_type1> s1, core::slice<container2> s2)
bool operator==(core::slice<container1> s1, sparse_range<value_type2, step_type2> s2)
bool operator!=(core::slice<container1> s1, sparse_range<value_type2, step_type2> s2)
bool operator<(core::slice<container1> s1, sparse_range<value_type2, step_type2> s2)
bool operator>(core::slice<container1> s1, sparse_range<value_type2, step_type2> s2)
bool operator<=(core::slice<container1> s1, sparse_range<value_type2, step_type2> s2)
bool operator>=(core::slice<container1> s1, sparse_range<value_type2, step_type2> s2)

Compares a range container to a generic slice by calling compare().

bool operator==(sparse_range<value_type1, step_type1> s1, range<value_type2> s2)
bool operator!=(sparse_range<value_type1, step_type1> s1, range<value_type2> s2)
bool operator<(sparse_range<value_type1, step_type1> s1, range<value_type2> s2)
bool operator>(sparse_range<value_type1, step_type1> s1, range<value_type2> s2)
bool operator<=(sparse_range<value_type1, step_type1> s1, range<value_type2> s2)
bool operator>=(sparse_range<value_type1, step_type1> s1, range<value_type2> s2)
bool operator==(range<value_type1> s1, sparse_range<value_type2, step_type2> s2)
bool operator!=(range<value_type1> s1, sparse_range<value_type2, step_type2> s2)
bool operator<(range<value_type1> s1, sparse_range<value_type2, step_type2> s2)
bool operator>(range<value_type1> s1, sparse_range<value_type2, step_type2> s2)
bool operator<=(range<value_type1> s1, sparse_range<value_type2, step_type2> s2)
bool operator>=(range<value_type1> s1, sparse_range<value_type2, step_type2> s2)

Compares a sparse_range container to a range by calling compare().

Examples

#include <std/ascii_stream.h>
#include <std/sparse_range.h>

using namespace core;

int main()
{
    sparse_range<int> f(3, 15, 3);
    cout << f << endl;
    return 0;
}
{3, 6, 9, 12}

Simple Containers

Standard Containers

Interface Containers

Specialized Containers

Input/Output

Algorithm

Clone this wiki locally