template<class TYPE> class GArray: public GContainer<TYPE>

Dynamic array.

Inheritance:


Public Methods

[more] GArray()
Constructs an empty array.
[more] GArray(int hibound)
Constructs an array with subscripts in range 0 to hibound.
[more] GArray(int lobound, int hibound)
Constructs an array with subscripts in range lobound to hibound.
[more] GArray(const GContainer<TYPE> &gc)
Constructs an array by copying the elements of container gc.
[more] GArray(const GArray<TYPE> &gc)
Copy constructor.
[more]int lbound() const
Returns the lower bound of the valid subscript range.
[more]int hbound() const
Returns the upper bound of the valid subscript range.
[more]TYPE& operator[](int n)
Returns a reference to the array element for subscript n.
[more]const TYPE& operator[](int n) const
Returns a constant reference to the array element for subscript n.
[more]TYPE& operator[](GPosition pos)
Returns a reference to the array element for position n.
[more]const TYPE& operator[](GPosition pos) const
Returns a constant reference to the array element for position n.
[more] operator TYPE* ()
Returns a pointer for reading or writing the array elements.
[more] operator const TYPE* () const
Returns a pointer for reading (but not modifying) the array elements.
[more]void empty()
Erases the array contents.
[more]void touch(int n)
Extends the subscript range so that is contains n.
[more]void resize(int hibound)
Resets the valid subscript range to 0---hibound.
[more]void resize(int lobound, int hibound)
Resets the valid subscript range to lobound---hibound.
[more]void shift(int disp)
Shifts the valid subscript range.
[more]void del(int n, unsigned int howmany=1)
Deletes array elements.
[more]void ins(int n, const TYPE &val, unsigned int howmany=1)
Insert new elements into an array.
[more]GArray<TYPE> & operator= (const GArray &ga)
Copy operator.


Inherited from GContainer:

Public Methods

ovirtual const TYPE* get(const GPosition &pos) const
ovirtual TYPE* get(const GPosition &pos)


Inherited from GContainerBase:

Public Methods

ovirtual int size() const
ovirtual GPosition firstpos() const
ovirtual GPosition lastpos() const
ovirtual void nextpos(GPosition &pos) const
ovirtual void prevpos(GPosition &pos) const


Documentation

Dynamic array. Template class GArray<TYPE> implements an array of elements of type TYPE. Each element is identified by an integer subscript. The valid subscripts range is defined by dynamically adjustable lower- and upper-bounds. Besides accessing and setting elements, member functions are provided to insert or delete elements at specified positions.

This template class must be able to access

o GArray()
Constructs an empty array. The valid subscript range is initially empty. Member function touch and resize provide convenient ways to enlarge the subscript range.

o GArray(int hibound)
Constructs an array with subscripts in range 0 to hibound. The subscript range can be subsequently modified with member functions touch and resize.
Parameters:
hibound - upper bound of the initial subscript range.

o GArray(int lobound, int hibound)
Constructs an array with subscripts in range lobound to hibound. The subscript range can be subsequently modified with member functions touch and resize.
Parameters:
lobound - lower bound of the initial subscript range.
hibound - upper bound of the initial subscript range.

o GArray(const GContainer<TYPE> &gc)
Constructs an array by copying the elements of container gc. The valid subscript will range from zero to the number of elements in container gc minus one.
Parameters:
gc - container from which elements will be copied.

o GArray(const GArray<TYPE> &gc)
Copy constructor. The resulting array will have the same valid subscript range as array gc. All elements in gc will be copied into the constructed array.

oint lbound() const
Returns the lower bound of the valid subscript range.

oint hbound() const
Returns the upper bound of the valid subscript range.

oTYPE& operator[](int n)
Returns a reference to the array element for subscript n. This reference can be used for both reading (as "a[n]") and writing (as "a[n]=v") an array element. This operation will not extend the valid subscript range: an exception GException is thrown if argument n is not in the valid subscript range.

