template<class T>
class RWTValVirtualArray< T >
- Deprecated:
- As of SourcePro 11.1.
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.
- Note
- Take care that the destructor of the abstract page heap is not called before all virtual arrays built from it have been destroyed.
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. 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 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. 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:
- well-defined copy semantics (
T::T(const T&)
or equivalent);
- well-defined assignment semantics
(T::operator=(const T&)
or equivalent).
- Synopsis
- Persistence
- None
- Example
#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()
{
for (int i = 0; i < 10000; i++)
vec1 [i] = i;
std::cout << vec1[100] << std::endl;
std::cout << vec1[300] << std::endl;
std::cout << vec2.
length() << std::endl;
std::cout << vec2[0] << std::endl;
return 0;
}
Program output: