Detailed Description

template<typename T>
class wxArray< T >

This section describes the so called "dynamic arrays".

This is a C array-like type safe 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 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" function 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 cannot 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 the WX_DEFINE_XXXARRAY() macros, 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.

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, its 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 the 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 the 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:

But is not that complicated and allows the code to be compiled with any, however dumb, C++ compiler in the world.

Remember to include <wx/arrimpl.cpp> just before each WX_DEFINE_OBJARRAY() occurrence in your code, even if you have several in the same file.

Things are much simpler for wxArray and wxSortedArray however: it is enough just to write:

WX_DEFINE_ARRAY_INT(int, ArrayOfInts);

WX_DEFINE_SORTED_ARRAY_INT(int, ArrayOfSortedInts);

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!).

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, you'd use one of the following variants for an integer array:

Public Member Functions

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.

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 the 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.

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. More...

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. More...

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. More...

Member Function Documentation

Appends the given number of copies of the item to the array consisting of the elements of type T.

This version is used with wxArray.

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 the copies parameter and modify the elements in place later if you plan to append a lot of items.

This version is used with wxObjArray. The array will take ownership of the item, deleting it when the item is deleted from the array. Note that you cannot append more than one pointer as reusing it would lead to deleting it twice (or more) resulting in 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 the copies parameter and modify the elements in place later if you plan to append a lot of items.

Appends the given number of copies of the item to the array consisting of the elements of type T.

This version is used with wxObjArray. The array will make a copy of the item and will not take ownership of the original item.

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 the copies parameter and modify the elements in place later if you plan to append a lot of items.

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.

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.

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.

Note

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.

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];

array.Remove(item);

delete item;

See also WX_CLEAR_ARRAY() macro which deletes all elements of a wxArray (supposed to contain pointers).

Notice that for sorted arrays this method uses binary search to find the item so it doesn't necessarily remove the first matching item, but the first one found by the binary search.

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];

delete item;

array.RemoveAt(n);

See also WX_CLEAR_ARRAY() macro which deletes all elements of a wxArray (supposed to contain pointers).

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.

The notation "CMPFUNCT<T>" should be read as if we had the following declaration:

templateint 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.

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.