![]() |
AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
|
Implementation of an array-based vector. More...
Inheritance diagram for aud.Vector< T >:
Collaboration diagram for aud.Vector< T >:Classes | |
| class | Iterator |
| Forward iterator. More... | |
Public Member Functions | |
| Vector () | |
| create empty vector More... | |
| int | size () |
| get number of entries [O(1)] More... | |
| int | capacity () |
| Get Capacity, i.e., the maximum size the vector can grow without allocating new storage. More... | |
| boolean | empty () |
| Is vector empty? [O(1)]. More... | |
| T | front () |
| get first entry [O(1)] More... | |
| T | back () |
| get last entry [O(1)] More... | |
| T | at (int i) |
| get i-th entry [O(1)] More... | |
| void | set (T obj, int i) |
| set i-th entry [O(1)] More... | |
| void | reserve (int n) |
Ensure capacity for n entries. More... | |
| void | resize (int n) |
Set size of vector. More... | |
| void | push_back (T obj) |
insert new entry obj at the end [O(1) for sufficient capacity] More... | |
| void | insert (int i, T obj) |
| Insert new entry obj at position i [O(n)]. More... | |
| void | pop_back () |
| remove last enytry [O(1)] More... | |
| void | erase (int i) |
| remove entry at position i [O(n)] More... | |
| void | clear () |
| remove all entries (capacity remains) [O(1)] More... | |
| Iterator | iterator () |
| get forward iterator More... | |
| String | toString () |
Implementation of an array-based vector.
The implementation is inspired by std::vector of the C++ standard library. (There is a similar data structure java.util.Vector in the Java collections.)
Vectors can grow and shrink in size. Shrinking never frees memory. Growing (see reserve) over-allocates memory.
Implementation notes:
check of the implementation ensures that an index is valid or throws a java.lang.ArrayIndexOutOfBoundsException otherwise. reserve allocates at least MIN_SIZE. This prevents unnecessary reallocations for small vectors. data_[j] for j in size_,...,capacity_-1 should be null! This way the garbage collection may dispose objects early. The implementation of methods reserve,resize, pop_back, and erase takes care of this. (This convention mimicks the behavior of the C++ std::vector although constructors and destructors work differently.) at due to Java's use of references. Use set instead. Definition at line 44 of file Vector.java.
| aud.Vector< T >.Vector | ( | ) |
| T aud.Vector< T >.at | ( | int | i | ) |
get i-th entry [O(1)]
| ArrayIndexOutOfBoundsException |
Definition at line 92 of file Vector.java.
Referenced by aud.Vector< T >.back(), aud.Vector< T >.front(), aud.test.BinaryTreeTest.testBinaryTree(), aud.test.GraphTest.testDirectedGraph(), aud.test.VectorTest.testInvalid_at(), aud.test.SparseMatrixCSTest.testMatrix(), aud.test.SparseMatrixTest.testMatrix(), aud.test.SparseMatrixTest.testSymmatrixMatrix(), aud.test.GraphTest.testUndirectedGraph(), and aud.test.VectorTest.testVec().
Here is the caller graph for this function:| T aud.Vector< T >.back | ( | ) |
get last entry [O(1)]
| ArrayIndexOutOfBoundsException |
Definition at line 84 of file Vector.java.
References aud.Vector< T >.at(), and aud.Vector< T >.size().
Referenced by aud.test.VectorTest.testInvalid_back(), and aud.test.VectorTest.testVec().
Here is the call graph for this function:
Here is the caller graph for this function:| int aud.Vector< T >.capacity | ( | ) |
Get Capacity, i.e., the maximum size the vector can grow without allocating new storage.
Definition at line 67 of file Vector.java.
Referenced by aud.Vector< T >.resize(), aud.test.VectorTest.testSize(), and aud.test.VectorTest.testVec().
Here is the caller graph for this function:| void aud.Vector< T >.clear | ( | ) |
remove all entries (capacity remains) [O(1)]
Definition at line 202 of file Vector.java.
| boolean aud.Vector< T >.empty | ( | ) |
Is vector empty? [O(1)].
Definition at line 73 of file Vector.java.
References aud.Vector< T >.size().
Referenced by aud.test.VectorTest.testCtor(), and aud.test.VectorTest.testVec().
Here is the call graph for this function:
Here is the caller graph for this function:| void aud.Vector< T >.erase | ( | int | i | ) |
remove entry at position i [O(n)]
| ArrayIndexOutOfBoundsException |
Definition at line 192 of file Vector.java.
Referenced by aud.test.VectorTest.testInvalid_erase(), and aud.test.VectorTest.testVec().
Here is the caller graph for this function:| T aud.Vector< T >.front | ( | ) |
get first entry [O(1)]
| ArrayIndexOutOfBoundsException |
Definition at line 78 of file Vector.java.
References aud.Vector< T >.at().
Referenced by aud.test.VectorTest.testInvalid_front(), and aud.test.VectorTest.testVec().
Here is the call graph for this function:
Here is the caller graph for this function:| void aud.Vector< T >.insert | ( | int | i, |
| T | obj | ||
| ) |
Insert new entry obj at position i [O(n)].
Requires 0 <= i <= size.
Definition at line 166 of file Vector.java.
Referenced by aud.test.VectorTest.testVec().
Here is the caller graph for this function:| Iterator aud.Vector< T >.iterator | ( | ) |
get forward iterator
Definition at line 234 of file Vector.java.
| void aud.Vector< T >.pop_back | ( | ) |
remove last enytry [O(1)]
| ArrayIndexOutOfBoundsException |
Definition at line 182 of file Vector.java.
Referenced by aud.test.VectorTest.testInvalid_pop_back(), and aud.test.VectorTest.testVec().
Here is the caller graph for this function:| void aud.Vector< T >.push_back | ( | T | obj | ) |
insert new entry obj at the end [O(1) for sufficient capacity]
Definition at line 155 of file Vector.java.
Referenced by aud.test.BinaryTreeTest.testBinaryTree(), aud.test.VectorTest.testInvalid_at(), aud.test.VectorTest.testInvalid_erase(), aud.test.VectorTest.testSize(), aud.test.VectorTest.testVec(), and aud.test.VectorTest.testVecIterator().
Here is the caller graph for this function:| void aud.Vector< T >.reserve | ( | int | n | ) |
Ensure capacity for n entries.
This method may allocates new storage and copy data in O(n). New entries equal null.
Definition at line 120 of file Vector.java.
Referenced by aud.Vector< T >.resize().
Here is the caller graph for this function:| void aud.Vector< T >.resize | ( | int | n | ) |
Set size of vector.
Shrinks or grows the vector. On Growing, any "new" entries equal null.
Definition at line 138 of file Vector.java.
References aud.Vector< T >.capacity(), and aud.Vector< T >.reserve().
Here is the call graph for this function:| void aud.Vector< T >.set | ( | T | obj, |
| int | i | ||
| ) |
| int aud.Vector< T >.size | ( | ) |
get number of entries [O(1)]
Definition at line 60 of file Vector.java.
Referenced by aud.Vector< T >.back(), aud.Vector< T >.empty(), aud.test.BinaryTreeTest.testBinaryTree(), aud.test.GraphTest.testDirectedGraph(), aud.test.SparseMatrixTest.testMatrix(), aud.test.VectorTest.testSize(), aud.test.GraphTest.testUndirectedGraph(), aud.test.VectorTest.testVec(), and aud.Vector< T >.toString().
Here is the caller graph for this function:| String aud.Vector< T >.toString | ( | ) |
Definition at line 239 of file Vector.java.
References aud.Vector< T >.size().
Here is the call graph for this function: