Skip to content
Ned Bingham edited this page Mar 23, 2017 · 22 revisions

std/array.h


template <class value_type>
struct array

This structure implements dynamic array.

Member Types

  • typedef value_type type so that generic wrappers can access this containers value_type
  • struct iterator
  • struct const_iterator

Member Variables

  • value_type *data is a pointer to the data in memory.
  • int count keeps track of the number of elements stored in this array.
  • int capacity keeps track of the number of elements we can store at this location in memory.

Member Functions

Constructor

array()

The default constructor, initializes data to NULL, count to 0 and capacity to 0.

array(const value_type &value)
array(const container &store)
array(const array<value_type> &store)

Copy the contents of another container into this array.

array(iterator left, iterator right)
array(const_iterator left, const_iterator right)
array(container::iterator left, container::iterator right)
array(container::const_iterator left, container::const_iterator right)

copy the elements within the iterator range [left,right) into this array.

Utility

static array<value_type> values(int n, ...) initializes and returns an array given n values.

int size() Returns count.

Iterators

iterator begin()
const_iterator begin() const

Returns an iterator to the first element.

iterator end()
const_iterator end() const

returns an iterator to one after the last element.

iterator rbegin()
const_iterator rbegin() const

returns an iterator to the last element.

iterator rend()
const_iterator rend() const

returns an iterator to one before the first element.

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

returns an iterator to the ith element.

Accessors

value_type &front() Returns the value of the first element.

value_type &back() Returns the value of the last element.

value_type &get(int i) Returns the value of the ith element.

value_type *ptr(int i) Returns a pointer to the ith element.

value_type &operator[](int i) Returns the value of the ith element.

Slicing

slice<array<value_type> > deref()

wraps this array 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.

array<int> idx()

If this container is filled with iterators, it returns the index of each iterator.

slice<range<iterator> > sub(int start, int end)
slice<range<const_iterator> > sub(int start, int end) const
array<value_type> subcpy(int start, int end) const

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

slice<range<iterator> > sub(int start)
slice<range<const_iterator> > sub(int start) const
array<value_type> subcpy(int start) const

returns a slice or copy of this container of the elements after start.

slice<range<iterator> > sub()
slice<range<const_iterator> > sub() const
array<value_type> subcpy()

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

static slice<range<const_iterator> > sub(iterator start, iterator end)
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.

array<container::iterator> sample(container &c)
array<container::const_iterator> 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 alloc_front(unsigned int n = 1)
void alloc_back(unsigned int n = 1)

This expands the array n elements into the pre-allocated capacity if there is enough, placing the new elements at the beginning or end of the array. Otherwise, it reallocates the array with some additional capacity.

void alloc_back_unsafe(unsigned int n = 1)

This assumes that there is enough capacity to expand the array size without reallocation. This is used internally.

static void drop(iterator start, iterator end)

This deletes a range of array iterators from its parent container.

void drop(int start, int end)

This deletes a range of elements from this array from start to end.

void drop_back(int n = 1)
void drop_front(int n = 1)

deletes n elements from the beginning or end of the array.

static array<value_type> pop(iterator start, iterator end)

removes a range of elements from its parent array and places them in a new array which is then returned to the user.

array<value_type> pop(int start, int end)

removes a range of elements from this array and places them in a new array which is then returned to the user.

array<value_type> pop_back(int n = 1)
array<value_type> pop_front(int n = 1)

removes n elements from the front or back of this array and places them in a new array which is then returned to the user.

void push_back(value_type v)
void push_front(value_type v)

place a new element on the front or back of the array, resizing the array if necessary. See alloc_front() and alloc_back().

void push_back_unsafe(value_type v)

place a new element on the back of the array, assuming that there is enough space. See alloc_back_unsafe().

void append_back(const container &c)
void append_front(const container &c)

place all of the elements in c on the front or back of the array, resizing the array if necessary.

void append_back_unsafe(const container &c)

place all of the elements in c on the back of the array assuming there is enough space. See alloc_back_unsafe.

