ARSA Language  2.8
(Encryption, GPU and Web 3.0)
array Class Reference

#include <array.h>

Public Member Functions

int array ()
 allocate new memory of array More...
 
int array (int start_count)
 Constructs an array and allocates an initial chunk of memory. More...
 
int push_back (int id, const T &element)
 
int push_front (int id, const T &element)
 
void reallocate (int id, int new_size)
 Reallocates the array, make it bigger or smaller. More...
 
void set_used (int id, int usedNow)
 Sets the size of the array and allocates new elements if necessary. More...
 
const T & get (int id, int index)
 Direct const access operator. More...
 
int size (int id)
 Get number of occupied elements of the array. More...
 
int erase (int id, int index)
 
void clear (int id)
 Clears the array and deletes all allocated memory. More...
 
bool close (int id)
 free memory of array More...
 
int insert (int id, const T &element, int index)
 
int search (int id, const T &element)
 
int search_array (int id, const T &element)
 
bool sort (int id)
 
void for (int id, void *function)
 get element from array with for loop.
More...
 
bool set (int id, int index, const T &new_element)
 update element in specific slot.
More...
 
void log (int id)
 print all elements in log window More...
 
bool save (int id, const char *filename, const char *option="xml")
 save all elements to file as xml or json More...
 
const char * tostring (int id, const char *option="xml")
 save all elements and value to string buffer as xml or json More...
 

Detailed Description

Definition at line 8 of file array.h.

Constructor & Destructor Documentation

◆ array() [1/2]

int array::array ( )

allocate new memory of array

Returns
id >= 0 is successed, -1 is failed.
Must call array.close() for free memory.

◆ array() [2/2]

int array::array ( int  start_count)

Constructs an array and allocates an initial chunk of memory.

Parameters
start_countAmount of elements to pre-allocate.
Returns
id >= 0 is successed, -1 is failed.
Must call array.close() for free memory.

Member Function Documentation

◆ clear()

void array::clear ( int  id)

Clears the array and deletes all allocated memory.

Parameters
idarray id.

◆ close()

bool array::close ( int  id)

free memory of array

Parameters
idarray id.
Returns
true is successed, false is failed.

◆ erase()

int array::erase ( int  id,
int  index 
)

Erases an element from the array.
May be slow, because all elements following after the erased element have to be copied.

Parameters
idarray id.
indexIndex of element to be operate.
Returns
new size of array is successed, 0 is empty, -1 is failed.

◆ for()

void array::for ( int  id,
void *  function 
)

get element from array with for loop.

Parameters
idarray id.
functioncall the function until end loop.
Automatic built-in index loop each, can using "null" with function: array.get() array.set()
Using break() or return() to stop them.

◆ get()

const T& array::get ( int  id,
int  index 
)

Direct const access operator.

Parameters
idarray id.
indexIndex of element to be operate.
Returns
Element of array from index is successed, empty string is failed.

◆ insert()

int array::insert ( int  id,
const T &  element,
int  index 
)

Insert item into array at specified position.
Please use this only if you know what you are doing(possible performance loss).The preferred method of adding elements should be array.push_back().

Parameters
idarray id.
elementElement to be inserted.
indexWhere position to insert the new element.
Returns
new size of array is successed, 0 or -1 is failed.

◆ log()

void array::log ( int  id)

print all elements in log window

Parameters
idarray id.

◆ push_back()

int array::push_back ( int  id,
const T &  element 
)

Adds an element at back of array.
If the array is too small to add this new element it is made bigger.

Parameters
idarray id.
elementElement to add at the back of the array.
Returns
new size of array is successed, 0 or -1 is failed.

◆ push_front()

int array::push_front ( int  id,
const T &  element 
)

Adds an element at the front of the array.
If the array is to small to add this new element, the array is made bigger.Please note that this is slow, because the whole array needs to be copied for this.

Parameters
idarray id.
elementElement to add at the front of the array.
Returns
new size of array is successed, 0 or -1 is failed.

◆ reallocate()

void array::reallocate ( int  id,
int  new_size 
)

Reallocates the array, make it bigger or smaller.

Parameters
idarray id.
new_sizeNew size of array.

◆ save()

bool array::save ( int  id,
const char *  filename,
const char *  option = "xml" 
)

save all elements to file as xml or json

Parameters
idarray id.
optionfile type xml or json. default: xml
Returns
true is successed, false is failed.

◆ search()

int array::search ( int  id,
const T &  element 
)

Finds an element in linear time, which is very slow.
Only works if == operator is implemented.

Parameters
idarray id.
elementElement to search for.
Returns
index of array is successed, -1 is failed.

◆ search_array()

int array::search_array ( int  id,
const T &  element 
)

Finds an element in linear time, which is very slow.
also return new array include a set of data founded if.

Parameters
idarray id.
elementElement to search for.
Returns
id of new array is successed, -1 is failed.
Must call array.close() for free memory.

◆ set()

bool array::set ( int  id,
int  index,
const T &  new_element 
)

update element in specific slot.

Parameters
idarray id.
indexWhere position to insert the new element.
new_elementElement to update.
Returns
true is successed, false is failed.

◆ set_used()

void array::set_used ( int  id,
int  usedNow 
)

Sets the size of the array and allocates new elements if necessary.

Parameters
idarray id.
usedNowAmount of elements now used.

◆ size()

int array::size ( int  id)

Get number of occupied elements of the array.

Parameters
idarray id.
Returns
Size of elements in the array which are actually occupied. empty is 0.

◆ sort()

bool array::sort ( int  id)

Sorts the array using heapsort.
There is no additional memory waste and the algorithm performs O(n*log n) in worst case.

Parameters
idarray id.
Returns
true is successed, false is failed.

◆ tostring()

const char* array::tostring ( int  id,
const char *  option = "xml" 
)

save all elements and value to string buffer as xml or json

Parameters
idarray id.
optionfile type xml or json. default: xml
Returns
successed return array buffer in string format, otherwise is empty string

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