Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

RWGSortedVector(val)


RWGSortedVector(val)-->RWGVector(val)

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

Synopsis

#include <rw/gsortvec.h>
declare(RWGSortedVector,val)
implement(RWGSortedVector, val)
RWGSortedVector(val) v;    // A sorted vector of vals .

Description

Class RWGSortedVector(val) represents a vector of elements of val val, sorted using an insertion sort. The elements can be retrieved using an index or a search. Duplicates are allowed. Objects of val RWGSortedVector(val) are declared with macros defined in the standard C++ header file <generic.h>.

Note that it is a value-based collection: items are copied in and out of the collection.

The class val must have:

To use this class you must declare and implement its base class as well as the class itself. For example, here is how you declare and implement a sorted collection of doubles:

declare(RWGVector,double)                    // Declare base class
declare(RWGSortedVector,double)   // Declare sorted vector

// In one and only one .cpp file you must put the following:
implement(RWGVector,double)         // Implement base class
implement(RWGSortedVector,double)   // Implement sorted vector

For each val of RWGSortedVector you must include one (and only one) call to the macro implement somewhere in your code for both the RWGSortedVector itself and for its base class RWGVector.

Insertions and retrievals are done using a binary search. Note that the constructor of an RWGSortedVector(val) requires a pointer to a "comparison function." This function should have protoval:

int comparisonFunction(const val* a, const val* b);

and should return an int less than, greater than, or equal to zero, depending on whether the item pointed to by a is less than, greater than, or equal to the item pointed to by b. Candidates from the collection will appear as a, the key as b.

Persistence

None

Example

Here's an example of a sorted vector of ints:

#include <rw/gsortvec.h>
#include <rw/rstream.h>

declare(RWGVector,int)
declare(RWGSortedVector,int)
implement(RWGVector,int)
implement(RWGSortedVector,int)

// Declare and define the "comparison function":
int compFun(const int* a, const int* b)  {
  return *a - *b;
}

main()  {
  // Declare and define an instance,
  // using the comparison function 'compFun':
  RWGSortedVector(int) avec(compFun);

  // Do some insertions:
  avec.insert(3);          // 3
  avec.insert(17);         // 3 17
  avec.insert(5);          // 3  5 17

  cout << avec(1);         // Prints '5'
  cout << avec.index(17);  // Prints '2'
}

Public Constructors

RWGSortedVector(val)( int (*f)(const val*, const val*) );
RWGSortedVector(val)(int (*f)(const val*, const val*), 
                size_t N);

Public Member Functions

val
operator()(size_t i) const;
val
operator[](size_t i) const;
size_t
entries() const;
size_t
index(val v);
RWBoolean
insert(val v);
void
removeAt(size_t indx);
void
resize(size_t newCapacity);


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