indigo. tools. vector

Created23.03.2005
Last modified10.08.2005

Contains a generic resizable vector.  See Vector for details.

Summary
Implements a random-access-iterator for Vector.
Type of container this iterator works on.
Type of values this iterator references.
Returns the value pointed to by the iterator, therefore he has to point to a valid item in a vector.
Returns a pointer of the value pointed to by the iterator.
Sets the value pointed to by the iterator to val.
Moves the iterator one item forward in the vector, and returns the previous item.
Moves the iterator one item backward in the vector, and returns the next item.
Creates a copy of this iterator, moves it delta items forward in the vector, and returns the resulting iterator.
Creates a copy of this iterator, moves it delta items backward in the vector, and returns the resulting iterator.
Calculates the number of items this iterator is ahead of iter.
Moves the iterator delta items forward in the vector.
Moves the iterator delta items backward in the vector.
Returns a value < 0 if this iterator points to an item before iter, 0 if they are equal, and a value > 0 otherwise.
Wraps the builtin dynamic arrays.
Alias for the type of the values the vector stores.
A random-access-iterator for the vector.
Alias for Iterator.
Returns the number of items in the vector.
Returns the number of items in the vector.
Returns the number of items in the vector.
Returns true if the vector has length 0, otherwise false.
Returns true if the vector has length 0.
Returns the number of elements that can be stored in the vector without forcing a reallocation.
Returns a dynamic array that references the contents of the vector.
Returns a pointer to the first element of the vector.
Returns the first item in the vector.
Returns the last item in the vector.
Returns the first item in the vector.
Returns the last item in the vector.
Returns an iterator pointing to the first item in the vector.
Returns an iterator pointing behind the last item of the vector.
Returns a vector with a copy of this vector’s data.
Constructs a vector with the same contents as vec.
Returns the item at position i in the vector.
Returns a pointer to the item at position i in the list.
Returns the item at position i in the vector.
Returns the item at position i in the vector.
Returns true if the vector contains value, otherwise false.
Returns the number of occurences of value in the vector.
Returns the position of the first occurence of value in the vector, beginning at position from.
Returns the position of the last occurence of value in the vector, beginning before position before.
Returns a vector containing the first count items of the vector.
Returns a vector containing the items from position i to the end of the vector.
Returns a vector containing count items of the vector, beginning from position i.
Returns a vector containing the count last items of the vector.
Sets the length of the vector to size.
Preallocates enough memory for size elements.
Removes all elements from the vector.
Releases unused internal storage to the garbage collector.
This removes all elements from the vector and makes the internal storage reavailable to the garbage collector.
Inserts value at the end of the vector.
Inserts vec at the end of the vector.
Inserts vec at the end of the vector.
Appends all arguments to the vector.
Inserts value at the beginning of the vector.
Inserts the contents of vec at the beginning of the vector.
Inserts the contents of vec at the beginning of the vector.
Alias for append(T).
Alias for prepend(T).
Inserts value before the item at position i.
Inserts value before the item pointed to by i.
Inserts count times value before the item at position i.
Inserts count times value before the item pointed to by i.
Inserts vec before the item at position i.
Inserts vec before the item pointed to by i.
Inserts vec before the item at position i.
Inserts vec before the item pointed to by i.
Assigns value to the item at position i.
Removes count items, beginning at position i.
Removes the item pointed to by pos.
Removes all items from begin up to, but not including end.
Removes items.
Removes the item at position i by copying the last item of the vector over it and reducing the vectors length.
Removes the last item of the vector.
Removes the last count items of the vector.
This is the same as calling remove(size_t) for the first item.
Alias for removeLast().
Alias for removeFirst().
Removes the last item of the vector, and returns it.
Assigns value to all items in the vector.
Resizes the vector to size, and assigns value to all items in the vector.
Reverses the contents of the vector in place.
This is the same as append(T).
Creates a copy of the vector and appends vec, returns the result.
Creates a copy of vec and appends the vector, returns the result.
Creates a copy of the vector and appends vec, returns the result.
Applies dg to all elements in the vector, or until dg returns a nonzero value.
Returns true if the vector contains only one item which is equal to value.
Returns true if vec has the same length and contents as the vector, false otherwise.
Returns true if vec has the same length and contents as the vector, false otherwise.
This is the same as at().
Assigns value to the item at position i.
This is the same as data.
Returns an array with the items beginning at position from up to, but not including, the item at position to.
Returns an array with the items from begin up to, but not including, end.
Compares the contents of the vector with vec.
Compares the contents of the vector with vec.
Sorts the contents of the vector in place, and returns the vector.
Sorts the contents of the vector in place, using the comparison function lessThan.
Serializes this vector and all its items into the DataStream st.
Reads the contents of the vector previously serialized with writeTo() from the DataStream st.

VectorIterator

Implements a random-access-iterator for Vector.  Obtain an iterator from a vector with Vector.begin or Vector.end.  You can then navigate through the vector with the arithmetic operators, and compare iterators with each other.

The value property can be used to obtain/change the value the iterator points to (note that you cannot use operator * for this purpose).  The ptr property returns a pointer to the item, which is useful for structs as items.

Note

The iterator will not check for vector bounds, not even in debug mode.  You always have to ensure it points to a valid item in a vector, or to Vector.end.

Summary
Type of container this iterator works on.
Type of values this iterator references.
Returns the value pointed to by the iterator, therefore he has to point to a valid item in a vector.
Returns a pointer of the value pointed to by the iterator.
Sets the value pointed to by the iterator to val.
Moves the iterator one item forward in the vector, and returns the previous item.
Moves the iterator one item backward in the vector, and returns the next item.
Creates a copy of this iterator, moves it delta items forward in the vector, and returns the resulting iterator.
Creates a copy of this iterator, moves it delta items backward in the vector, and returns the resulting iterator.
Calculates the number of items this iterator is ahead of iter.
Moves the iterator delta items forward in the vector.
Moves the iterator delta items backward in the vector.
Returns a value < 0 if this iterator points to an item before iter, 0 if they are equal, and a value > 0 otherwise.

Types

ContainerType

Type of container this iterator works on.

ValueType

Type of values this iterator references.

Properties

value

Read

Returns the value pointed to by the iterator, therefore he has to point to a valid item in a vector.

If the vector contains structs, you cannot use this function to call a member function of the struct which needs to change the struct.  Use ptr for this purpose, it may also be faster.

Write

Sets the value pointed to by the iterator.  The iterator has to point to a valid item in a vector.

ptr

Returns a pointer of the value pointed to by the iterator.  The iterator has to point to a valid item in a vector.

See also value.

Functions

setValue()

void setValue(val)

Sets the value pointed to by the iterator to val.  The iterator has to point to a valid item in a vector.

Operators

opPostInc()

VectorIterator opPostInc()

Moves the iterator one item forward in the vector, and returns the previous item.  Use the prefix form if possible, it may be faster.

opPostDec()

VectorIterator opPostDec()

Moves the iterator one item backward in the vector, and returns the next item.  Use the prefix form if possible, it may be faster.

opAdd()

VectorIterator opAdd(ptrdiff_t delta)

Creates a copy of this iterator, moves it delta items forward in the vector, and returns the resulting iterator.

opSub(ptrdiff_t)

VectorIterator opSub(ptrdiff_t delta)

Creates a copy of this iterator, moves it delta items backward in the vector, and returns the resulting iterator.

opSub(VectorIterator)

ptrdiff_t opSub(VectorIterator iter)

Calculates the number of items this iterator is ahead of iter.  They must both belong to the same vector.

opAddAssign()

VectorIterator opAddAssign(ptrdiff_t delta)

Moves the iterator delta items forward in the vector.

opSubAssign()

VectorIterator opSubAssign(ptrdiff_t delta)

Moves the iterator delta items backward in the vector.

opCmp()

ptrdiff_t opCmp(VectorIterator iter)

Returns a value < 0 if this iterator points to an item before iter, 0 if they are equal, and a value > 0 otherwise.  The iterators must point to the same vector.

Vector

Wraps the builtin dynamic arrays.  It tries to fully behave like one, and greatly simplifies their use.  It automatically handles allocation of needed space in an efficient manner, and provides fast index-based access and a lot of convenience functions.  This comes at a cost: It is slightly larger than a dynamic array (12 bytes instead of 8 on a 32 bit machine).  Similar containers are List and LinkedList.  There are some hints on which container to choose (Container types).

