ASL
Array< T > Class Template Reference

Detailed Description

template<class T>
class asl::Array< T >

An Array is a contiguous and resizable array of any type of elements.

It can be used as a normal fixed-size array. The length() property holds its size:

Array<int> numbers(3);
numbers[0] = 4;
numbers[1] = 3;
numbers[2] = -2;
for(int i=0; i<numbers.length(); i++)
cout << numbers[i] << endl;

Elements can be appended at the end at any moment or the array resized or cleared:

Array<String> names;
names << "John" << "Susan" << "David"; // -> length = 3
names.resize(5); // -> length = 5
names.clear(); // -> length = 0

And you can concatenate the elements as a string if they are convertible to String:

String all = numbers.join(", "); // -> "4, 3, -2"

#include <Array.h>

Inheritance diagram for Array< T >:
Stack< Context > Queue< T > Stack< T >

Public Member Functions

 Array ()
 Creates an empty array.
 
 Array (int n)
 Creates an array of n elements.
 
 Array (const T *p, int n)
 Creates an array of n elements and copies them from the pointer p.
 
 Array (int n, const T &x)
 Creates an array of n elements and gives them the value x.
 
template<class K >
Array< K > with () const
 Returns a copy of this array with all element converted to another type.
 
int length () const
 Returns the number of elements in the array.
 
Arrayreserve (int m)
 Reserves space for m elements without increasing actual length (to make appending faster)
 
Arrayresize (int m)
 Resizes the array to m elements; up to m existing elements are preserved.
 
void clear ()
 Removes all elements in the array.
 
 operator const T * () const
 Returns a pointer to the base of the array. More...
 
 operator T* ()
 Returns a pointer to the base of the array. More...
 
const T * ptr () const
 Returns a pointer to the first element. More...
 
T * ptr ()
 Returns a pointer to the first element. More...
 
T * data ()
 Returns a pointer to the first element.
 
const T * data () const
 Returns a pointer to the first element.
 
bool operator== (const Array &b) const
 Tests for equality of all elements of both arrays.
 
const T & operator[] (int i) const
 Returns the element at index i.
 
T & operator[] (int i)
 Returns the element at index i.
 
const T & last () const
 Returns a reference to the last element.
 
T & last ()
 Returns a reference to the last element.
 
int indexOf (const T &x, int j=0) const
 Returns the index of the first element with value x; The search starts at position j; The value -1 is returned if no such element is found.
 
bool contains (const T &x) const
 Returns true if the array contains an element equal to x.
 
Arraydup ()
 Makes this array independent of others.
 
Array clone () const
 Returns an independent copy of this array.
 
void copy (const Array &b)
 Copies another array's contents into this array.
 
void copy (const T *p, int n)
 Copies n elements pointed to by p (the array is resized to n)
 
Arrayoperator= (const Array &b)
 Assigns array b into this array by reference.
 
Arrayoperator<< (const T &x)
 Adds element x at the end of the array.
 
Arrayoperator, (const T &x)
 The same as <<, useful to create pseudo-array-literals.
 
Arrayremove (int i, int n=1)
 Removes n elements (1 be default) starting at position i.
 
bool removeOne (const T &x, int i0=0)
 Removes the first element with value x starting at index i0; Returns true if an element was found and removed.
 
Arrayinsert (int k, const T &x)
 Inserts x at position k.
 
Array reversed () const
 Returns the elements of the array in reversed order.
 
Array slice (int i1, int i2=0) const
 Returns a section of the array, from element i1 up to but not including element i2; If i2 is omitted the subarray will take elements up te the last.
 
Arrayappend (const Array &b)
 Adds all elements from array b at the end of the array.
 
Arrayappend (const T *p, int n)
 Adds n elements from array pointed by p at the end of this array.
 
Array concat (const Array &b) const
 Returns the concatenation of this array and b.
 
Array operator| (const Array &b) const
 Returns the concatenation of this array and b.
 
Arraysort ()
 Sorts the array using the elements' < operator "in place".
 
template<class Less >
Arraysort (Less f)
 Sorts the array using the elements' < operator "in place".
 
template<class F >
ArraysortBy (F f, bool ascending=true)
 Sorts the array by the elements' f comparable property (in ascending order by default)
 
String join (const String &sep) const
 Returns a string representation of the array, formed by joining its elements with the given separator string sep; The elements need to be convertible to String.
 
template<class F >
Array map (F f) const
 Returns an array formed by applying a function to each item.
 
template<class K , class F >
Array< K > map_ (F f) const
 Returns an array of another type formed by applying a function to each item.
 
template<class F >
Array filter (F f) const
 Returns an array containing the items in this array satisfying a condition.
 
template<class F >
ArrayremoveIf (F f)
 Removes items that meet a predicate.
 
ArrayremoveLast ()
 Removes the last item in the array.
 

Member Function Documentation

◆ operator const T *()

operator const T * ( ) const

Returns a pointer to the base of the array.

Deprecated:
This conversion will not be implicit! Use .data()

◆ operator T*()

operator T* ( )

Returns a pointer to the base of the array.

Deprecated:
This conversion will not be implicit! Use .data()

◆ ptr() [1/2]

T* ptr ( )
inline

Returns a pointer to the first element.

Deprecated:
Use .data()

◆ ptr() [2/2]

const T* ptr ( ) const
inline

Returns a pointer to the first element.

Deprecated:
Use .data()

The documentation for this class was generated from the following files: