Module: Essential Tools Module Group: Generic Classes
RWGSortedVector(val)RWGVector(val)
#include <rw/gsortvec.h> declare(RWGSortedVector,val) implement(RWGSortedVector, val) RWGSortedVector(val) v; // A sorted vector of vals .
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 -- RWGSortedVector is deprecated. Please use RWTValSortedVector or RWTPtrSortedVector.
Note that it is a value-based collection: items are copied in and out of the collection.
The class val must have:
a default constructor;
well-defined copy semantics (val::val(const val&) or equivalent);
well-defined assignment semantics (val::operator=(const val&) or equivalent);
well-defined equality semantics (val::operator==(const val&) or equivalent);
well-defined less-than semantics (val::operator<(const val&) or equivalent).
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.
None
Here's an example of a sorted vector of ints:
#include <rw/gsortvec.h> #include <iostream> declare(RWGVector,int) declare(RWGSortedVector,int) implement(RWGVector,int) implement(RWGSortedVector,int) // Declare and define "comparison function" to partition even and odd numbers int compare_func(const int* a, const int* b) { return (*a % 2) - (*b % 2); } int main () { // Declare and define an instance using the comparison function RWGSortedVector (int) vec (compare_func); int someData [6] = { 3, 17, 4, 5, 32, -1 }; std::cout << "Insert the numbers: "; // Do some insertions: for (size_t i = 0; someData [i] > 0; ++i) { vec.insert(someData [i]); std::cout << someData [i] << ' '; } std::cout << "\n\nNow the RWGSortedVector is ordered as follows: "; for (size_t i = 0; i < vec.entries (); ++i) { std::cout << vec (i) << ' '; } std::cout << "\n\nThe second element is: " << vec (1) << "\n"; std::cout << "The index of the first odd element is: " << vec.index (1) << "\n\n"; return 0; }
Program output:
Insert the numbers: 3 17 4 5 32 Now the RWGSortedVector is ordered as follows: 4 32 3 17 5 The second element is: 32 The index of the first odd element is: 2
RWGSortedVector(val)( int (*f)(const val*, const val*) );
Constructs a sorted vector of elements of val val, using the comparison function pointed to by f. The initial capacity of the vector will be set by the value RW_DEFAULT_CAPACITY. The capacity will automatically be increased should too many items be inserted.
RWGSortedVector(val)(int (*f)(const val*, const val*), size_t N);
Constructs a sorted vector of elements of val val, using the comparison function pointed to by f. The initial capacity of the vector is N. The capacity is automatically increased if too many items are inserted.
val operator()(size_t i) const;
Returns the ith value in the vector. The index i must be between 0 and the length of the vector less one. No bounds checking is performed.
val operator[](size_t i) const;
Returns the ith value in the vector. The index i must be between 0 and the length of the vector less one. Bounds checking is performed.
size_t entries() const;
Returns the number of items currently in the collection.
size_t index(val v);
Returns the index of the item with value v. The value "RW_NPOS" is returned if the value does not occur in the vector. A binary search, using the comparison function, is done to find the value. If duplicates are present, the index of the first instance is returned.
bool insert(val v);
Inserts the new value v into the vector. A binary search, using the comparison function, is performed to determine where to insert the value. The item will be inserted after any duplicates. If the insertion causes the vector to exceed its capacity, it will automatically be resized by an amount given by RW_DEFAULT_CAPACITY.
void removeAt(size_t indx);
Removes the item at position indx from the collection. The value of indx must be between zero and the length of the collection less one. No bounds checking is performed. Old items from index indx+1 will be shifted to lower indices. For example, the item at index indx+1 will be moved to position indx, etc.
void resize(size_t newCapacity);
Changes the capacity of the collection to newCapacity, which must be at least as large as the present number of items in the collection. Note that the actual number of items in the collection does not change, just the capacity.
© 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.