PIP 5.5.3
Platform-Independent Primitives
Public Member Functions | List of all members
PIVector< T > Class Template Reference

Sequence linear container - dynamic size array of any type. More...

#include <pivector.h>

Inheritance diagram for PIVector< T >:
Inheritance graph
[legend]

Public Member Functions

 PIVector ()
 Constructs an empty array.
 
 PIVector (const T *data, size_t size)
 Contructs array from raw data. This constructor reserve size and copy from data pointer.
 
 PIVector (const PIVector< T > &v)
 Copy constructor.
 
 PIVector (std::initializer_list< T > init_list)
 Contructs array from C++11 initializer list. More...
 
 PIVector (size_t size, const T &e=T())
 Contructs array with size size filled elements e.
 
 PIVector (size_t size, std::function< T(size_t i)> f)
 Contructs array with size size and elements created by function f(size_t i). More...
 
 PIVector (PIVector< T > &&v)
 Move constructor.
 
PIVector< T > & operator= (const PIVector< T > &v)
 Assign operator.
 
PIVector< T > & operator= (PIVector< T > &&v)
 Assign move operator.
 
iterator begin ()
 Iterator to the first element. More...
 
iterator end ()
 Iterator to the element following the last element. More...
 
reverse_iterator rbegin ()
 Returns a reverse iterator to the first element of the reversed array. More...
 
reverse_iterator rend ()
 Returns a reverse iterator to the element following the last element of the reversed array. More...
 
size_t size () const
 Number of elements in the container. More...
 
ssize_t size_s () const
 Number of elements in the container as signed value. More...
 
size_t length () const
 Same as size(). More...
 
size_t capacity () const
 Number of elements that the container has currently allocated space for. More...
 
bool isEmpty () const
 Checks if the container has no elements. More...
 
bool isNotEmpty () const
 Checks if the container has elements. More...
 
bool any (std::function< bool(const T &e)> test) const
 Tests whether at least one element in the array passes the test implemented by the provided function test. More...
 
bool every (std::function< bool(const T &e)> test) const
 Tests whether all elements in the array passes the test implemented by the provided function test. More...
 
T & operator[] (size_t index)
 Full access to element by index. More...
 
const T & at (size_t index) const
 Read only access to element by index. More...
 
const T & atWhere (std::function< bool(const T &e)> test, ssize_t start=0, const T &def=T()) const
 Returns the first element of the array that passes the test implemented by the provided function test or def if there is no such element. More...
 
const T & lastAtWhere (std::function< bool(const T &e)> test, ssize_t start=-1, const T &def=T()) const
 Returns the last element of the array that passes the test implemented by the provided function test or def if there is no such element. More...
 
T & back ()
 Last element. More...
 
T & front ()
 Last element. More...
 
bool operator== (const PIVector< T > &v) const
 Compare operator with array v.
 
bool operator!= (const PIVector< T > &v) const
 Compare operator with array v.
 
bool contains (const T &e, ssize_t start=0) const
 Tests if element e exists in the array. More...
 
bool contains (const PIVector< T > &v, ssize_t start=0) const
 Tests if all elements of v exists in the array. More...
 
int entries (const T &e, ssize_t start=0) const
 Count elements equal e in the array. More...
 
int entries (std::function< bool(const T &e)> test, ssize_t start=0) const
 Count elements in the array passes the test implemented by the provided function test. More...
 
ssize_t indexOf (const T &e, ssize_t start=0) const
 Returns the first index at which a given element e can be found in the array, or -1 if it is not present. More...
 
ssize_t indexWhere (std::function< bool(const T &e)> test, ssize_t start=0) const
 Returns the first index passes the test implemented by the provided function test, or -1 if it is not present. can be found in the array, or -1 if it is not present. More...
 
ssize_t lastIndexOf (const T &e, ssize_t start=-1) const
 Returns the last index at which a given element e can be found in the array, or -1 if it is not present. More...
 
ssize_t lastIndexWhere (std::function< bool(const T &e)> test, ssize_t start=-1) const
 Returns the last index passes the test implemented by the provided function test, or -1 if it is not present. More...
 
T * data (size_t index=0)
 Pointer to array. More...
 
const T * data (size_t index=0) const
 Read only pointer to array. More...
 
PIVector< T > getRange (size_t index, size_t count) const
 Creates sub-array of this array. More...
 
template<typename T1 = T, typename std::enable_if<!std::is_trivially_copyable< T1 >::value, int >::type = 0>
PIVector< T > & clear ()
 Clear array, remove all elements. More...
 
PIVector< T > & fill (const T &e=T())
 Assigns element 'e' to all items in the array. More...
 
PIVector< T > & fill (std::function< T(size_t i)> f)
 Assigns result of function 'f(size_t i)' to all items in the array. More...
 
PIVector< T > & assign (const T &e=T())
 Same as fill(). More...
 
template<typename T1 = T, typename std::enable_if<!std::is_trivially_copyable< T1 >::value, int >::type = 0>
PIVector< T > & assign (size_t new_size, const T &f)
 First does resize(new_size) then fill(e). More...
 
PIVector< T > & resize (size_t new_size, const T &e=T())
 Sets size of the array, new elements are copied from e. More...
 
PIVector< T > & resize (size_t new_size, std::function< T(size_t i)> f)
 Sets size of the array, new elements created by function f(size_t i). More...
 
PIVector< T > & reserve (size_t new_size)
 Attempts to allocate memory for at least new_size elements. More...
 
PIVector< T > & insert (size_t index, const T &e=T())
 Inserts value e at index position in the array. More...
 
PIVector< T > & insert (size_t index, T &&e)
 Inserts value e at index position in the array. More...
 
PIVector< T > & insert (size_t index, const PIVector< T > &v)
 Inserts array v at index position in the array. More...
 
PIVector< T > & insert (size_t index, std::initializer_list< T > init_list)
 Inserts the given elements at index position in the array. More...
 
PIVector< T > & remove (size_t index, size_t count=1)
 Removes count elements from the middle of the array, starting at index position. More...
 
void swap (PIVector< T > &v)
 Swaps array v other with this array. More...
 
PIVector< T > & sort ()
 Sorts the elements in non-descending order. More...
 
PIVector< T > & sort (std::function< bool(const T &a, const T &b)> comp)
 Sorts the elements in non-descending order. More...
 
PIVector< T > & reverse ()
 Reverses this array. More...
 
PIVector< T > reversed () const
 Returns reversed array. More...
 
PIVector< T > & enlarge (ssize_t add_size, const T &e=T())
 Increases or decreases the size of the array by add_size elements. More...
 
PIVector< T > & removeOne (const T &e)
 Remove no more than one element equal e. More...
 
PIVector< T > & removeAll (const T &e)
 Remove all elements equal e. More...
 
PIVector< T > & removeWhere (std::function< bool(const T &e)> test)
 Remove all elements in the array passes the test implemented by the provided function test. More...
 
PIVector< T > & push_back (const T &e)
 Appends the given element e to the end of the array. More...
 
