SourcePro® API Reference Guide

 
List of all members | Public Member Functions
RWTValVirtualArray< T > Class Template Reference

Deprecated. A virtual array of templatized objects. More...

#include <rw/tvrtarry.h>

Public Member Functions

 RWTValVirtualArray (long size, RWVirtualPageHeap *heap)
 
 RWTValVirtualArray (const RWTValVirtualArray< T > &v)
 
 RWTValVirtualArray (const RWTVirtualSlice< T > &sl)
 
 ~RWTValVirtualArray ()
 
RWVirtualPageHeapheap () const
 
long length () const
 
RWTValVirtualArray< T > & operator= (const RWTValVirtualArray< T > &v)
 
void operator= (const RWTVirtualSlice< T > &sl)
 
operator= (const T &val)
 
operator[] (long i) const
 
RWTVirtualElement< T > operator[] (long i)
 
void reshape (long newLength)
 
void set (long i, const T &v)
 
RWTVirtualSlice< T > slice (long start, long length)
 
val (long i) const
 

Detailed Description

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:

Synopsis
#include <rw/tvrtarry.h>
RWTValVirtualArray<T> array(1000L, heap);
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()
{
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

Constructor & Destructor Documentation

template<class T>
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.

template<class T>
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.

template<class T>
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.

template<class T>
RWTValVirtualArray< T >::~RWTValVirtualArray ( )

Releases all pages allocated by the vector.

Member Function Documentation

template<class T>
RWVirtualPageHeap* RWTValVirtualArray< T >::heap ( ) const
inline

Returns a pointer to the heap from which the vector is getting its pages.

template<class T>
long RWTValVirtualArray< T >::length ( ) const
inline

Returns the length of the vector.

template<class T>
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.

template<class T>
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.

template<class T>
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.

template<class T>
T RWTValVirtualArray< T >::operator[] ( long  i) const
inline

Returns a copy of the value at index i.

Exceptions
RWBoundsErrThrown if index i is not between 0 and one less than the number of entries in self.
template<class T >
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.

Exceptions
RWBoundsErrThrown if index i is not between 0 and one less than the number of entries in self.
template<class T>
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.

template<class T>
void RWTValVirtualArray< T >::set ( long  i,
const T &  v 
)
inline

Sets the value at the index i to v.

Exceptions
RWBoundsErrThrown if index i is not between 0 and one less than the number of entries in self.
template<class T >
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.

template<class T>
T RWTValVirtualArray< T >::val ( long  i) const
inline

Returns a copy of the value at index i.

Exceptions
RWBoundsErrThrown if index i is not between 0 and one less than the number of entries in self.

Copyright © 2023 Rogue Wave Software, Inc., a Perforce company. All Rights Reserved.