PIP 5.5.3
Platform-Independent Primitives
Public Member Functions | Static Public Member Functions | Related Functions | List of all members
PIByteArray Class Reference

The PIByteArray class provides an array of bytes. More...

#include <pibytearray.h>

Inheritance diagram for PIByteArray:
Inheritance graph
[legend]

Public Member Functions

 PIByteArray ()
 Constructs an empty byte array.
 
 PIByteArray (const PIByteArray &o)
 Constructs copy of byte array "o".
 
 PIByteArray (const PIDeque< uchar > &o)
 Constructs copy of byte array "o".
 
 PIByteArray (const uint size)
 Constructs 0-filled byte array with size "size".
 
 PIByteArray (const void *data, const uint size)
 Constructs byte array from data "data" and size "size".
 
 PIByteArray (const uint size, uchar t)
 Constructs byte array with size "size" filled by "t".
 
 PIByteArray (std::initializer_list< uchar > init_list)
 Contructs array from C++11 initializer list. More...
 
void swap (PIByteArray &other)
 Swaps array v other with this array. More...
 
PIDeque< uchar >::iterator begin ()
 Iterator to the first element. More...
 
PIDeque< uchar >::iterator end ()
 Iterator to the element following the last element. More...
 
PIDeque< uchar >::reverse_iterator rbegin ()
 Returns a reverse iterator to the first element of the reversed array. More...
 
PIDeque< uchar >::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(uchar 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(uchar e)> test) const
 Tests whether all elements in the array passes the test implemented by the provided function test. More...
 
uchar & operator[] (size_t index)
 Full access to element by index. More...
 
uchar at (size_t index) const
 Read only access to element by index. More...
 
uchar & back ()
 Last element. More...
 
uchar & front ()
 Last element. More...
 
bool contains (uchar e, ssize_t start=0) const
 Tests if element e exists in the array. More...
 
int entries (uchar e, ssize_t start=0) const
 Count elements equal e in the array. More...
 
