Skip to content

index_list iterator

Ned Bingham edited this page Mar 17, 2017 · 1 revision

std/index_list.h


index_list

  • struct iterator

Points to an item in an list container.

Member Types

  • typedef value_type type so that generic wrappers can access the parent container's value_type

Member Variables

  • index_list<value_type> *root is a pointer to the parent container.
  • end_item *loc is the item pointed to by this iterator.

Member Functions

Constructor

iterator()

The default constructor. root and loc are both set to NULL.

iterator(index_list<value_type> *root, end_item *loc)

This sets root and loc directly. It's protected member that should only be used by the parent container.

iterator(const iterator &copy)

A copy constructor that sets root and loc.

Utility

operator bool() checks whether this iterator points to a valid item in the list.

Accessors

value_type &operator*() returns this iterator's value.

value_type *operator->() The dot operator for this iterator's value.

value_type *ptr() returns a pointer to this iterator's value.

value_type &get() returns this iterator's value.

iterator &ref() This is a placeholder function. It just returns the same iterator.

int idx() returns the current index of this iterator.

Iteration

iterator &operator++() Increments the iterator to the next item.

iterator &operator--() Decrements the iterator to the previous item.

iterator &operator+=(int n) Increments the iterator by n.

iterator &operator-=(int n) Decrements the iterator by n.

iterator operator+(int n) Returns the iterator n elements ahead of this one.

iterator operator-(int n) Returns the iterator n elements behind this one.

Comparison

bool operator==(iterator i)
bool operator!=(iterator i)
bool operator<(iterator i)
bool operator>(iterator i)
bool operator<=(iterator i)
bool operator>=(iterator i)
bool operator==(const_iterator i)
bool operator!=(const_iterator i)
bool operator<(const_iterator i)
bool operator>(const_iterator i)
bool operator<=(const_iterator i)
bool operator>=(const_iterator i)

Comparison operations between iterators compares index.

int operator-(iterator i)
int operator-(const_iterator i)

Returns the number of elements between i and this iterator by iterating from i to this iterator and counting.

Slicing

slice<range<iterator> > sub(int length)
index_list<value_type> subcpy(int length)

Returns a slice or copy starting at this iterator for a given length.

slice<range<iterator> > sub()
index_list<value_type> subcpy()

Returns a slice or copy from this iterator to the end of the container.

Modifiers

void drop(int n = 1)

If n is positive, this deletes all of the elements in the range [this, this+n) and the iterator is left pointing at the element that was at this+n. If n is negative, this deletes all of the elements in the range [this-n, this) and the iterator is left unchanged, pointing at this.

index_list<value_type> pop(int n = 1)

Same as drop() but the removed items are moved to their own list and returned instead of deleted.

void push(value_type v)

Places the value v into the list before the element pointed at by this iterator. The iterator remains valid and unchanged. However, due to the insertion, the index of the iterator increases by one.

void append(const container &c)

Places all of the elements in c into the list before the element pointed at by this iterator. The iterator remains valid and unchanged. However, due to the insertion, the index of the iterator increases by the number of elements in c.

void replace(int n, value_type v)
void replace(int n, const container &c)

If n is positive, this replaces the elements in the range [this, this+n) with the value v or all of the elements in the container c. This iterator is left pointing at the element that was at this+n. If n is negative, this replaces the elements in the range [this-n, this) with the value v or all of the elements in the container c. This iterator is left unchanged, pointing at this.

void swap(iterator_type i)

swap the values between these two iterators. The iterators are left unchanged.

iterator &operator=(iterator i)

set the index of this iterator equal to the index of another.

Examples

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

using namespace core;

int main()
{
    index_list<int> lst = index_list<int>::values(8, 9, 2, 4, 6, 4, 2, 8, 7);
    for (index_list<int>::iterator i = lst.begin(); i < lst.end(); i++)
        cout << i.idx() << ": " << *i << endl;
    lst.at(3).drop(3);
    cout << lst << endl;
    return 0;
}
0: 9
1: 2
2: 4
3: 6
4: 4
5: 2
6: 8
7: 7
{9, 2, 4, 8, 7}

Simple Containers

Standard Containers

Interface Containers

Specialized Containers

Input/Output

Algorithm

Clone this wiki locally