PIVector< T > & push_back (T &&e)
 Appends the given element e to the end of the array. More...
 
PIVector< T > & push_back (std::initializer_list< T > init_list)
 Appends the given elements to the end of the array. More...
 
PIVector< T > & push_back (const PIVector< T > &v)
 Appends the given array v to the end of the array. More...
 
PIVector< T > & append (const T &e)
 Appends the given element e to the end of the array. More...
 
PIVector< T > & append (T &&e)
 Appends the given element e to the end of the array. More...
 
PIVector< T > & append (std::initializer_list< T > init_list)
 Appends the given elements to the end of the array. More...
 
PIVector< T > & append (const PIVector< T > &v)
 Appends the given array v to the end of the array. More...
 
PIVector< T > & operator<< (const T &e)
 Appends the given element e to the end of the array. More...
 
PIVector< T > & operator<< (T &&e)
 Appends the given element e to the end of the array. More...
 
PIVector< T > & operator<< (const PIVector< T > &v)
 Appends the given array v to the end of the array. More...
 
PIVector< T > & push_front (const T &e)
 Appends the given element e to the begin of the array. More...
 
PIVector< T > & push_front (T &&e)
 Appends the given element e to the begin of the array. More...
 
PIVector< T > & push_front (const PIVector< T > &v)
 Appends the given array v to the begin of the array. More...
 
PIVector< T > & push_front (std::initializer_list< T > init_list)
 Appends the given elements to the begin of the array. More...
 
PIVector< T > & prepend (const T &e)
 Appends the given element e to the begin of the array. More...
 
PIVector< T > & prepend (T &&e)
 Appends the given element e to the begin of the array. More...
 
PIVector< T > & prepend (const PIVector< T > &v)
 Appends the given array v to the begin of the array. More...
 
PIVector< T > & prepend (std::initializer_list< T > init_list)
 Appends the given elements to the begin of the array. More...
 
PIVector< T > & pop_back ()
 Remove one element from the end of the array. More...
 
PIVector< T > & pop_front ()
 Remove one element from the begining of the array. More...
 
take_back ()
 Remove one element from the end of the array and return it. More...
 
take_front ()
 Remove one element from the begining of the array and return it. More...
 
template<typename ST >
PIVector< ST > toType () const
 Returns an array converted to another type. More...
 
PIVector< T > filter (std::function< bool(const T &e)> test) const
 Returns a new array with all elements that pass the test implemented by the provided function bool test(const T & e). More...
 
PIVector< T > filterIndexed (std::function< bool(size_t index, const T &e)> test) const
 Same as filter() but with index parameter in test. More...
 
PIVector< T > filterReverse (std::function< bool(const T &e)> test) const
 Same as filter() but from end to begin (from right to left). More...
 
PIVector< T > filterReverseIndexed (std::function< bool(size_t index, const T &e)> test) const
 Same as filterReverse() but with index parameter in test. More...
 
void forEach (std::function< void(const T &e)> f) const
 Execute function void f(const T & e) for every element in array. More...
 
PIVector< T > & forEach (std::function< void(T &e)> f)
 Execute function void f(T & e) for every element in array. More...
 
void forEachIndexed (std::function< void(size_t index, const T &e)> f) const
 Same as forEach() but with index parameter in f. More...
 
PIVector< T > & forEachIndexed (std::function< void(size_t index, T &e)> f)
 Same as forEachIndexed(), but allows you to change the elements of the array. More...
 
void forEachReverse (std::function< void(const T &e)> f) const
 Same as forEach() but from end to begin (from right to left). More...
 
PIVector< T > & forEachReverse (std::function< void(T &e)> f)
 Same as forEachReverse(), but allows you to change the elements of the array. More...
 
void forEachReverseIndexed (std::function< void(size_t index, const T &e)> f) const
 Same as forEachIndexed() but from end to begin (from right to left). More...
 
PIVector< T > & forEachReverseIndexed (std::function< void(size_t index, T &e)> f)
 Same as forEachReverseIndexed(), but allows you to change the elements of the array. More...
 
template<typename ST >
PIVector< ST > map (std::function< ST(const T &e)> f) const
 Сreates a new array populated with the results of calling a provided function ST f(const T & e) on every element in the calling array. More...
 
template<typename ST >
PIVector< ST > mapIndexed (std::function< ST(size_t index, const T &e)> f) const
 Same as map() but with index parameter in f. More...
 
template<typename ST >
PIVector< ST > mapReverse (std::function< ST(const T &e)> f) const
 Same as map() but from end to begin (from right to left). More...
 
template<typename ST >
PIVector< ST > mapReverseIndexed (std::function< ST(size_t index, const T &e)> f) const
 Same as mapReverse() but with index parameter in f. More...
 
template<typename ST >
ST reduce (std::function< ST(const T &e, const ST &acc)> f, const ST &initial=ST()) const
 Applies the function ST f(const T & e, const ST & acc) to each element of the array (from left to right), returns one value. More...
 
template<typename ST >
ST reduceIndexed (std::function< ST(size_t index, const T &e, const ST &acc)> f, const ST &initial=ST()) const
 Same as reduce() but with index parameter in f. More...
 
template<typename ST >
ST reduceReverse (std::function< ST(const T &e, const ST &acc)> f, const ST &initial=ST()) const
 Same as reduce() but from end to begin (from right to left). More...
 
template<typename ST >
ST reduceReverseIndexed (std::function< ST(size_t index, const T &e, const ST &acc)> f, const ST &initial=ST()) const
 Same as reduceReverse() but with index parameter in f. More...
 
PIVector< PIVector< T > > reshape (size_t rows, size_t cols, ReshapeOrder order=ReshapeByRow) const
 Changes the dimension of the array, creates a two-dimensional array from a one-dimensional array. More...
 
template<typename C , typename std::enable_if< std::is_same< T, PIVector< C > >::value, int >::type = 0>
PIVector< C > flatten (ReshapeOrder order=ReshapeByRow) const
 Changes the dimension of the array, creates a one-dimensional array from a two-dimensional array. More...
 
template<typename C , typename std::enable_if< std::is_same< T, PIVector< C > >::value, int >::type = 0>
PIVector< PIVector< C > > reshape (size_t rows, size_t cols, ReshapeOrder order=ReshapeByRow) const
 Changes the dimension of the two-dimensional array. More...
 
PIVector< PIVector< T > > split (const T &separator) const
 Divides an array into a two-dimensional array using the separator separator. More...
 
PIVector< PIVector< T > > splitBySize (size_t sz) const
 Divides an array into a two-dimensional array in chunks of no more than sz. More...
 
PIVector< T > takeRange (size_t index, size_t count)
 Cut sub-array of this array. More...
 

Detailed Description

template<typename T>
class PIVector< T >

Sequence linear container - dynamic size array of any type.