Vector implements almost all of the functions that QVector (http://doc.trolltech.com/4.0/qvector.html) has, thus also providing an STL-compatible interface, as far as this is possible in D.  Finally Vector implements the standard properties and operators of D.

The Vector is “automagically” instantiated with or without support for comparisons and sorting, depending on the type you supply.

Vector!(int) sortableVector;
Vector!(structWithoutOpCmp) unsortableVector;

To access items, use operator [], ptrAt(), at() or value(size_t).  Use data to convert the vector into a dynamic array, and ptr to obtain a pointer to the first element.

To find all occurences of a particular value in a vector, use indexOf() or lastIndexOf().  Both return the index of the matching item, or length if they could not find one.  If you simply want to check whether the vector contains a value, use contains().  Use count(T) to count the number of occurences.

To change the contents of the vector, there are the functions insert(size_t, T), remove(size_t), prepend(T) and append(T).  While append(T) is fast (constant time), the others can be slow for large vectors.  Use List or LinkedList if you need fast insertions/deletions.  If you need to concatenate a number of vectors, arrays or items, use appendAll().

A vector can be resized at any time by calling resize() or changing the length property.  Other functions like append(T) change the length as needed.  Vector tries to reduce the number of reallocations by preallocating more memory than actually needed.  If you know in advance approximately how many items the vector will contain, call reserve().  Call capacity to find out how much memory the vector actually allocated.

Vector also provides iterator-based access with Iterator.  These are random-access-iterators with full operator overloads.  The only difference is that you cannot use operator * to retrieve the value the iterator points to.  Use VectorIterator.value instead:

Vector!(int) vec;
// ...
for (vec.Iterator iter = vec.begin, endIter = vec.end; iter != endIter; ++iter)
printf("%i\n", iter.value);

Note

If you store objects (i.e. instances of a class) in a vector, the vector only stores references to the objects.  They are initialized with null by default.  Functions like contains() always compare on identity (with is).

If you store pointers, object references or dynamic arrays in the container, they will not get nullified automatically when deleting items.  This may lead to space waste because the garbage collector thinks they are still being used.  Call squeeze() to get rid of them.

Summary
Alias for the type of the values the vector stores.
A random-access-iterator for the vector.
Alias for Iterator.
Returns the number of items in the vector.
Returns the number of items in the vector.
Returns the number of items in the vector.
Returns true if the vector has length 0, otherwise false.
Returns true if the vector has length 0.
Returns the number of elements that can be stored in the vector without forcing a reallocation.
Returns a dynamic array that references the contents of the vector.
Returns a pointer to the first element of the vector.
Returns the first item in the vector.
Returns the last item in the vector.
Returns the first item in the vector.
Returns the last item in the vector.
Returns an iterator pointing to the first item in the vector.
Returns an iterator pointing behind the last item of the vector.
Returns a vector with a copy of this vector’s data.
Constructs a vector with the same contents as vec.
Returns the item at position i in the vector.
Returns a pointer to the item at position i in the list.
Returns the item at position i in the vector.
Returns the item at position i in the vector.
Returns true if the vector contains value, otherwise false.
Returns the number of occurences of value in the vector.
Returns the position of the first occurence of value in the vector, beginning at position from.
Returns the position of the last occurence of value in the vector, beginning before position before.
Returns a vector containing the first count items of the vector.
Returns a vector containing the items from position i to the end of the vector.
Returns a vector containing count items of the vector, beginning from position i.
Returns a vector containing the count last items of the vector.
Sets the length of the vector to size.
Preallocates enough memory for size elements.
Removes all elements from the vector.
Releases unused internal storage to the garbage collector.
This removes all elements from the vector and makes the internal storage reavailable to the garbage collector.
Inserts value at the end of the vector.
Inserts vec at the end of the vector.
Inserts vec at the end of the vector.
Appends all arguments to the vector.
Inserts value at the beginning of the vector.
Inserts the contents of vec at the beginning of the vector.
Inserts the contents of vec at the beginning of the vector.
Alias for append(T).
Alias for prepend(T).
Inserts value before the item at position i.
Inserts value before the item pointed to by i.
Inserts count times value before the item at position i.
Inserts count times value before the item pointed to by i.
Inserts vec before the item at position i.
Inserts vec before the item pointed to by i.
Inserts vec before the item at position i.
Inserts vec before the item pointed to by i.
Assigns value to the item at position i.
Removes count items, beginning at position i.
Removes the item pointed to by pos.
Removes all items from begin up to, but not including end.
Removes items.
Removes the item at position i by copying the last item of the vector over it and reducing the vectors length.
Removes the last item of the vector.
Removes the last count items of the vector.
This is the same as calling remove(size_t) for the first item.
Alias for removeLast().
Alias for removeFirst().
Removes the last item of the vector, and returns it.
Assigns value to all items in the vector.
Resizes the vector to size, and assigns value to all items in the vector.
Reverses the contents of the vector in place.
This is the same as append(T).
Creates a copy of the vector and appends vec, returns the result.
Creates a copy of vec and appends the vector, returns the result.
Creates a copy of the vector and appends vec, returns the result.
Applies dg to all elements in the vector, or until dg returns a nonzero value.
Returns true if the vector contains only one item which is equal to value.
Returns true if vec has the same length and contents as the vector, false otherwise.
Returns true if vec has the same length and contents as the vector, false otherwise.
This is the same as at().
Assigns value to the item at position i.
This is the same as data.
Returns an array with the items beginning at position from up to, but not including, the item at position to.
Returns an array with the items from begin up to, but not including, end.
Compares the contents of the vector with vec.
Compares the contents of the vector with vec.
Sorts the contents of the vector in place, and returns the vector.
Sorts the contents of the vector in place, using the comparison function lessThan.
Serializes this vector and all its items into the DataStream st.
Reads the contents of the vector previously serialized with writeTo() from the DataStream st.

Types

ValueType

Alias for the type of the values the vector stores.

Iterator

A random-access-iterator for the vector.  See VectorIterator.

iterator

Alias for Iterator.  Provided for STL compatibility.

Properties

length

Read

Returns the number of items in the vector.

Write

Sets the length of the vector.  Synonym for resize().

count

Returns the number of items in the vector.  Synonym for length.  Note that you cannot use count as a writable property like length, because there are overloads of it that have a different meaning.

size

Returns the number of items in the vector.  Synonym for length.

isEmpty

Returns true if the vector has length 0, otherwise false.

empty

Returns true if the vector has length 0.  Alias for isEmpty.  Provided for STL compatibility.

capacity

Read

Returns the number of elements that can be stored in the vector without forcing a reallocation.

Write

Sets the capacity of the vector.  Synonym for reserve().

See also reserve().

data

Read

Returns a dynamic array that references the contents of the vector.  It points to the same data as the vector as long as the length is not changed.

Write

Sets data to point to the same items as vec.  No copy is taken, therefore vec and this vector will reference the same data.  This operation is very fast.  Note that the vector will reallocate the data as soon as you increase its length.

See also ptr.

ptr

Read

Returns a pointer to the first element of the vector.  It points to the same data as the vector as long as the length is not changed.

Note that this function does not return null if the vector is empty, even if the capacity is 0.  Use isEmpty.

See also data.

first

Read

Returns the first item in the vector.  It assumes that the vector is not empty.  Note that if you store structs in the vector, this function cannot be used to call a member function which needs to modify the struct.

Write

Replaces the first element with value.  The vector must not be empty.

See also last.

last

Read

Returns the last item in the vector.  It assumes that the vector is not empty.  Note that if you store structs in the vector, this function cannot be used to call a member function which needs to modify the struct.

Write

Replaces the last element with value.  The vector must not be empty.

See also first.

front

Returns the first item in the vector.  Alias for first.  Provided for STL compatibility.

back

Returns the last item in the vector.  Alias for last.  Provided for STL compatibility.

begin

Returns an iterator pointing to the first item in the vector.  If the vector is empty, this is the same as end.

See also end.

end

Returns an iterator pointing behind the last item of the vector.

See also begin.

dup

Returns a vector with a copy of this vector’s data.  If you want just the plain array, call data.  If you want a copy of the plain array, use data.dup, not dup.data.

Functions

fromArray()

static Vector fromArray(T[] vec)

Constructs a vector with the same contents as vec.  The contents are copied.  If you want to create a vector which directly operates on the array, use data().

at()

T at(size_t i)

Returns the item at position i in the vector. i must be a valid position in the vector.  Note that you cannot use this function to call a member function of the value that needs to change the value.  This also applies to setting the length of a dynamic array.

See also value(size_t).

ptrAt()

T* ptrAt(size_t i)

Returns a pointer to the item at position i in the list. i must be a valid position in the list.  This function must be used if you want to call member functions of the value that change it.

See also value(size_t), at().

value(size_t)

T value(size_t i)

Returns the item at position i in the vector.  If i is not a valid position, it returns a default initialized item.  Note that you cannot use this function to call a member function of the value that needs to change the value.  This also applies to setting the length of a dynamic array.

See also at().

value(size_t, T)

T value(size_t i,
defValue)

Returns the item at position i in the vector.  If i is not a valid position, it returns defValue.

See also at().

contains()

int contains(value)

Returns true if the vector contains value, otherwise false.  If the vector stores objects, these are compared on identity, not equality.

See also count(T), indexOf().

count(T)

size_t count(value)

Returns the number of occurences of value in the vector.  If the vector stores objects, these are compared on identity, not equality.

See also contains().

indexOf()

size_t indexOf(value,  
size_t from =  0)

Returns the position of the first occurence of value in the vector, beginning at position from.  If value could not be found, length is returned.  If the vector stores objects, these are compared on identity, not equality.

See also contains(), lastIndexOf().

lastIndexOf()

size_t lastIndexOf(value,
size_t before)

Returns the position of the last occurence of value in the vector, beginning before position before.  If value could not be found, length is returned.  If the vector stores objects, these are compared on identity, not equality.

vec.lastIndexOf(5, 1)             // will search only the first item.
vec.lastIndexOf(5, vec.length-1) // will search all but the last item.

See also indexOf().

left()

Vector left(size_t count)

Returns a vector containing the first count items of the vector. count must not be greater than length.  The data is copied.  If you do not want a copy to occur, use opSlice().

See also mid(size_t), right().

mid(size_t)

Vector mid(size_t i)

Returns a vector containing the items from position i to the end of the vector. i must be a valid position in the vector.  The data is copied.  If you do not want a copy to occur, use