int entries (std::function< bool(uchar 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 uchar &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 uchar &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 uchar &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 uchar &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...
 
uchar * data (size_t index=0)
 Pointer to array. More...
 
const uchar * data (size_t index=0) const
 Read only pointer to array. More...
 
PIByteArrayclear ()
 Clear array, remove all elements. More...
 
PIByteArrayfill (uchar e=0)
 Assigns element 'e' to all items in the array. More...
 
PIByteArrayfill (std::function< uchar(size_t i)> f)
 Assigns result of function 'f(size_t i)' to all items in the array. More...
 
PIByteArrayassign (uchar e=0)
 Same as fill(). More...
 
PIByteArrayassign (size_t new_size, uchar e)
 First does resize(new_size) then fill(e). More...
 
PIByteArrayresize (size_t new_size, uchar e=0)
 Sets size of the array, new elements are copied from e. More...
 
PIByteArrayresize (size_t new_size, std::function< uchar(size_t i)> f)
 Sets size of the array, new elements created by function f(size_t i). More...
 
PIByteArray resized (uint new_size) const
 Return resized byte array.
 
PIByteArrayreserve (size_t new_size)
 Attempts to allocate memory for at least new_size elements. More...
 
PIByteArrayinsert (size_t index, uchar e=0)
 Inserts value e at index position in the array. More...
 
PIByteArrayinsert (size_t index, const PIByteArray &v)
 Inserts array v at index position in the array. More...
 
PIByteArrayinsert (size_t index, std::initializer_list< uchar > init_list)
 Inserts the given elements at index position in the array. More...
 
PIByteArrayremove (size_t index, size_t count=1)
 Removes count elements from the middle of the array, starting at index position. More...
 
PIByteArray getRange (size_t index, size_t count) const
 Return sub-array starts from "index" and has "count" or less bytes.
 
PIByteArray takeRange (size_t index, size_t count)
 Cut sub-array of this array. More...
 
PIByteArrayreverse ()
 Reverses this array. More...
 
PIByteArray reversed () const
 Returns reversed array. More...
 
PIByteArrayenlarge (ssize_t add_size, uchar e=0)
 Increases or decreases the size of the array by add_size elements. More...
 
PIByteArrayremoveOne (uchar e)
 Remove no more than one element equal e. More...
 
PIByteArrayremoveAll (uchar e)
 Remove all elements equal e. More...
 
PIByteArrayremoveWhere (std::function< bool(uchar e)> test)
 Remove all elements in the array passes the test implemented by the provided function test. More...
 
PIByteArraypush_back (uchar e)
 Appends the given element e to the end of the array. More...
 
PIByteArraypush_back (std::initializer_list< uchar > init_list)
 Appends the given elements to the end of the array. More...
 
PIByteArraypush_back (const PIByteArray &v)
 Appends the given array v to the end of the array. More...
 
PIByteArraypush_back (const void *data_, int size_)
 Add to the end data "data" with size "size".
 
PIByteArraypush_front (uchar e)
 Appends the given element e to the begin of the array. More...
 
PIByteArraypush_front (const PIByteArray &v)
 Appends the given array v to the begin of the array. More...
 
PIByteArraypush_front (std::initializer_list< uchar > init_list)
 Appends the given elements to the begin of the array. More...
 
PIByteArrayprepend (uchar e)
 Appends the given element e to the begin of the array. More...
 
PIByteArrayprepend (const PIByteArray &v)
 Appends the given array v to the begin of the array. More...
 
PIByteArrayprepend (std::initializer_list< uchar > init_list)
 Appends the given elements to the begin of the array. More...
 
PIByteArraypop_back ()
 Remove one element from the end of the array. More...
 
PIByteArraypop_front ()
 Remove one element from the begining of the array. More...
 
uchar take_back ()
 Remove one element from the end of the array and return it. More...
 
uchar take_front ()
 Remove one element from the begining of the array and return it. More...
 
PIByteArray filter (std::function< bool(const uchar &e)> test) const
 Returns a new array with all elements that pass the test implemented by the provided function test. More...
 
void forEach (std::function< void(const uchar &e)> f) const
 Execute function void f(const uchar & e) for every element in array. More...
 
PIByteArrayforEach (std::function< void(uchar &e)> f)
 Execute function void f(uchar & e) for every element in array. More...
 
template<typename ST >
PIDeque< ST > map (std::function< ST(const uchar &e)> f) const
 Сreates a new array populated with the results of calling a provided function ST f(const uchar & e) on every element in the calling array. More...
 
template<typename ST >
ST reduce (std::function< ST(const uchar &e, const ST &acc)> f, const ST &initial=ST()) const
 Applies the function ST f(const uchar & e, const ST & acc) to each element of the array (from left to right), returns one value. More...
 
PIByteArrayconvertToBase64 ()
 Convert data to Base 64 and return this byte array.
 
PIByteArrayconvertFromBase64 ()
 Convert data from Base 64 and return this byte array.
 
PIByteArray toBase64 () const
 Return converted to Base 64 data.
 
PIString toString (int base=16) const
 Return string representation of data, each byte in "base" base, separated by spaces.
 
PIString toHex () const
 Returns a hex encoded copy of the byte array, without spaces. The hex encoding uses the numbers 0-9 and the letters a-f.
 
PIByteArrayappend (const void *data_, int size_)
 Add to the end data "data" with size "size".
 
PIByteArrayappend (const PIByteArray &data_)
 Add to the end byte array "data".
 
PIByteArrayappend (uchar t)
 Add to the end "t".
 
PIByteArrayappend (std::initializer_list< uchar > init_list)
 Appends the given elements to the end of the array. More...
 
uchar checksumPlain8 (bool inverse=true) const
 Returns 8-bit checksum. More...
 
uint checksumPlain32 (bool inverse=true) const
 Returns 32-bit checksum. More...
 
uchar checksumCRC8 () const
 Returns 8-bit checksum CRC-8.
 
ushort checksumCRC16 () const
 Returns 16-bit checksum CRC-16.
 
uint checksumCRC32 () const
 Returns 32-bit checksum CRC-32.
 
uint hash () const
 Returns hash of content.
 
- Public Member Functions inherited from PIBinaryStream< PIByteArray >
bool binaryStreamAppend (const void *d, size_t s)
 Write data.
 
void binaryStreamAppend (T v)
 Write data.
 
bool binaryStreamTake (void *d, size_t s)
 Read data.
 
ssize_t binaryStreamSize () const
 Returns remain size. More...
 
int binaryStreamTakeInt ()
 Read int.
 
bool wasReadError () const
 Returns whether there has been an incomplete read since last resetReadError() or after the stream was created.
 
void resetReadError ()
 Reset incomplete read flag.
 

Static Public Member Functions

static PIByteArray fromBase64 (const PIByteArray &base64)
 Return converted from Base 64 data.
 

Related Functions

(Note that these are not member functions.)

PICout operator<< (PICout s, const PIBitArray &ba)
 Output operator to PICout.
 
bool operator< (const PIByteArray &v0, const PIByteArray &v1)
 Byte arrays compare operator.
 
bool operator> (const PIByteArray &v0, const PIByteArray &v1)
 Byte arrays compare operator.
 
bool operator== (const PIByteArray &v0, const PIByteArray &v1)
 Byte arrays compare operator.
 
bool operator!= (const PIByteArray &v0, const PIByteArray &v1)
 Byte arrays compare operator.
 
PIByteArray operator& (const PIByteArray &v0, const PIByteArray &v1)
 Returns bit-wise "and". If non-equal size, then returns empty PIByteArray.
 
PIByteArray operator| (const PIByteArray &v0, const PIByteArray &v1)
 Returns bit-wise "or". If non-equal size, then returns empty PIByteArray.
 
PIByteArray operator^ (const PIByteArray &v0, const PIByteArray &v1)
 Returns bit-wise "xor". If non-equal size, then returns empty PIByteArray.
 
PICout operator<< (PICout s, const PIByteArray &ba)
 Output operator to PICout.
 
template<>
uint piHash (const PIByteArray &ba)
 Returns PIByteArray::hash() of "ba".
 
template<>
void piSwap (PIByteArray &f, PIByteArray &s)
 Swap contents betwee "f" and "s".
 
template<typename T >
PIByteArray piSerialize (const T &value)
 Store "value" to bytearray and returns it.
 
template<typename T >
piDeserialize (const PIByteArray &data)
 Restore type "T" from bytearray "data" and returns it.
 

Detailed Description

The PIByteArray class provides an array of bytes.

PIByteArray used to store raw bytes. It can be constructed from any data and size. You can use PIByteArray as binary stream to serialize/deserialize any objects and data. See details Input/Output stream. This class use PIDeque<uchar> and provide some handle function to manipulate it.

Usage

PIByteArray subclass PIBinaryStream and can be used to store custom data and manipulate it. Store operators places data at the end of array, restore operators takes data from the beginning of array. In addition there are Hex and Base64 convertions.

Attention

Stream operator of PIByteArray store byte array as vector, not simply append content of byte array. This operators useful to transmit custom data as PIByteArray packed into parent byte array, e.g. to form packet from PIByteArray. To append one byte array to another use funtion append().

PIByteArray ba, sba;
uchar uc(127);
sba << uc; // byte array with one byte
ba << sba; // stream operator
piCout << ba; // result
// {1, 0, 0, 0, 127}
ba.clear();
ba.append(sba);
piCout << ba; // result
// {127}
The PIByteArray class provides an array of bytes.
Definition: pibytearray.h:42
PIByteArray & clear()
Clear array, remove all elements.
Definition: pibytearray.h:507
PIByteArray & append(const void *data_, int size_)
Add to the end data "data" with size "size".
Definition: pibytearray.h:1094
#define piCout
Macro used for conditional (piDebug) output to PICout(StdOut)
Definition: picout.h:35

Constructor & Destructor Documentation

◆ PIByteArray()

PIByteArray::PIByteArray ( std::initializer_list< uchar >  init_list)
inline

Contructs array from C++11 initializer list.

PIByteArray v{1,2,3};
piCout << v; // {1, 2, 3}

Member Function Documentation

◆ swap()

void PIByteArray::swap ( PIByteArray other)
inline

Swaps array v other with this array.

This operation is very fast and never fails.

◆ begin()

PIDeque< uchar >::iterator PIByteArray::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()

PIDeque< uchar >::iterator PIByteArray::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()

PIDeque< uchar >::reverse_iterator PIByteArray::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()

PIDeque< uchar >::reverse_iterator PIByteArray::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()

size_t PIByteArray::size ( ) const
inline

Number of elements in the container.

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

◆ size_s()

ssize_t PIByteArray::size_s ( ) const
inline

Number of elements in the container as signed value.

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

◆ length()

size_t PIByteArray::length ( ) const
inline

◆ capacity()

size_t PIByteArray::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()

bool PIByteArray::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()

bool PIByteArray::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()

bool PIByteArray::any ( std::function< bool(uchar 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.
See also
every(), contains(), entries(), forEach()

◆ every()

bool PIByteArray::every ( std::function< bool(uchar 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.
See also
any(), contains(), entries(), forEach()

◆ operator[]()

uchar & PIByteArray::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.

See also
at()

◆ at()

uchar PIByteArray::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.

◆ back()

uchar & PIByteArray::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()

uchar & PIByteArray::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()

bool PIByteArray::contains ( uchar  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).

PIByteArray 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()

◆ entries() [1/2]

int PIByteArray::entries ( uchar  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).

See also
every(), any(), contains(), indexOf()

◆ entries() [2/2]

int PIByteArray::entries ( std::function< bool(uchar 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(), indexWhere()

◆ indexOf()

ssize_t PIByteArray::indexOf ( const uchar &  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).

PIByteArray 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()

ssize_t PIByteArray::indexWhere ( std::function< bool(const uchar &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).

PIByteArray v{2, 5, 9};
piCout << v.indexWhere([](const uchar & s){return s > 3;}); // 1
piCout << v.indexWhere([](const uchar & s){return s > 3;}, 2); // 2
piCout << v.indexWhere([](const uchar & s){return s > 10;}); // -1
See also
indexOf(), lastIndexOf(), lastIndexWhere(), contains()

◆ lastIndexOf()

ssize_t PIByteArray::lastIndexOf ( const uchar &  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).

PIByteArray 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()

ssize_t PIByteArray::lastIndexWhere ( std::function< bool(const uchar &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]

uchar * PIByteArray::data ( size_t  index = 0)
inline

Pointer to array.

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

◆ data() [2/2]

const uchar * PIByteArray::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.

◆ clear()

PIByteArray & PIByteArray::clear ( )
inline

Clear array, remove all elements.

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

◆ fill() [1/2]

PIByteArray & PIByteArray::fill ( uchar  e = 0)
inline

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

See also
resize()

◆ fill() [2/2]

PIByteArray & PIByteArray::fill ( std::function< uchar(size_t i)>  f)
inline

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

See also
resize()

◆ assign() [1/2]

PIByteArray & PIByteArray::assign ( uchar  e = 0)
inline

Same as fill().

See also
fill(), resize()

◆ assign() [2/2]

PIByteArray & PIByteArray::assign ( size_t  new_size,
uchar  e 
)
inline

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

See also
fill(), resize()

◆ resize() [1/2]

PIByteArray & PIByteArray::resize ( size_t  new_size,
uchar  e = 0 
)
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]

PIByteArray & PIByteArray::resize ( size_t  new_size,
std::function< uchar(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()

PIByteArray & PIByteArray::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/3]

PIByteArray & PIByteArray::insert ( size_t  index,
uchar  e = 0 
)
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/3]

PIByteArray & PIByteArray::insert ( size_t  index,
const PIByteArray 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() [3/3]

PIByteArray & PIByteArray::insert ( size_t  index,
std::initializer_list< uchar >  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()

PIByteArray & PIByteArray::remove ( size_t  index,
size_t  count = 1 
)
inline

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

See also
resize(), insert(), removeOne(), removeAll(), removeWhere()

◆ takeRange()

PIByteArray PIByteArray::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.

◆ reverse()

PIByteArray & PIByteArray::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.

See also
reversed()

◆ reversed()

PIByteArray PIByteArray::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()

PIByteArray & PIByteArray::enlarge ( ssize_t  add_size,
uchar  e = 0 
)
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()

PIByteArray & PIByteArray::removeOne ( uchar  e)
inline

Remove no more than one element equal e.

See also
remove(), removeAll(), removeWhere()

◆ removeAll()

PIByteArray & PIByteArray::removeAll ( uchar  e)
inline

Remove all elements equal e.

See also
remove(), removeOne(), removeWhere()

◆ removeWhere()

PIByteArray & PIByteArray::removeWhere ( std::function< bool(uchar e)>  test)
inline

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

See also
remove(), removeOne(), removeWhere()

◆ push_back() [1/3]

PIByteArray & PIByteArray::push_back ( uchar  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.

See also
push_front(), append(), prepend(), insert()

◆ push_back() [2/3]

PIByteArray & PIByteArray::push_back ( std::initializer_list< uchar >  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() [3/3]

PIByteArray & PIByteArray::push_back ( const PIByteArray v)
inline

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

Overloaded function.

See also
push_back()

◆ push_front() [1/3]

PIByteArray & PIByteArray::push_front ( uchar  e)
inline

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

If there is free space at the beginning of the array, 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 there is no free space at the beginning of the array then all iterators and references (including the past-the-begin iterator) are invalidated. Otherwise only the past-the-begin iterator is invalidated.

See also
push_back(), append(), prepend(), insert()

◆ push_front() [2/3]

PIByteArray & PIByteArray::push_front ( const PIByteArray v)
inline

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

Overloaded function.

See also
push_front()

◆ push_front() [3/3]

PIByteArray & PIByteArray::push_front ( std::initializer_list< uchar >  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/3]

PIByteArray & PIByteArray::prepend ( uchar  e)
inline

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

If there is free space at the beginning of the array, 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 there is no free space at the beginning of the array then all iterators and references (including the past-the-begin iterator) are invalidated. Otherwise only the past-the-begin iterator is invalidated.

See also
push_back(), append(), prepend(), insert()

◆ prepend() [2/3]

PIByteArray & PIByteArray::prepend ( const PIByteArray v)
inline

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

Overloaded function.

See also
prepend()

◆ prepend() [3/3]

PIByteArray & PIByteArray::prepend ( std::initializer_list< uchar >  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()

PIByteArray & PIByteArray::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.

See also
pop_front(), take_back(), take_front()

◆ pop_front()

PIByteArray & PIByteArray::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.

See also
pop_back(), take_back(), take_front()

◆ take_back()

uchar PIByteArray::take_back ( )
inline

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

See also
take_front(), pop_back(), pop_front()

◆ take_front()

uchar PIByteArray::take_front ( )
inline

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

See also
take_front(), pop_back(), pop_front()

◆ filter()

PIByteArray PIByteArray::filter ( std::function< bool(const uchar &e)>  test) const
inline

Returns a new array with all elements that pass the test implemented by the provided function test.

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

◆ forEach() [1/2]

void PIByteArray::forEach ( std::function< void(const uchar &e)>  f) const
inline

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

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

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

◆ forEach() [2/2]

PIByteArray & PIByteArray::forEach ( std::function< void(uchar &e)>  f)
inline

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

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

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

◆ map()

template<typename ST >
PIDeque< ST > PIByteArray::map ( std::function< ST(const uchar &e)>  f) const
inline

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

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

PIByteArray v{0x31, 0x0A, 0xFF};
PIStringList sl = v.map<PIString>([](const uchar & i){return PIString::fromNumber(i, 16);});
piCout << sl; {"31", "A", "FF"}
String class.
Definition: pistring.h:42
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
Based on PIDeque<PIString> strings list.
Definition: pistringlist.h:36
See also
forEach(), reduce()

◆ reduce()

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

Applies the function ST f(const uchar & 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 uchar & 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.
PIByteArray v{1, 2, 3, 4, 5};
PIString s = v.reduce<PIString>([](const uchar & e, const PIString & acc){return acc + PIString::fromNumber(e);});
piCout << s; // "12345"
See also
forEach(), map()

◆ append()

PIByteArray & PIByteArray::append ( std::initializer_list< uchar >  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()

◆ checksumPlain8()

uchar PIByteArray::checksumPlain8 ( bool  inverse = true) const

Returns 8-bit checksum.

This is simple sum of all bytes, if "inverse" then add 1 and inverse. Pseudocode:

for (i)
sum += at(i);
if (inverse) return ~(sum + 1);
else return sum;
uchar at(size_t index) const
Read only access to element by index.
Definition: pibytearray.h:238

◆ checksumPlain32()

uint PIByteArray::checksumPlain32 ( bool  inverse = true) const

Returns 32-bit checksum.

This is sum of all bytes multiplied by index+1, if inverse then add 1 and inverse. Pseudocode:

for (i)
sum += at(i) * (i + 1);
if (inverse) return ~(sum + 1);
else return sum;