The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements. This means that a pointer to an element of a PIVector may be passed to any function that expects a pointer to an element of an array. To add elements you can use functions append() and insert(), to remove elements you can use functions remove() and removeOne(), removeWhere(). Change size by function resize() and assign(). Items in an array are numbered, starting from zero. This number is called the item's index. So the first item has index 0, the last has index size() - 1. A set of various convenient functions is also available for the array, for example: indexOf(), contains(), entries(), isEmpty(), isNotEmpty(), every(), any(), forEach(), indexWhere(), getRange(), sort(), map(), reduce(), filter(), flatten(), reshape() and others.

The storage of the PIVector is handled automatically, being expanded as needed. PIVector usually occupy more space than static arrays, because more memory is allocated to handle future growth. This way a PIVector does not need to reallocate each time an element is inserted, but only when the additional memory is exhausted. The total amount of allocated memory can be queried using capacity() function. Reallocations are usually costly operations in terms of performance. The reserve() function can be used to eliminate reallocations if the number of elements is known beforehand.

The complexity (efficiency) of common operations on PIVector is as follows:

See also
PIDeque, PIMap

Constructor & Destructor Documentation

◆ PIVector() [1/2]

template<typename T >
PIVector< T >::PIVector ( std::initializer_list< T >  init_list)
inline

Contructs array from C++11 initializer list.

PIVector <int> v{1,2,3};
piCout << v; // {1, 2, 3}
#define piCout
Macro used for conditional (piDebug) output to PICout(StdOut)
Definition: picout.h:35

◆ PIVector() [2/2]

template<typename T >
PIVector< T >::PIVector ( size_t  size,
std::function< T(size_t i)>  f 
)
inline

Contructs array with size size and elements created by function f(size_t i).

Can use Lambda expressions as constructor argument.

PIVector <int> v(5, [](size_t i){return i*2;});
piCout << v; // {0, 2, 4, 6, 8}

Member Function Documentation

◆ begin()

template<typename T >
iterator PIVector< T >::begin ( )
inline

Iterator to the first element.

If the array is empty, the returned iterator is equal to end().

Returns
STL-Style Iterators
See also
end(), rbegin(), rend()

◆ end()

template<typename T >
iterator PIVector< T >::end ( )
inline

Iterator to the element following the last element.

This element acts as a placeholder; attempting to access it results in undefined behavior.

Returns
STL-Style Iterators
See also
begin(), rbegin(), rend()

◆ rbegin()

template<typename T >
reverse_iterator PIVector< T >::rbegin ( )
inline

Returns a reverse iterator to the first element of the reversed array.

It corresponds to the last element of the non-reversed array. If the array is empty, the returned iterator is equal to rend().

Returns
STL-Style Iterators
See also
rend(), begin(), end()

◆ rend()

template<typename T >
reverse_iterator PIVector< T >::rend ( )
inline

Returns a reverse iterator to the element following the last element of the reversed array.

It corresponds to the element preceding the first element of the non-reversed array. This element acts as a placeholder, attempting to access it results in undefined behavior.

Returns
STL-Style Iterators
See also
rbegin(), begin(), end()

◆ size()

template<typename T >
size_t PIVector< T >::size ( ) const
inline

Number of elements in the container.

See also
size_s(), capacity(), isEmpty(), isNotEmpty(), resize(), reserve()

◆ size_s()

template<typename T >
ssize_t PIVector< T >::size_s ( ) const
inline

Number of elements in the container as signed value.

See also
size(), capacity(), isEmpty(), isNotEmpty(), resize(), reserve()

◆ length()

template<typename T >
size_t PIVector< T >::length ( ) const
inline

◆ capacity()

template<typename T >
size_t PIVector< T >::capacity ( ) const
inline

Number of elements that the container has currently allocated space for.

To find out the actual number of items, use the function size().

See also
reserve(), size(), size_s()

◆ isEmpty()

template<typename T >
bool PIVector< T >::isEmpty ( ) const
inline

Checks if the container has no elements.

Returns
true if the container is empty, false otherwise
See also
size(), size_s(), isEmpty(), isNotEmpty(), resize(), reserve()

◆ isNotEmpty()

template<typename T >
bool PIVector< T >::isNotEmpty ( ) const
inline

Checks if the container has elements.

Returns
true if the container is not empty, false otherwise
See also
size(), size_s(), isEmpty(), isNotEmpty(), resize(), reserve()

◆ any()

template<typename T >
bool PIVector< T >::any ( std::function< bool(const T &e)>  test) const
inline

Tests whether at least one element in the array passes the test implemented by the provided function test.

Returns
true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. Always returns false if is empty.
PIVector<int> v{1, 2, 8, 9};
piCout << v.any([](int e){return e % 2 == 0;}); // true
piCout << v.any([](int e){return e == 3;}); // false
See also
every(), contains(), entries(), forEach()

◆ every()

template<typename T >
bool PIVector< T >::every ( std::function< bool(const T &e)>  test) const
inline

Tests whether all elements in the array passes the test implemented by the provided function test.

Returns
true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. Always returns true if is empty.
PIVector<int> v{1, 2, 8, 9};
piCout << v.every([](int e){return e % 2 == 0;}); // false
piCout << v.every([](int e){return e > 0;}); // true
See also
any(), contains(), entries(), forEach()

◆ operator[]()

template<typename T >
T & PIVector< T >::operator[] ( size_t  index)
inline

Full access to element by index.

Element index starts from 0. Element index must be in range from 0 to size()-1. Otherwise will be undefined behavior.

PIVector<int> v{1, 2, 8, 9};
piCout << v[2]; // 8
v[2] = 5;
piCout << v; // {1, 2, 5, 9}
See also
at()

◆ at()

template<typename T >
const T & PIVector< T >::at ( size_t  index) const
inline

Read only access to element by index.

Element index starts from 0. Element index must be in range from 0 to size()-1. Otherwise will be undefined behavior.

◆ atWhere()

template<typename T >
const T & PIVector< T >::atWhere ( std::function< bool(const T &e)>  test,
ssize_t  start = 0,
const T &  def = T() 
) const
inline

Returns the first element of the array that passes the test implemented by the provided function test or def if there is no such element.

See also
indexWhere()

◆ lastAtWhere()

template<typename T >
const T & PIVector< T >::lastAtWhere ( std::function< bool(const T &e)>  test,
ssize_t  start = -1,
const T &  def = T() 
) const
inline

Returns the last element of the array that passes the test implemented by the provided function test or def if there is no such element.

See also
lastIndexWhere()

◆ back()

template<typename T >
T & PIVector< T >::back ( )
inline

Last element.

Returns a reference to the last item in the array. This function assumes that the array isn't empty. Otherwise will be undefined behavior.

◆ front()

template<typename T >
T & PIVector< T >::front ( )
inline

Last element.

Returns a reference to the last item in the array. This function assumes that the array isn't empty. Otherwise will be undefined behavior.

◆ contains() [1/2]

template<typename T >
bool PIVector< T >::contains ( const T &  e,
ssize_t  start = 0 
) const
inline

Tests if element e exists in the array.