oconst TYPE& operator[](int n) const
Returns a constant reference to the array element for subscript n. This reference can only be used for reading (as "a[n]") an array element. This operation will not extend the valid subscript range: an exception GException is thrown if argument n is not in the valid subscript range. This variant of operator[] is necessary when dealing with a const GArray<TYPE>.

oTYPE& operator[](GPosition pos)
Returns a reference to the array element for position n. This reference can be used for both reading and writing an array element. An exception GException is thrown if argument n is not a valid GPosition for this container.

oconst TYPE& operator[](GPosition pos) const
Returns a constant reference to the array element for position n. This reference can only be used for reading an array element. An exception GException is thrown if argument n is not a valid GPosition for this container. This variant of operator[] is necessary when dealing with a const GArray<TYPE>.

o operator TYPE* ()
Returns a pointer for reading or writing the array elements. This pointer can be used to access the array elements with the same subscripts and the usual bracket syntax. This pointer remains valid as long as the valid subscript range is unchanged. If you change the subscript range, you must stop using the pointers returned by prior invocation of this conversion operator.

o operator const TYPE* () const
Returns a pointer for reading (but not modifying) the array elements. This pointer can be used to access the array elements with the same subscripts and the usual bracket syntax. This pointer remains valid as long as the valid subscript range is unchanged. If you change the subscript range, you must stop using the pointers returned by prior invocation of this conversion operator.

ovoid empty()
Erases the array contents. All elements in the array are destroyed. The valid subscript range is set to the empty range.

ovoid touch(int n)
Extends the subscript range so that is contains n. This function does nothing if n is already int the valid subscript range. If the valid range was empty, both the lower bound and the upper bound are set to n. Otherwise the valid subscript range is extended to encompass n. This function is very handy when called before setting an array element:
        int lineno=1;
        GArray<GString> a;
        while (! end_of_file()) { 
          a.touch[lineno]; 
          a[lineno++] = read_a_line(); 
        }
      

ovoid resize(int hibound)
Resets the valid subscript range to 0---hibound. This function may destroy some array elements and may construct new array elements with the null constructor. Setting hibound to -1 resets the valid subscript range to the empty range.
Parameters:
hibound - upper bound of the new subscript range.

ovoid resize(int lobound, int hibound)
Resets the valid subscript range to lobound---hibound. This function may destroy some array elements and may construct new array elements with the null constructor. Setting lobound to 0 and hibound to -1 resets the valid subscript range to the empty range.
Parameters:
lobound - lower bound of the new subscript range.
hibound - upper bound of the new subscript range.

ovoid shift(int disp)
Shifts the valid subscript range. Argument disp is added to both bounds of the valid subscript range. Array elements previously located at subscript x will now be located at subscript x+disp.

ovoid del(int n, unsigned int howmany=1)
Deletes array elements. The array elements corresponding to subscripts n...n+howmany-1 are destroyed. All array elements previously located at subscripts greater or equal to n+howmany are moved to subscripts starting with n. The new subscript upper bound is reduced in order to account for this shift.
Parameters:
n - subscript of the first element to delete.
howmany - number of elements to delete.

ovoid ins(int n, const TYPE &val, unsigned int howmany=1)
Insert new elements into an array. This function inserts howmany elements at position n into the array. The initial value val is copied into the new elements. All array elements previously located at subscripts n and higher are moved to subscripts n+howmany and higher. The upper bound of the valid subscript range is increased in order to account for this shift.
Parameters:
n - subscript of the first inserted element.
val - initial value of the new elements.
howmany - number of elements to insert.

oGArray<TYPE> & operator= (const GArray &ga)
Copy operator. All elements in array *this are destroyed. The valid subscript range is set to the valid subscript range of array ga. All elements of ga are copied into array *this.


Direct child classes:
GSArray

Alphabetic index HTML hierarchy of classes or Java