jvar
JS inspired Variant and Json
jvar::BArray Class Reference

Detailed Description

BArray is a base class and maintains a contiguous chunk of memory as an dynamic array. It can also work with fixed memory. It allows finding items and inserting them in order using binary search. Unless there is a special need, use ObjArray class instead of using this directly.

Examples:
misc.cpp.

#include <arr.h>

+ Inheritance diagram for jvar::BArray:
+ Collaboration diagram for jvar::BArray:

Public Types

enum  { FLAG_FIXEDBUF = 0x1 }
typedef int(* Compare )(const void *, const void *)

Public Member Functions

 BArray (size_t elemsize, Compare comp)
 BArray (const BArray &src)
 BArray (BArray &src)
BArrayoperator= (const BArray &src)
BArrayoperator= (const BArray *src)
void useFixedMem (void *memptr, int *countptr, int maxlen)
void clear ()
void * insert (int pos, const void *elem)
void * append (const void *elem)
void * add (const void *elem)
void * addOrModify (const void *elem, bool modifyfound=true)
bool remove (int pos)
bool remove (const void *elem)
void * find (const void *elem)
void * get (int pos)
int length ()
bool full ()
void sort (Compare comp=NULL)
bool findPos (const void *findelem, int &pos)
void reserve (int elemcount)

Public Attributes

uint mFlags

Protected Member Functions

void copyFrom (BArray &src, bool alloconly, bool move)
bool binSearch (const void *findelem, int &pos)
void ensureAlloc (int desiredlen)

Member Enumeration Documentation

anonymous enum
Enumerator:
FLAG_FIXEDBUF 

Array is currently using fixed buffer provided via useFixedMemory


Constructor & Destructor Documentation

jvar::BArray::BArray ( size_t  elemsize,
Compare  comp 
) [inline]

Constructor

Parameters:
elemsizeSize of the array element
compCompare function
jvar::BArray::BArray ( const BArray src) [inline]

Constructor

Parameters:
srcAnother array
jvar::BArray::BArray ( BArray src) [inline]

Constructor

Parameters:
srcAnother array

Member Function Documentation

void* jvar::BArray::add ( const void *  elem) [inline]

Adds an element in order using the compare function

Parameters:
elemPointer to the element which will be copied
Returns:
Pointer to the element in array

References addOrModify().

void * jvar::BArray::addOrModify ( const void *  elem,
bool  modifyfound = true 
)

Adds an element or modifies it if it already exists

Parameters:
elemPointer to element which will be copied
modifyfoundShould it be modified (true=allow, false=don't allow)
Returns:
Pointer to the element in array

References insert().

Referenced by add().

void* jvar::BArray::append ( const void *  elem) [inline]

Appends or adds an element at the end of the array

Parameters:
elemPointer to the element which will be copied
Returns:
Pointer to the element in array

References insert(), and length().

Referenced by jvar::ObjArray< int >::append().

Deletes all elements

References FLAG_FIXEDBUF, jvar::Buffer::free(), and mFlags.

Referenced by jvar::PropArray< Variant >::clear().

void * jvar::BArray::find ( const void *  elem)

Finds an element in array that matches the provided element

Parameters:
elemPointer to an element to search
Returns:
Pointer to the element in array
bool jvar::BArray::findPos ( const void *  findelem,
int &  pos 
) [inline]

Finds the position of an element

Parameters:
findelemPointer to an element to search
posReturns the position of the found element
Returns:
True if found, false otherwise
bool jvar::BArray::full ( ) [inline]

Determines if the array is at capacity.

Returns:
True if full, false otherwise

Referenced by insert().

void* jvar::BArray::get ( int  pos) [inline]

Retrieves an element from the array. Note: This function does not do any bounds checking.

Parameters:
posPosition to retrieve the element

Reimplemented in jvar::ObjArray< T >, jvar::ObjArray< DataElem >, jvar::ObjArray< Variant >, jvar::ObjArray< std::string >, and jvar::ObjArray< int >.

void * jvar::BArray::insert ( int  pos,
const void *  elem 
)

Insert an element into the array

Parameters:
posPosition to insert at
elemPointer to element which will be copied

References FLAG_FIXEDBUF, full(), length(), and mFlags.

Referenced by addOrModify(), jvar::ObjArray< int >::addOrModify(), append(), and jvar::ObjArray< int >::insert().

bool jvar::BArray::remove ( int  pos)

Removes an element from the array

Parameters:
posPosition of the item to remove
Returns:
Success

Reimplemented in jvar::ObjArray< T >, jvar::ObjArray< DataElem >, jvar::ObjArray< Variant >, jvar::ObjArray< std::string >, and jvar::ObjArray< int >.

References FLAG_FIXEDBUF, length(), and mFlags.

bool jvar::BArray::remove ( const void *  elem)

Removes an element from array that matches the provided element

Parameters:
elemPointer to an element to search
Returns:
Success
void jvar::BArray::reserve ( int  elemcount) [inline]

Ensures that the array has at least the specified allocation

Parameters:
elemcountNumber of element to reserve
void jvar::BArray::sort ( Compare  comp = NULL) [inline]

Sorts the array

Parameters:
compCompare function (if NULL, the one from constructor is used)
void jvar::BArray::useFixedMem ( void *  memptr,
int *  countptr,
int  maxlen 
)

Enters fixed memory mode using the provided memory

Parameters:
memptrPointer to allocated fixed memory
countptrPointer to a variable where current count is held
maxlenMaximum number of elements (size specified in constructor)

References FLAG_FIXEDBUF, jvar::Buffer::free(), and mFlags.


Member Data Documentation

Flags for this array

Referenced by clear(), insert(), remove(), and useFixedMem().


The documentation for this class was generated from the following files:
 All Classes Functions Variables Enumerator