Optional argument start - the position in this array at which to begin searching. If the index is greater than or equal to the array's size, false is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back. Default: 0 (entire array is searched).

PIVector<int> v{1, 2, 3, 4};
piCout << v.contains(3); // true
piCout << v.contains(5); // false
piCout << v.contains(3, 3); // false
piCout << v.contains(3, -2); // true
piCout << v.contains(3, -99); // true
Returns
true if the array contains an occurrence of element e, otherwise it returns false.
See also
every(), any(), entries(), forEach()

◆ contains() [2/2]

template<typename T >
bool PIVector< T >::contains ( const PIVector< T > &  v,
ssize_t  start = 0 
) const
inline

Tests if all elements of v exists in the array.

PIVector<int> v{1, 2, 3, 4};
piCout << v.contains({1,4}); // true
piCout << v.contains({1,5}); // false
See also
every(), any(), entries(), forEach()

◆ entries() [1/2]

template<typename T >
int PIVector< T >::entries ( const T &  e,
ssize_t  start = 0 
) const
inline

Count elements equal e in the array.

Optional argument start - the position in this array at which to begin searching. If the index is greater than or equal to the array's size, 0 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back. Default: 0 (entire array is searched).

PIVector<int> v{2, 2, 4, 2, 6};
piCout << v.entries(2); // 3
piCout << v.entries(2, 2); // 1
piCout << v.entries(2, -4); // 2
See also
every(), any(), contains(), forEach(), indexOf()

◆ entries() [2/2]

template<typename T >
int PIVector< T >::entries ( std::function< bool(const T &e)>  test,
ssize_t  start = 0 
) const
inline

Count elements in the array passes the test implemented by the provided function test.

Overloaded function. Optional argument start - the position in this array at which to begin searching. If the index is greater than or equal to the array's size, 0 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back. Default: 0 (entire array is searched).

See also
every(), any(), contains(), forEach(), indexWhere()

◆ indexOf()

template<typename T >
ssize_t PIVector< T >::indexOf ( const T &  e,
ssize_t  start = 0 
) const
inline

Returns the first index at which a given element e can be found in the array, or -1 if it is not present.

Optional argument start - the position in this array at which to begin searching. If the index is greater than or equal to the array's size, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back. Default: 0 (entire array is searched).

PIVector<int> v{2, 5, 9};
piCout << v.indexOf(2); // 0
piCout << v.indexOf(7); // -1
piCout << v.indexOf(9, 2); // 2
piCout << v.indexOf(2, -1); // -1
piCout << v.indexOf(2, -3); // 0
See also
indexWhere(), lastIndexOf(), lastIndexWhere(), contains()

◆ indexWhere()

template<typename T >
ssize_t PIVector< T >::indexWhere ( std::function< bool(const T &e)>  test,
ssize_t  start = 0 
) const
inline

Returns the first index passes the test implemented by the provided function test, or -1 if it is not present. can be found in the array, or -1 if it is not present.

Optional argument start - the position in this array at which to begin searching. If the index is greater than or equal to the array's size, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back. Default: 0 (entire array is searched).

PIVector<PIString> v{"do", "re", "mi", "re"};
piCout << v.indexWhere([](const PIString & s){return s.startsWith('r');}); // 1
piCout << v.indexWhere([](const PIString & s){return s.startsWith('r');}, 2); // 3
piCout << v.indexWhere([](const PIString & s){return s.startsWith('k');}); // -1
String class.
Definition: pistring.h:42
bool startsWith(const char c) const
Returns if string starts with "c".
Definition: pistring.h:1308
See also
indexOf(), lastIndexOf(), lastIndexWhere(), contains()

◆ lastIndexOf()

template<typename T >
ssize_t PIVector< T >::lastIndexOf ( const T &  e,
ssize_t  start = -1 
) const
inline

Returns the last index at which a given element e can be found in the array, or -1 if it is not present.

Optional argument start - the position in this array at which to start searching backwards. If the index is greater than or equal to the array's size, causes the whole array to be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Therefore, if calculated index less than 0, the array is not searched, and the method returns -1. Note: if the provided index is negative, the array is still searched from back to front. Default: -1 (entire array is searched).

PIVector<int> v{2, 5, 9, 2};
piCout << v.lastIndexOf(2); // 3
piCout << v.lastIndexOf(7); // -1
piCout << v.lastIndexOf(2, 2); // 0
piCout << v.lastIndexOf(2, -3); // 0
piCout << v.lastIndexOf(2, -300); // -1
piCout << v.lastIndexOf(2, 300); // 3
See also
indexOf(), indexWhere(), lastIndexWhere(), contains()

◆ lastIndexWhere()

template<typename T >
ssize_t PIVector< T >::lastIndexWhere ( std::function< bool(const T &e)>  test,
ssize_t  start = -1 
) const
inline

Returns the last index passes the test implemented by the provided function test, or -1 if it is not present.

Optional argument start - the position in this array at which to start searching backwards. If the index is greater than or equal to the array's size, causes the whole array to be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Therefore, if calculated index less than 0, the array is not searched, and the method returns -1. Note: if the provided index is negative, the array is still searched from back to front. Default: -1 (entire array is searched).

See also
indexOf(), lastIndexOf(), indexWhere(), contains()

◆ data() [1/2]

template<typename T >
T * PIVector< T >::data ( size_t  index = 0)
inline

Pointer to array.

Optional argument index the position in this array, where is pointer. Default: start of array.

PIVector<int> v{2, 5, 9, 2};
int a[2] = {12, 13};
memcpy(vec.data(1), a, 2 * sizeof(int));
piCout << v; // {2, 12, 13, 2}

◆ data() [2/2]

template<typename T >
const T * PIVector< T >::data ( size_t  index = 0) const
inline

Read only pointer to array.

The pointer can be used to access and modify the items in the array. The pointer remains valid as long as the array isn't reallocated. Optional argument index the position in this array, where is pointer. Default: start of array.

PIVector<int> v{1, 3, 5};
int a[3];
memcpy(a, v.data(), a.size() * sizeof(int));
piCout << a[0] << a[1] << a[2]; // 1 3 5

◆ getRange()

template<typename T >
PIVector< T > PIVector< T >::getRange ( size_t  index,
size_t  count 
) const
inline

Creates sub-array of this array.

Parameters
index- index of this array where sub-array starts
count- sub-array size

Index must be in range from 0 to size()-1. If sub-array size more than this array size, than ends early.

◆ clear()

template<typename T >
template<typename T1 = T, typename std::enable_if<!std::is_trivially_copyable< T1 >::value, int >::type = 0>
PIVector< T > & PIVector< T >::clear ( )
inline

Clear array, remove all elements.

Note
Reserved memory will not be released.
See also
resize()

◆ fill() [1/2]

template<typename T >
PIVector< T > & PIVector< T >::fill ( const T &  e = T())
inline

Assigns element 'e' to all items in the array.