static void replace(iterator start, iterator end, value_type v)
static void replace(iterator start, iterator end, const container &c)

replace the elements in the range [start,end) in their parent container with either the value v or all of the elements in the container c.

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

replace the elements in the range [start,end) in this array with either the value v or all of the elements in the container c.

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

replace the first or last n elements of this array with either the value v or all of the elements in the container c.

void swap(array<value_type> &arr)

swaps the contents of this container with another.

void resize(int n, const value_type &v = value_type())

resize the container to a count of n elements, adding elements of v on the back or deleting elements from the back if necessary.

void reserve(int n)

ensure that the capacity of this array is at least n by allocating more memory if necessary.

void extend(int n)

add n to the current capacity by allocating more memory.

void clear()

clear all of the elements out of the array. The array stays allocated.

void release()

clear all of the elements out of the array and de-allocate it.

array<value_type> &operator=(const container &c)
array<value_type> &operator=(const array<value_type> &c)

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

Non-Member Functions

Arithmetic

array<value_type> &operator<<(array<value_type> &os, const container &c)
array<value_type> &operator<<(array<value_type> &os, const value_type &v)

insert either the value v or all of the elements of the container c onto the end of the array.

array<value_type> operator+(const array<value_type> &os, const container &c)
array<value_type> operator+(const array<value_type> &os, const value_type &v)

combine the elements of two containers and return the result. This doesn't affect either container.

Comparison

bool operator==(array<value_type1> a1, array<value_type2> a2)
bool operator!=(array<value_type1> a1, array<value_type2> a2)
bool operator<(array<value_type1> a1, array<value_type2> a2)
bool operator>(array<value_type1> a1, array<value_type2> a2)
bool operator<=(array<value_type1> a1, array<value_type2> a2)
bool operator>=(array<value_type1> a1, array<value_type2> a2)

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

bool operator==(array<value_type1> a1, core::slice<container2> a2)
bool operator!=(array<value_type1> a1, core::slice<container2> a2)
bool operator<(array<value_type1> a1, core::slice<container2> a2)
bool operator>(array<value_type1> a1, core::slice<container2> a2)
bool operator<=(array<value_type1> a1, core::slice<container2> a2)
bool operator>=(array<value_type1> a1, core::slice<container2> a2)
bool operator==(core::slice<container1> a1, array<value_type2> a2)
bool operator!=(core::slice<container1> a1, array<value_type2> a2)
bool operator<(core::slice<container1> a1, array<value_type2> a2)
bool operator>(core::slice<container1> a1, array<value_type2> a2)
bool operator<=(core::slice<container1> a1, array<value_type2> a2)
bool operator>=(core::slice<container1> a1, array<value_type2> a2)

Compares an array to a generic slice by comparing each element from the beginning until a difference is found.

bool operator==(array<value_type1> a1, range<value_type2> a2)
bool operator!=(array<value_type1> a1, range<value_type2> a2)
bool operator<(array<value_type1> a1, range<value_type2> a2)
bool operator>(array<value_type1> a1, range<value_type2> a2)
bool operator<=(array<value_type1> a1, range<value_type2> a2)
bool operator>=(array<value_type1> a1, range<value_type2> a2)
bool operator==(range<value_type1> a1, array<value_type2> a2)
bool operator!=(range<value_type1> a1, array<value_type2> a2)
bool operator<(range<value_type1> a1, array<value_type2> a2)
bool operator>(range<value_type1> a1, array<value_type2> a2)
bool operator<=(range<value_type1> a1, array<value_type2> a2)
bool operator>=(range<value_type1> a1, array<value_type2> a2)

Compares an array to a range by comparing each element from the beginning until a difference is found.

Examples

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

using namespace core;

int main()
{
    array<int> arr = range<int>(0, 10);
    cout << arr << endl;
    return 0;
}

Simple Containers

Standard Containers

Interface Containers

Specialized Containers

Input/Output

Algorithm

Clone this wiki locally