SourcePro C++ 12.0 |
SourcePro® C++ API Reference Guide |
SourcePro C++ Documentation Home |
Deprecated. A virtual array of templatized objects. More...
#include <rw/tvrtarry.h>
Public Member Functions | |
T | val (long i) const |
void | set (long i, const T &v) |
RWTValVirtualArray (long size, RWVirtualPageHeap *heap) | |
RWTValVirtualArray (const RWTValVirtualArray< T > &v) | |
RWTValVirtualArray (const RWTVirtualSlice< T > &sl) | |
~RWTValVirtualArray () | |
RWTValVirtualArray< T > & | operator= (const RWTValVirtualArray< T > &v) |
void | operator= (const RWTVirtualSlice< T > &sl) |
T | operator= (const T &val) |
long | length () const |
T | operator[] (long i) const |
RWTVirtualElement< T > | operator[] (long i) |
RWTVirtualSlice< T > | slice (long start, long length) |
void | reshape (long newLength) |
RWVirtualPageHeap * | heap () const |
This class represents a virtual array of elements of type T
of almost any length. Individual elements are brought into physical memory as needed. If an element is updated, it is automatically marked as "dirty" and will be rewritten to the swapping medium.
The swap space is provided by an abstract page heap specified by the constructor. Any number of virtual arrays can use the same abstract page heap.
The class supports reference counting using a copy-on-write technique, so, for example, returning a virtual array by value is efficient. Be aware, however, that if the copy-on-write machinery must make a copy, large arrays can be time-consuming.
For efficiency, more than one element can (and should) be put on a page. The actual number of elements is equal to the page size divided by the element size, rounded downwards. Example: for a page size of 512 bytes, and an element size of 8, then 64 elements would be put on a page.
The indexing operator operator[](long) actually returns an object of type RWTVirtualElement<T>. Consider this example:
double d = vec[j];
vec[i] = 22.0;
Assume that vec
is of type RWTValVirtualArray<double>. The expression vec
[j] returns an object of type RWTVirtualElement<double>, which contains a reference to the element being addressed. In the first line, this expression is being used to initialize a double
. The class RWTVirtualElement<T> contains a type conversion operator to convert itself to a T
, in this case a double
. The compiler uses this to initialize d
in the first line. In the second line, the expression vec
[i] is being used as an lvalue. In this case, the compiler uses the assignment operator for RWTVirtualElement<T>. This assignment operator recognizes that the expression is being used as an lvalue and automatically marks the appropriate page as "dirty," thus guaranteeing that it will be written back out to the swapping medium.
Slices, as well as individual elements, can also be addressed. These should be used wherever possible as they are much more efficient because they allow a page to be locked and used multiple times before unlocking.
The class T
must have:
T::T(const T&)
or equivalent);(T::operator=(const T&)
or equivalent).#include <rw/tvrtarry.h> RWVirtualPageHeap* heap; RWTValVirtualArray<T> array(1000L, heap);
None
#include <iostream> #include <rw/tvrtarry.h> #include <rw/diskpage.h> struct ErsatzInt { private: char buf_ [16]; public: ErsatzInt (int i) { sprintf (buf_, "%d", i); } const char* value () const { return buf_; } friend std::ostream& operator<< (std::ostream& s, const ErsatzInt& ref) { s << atoi (ref.value ()); return s; } }; int main() { RWDiskPageHeap heap; RWTValVirtualArray<ErsatzInt> vec1(10000L, &heap); for (int i = 0; i < 10000; i++) vec1 [i] = i; // Some compilers may need a cast here std::cout << vec1[100] << std::endl; // Prints "100" std::cout << vec1[300] << std::endl; // Prints "300" RWTValVirtualArray<ErsatzInt> vec2 = vec1.slice(5000L, 500L); std::cout << vec2.length() << std::endl; // Prints "500" std::cout << vec2[0] << std::endl; // Prints "5000"; return 0; }
Program output:
100 300 500 5000
RWTValVirtualArray< T >::RWTValVirtualArray | ( | long | size, | |
RWVirtualPageHeap * | heap | |||
) |
Constructs a vector of length size. The pages for the vector are allocated from the page heap given by heap which can be of any type.
RWTValVirtualArray< T >::RWTValVirtualArray | ( | const RWTValVirtualArray< T > & | v | ) |
Constructs a vector as a copy of v. The resultant vector uses the same heap and has the same length as v. The actual copy is not made until a write, minimizing the amount of heap allocations and copying that must be done.
RWTValVirtualArray< T >::RWTValVirtualArray | ( | const RWTVirtualSlice< T > & | sl | ) |
Constructs a vector from a slice of another vector. The resultant vector uses the same heap as the vector whose slice is being taken. Its length is given by the length of the slice. The copy is made immediately.
RWTValVirtualArray< T >::~RWTValVirtualArray | ( | ) |
Releases all pages allocated by the vector.
RWVirtualPageHeap* RWTValVirtualArray< T >::heap | ( | ) | const [inline] |
Returns a pointer to the heap from which the vector is getting its pages.
long RWTValVirtualArray< T >::length | ( | ) | const [inline] |
Returns the length of the vector.
T RWTValVirtualArray< T >::operator= | ( | const T & | val | ) |
Sets all elements in self equal to val, and returns a copy of val. This operator is actually quite efficient because it can work with many elements on a single page at once.
void RWTValVirtualArray< T >::operator= | ( | const RWTVirtualSlice< T > & | sl | ) |
Sets self equal to a slice of another vector. The resultant vector uses the same heap as the vector whose slice is being taken. Its length is given by the length of the slice. The copy is made immediately.
RWTValVirtualArray<T>& RWTValVirtualArray< T >::operator= | ( | const RWTValVirtualArray< T > & | v | ) |
Sets self to a copy of v. The resultant vector will use the same heap and have the same length as v. The actual copy will not be made until a write, minimizing the amount of heap allocations and copying that must be done.
RWTVirtualElement< T > RWTValVirtualArray< T >::operator[] | ( | long | i | ) | [inline] |
Returns a reference to the value at index i. The results can be used as an lvalue.
RWBoundsErr | Thrown if index i is not between 0 and one less than the number of entries in self. |
T RWTValVirtualArray< T >::operator[] | ( | long | i | ) | const [inline] |
Returns a copy of the value at index i.
RWBoundsErr | Thrown if index i is not between 0 and one less than the number of entries in self. |
void RWTValVirtualArray< T >::reshape | ( | long | newLength | ) | [inline] |
Changes the length of the vector to newLength. If the vector's length is increased, then the value of the new elements is undefined.
void RWTValVirtualArray< T >::set | ( | long | i, | |
const T & | v | |||
) | [inline] |
Sets the value at the index i to v.
RWBoundsErr | Thrown if index i is not between 0 and one less than the number of entries in self. |
RWTVirtualSlice< T > RWTValVirtualArray< T >::slice | ( | long | start, | |
long | length | |||
) | [inline] |
Returns a reference to a slice of self. The value start is the starting index of the slice, the value length its extent. The results can be used as an lvalue.
T RWTValVirtualArray< T >::val | ( | long | i | ) | const [inline] |
Returns a copy of the value at index i.
RWBoundsErr | Thrown if index i is not between 0 and one less than the number of entries in self. |
© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.