PIVector<int> v{1, 3, 5};
v.fill(7);
piCout << v; // {7, 7, 7}
PIVector< T > & fill(const T &e=T())
Assigns element 'e' to all items in the array.
Definition: pivector.h:1169
See also
resize()

◆ fill() [2/2]

template<typename T >
PIVector< T > & PIVector< T >::fill ( std::function< T(size_t i)>  f)
inline

Assigns result of function 'f(size_t i)' to all items in the array.

PIVector<int> v{1, 3, 5};
v.fill([](size_t i){return i*2;});
piCout << v; // {0, 2, 4}
See also
resize()

◆ assign() [1/2]

template<typename T >
PIVector< T > & PIVector< T >::assign ( const T &  e = T())
inline

Same as fill().

See also
fill(), resize()

◆ assign() [2/2]

template<typename T >
template<typename T1 = T, typename std::enable_if<!std::is_trivially_copyable< T1 >::value, int >::type = 0>
PIVector< T > & PIVector< T >::assign ( size_t  new_size,
const T &  f 
)
inline

First does resize(new_size) then fill(e).

See also
fill(), resize()

◆ resize() [1/2]

template<typename T >
PIVector< T > & PIVector< T >::resize ( size_t  new_size,
const T &  e = T() 
)
inline

Sets size of the array, new elements are copied from e.

If new_size is greater than the current size(), elements are added to the end; the new elements are initialized from e. If new_size is less than the current size(), elements are removed from the end.

See also
size(), clear()

◆ resize() [2/2]

template<typename T >
PIVector< T > & PIVector< T >::resize ( size_t  new_size,
std::function< T(size_t i)>  f 
)
inline

Sets size of the array, new elements created by function f(size_t i).

If new_size is greater than the current size(), elements are added to the end; the new elements created by function f(size_t i). If new_size is less than the current size(), elements are removed from the end.

See also
size(), clear()

◆ reserve()

template<typename T >
PIVector< T > & PIVector< T >::reserve ( size_t  new_size)
inline

Attempts to allocate memory for at least new_size elements.

If you know in advance how large the array will be, you should call this function to prevent reallocations and memory fragmentation. If new_size is greater than the current capacity(), new storage is allocated, otherwise the function does nothing. This function does not change the size() of the array.

See also
size(), capacity(), resize()

◆ insert() [1/4]

template<typename T >
PIVector< T > & PIVector< T >::insert ( size_t  index,
const T &  e = T() 
)
inline

Inserts value e at index position in the array.

The index must be greater than 0 and less than or equal to size().

See also
append(), prepend(), remove()

◆ insert() [2/4]

template<typename T >
PIVector< T > & PIVector< T >::insert ( size_t  index,
T &&  e 
)
inline

Inserts value e at index position in the array.

The index must be greater than 0 and less than or equal to size().

See also
append(), prepend(), remove()

◆ insert() [3/4]

template<typename T >
PIVector< T > & PIVector< T >::insert ( size_t  index,
const PIVector< T > &  v 
)
inline

Inserts array v at index position in the array.

The index must be greater than or equal to 0 and less than or equal to size().

See also
append(), prepend(), remove()

◆ insert() [4/4]

template<typename T >
PIVector< T > & PIVector< T >::insert ( size_t  index,
std::initializer_list< T >  init_list 
)
inline

Inserts the given elements at index position in the array.

The index must be greater than or equal to 0 and less than or equal to size(). Inserts the given elements from C++11 initializer list.

See also
append(), prepend(), remove()

◆ remove()

template<typename T >
PIVector< T > & PIVector< T >::remove ( size_t  index,
size_t  count = 1 
)
inline

Removes count elements from the middle of the array, starting at index position.

PIVector<int> v{1, 3, 7, 5};
v.remove(1, 2);
piCout << v; // {1, 5}
PIVector< T > & remove(size_t index, size_t count=1)
Removes count elements from the middle of the array, starting at index position.
Definition: pivector.h:1391
See also
resize(), insert(), removeOne(), removeAll(), removeWhere()

◆ swap()

template<typename T >
void PIVector< T >::swap ( PIVector< T > &  v)
inline

Swaps array v other with this array.

This operation is very fast and never fails.

◆ sort() [1/2]

template<typename T >
PIVector< T > & PIVector< T >::sort ( )
inline

Sorts the elements in non-descending order.

The order of equal elements is not guaranteed to be preserved. Elements are compared using operator<. Sorting provided by std::stable_sort. Complexity O(N·log(N)).

PIVector<int> v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
v.sort();
piCout << v; // {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
PIVector< T > & sort()
Sorts the elements in non-descending order.
Definition: pivector.h:1435
See also
sort(std::function<bool(const T &a, const T &b)> comp)

◆ sort() [2/2]

template<typename T >
PIVector< T > & PIVector< T >::sort ( std::function< bool(const T &a, const T &b)>  comp)
inline

Sorts the elements in non-descending order.

The order of equal elements is not guaranteed to be preserved. Elements are compared using the given binary comparison function comp. which returns ​true if the first argument is less than (i.e. is ordered before) the second. The signature of the comparison function should be equivalent to the following:

bool comp(const T &a, const T &b);

While the signature does not need to have const &, the function must not modify the objects passed to it. The function must return false for identical elements, otherwise, it will lead to undefined program behavior and memory errors. Sorting provided by std::stable_sort. Complexity O(N·log(N)).

PIVector<int> v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
v.sort([](const int & a, const int & b){return a > b;});
piCout << v; // {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
See also
sort()

◆ reverse()

template<typename T >
PIVector< T > & PIVector< T >::reverse ( )
inline

Reverses this array.

This method reverses an array in place. The first array element becomes the last, and the last array element becomes the first. The reverse method transposes the elements of the calling array object in place, mutating the array, and returning a reference to the array.

PIVector<int> v{1, 3, 7, 5};
v.reverse();
piCout << v; // {5, 7, 3, 1}
PIVector< T > & reverse()
Reverses this array.
Definition: pivector.h:1489
See also
reversed()

◆ reversed()

template<typename T >
PIVector< T > PIVector< T >::reversed ( ) const
inline

Returns reversed array.

Returns a copy of the array with elements in reverse order. The first array element becomes the last, and the last array element becomes the first.

See also
reverse()

◆ enlarge()

template<typename T >
PIVector< T > & PIVector< T >::enlarge ( ssize_t  add_size,
const T &  e = T() 
)
inline

Increases or decreases the size of the array by add_size elements.

If add_size > 0 then elements are added to the end of the array. If add_size < 0 then elements are removed from the end of the array. If add_size < 0 and there are fewer elements in the array than specified, then the array becomes empty.

See also
resize()

◆ removeOne()

template<typename T >
PIVector< T > & PIVector< T >::removeOne ( const T &  e)
inline

Remove no more than one element equal e.

PIVector<int> v{3, 2, 5, 2, 7};
v.removeOne(2);
piCout << v; // {3, 5, 2, 7}
PIVector< T > & removeOne(const T &e)
Remove no more than one element equal e.
Definition: pivector.h:1538
See also
remove(), removeAll(), removeWhere()

◆ removeAll()

template<typename T >
PIVector< T > & PIVector< T >::removeAll ( const T &  e)
inline

Remove all elements equal e.

PIVector<int> v{3, 2, 5, 2, 7};
v.removeAll(2);
piCout << v; // {3, 5, 7}
PIVector< T > & removeAll(const T &e)
Remove all elements equal e.
Definition: pivector.h:1557
See also
remove(), removeOne(), removeWhere()

◆ removeWhere()

template<typename T >
PIVector< T > & PIVector< T >::removeWhere ( std::function< bool(const T &e)>  test)
inline

Remove all elements in the array passes the test implemented by the provided function test.

PIVector<int> v{3, 2, 5, 2, 7};
v.removeWhere([](const int & i){return i > 2;});
piCout << v; // {2, 2}
PIVector< T > & removeWhere(std::function< bool(const T &e)> test)
Remove all elements in the array passes the test implemented by the provided function test.
Definition: pivector.h:1581
See also
remove(), removeOne(), removeWhere()

◆ push_back() [1/4]

template<typename T >
PIVector< T > & PIVector< T >::push_back ( const T &  e)
inline

Appends the given element e to the end of the array.

If size() is less than capacity(), which is most often then the addition will be very fast. In any case, the addition is fast and does not depend on the size of the array. If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated. Otherwise only the past-the-end iterator is invalidated.

PIVector<int> v{1, 2, 3};
v.push_back(4);
v.push_back(5);
piCout << v; // {1, 2, 3, 4, 5}
PIVector< T > & push_back(const T &e)
Appends the given element e to the end of the array.
Definition: pivector.h:1618
See also
push_front(), append(), prepend(), insert()

◆ push_back() [2/4]

template<typename T >
PIVector< T > & PIVector< T >::push_back ( T &&  e)
inline

Appends the given element e to the end of the array.

Overloaded function.

See also
push_back()

◆ push_back() [3/4]

template<typename T >
PIVector< T > & PIVector< T >::push_back ( std::initializer_list< T >  init_list)
inline

Appends the given elements to the end of the array.

Overloaded function. Appends the given elements from C++11 initializer list.

See also
push_back()

◆ push_back() [4/4]

template<typename T >
PIVector< T > & PIVector< T >::push_back ( const PIVector< T > &  v)
inline

Appends the given array v to the end of the array.

Overloaded function.

See also
push_back()

◆ append() [1/4]

template<typename T >
PIVector< T > & PIVector< T >::append ( const T &  e)
inline

Appends the given element e to the end of the array.

If size() is less than capacity(), which is most often then the addition will be very fast. In any case, the addition is fast and does not depend on the size of the array. If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated. Otherwise only the past-the-end iterator is invalidated.

PIVector<int> v{1, 2, 3};
v.append(4);
v.append(5);
piCout << v; // {1, 2, 3, 4, 5}
PIVector< T > & append(const T &e)
Appends the given element e to the end of the array.
Definition: pivector.h:1701
See also
prepend(), push_front(), push_back(), insert()

◆ append() [2/4]

template<typename T >
PIVector< T > & PIVector< T >::append ( T &&  e)
inline

Appends the given element e to the end of the array.

Overloaded function.

See also
append()

◆ append() [3/4]

template<typename T >
PIVector< T > & PIVector< T >::append ( std::initializer_list< T >  init_list)
inline

Appends the given elements to the end of the array.

Overloaded function. Appends the given elements from C++11 initializer list.

See also
append()

◆ append() [4/4]

template<typename T >
PIVector< T > & PIVector< T >::append ( const PIVector< T > &  v)
inline

Appends the given array v to the end of the array.

Overloaded function.

PIVector<int> v{1, 2, 3};
piCout << v; // {1, 2, 3, 4, 5}
See also
append()

◆ operator<<() [1/3]

template<typename T >
PIVector< T > & PIVector< T >::operator<< ( const T &  e)
inline

Appends the given element e to the end of the array.

PIVector<int> v{1, 2, 3};
v << 4 << 5;
piCout << v; // {1, 2, 3, 4, 5}
See also
append()

◆ operator<<() [2/3]

template<typename T >
PIVector< T > & PIVector< T >::operator<< ( T &&  e)
inline

Appends the given element e to the end of the array.

PIVector<int> v{1, 2, 3};
v << 4 << 5;
piCout << v; // {1, 2, 3, 4, 5}
See also
append()

◆ operator<<() [3/3]

template<typename T >
PIVector< T > & PIVector< T >::operator<< ( const PIVector< T > &  v)
inline

Appends the given array v to the end of the array.

PIVector<int> v{1, 2, 3};
v << PIVector<int>{4, 5};
piCout << v; // {1, 2, 3, 4, 5}
See also
append()

◆ push_front() [1/4]

template<typename T >
PIVector< T > & PIVector< T >::push_front ( const T &  e)
inline

Appends the given element e to the begin of the array.

Adding an element to the beginning takes longer than to the end. This time is directly proportional to the size of the array. All iterators and references are invalidated.

PIVector<int> v{1, 2, 3};
piCout << v; // {5, 4, 1, 2, 3}
PIVector< T > & push_front(const T &e)
Appends the given element e to the begin of the array.
Definition: pivector.h:1785
See also
push_back(), append(), prepend(), insert()

◆ push_front() [2/4]

template<typename T >
PIVector< T > & PIVector< T >::push_front ( T &&  e)
inline

Appends the given element e to the begin of the array.

Overloaded function.

See also
push_front()

◆ push_front() [3/4]

template<typename T >
PIVector< T > & PIVector< T >::push_front ( const PIVector< T > &  v)
inline

Appends the given array v to the begin of the array.

Overloaded function.

PIVector<int> v{1, 2, 3};
piCout << v; // {4, 5, 1, 2, 3}
See also
push_front()

◆ push_front() [4/4]

template<typename T >
PIVector< T > & PIVector< T >::push_front ( std::initializer_list< T >  init_list)
inline

Appends the given elements to the begin of the array.

Overloaded function. Appends the given elements from C++11 initializer list.

See also
append()

◆ prepend() [1/4]

template<typename T >
PIVector< T > & PIVector< T >::prepend ( const T &  e)
inline

Appends the given element e to the begin of the array.

Adding an element to the beginning takes longer than to the end. This time is directly proportional to the size of the array. All iterators and references are invalidated.

PIVector<int> v{1, 2, 3};
v.prepend(4);
v.prepend(5);
piCout << v; // {5, 4, 1, 2, 3}
PIVector< T > & prepend(const T &e)
Appends the given element e to the begin of the array.
Definition: pivector.h:1836
See also
append(), push_back(), push_front(), insert()

◆ prepend() [2/4]

template<typename T >
PIVector< T > & PIVector< T >::prepend ( T &&  e)
inline

Appends the given element e to the begin of the array.

Overloaded function.

See also
prepend()

◆ prepend() [3/4]

