Complicated ones:
wx_define_array
wx_define_exported_array
wx_define_user_exported_array
wx_define_sorted_array
wx_define_sorted_exported_array
wx_define_sorted_user_exported_array
wx_declare_exported_objarray
wx_declare_user_exported_objarray
wx_define_objarray
wx_define_exported_objarray
wx_define_user_exported_objarray
Simple types:
wx_define_array_int
wx_define_exported_array_int
wx_define_user_exported_array_int
wx_define_sorted_array_int
wx_define_sorted_exported_array_int
wx_define_sorted_user_exported_array_int
Full Descriptions:
wx_define_array_int
wx_define_exported_array_int
wx_define_user_exported_array_int
wx_define_sorted_array_int
wx_define_sorted_exported_array_int
wx_define_sorted_user_exported_array_int
wxarray
this section describes the so called dynamic arrays. this is a c array-like data structure i.e. the member access time is constant (and not linear according to the number of container elements as for linked lists). however, these arrays are dynamic in the sense that they will automatically allocate more memory if there is not enough of it for adding a new element. they also perform range checking on the index values but in debug mode only, so please be sure to compile your application in debug mode to use it (see debugging overview for details). so, unlike the arrays in some other languages, attempt to access an element beyond the arrays bound doesn't automatically expand the array but provokes an assertion failure instead in debug build and does nothing (except possibly crashing your program) in the release build.
the array classes were designed to be reasonably efficient, both in terms of run-time speed and memory consumption and the executable size. the speed of array item access is, of course, constant (independent of the number of elements) making them much more efficient than linked lists (wxlist). adding items to the arrays is also implemented in more or less constant time - but the price is preallocating the memory in advance. in the memory management section you may find some useful hints about optimizing wxarray memory usage. as for executable size, all wxarray functions are inline, so they do not take any space at all.
wxwidgets has three different kinds of array. all of them derive from wxbasearray class which works with untyped data and can not be used directly. the standard macros wx_define_array(), wx_define_sorted_array() and wx_define_objarray() are used to define a new class deriving from it. the classes declared will be called in this documentation wxarray, wxsortedarray and wxobjarray but you should keep in mind that no classes with such names actually exist, each time you use one of wx_define_xxxarray macro you define a class with a new name. in fact, these names are "template" names and each usage of one of the macros mentioned above creates a template specialization for the given element type.
wxarray is suitable for storing integer types and pointers which it does not treat as objects in any way, i.e. the element pointed to by the pointer is not deleted when the element is removed from the array. it should be noted that all of wxarray's functions are inline, so it costs strictly nothing to define as many array types as you want (either in terms of the executable size or the speed) as long as at least one of them is defined and this is always the case because wxarrays are used by wxwidgets internally. this class has one serious limitation: it can only be used for storing integral types (bool, char, short, int, long and their unsigned variants) or pointers (of any kind). an attempt to use with objects of sizeof() greater than sizeof(long) will provoke a runtime assertion failure, however declaring a wxarray of floats will not (on the machines where sizeof(float) <= sizeof(long)), yet it will not work, please use wxobjarray for storing floats and doubles (nb: a more efficient wxarraydouble class is scheduled for the next release of wxwidgets).
wxsortedarray is a wxarray variant which should be used when searching in the array is a frequently used operation. it requires you to define an additional function for comparing two elements of the array element type and always stores its items in the sorted order (according to this function). thus, it is index() function execution time is o(log(n)) instead of o(n) for the usual arrays but the add() method is slower: it is o(log(n)) instead of constant time (neglecting time spent in memory allocation routine). however, in a usual situation elements are added to an array much less often than searched inside it, so wxsortedarray may lead to huge performance improvements compared to wxarray. finally, it should be noticed that, as wxarray, wxsortedarray can be only used for storing integral types or pointers.
wxobjarray class treats its elements like "objects". it may delete them when they are removed from the array (invoking the correct destructor) and copies them using the objects copy constructor. in order to implement this behaviour the definition of the wxobjarray arrays is split in two parts: first, you should declare the new wxobjarray class using wx_declare_objarray() macro and then you must include the file defining the implementation of template type: <wx/arrimpl.cpp> and define the array class with wx_define_objarray() macro from a point where the full (as opposed to 'forward') declaration of the array elements class is in scope. as it probably sounds very complicated here is an example:
#include <wx/dynarray.h>it is not as elegant as writing
// we must forward declare the array because it is used inside the class
// declaration
class mydirectory;
class myfile;
// this defines two new types: arrayofdirectories and arrayoffiles which can be
// now used as shown below
wx_declare_objarray(mydirectory, arrayofdirectories);
wx_declare_objarray(myfile, arrayoffiles);
class mydirectory
{
...
arrayofdirectories m_subdirectories; // all subdirectories
arrayoffiles m_files; // all files in this directory
};
...
// now that we have mydirectory declaration in scope we may finish the
// definition of arrayofdirectories -- note that this expands into some c++
// code and so should only be compiled once (i.e., don't put this in the
// header, but into a source file or you will get linking errors)
#include <wx/arrimpl.cpp> // this is a magic incantation which must be done!
wx_define_objarray(arrayofdirectories);
// that's all!
typedef std::vector<mydirectory> arrayofdirectories;but is not that complicated and allows the code to be compiled with any, however dumb, c++ compiler in the world.
things are much simpler for wxarray and wxsortedarray however: it is enough just to write
wx_define_array_int(int, arrayofints);i.e. there is only one define macro and no need for separate declare one. for the arrays of the primitive types, the macros wx_define_array_char/short/int/size_t/long/double should be used depending on the sizeof of the values (notice that storing values of smaller type, e.g. shorts, in an array of larger one, e.g. array_int, does not work on all architectures!).
wx_define_sorted_array_int(int, arrayofsortedints);
see also:
container classes overview, wxlist
include files
<wx/dynarray.h> for wxarray and wxsortedarray and additionally <wx/arrimpl.cpp> for wxobjarray.
function groups
macros for template array definition
constructors and destructors
memory management
number of elements and simple item access
adding items
removing items
searching and sorting
wx_define_array
wx_define_sorted_array
wx_declare_objarray
wx_define_objarray
wx_append_array
wx_prepend_array
wx_clear_array
default constructors
wxarray copy constructor and assignment operator
wxarray::~wxarray
wxarray::add
wxarray::alloc
wxarray::clear
wxarray::count
wxobjarray::detach
wxarray::empty
wxarray::getcount
wxarray::index
wxarray::insert
wxarray::isempty
wxarray::item
wxarray::last
wxarray::remove
wxarray::removeat
wxarray::setcount
wxarray::shrink
wxarray::sort
macros for template array definition
to use an array you must first define the array class. this is done with the help of the macros in this section. the class of array elements must be (at least) forward declared for wx_define_array, wx_define_sorted_array and wx_declare_objarray macros and must be fully declared before you use wx_define_objarray macro.
wx_define_array
wx_define_exported_array
wx_define_user_exported_array
wx_define_sorted_array
wx_define_sorted_exported_array
wx_define_sorted_user_exported_array
wx_declare_exported_objarray
wx_declare_user_exported_objarray
wx_define_objarray
wx_define_exported_objarray
wx_define_user_exported_objarray
to slightly complicate the matters even further, the operator -> defined by default for the array iterators by these macros only makes sense if the array element type is not a pointer itself and, although it still works, this provokes warnings from some compilers and to avoid them you should use the _ptr versions of the macros above. for example, to define an array of pointers to double you should use:
wx_define_array_ptr(double *, myarrayofdoublepointers);note that the above macros are generally only useful for wxobject types. there are separate macros for declaring an array of a simple type, such as an int.
the following simple types are supported:
int
long
size_t
double
to create an array of a simple type, simply append the type you want in caps to the array definition.
for example, for an integer array, you'd use one of the following variants:
wx_define_array_int
wx_define_exported_array_int
wx_define_user_exported_array_int
wx_define_sorted_array_int
wx_define_sorted_exported_array_int
wx_define_sorted_user_exported_array_int
constructors and destructors
array classes are 100% c++ objects and as such they have the appropriate copy constructors and assignment operators. copying wxarray just copies the elements but copying wxobjarray copies the arrays items. however, for memory-efficiency sake, neither of these classes has virtual destructor. it is not very important for wxarray which has trivial destructor anyhow, but it does mean that you should avoid deleting wxobjarray through a wxbasearray pointer (as you would never use wxbasearray anyhow it shouldn't be a problem) and that you should not derive your own classes from the array classes.
wxarray default constructor
wxarray copy constructors and assignment operators
~wxarray
memory management
automatic array memory management is quite trivial: the array starts by preallocating some minimal amount of memory (defined by wx_array_default_initial_size) and when further new items exhaust already allocated memory it reallocates it adding 50% of the currently allocated amount, but no more than some maximal number which is defined by array_maxsize_increment constant. of course, this may lead to some memory being wasted (array_maxsize_increment in the worst case, i.e. 4kb in the current implementation), so the shrink() function is provided to deallocate the extra memory. the alloc() function can also be quite useful if you know in advance how many items you are going to put in the array and will prevent the array code from reallocating the memory more times than needed.
alloc
shrink
number of elements and simple item access
functions in this section return the total number of array elements and allow to retrieve them - possibly using just the c array indexing [] operator which does exactly the same as item() method.
count
getcount
isempty
item
last
adding items
add
insert
setcount
wx_append_array
wx_prepend_array
removing items
wx_clear_array
empty
clear
removeat
remove
searching and sorting
index
sort
wx_define_array
wx_define_array(t, name)
wx_define_exported_array(t, name)
wx_define_user_exported_array(t, name, exportspec)
this macro defines a new array class named name and containing the elements of type t. the second form is used when compiling wxwidgets as a dll under windows and array needs to be visible outside the dll. the third is needed for exporting an array from a user dll.
example:
wx_define_array_int(int, myarrayint);note that wxwidgets predefines the following standard array classes: wxarrayint, wxarraylong and wxarrayptrvoid.
class myclass;
wx_define_array(myclass *, arrayofmyclass);
wx_define_sorted_array
wx_define_sorted_array(t, name)
wx_define_sorted_exported_array(t, name)
wx_define_sorted_user_exported_array(t, name)
this macro defines a new sorted array class named name and containing the elements of type t. the second form is used when compiling wxwidgets as a dll under windows and array needs to be visible outside the dll. the third is needed for exporting an array from a user dll.
example:
wx_define_sorted_array_int(int, mysortedarrayint);you will have to initialize the objects of this class by passing a comparison function to the array object constructor like this:
class myclass;
wx_define_sorted_array(myclass *, arrayofmyclass);
int compareints(int n1, int n2)
{
return n1 - n2;
}
wxsortedarrayint sorted(compareints);
int comparemyclassobjects(myclass *item1, myclass *item2)
{
// sort the items by their address...
return stricmp(item1->getaddress(), item2->getaddress());
}
wxarrayofmyclass another(comparemyclassobjects);
wx_declare_objarray
wx_declare_objarray(t, name)
wx_declare_exported_objarray(t, name)
wx_declare_user_exported_objarray(t, name)
this macro declares a new object array class named name and containing the elements of type t. the second form is used when compiling wxwidgets as a dll under windows and array needs to be visible outside the dll. the third is needed for exporting an array from a user dll.
example:
class myclass;you must use wx_define_objarray() macro to define the array class - otherwise you would get link errors.
wx_declare_objarray(myclass, wxarrayofmyclass); // note: not "myclass *"!
wx_define_objarray
wx_define_objarray(name)
wx_define_exported_objarray(name)
wx_define_user_exported_objarray(name)
this macro defines the methods of the array class name not defined by the wx_declare_objarray() macro. you must include the file <wx/arrimpl.cpp> before using this macro and you must have the full declaration of the class of array elements in scope! if you forget to do the first, the error will be caught by the compiler, but, unfortunately, many compilers will not give any warnings if you forget to do the second - but the objects of the class will not be copied correctly and their real destructor will not be called. the latter two forms are merely aliases of the first to satisfy some people's sense of symmetry when using the exported declarations.
example of usage:
// first declare the class!
class myclass
{
public:
myclass(const myclass&);
...
virtual ~myclass();
};
#include <wx/arrimpl.cpp>
wx_define_objarray(wxarrayofmyclass);
wx_append_array
void wx_append_array(wxarray& array, wxarray& other)
this macro may be used to append all elements of the other array to the array. the two arrays must be of the same type.
wx_prepend_array
void wx_prepend_array(wxarray& array, wxarray& other)
this macro may be used to prepend all elements of the other array to the array. the two arrays must be of the same type.
wx_clear_array
void wx_clear_array(wxarray& array)
this macro may be used to delete all elements of the array before emptying it. it can not be used with wxobjarrays - but they will delete their elements anyhow when you call empty().
default constructors
wxarray()
wxobjarray()
default constructor initializes an empty array object.
wxsortedarray(int (*)(t first, t second)comparefunction)
there is no default constructor for wxsortedarray classes - you must initialize it with a function to use for item comparison. it is a function which is passed two arguments of type t where t is the array element type and which should return a negative, zero or positive value according to whether the first element passed to it is less than, equal to or greater than the second one.
wxarray copy constructor and assignment operator
wxarray(const wxarray& array)
wxsortedarray(const wxsortedarray& array)
wxobjarray(const wxobjarray& array)
wxarray& operator=(const wxarray& array)
wxsortedarray& operator=(const wxsortedarray& array)
wxobjarray& operator=(const wxobjarray& array)
the copy constructors and assignment operators perform a shallow array copy (i.e. they don't copy the objects pointed to even if the source array contains the items of pointer type) for wxarray and wxsortedarray and a deep copy (i.e. the array element are copied too) for wxobjarray.
wxarray::~wxarray
~wxarray()
~wxsortedarray()
~wxobjarray()
the wxobjarray destructor deletes all the items owned by the array. this is not done by wxarray and wxsortedarray versions - you may use wx_clear_array macro for this.
wxarray::add
void add(t item, size_t copies = 1)
void add(t *item)
void add(t &item, size_t copies = 1)
appends the given number of copies of the item to the array consisting of the elements of type t.
the first version is used with wxarray and wxsortedarray. the second and the third are used with wxobjarray. there is an important difference between them: if you give a pointer to the array, it will take ownership of it, i.e. will delete it when the item is deleted from the array. if you give a reference to the array, however, the array will make a copy of the item and will not take ownership of the original item. once again, it only makes sense for wxobjarrays because the other array types never take ownership of their elements. also note that you cannot append more than one pointer as reusing it would lead to deleting it twice (or more) and hence to a crash.
you may also use wx_append_array macro to append all elements of one array to another one but it is more efficient to use copies parameter and modify the elements in place later if you plan to append a lot of items.
wxarray::alloc
void alloc(size_t count)
preallocates memory for a given number of array elements. it is worth calling when the number of items which are going to be added to the array is known in advance because it will save unneeded memory reallocation. if the array already has enough memory for the given number of items, nothing happens. in any case, the existing contents of the array is not modified.
wxarray::clear
void clear()
this function does the same as empty() and additionally frees the memory allocated to the array.
wxarray::count
size_t count() const
same as getcount(). this function is deprecated - it exists only for compatibility.
wxobjarray::detach
t * detach(size_t index)
removes the element from the array, but, unlike, remove() doesn't delete it. the function returns the pointer to the removed element.
wxarray::empty
void empty()
empties the array. for wxobjarray classes, this destroys all of the array elements. for wxarray and wxsortedarray this does nothing except marking the array of being empty - this function does not free the allocated memory, use clear() for this.
wxarray::getcount
size_t getcount() const
return the number of items in the array.
wxarray::index
int index(t& item, bool searchfromend = false) const
int index(t& item) const
the first version of the function is for wxarray and wxobjarray, the second is for wxsortedarray only.
searches the element in the array, starting from either beginning or the end depending on the value of searchfromend parameter. wxnot_found is returned if the element is not found, otherwise the index of the element is returned.
linear search is used for the wxarray and wxobjarray classes but binary search in the sorted array is used for wxsortedarray (this is why searchfromend parameter doesn't make sense for it).
nb: even for wxobjarray classes, the operator==() of the elements in the array is not used by this function. it searches exactly the given element in the array and so will only succeed if this element had been previously added to the array, but fail even if another, identical, element is in the array.
wxarray::insert
void insert(t item, size_t n, size_t copies = 1)
void insert(t *item, size_t n)
void insert(t &item, size_t n, size_t copies = 1)
insert the given number of copies of the item into the array before the existing item n - thus, insert(something, 0u) will insert an item in such way that it will become the first array element.
please see add() for explanation of the differences between the overloaded versions of this function.
wxarray::isempty
bool isempty() const
returns true if the array is empty, false otherwise.
wxarray::item
t& item(size_t index) const
returns the item at the given position in the array. if index is out of bounds, an assert failure is raised in the debug builds but nothing special is done in the release build.
the returned value is of type "reference to the array element type" for all of the array classes.
wxarray::last
t& last() const
returns the last element in the array, i.e. is the same as item(getcount() - 1). an assert failure is raised in the debug mode if the array is empty.
the returned value is of type "reference to the array element type" for all of the array classes.
wxarray::remove
remove(t item)
removes an element from the array by value: the first item of the array equal to item is removed, an assert failure will result from an attempt to remove an item which doesn't exist in the array.
when an element is removed from wxobjarray it is deleted by the array - use detach() if you don't want this to happen. on the other hand, when an object is removed from a wxarray nothing happens - you should delete it manually if required:
t *item = array[n];see also wx_clear_array macro which deletes all elements of a wxarray (supposed to contain pointers).
delete item;
array.remove(n)
wxarray::removeat
removeat(size_t index, size_t count = 1)
removes count elements starting at index from the array. when an element is removed from wxobjarray it is deleted by the array - use detach() if you don't want this to happen. on the other hand, when an object is removed from a wxarray nothing happens - you should delete it manually if required:
t *item = array[n];see also wx_clear_array macro which deletes all elements of a wxarray (supposed to contain pointers).
delete item;
array.removeat(n)
wxarray::setcount
void setcount(size_t count, t defval = t(0))
this function ensures that the number of array elements is at least count. if the array has already count or more items, nothing is done. otherwise, count - getcount() elements are added and initialized to the value defval.
see also
getcount
wxarray::shrink
void shrink()
frees all memory unused by the array. if the program knows that no new items will be added to the array it may call shrink() to reduce its memory usage. however, if a new item is added to the array, some extra memory will be allocated again.
wxarray::sort
void sort(cmpfunc<t> comparefunction)
the notation cmpfunc<t> should be read as if we had the following declaration:
template int cmpfunc(t *first, t *second);where t is the type of the array elements. i.e. it is a function returning int which is passed two arguments of type t *.
sorts the array using the specified compare function: this function should return a negative, zero or positive value according to whether the first element passed to it is less than, equal to or greater than the second one.
wxsortedarray doesn't have this function because it is always sorted.