Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

RWTValVirtualArray<T>

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

Synopsis

#include <rw/tvrtarry.h>
RWVirtualPageHeap* heap;
RWTValVirtualArray<T> array(1000L, heap);

Description

This class represents a virtual array of elements of type T of almost any length. Individual elements are brought into physical memory as needed basis. 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 which is specified by the constructor. Any number of virtual arrays can use the same abstract page heap. You must 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 from a function is as efficient as it can be. Be aware, however, that if the copy-on-write machinery finds that a copy must ultimately be made, then for large arrays this could take quite a bit of time.

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] will return an object of type RWTVirtualElement<double>, which will contain 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:

In addition, you must never take the address of an element.

Persistence

None

Example

In this example, a virtual vector of objects of type ErsatzInt is exercised. A disk-based page heap is used for swapping space.

#include <rw/tvrtarry.h>
#include <rw/rstream.h>
#include <rw/diskpage.h>
#include <stdlib.h>
#include <stdio.h>

struct ErsatzInt {
  char  buf[8];
  ErsatzInt(int i) { sprintf(buf, "%d", i); }
  friend ostream& operator<<(ostream& str, ErsatzInt& i)
    { str << atoi(i.buf); return str; }
};

main() {
  RWDiskPageHeap heap;
  RWTValVirtualArray<ErsatzInt> vec1(10000L, &heap);

  for (long i=0; i<10000L; i++)
    vec1[i] = i;        // Some compilers may need a cast here

  cout << vec1[100] << endl;   // Prints "100"
  cout << vec1[300] << endl;   // Prints "300"

  RWTValVirtualArray<ErsatzInt> vec2 = vec1.slice(5000L, 500L);
  cout << vec2.length() << endl;     // Prints "500"
  cout << vec2[0] << endl;           // Prints "5000";

  return 0;
}

Program output:

100
300
500
5000

Public Constructors

RWTValVirtualArray<T>(long size, RWVirtualPageHeap* heap);
RWTValVirtualArray<T>(const RWTValVirtualArray<T>& v);
RWTValVirtualArray<T>(const RWTVirtualSlice<T>& sl);

Public Destructor

~RWTValVirtualArray<T>();

Public Operators

RWTValVirtualArray&
operator=(const RWTValVirtualArray<T>& v); 
void
operator=(const RWTVirtualSlice<T>& sl);
T
operator=(const T& val); 
T
operator[](long i) const; 
RWTVirtualElement<T>
operator[](long);

Public Member Functions

long
length() const; 
T
val(long i) const; 
void
set(long i, const T& v); 
RWTVirtualSlice<T>
slice(long start, long length); 
void
reshape(long newLength); 
RWVirtualPageHeap*
heap() const; 


Previous fileTop of documentContentsIndexNext file
©Copyright 1999, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.