template<typename T >
PIVector< T > & PIVector< T >::prepend ( const PIVector< T > &  v)
inline

Appends the given array v to the begin of the array.

Overloaded function.

PIVector<int> v{1, 2, 3};
piCout << v; // {4, 5, 1, 2, 3}
See also
prepend()

◆ prepend() [4/4]

template<typename T >
PIVector< T > & PIVector< T >::prepend ( std::initializer_list< T >  init_list)
inline

Appends the given elements to the begin of the array.

Overloaded function. Appends the given elements from C++11 initializer list.

See also
append()

◆ pop_back()

template<typename T >
PIVector< T > & PIVector< T >::pop_back ( )
inline

Remove one element from the end of the array.

Deleting an element from the end is very fast and does not depend on the size of the array.

PIVector<int> v{1, 2, 3};
piCout << v; // {1, 2}
PIVector< T > & pop_back()
Remove one element from the end of the array.
Definition: pivector.h:1884
See also
pop_front(), take_back(), take_front()

◆ pop_front()

template<typename T >
PIVector< T > & PIVector< T >::pop_front ( )
inline

Remove one element from the begining of the array.

Removing an element from the beginning takes longer than from the end. This time is directly proportional to the size of the array. All iterators and references are invalidated.

PIVector<int> v{1, 2, 3};
piCout << v; // {2, 3}
PIVector< T > & pop_front()
Remove one element from the begining of the array.
Definition: pivector.h:1906
See also
pop_back(), take_back(), take_front()

◆ take_back()

template<typename T >
T PIVector< T >::take_back ( )
inline

Remove one element from the end of the array and return it.

PIVector<int> v{1, 2, 3};
piCout << v.take_back(); // 3
piCout << v; // {1, 2}
See also
take_front(), pop_back(), pop_front()

◆ take_front()

template<typename T >
T PIVector< T >::take_front ( )
inline

Remove one element from the begining of the array and return it.

PIVector<int> v{1, 2, 3};
piCout << v.take_front(); // 1
piCout << v; // {2, 3}
See also
take_front(), pop_back(), pop_front()

◆ toType()

template<typename T >
template<typename ST >
PIVector< ST > PIVector< T >::toType ( ) const
inline

Returns an array converted to another type.

PIVector<double> v{1.1, 2.5, 3.8};
PIVector<int> v2 = v.toType<int>();
piCout << v2; // {1, 2, 3}
PIVector< ST > toType() const
Returns an array converted to another type.
Definition: pivector.h:1952
See also
map()

◆ filter()

template<typename T >
PIVector< T > PIVector< T >::filter ( std::function< bool(const T &e)>  test) const
inline

Returns a new array with all elements that pass the test implemented by the provided function bool test(const T & e).

PIVector<int> v{3, 2, 5, 2, 7};
PIVector<int> v2 = v.filter([](const int & i){return i > 2;});
piCout << v2; // {3, 5, 7}
PIVector< T > filter(std::function< bool(const T &e)> test) const
Returns a new array with all elements that pass the test implemented by the provided function bool te...
Definition: pivector.h:1972
See also
map(), any(), every()

◆ filterIndexed()

template<typename T >
PIVector< T > PIVector< T >::filterIndexed ( std::function< bool(size_t index, const T &e)>  test) const
inline

Same as filter() but with index parameter in test.

See also
filter()

◆ filterReverse()

template<typename T >
PIVector< T > PIVector< T >::filterReverse ( std::function< bool(const T &e)>  test) const
inline

Same as filter() but from end to begin (from right to left).

See also
filter()

◆ filterReverseIndexed()

template<typename T >
PIVector< T > PIVector< T >::filterReverseIndexed ( std::function< bool(size_t index, const T &e)>  test) const
inline

Same as filterReverse() but with index parameter in test.

See also
filterReverse()

◆ forEach() [1/2]

template<typename T >
void PIVector< T >::forEach ( std::function< void(const T &e)>  f) const
inline

Execute function void f(const T & e) for every element in array.

Does not allow changing array elements. To edit elements, use the function like void f(T & e)

PIVector<int> v{1, 2, 3, 4, 5};
int s = 0;
v.forEach([&s](const int & e){s += e;});
piCout << s; // 15
void forEach(std::function< void(const T &e)> f) const
Execute function void f(const T & e) for every element in array.
Definition: pivector.h:2027
See also
filter(), map(), reduce(), any(), every()

◆ forEach() [2/2]

template<typename T >
PIVector< T > & PIVector< T >::forEach ( std::function< void(T &e)>  f)
inline

Execute function void f(T & e) for every element in array.

Overloaded function. Allows you to change the elements of the array.

PIVector<int> v{1, 2, 3, 4, 5};
v.forEach([](int & e){e++;});
piCout << v; // {2, 3, 4, 5, 6}
See also
filter(), map(), reduce(), any(), every()

◆ forEachIndexed() [1/2]

template<typename T >
void PIVector< T >::forEachIndexed ( std::function< void(size_t index, const T &e)>  f) const
inline

Same as forEach() but with index parameter in f.

See also
forEach()

◆ forEachIndexed() [2/2]

template<typename T >
PIVector< T > & PIVector< T >::forEachIndexed ( std::function< void(size_t index, T &e)>  f)
inline

Same as forEachIndexed(), but allows you to change the elements of the array.

See also
forEach(), forEachIndexed()

◆ forEachReverse() [1/2]

template<typename T >
void PIVector< T >::forEachReverse ( std::function< void(const T &e)>  f) const
inline

Same as forEach() but from end to begin (from right to left).

See also
forEach()

◆ forEachReverse() [2/2]

template<typename T >
PIVector< T > & PIVector< T >::forEachReverse ( std::function< void(T &e)>  f)
inline

Same as forEachReverse(), but allows you to change the elements of the array.

See also
forEach(), forEachReverse()

◆ forEachReverseIndexed() [1/2]

template<typename T >
void PIVector< T >::forEachReverseIndexed ( std::function< void(size_t index, const T &e)>  f) const
inline

Same as forEachIndexed() but from end to begin (from right to left).

See also
forEachIndexed(), forEachReverse(), forEach()

◆ forEachReverseIndexed() [2/2]

template<typename T >
PIVector< T > & PIVector< T >::forEachReverseIndexed ( std::function< void(size_t index, T &e)>  f)
inline

Same as forEachReverseIndexed(), but allows you to change the elements of the array.

See also
forEachReverseIndexed(), forEachIndexed(), forEachReverse(), forEach()

◆ map()

template<typename T >
template<typename ST >
PIVector< ST > PIVector< T >::map ( std::function< ST(const T &e)>  f) const
inline

Сreates a new array populated with the results of calling a provided function ST f(const T & e) on every element in the calling array.

Calls a provided functionST f(const T & e) once for each element in an array, in order, and constructs a new array from the results.

PIVector<int> v{1, 2, 3};
PIVector<PIString> sl = v.map<PIString>([](const int & i){return PIString::fromNumber(i);});
piCout << sl; // {"1", "2", "3"}
static PIString fromNumber(const short value, int base=10, bool *ok=0)
Returns string contains numeric representation of "value" in base "base".
Definition: pistring.h:1776
See also
forEach(), reduce()

◆ mapIndexed()

template<typename T >
template<typename ST >
PIVector< ST > PIVector< T >::mapIndexed ( std::function< ST(size_t index, const T &e)>  f) const
inline

Same as map() but with index parameter in f.

PIVector<int> v{1, 2, 3};
PIVector<PIString> sl = v.mapIndexed<PIString>([](size_t index, const int & i){return PIString::fromNumber(index);});
piCout << sl; // {"0", "1", "2"}
See also
map()

◆ mapReverse()

template<typename T >
template<typename ST >
PIVector< ST > PIVector< T >::mapReverse ( std::function< ST(const T &e)>  f) const
inline

Same as map() but from end to begin (from right to left).

PIVector<int> v{1, 2, 3};
PIVector<PIString> sl = v.mapReverse<PIString>([](const int & i){return PIString::fromNumber(i);});
piCout << sl; // {"3", "2", "1"}
See also
map()

◆ mapReverseIndexed()

template<typename T >
template<typename ST >
PIVector< ST > PIVector< T >::mapReverseIndexed ( std::function< ST(size_t index, const T &e)>  f) const
inline

Same as mapReverse() but with index parameter in f.

PIVector<int> v{1, 2, 3};
PIVector<PIString> sl = v.mapReverseIndexed<PIString>([](size_t index, const int & i){return PIString::fromNumber(index);});
piCout << sl; // {"2", "1", "0"}
See also
mapReverse()

◆ reduce()

template<typename T >
template<typename ST >
ST PIVector< T >::reduce ( std::function< ST(const T &e, const ST &acc)>  f,
const ST &  initial = ST() 
) const
inline

Applies the function ST f(const T & e, const ST & acc) to each element of the array (from left to right), returns one value.

The reduce() method performs the f function once for each element in the array. If the initial argument is passed when calling reduce(), then when the function f is called for the first time, the value of acc will be assigned to initial. If the array is empty, the value initial will be returned.

Parameters
fis a function like ST f(const T & e, const ST & acc), executed for each element of the array. It takes two arguments:
  • e - current element of the array
  • acc - accumulator accumulating the value which this function returns after visiting the next element
initialoptional Object used as the second argument when the f function is first called.
PIVector<int> v{1, 2, 3, 4, 5};
int s = v.reduce<int>([](const int & e, const int & acc){return e + acc;});
piCout << s; // 15
ST reduce(std::function< ST(const T &e, const ST &acc)> f, const ST &initial=ST()) const
Applies the function ST f(const T & e, const ST & acc) to each element of the array (from left to rig...
Definition: pivector.h:2233
See also
forEach(), map()

◆ reduceIndexed()

template<typename T >
template<typename ST >
ST PIVector< T >::reduceIndexed ( std::function< ST(size_t index, const T &e, const ST &acc)>  f,
const ST &  initial = ST() 
) const
inline

Same as reduce() but with index parameter in f.

See also
reduce()

◆ reduceReverse()

template<typename T >
template<typename ST >
ST PIVector< T >::reduceReverse ( std::function< ST(const T &e, const ST &acc)>  f,
const ST &  initial = ST() 
) const
inline

Same as reduce() but from end to begin (from right to left).

See also
reduce()

◆ reduceReverseIndexed()

template<typename T >
template<typename ST >
ST PIVector< T >::reduceReverseIndexed ( std::function< ST(size_t index, const T &e, const ST &acc)>  f,
const ST &  initial = ST() 
) const
inline

Same as reduceReverse() but with index parameter in f.

See also
reduceReverse()

◆ reshape() [1/2]

template<typename T >
PIVector< PIVector< T > > PIVector< T >::reshape ( size_t  rows,
size_t  cols,
ReshapeOrder  order = ReshapeByRow 
) const
inline

Changes the dimension of the array, creates a two-dimensional array from a one-dimensional array.

Parameters
rowssize external array
colssize internal arrays
orderthe order of traversing the source array is set using ReshapeOrder
PIVector<int> v{1, 2, 3, 4};
piCout << m1; // {{1, 2}, {3, 4}}
piCout << m2; // {{1, 3}, {2, 4}}
PIVector< PIVector< T > > reshape(size_t rows, size_t cols, ReshapeOrder order=ReshapeByRow) const
Changes the dimension of the array, creates a two-dimensional array from a one-dimensional array.
Definition: pivector.h:2296
@ ReshapeByColumn
Definition: picontainers.h:226
See also
map(), reduce(), flatten()

◆ flatten()

template<typename T >
template<typename C , typename std::enable_if< std::is_same< T, PIVector< C > >::value, int >::type = 0>
PIVector< C > PIVector< T >::flatten ( ReshapeOrder  order = ReshapeByRow) const
inline

Changes the dimension of the array, creates a one-dimensional array from a two-dimensional array.

Makes the array flat. Еhe order of traversing the source array is set using ReshapeOrder.

PIVector<int> v{1, 2, 3, 4, 5, 6};
piCout << xv; // {{1, 2}, {3, 4}, {5, 6}}
piCout << xv.flatten<int>(); // {1, 2, 3, 4, 5, 6}
See also
map(), reduce(), reshape()

◆ reshape() [2/2]

template<typename T >
template<typename C , typename std::enable_if< std::is_same< T, PIVector< C > >::value, int >::type = 0>
PIVector< PIVector< C > > PIVector< T >::reshape ( size_t  rows,
size_t  cols,
ReshapeOrder  order = ReshapeByRow 
) const
inline

Changes the dimension of the two-dimensional array.

Parameters
rowssize external array
colssize internal arrays
orderthe order of traversing the source array is set using ReshapeOrder
PIVector<int> v{1, 2, 3, 4, 5, 6};
piCout << xv; // {{1, 2}, {3, 4}, {5, 6}}
piCout << xv.reshape<int>(2,3); // {{1, 2, 3}, {4, 5, 6}}
See also
map(), reduce(), reshape()

◆ split()

template<typename T >
PIVector< PIVector< T > > PIVector< T >::split ( const T &  separator) const
inline

Divides an array into a two-dimensional array using the separator separator.

PIVector<int> v{1, 2, 3, 99, 4, 5, 99, 6};
piCout << v.split(99); // {{1, 2, 3}, {4, 5}, {6}}
See also
splitBySize()

◆ splitBySize()

template<typename T >
PIVector< PIVector< T > > PIVector< T >::splitBySize ( size_t  sz) const
inline

Divides an array into a two-dimensional array in chunks of no more than sz.

See also
split()

◆ takeRange()

template<typename T >
PIVector< T > PIVector< T >::takeRange ( size_t  index,
size_t  count 
)
inline

Cut sub-array of this array.

Parameters
index- index of this array where sub-array starts
count- sub-array size

Index must be in range from 0 to size()-1. If sub-array size more than this array size, than ends early.