Skip to content
Ned Bingham edited this page Mar 24, 2017 · 5 revisions

std/slice.h


template <class container>
struct slice : container

Interface containers like this one serve to apply a different interface to an existing container. Slices allow you to interact with a container of iterators or pointers as if it were instead a container of values.

Member Types

  • typedef elem_type<container>::type index_type This is the type signature of the iterator or pointer that the wrapped container stores.
  • typedef elem_type<index_type>::type type This is the type signature of the value that the iterators or pointers point to. This is effectively the slice's value_type.
  • struct iterator
  • struct const_iterator

Non-Member Types

template <class container>
struct elem_type
{
  typedef typename container::type type;
};

template <class value>
struct elem_type<value*>
{
  typedef value type;
};

This serves to extract the type information of an iterator, a pointer, or a container. This is necessary for keeping slices relatively generic and insensitive to the storage medium.

Member Functions

Constructor

slice()

The default constructor, calls the default constructor of the wrapped container.

slice(const container2 &store)

Call the copy constructor of the wrapped container.

slice(iterator left, iterator right)
slice(const_iterator left, const_iterator right)

Call the range copy constructor of the wrapped container.

Iterators

iterator begin()
const_iterator begin() const

Returns an iterator to the first element wrapped by the slice iterator.

iterator end()
const_iterator end() const

returns an iterator to one after the last element wrapped by the slice iterator.

iterator rbegin()
const_iterator rbegin() const

returns an iterator to the last element wrapped by the slice iterator.

iterator rend()
const_iterator rend() const

returns an iterator to one before the first element wrapped by the slice iterator.

iterator at(int i)
const_iterator at(int i) const

returns an iterator to the ith element wrapped by the slice iterator.

Accessors

type &front() Returns the value that the first element points to.

value_type &back() Returns the value that the last element points to.

value_type &get(int i) Returns the value that the ith element points to.

value_type *ptr(int i) Returns a pointer to the value that the ith element points to (this distinction is important because this returns a pointer and not an iterator).

index_type &ref(int i) Returns the raw iterator instead of the value of that iterator.

value_type &operator[](int i) Returns the value that the ith element points to.

Slicing

container &ref()

Return the wrapped container.

Note: we normally have the function deref() which wraps the container in a slice. If we were to do that here, we run into recursive instantiation of templates and the g++ compiler crashes. To properly implement this, we would have to add a non-template integer to the slice structure which tells us how far to de-reference. For now, this functionality hasn't been implemented.

slice<container> sub(int start, int end)
slice<container> subcpy(int start, int end)

Returns a smaller slice of the elements within the range [start,end) with respect to this slice's iterators.

slice<container> sub(int start)
slice<container> subcpy(int start)

returns a smaller slice of this container of the elements after start with respect to this slice's iterators.

slice<container> sub()
slice<container> subcpy()

Returns this.

static slice<container> sub(iterator start, iterator end)
static slice<container> sub(const_iterator start, const_iterator end)

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

Non-Member Functions

Comparison

int compare(const container1 &a, const container2 &b)
bool equal_to(const container1 &a, const container2 &b)
bool less_than(const container1 &a, const container2 &b)
bool greater_than(const container1 &a, const container2 &b)

Compares two arbitrary containers by comparing each element from the beginning.

bool operator==(slice<container1> s1, slice<container2> s2)
bool operator!=(slice<container1> s1, slice<container2> s2)
bool operator<(slice<container1> s1, slice<container2> s2)
bool operator>(slice<container1> s1, slice<container2> s2)
bool operator<=(slice<container1> s1, slice<container2> s2)
bool operator>=(slice<container1> s1, slice<container2> s2)

Compares two slices by comparing each element from the beginning until a difference is found.

Examples

array<int> arr = range<int>(0, 10);
cout << "arr = " << arr << endl << endl;

slice<range<array<int>::iterator> > slc = arr.sub(2, 6);
cout << "slc = " << slc << endl << endl;

cout << "slc[1] = 100" << endl;
slc[1] = 100;
cout << "slc = " << slc << endl;
cout << "arr = " << arr << endl << endl;

slice<array<array<int>::iterator> > slc2 = array<int>::values(4, 2, 5, 0, 1).sample(arr);
cout << "slc2 = " << slc2 << endl << endl;

cout << "slc2[1] = 200" << endl;
slc2[1] = 200;
cout << "slc2 = " << slc2 << endl;
cout << "arr = " << arr << endl;
cout << "slc = " << slc << endl;
arr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

slc = {2, 3, 4, 5}

slc[1] = 100
slc = {2, 100, 4, 5}
arr = {0, 1, 2, 100, 4, 5, 6, 7, 8, 9}

slc2 = {2, 5, 0, 1}

slc2[1] = 200
slc2 = {2, 200, 0, 1}
arr = {0, 1, 2, 100, 4, 200, 6, 7, 8, 9}
slc = {2, 100, 4, 200}

Simple Containers

Standard Containers

Interface Containers

Specialized Containers

Input/Output

Algorithm

Clone this wiki locally