Public Member Functions |
| CollectorMap () |
| Constructor.
|
| CollectorMap (const KeyPolicy &keyPolicy) |
| Allows the user to choose if keys can be duplicated.
|
virtual | ~CollectorMap () |
| Destructor handles removal of the elements within the collection.
|
| CollectorMap (const CollectorMap &cmap) |
| Copy constructor invokes the copy policy as provided by the users.
|
CollectorMap & | operator= (const CollectorMap &cmap) |
| Assignment operator for the CollectorMap class object.
|
int | size () const |
| Returns the size of the collection.
|
int | count (const K &key) const |
| Returns the number of keys found in the list.
|
void | add (const K &key, const T &value) |
| Adds the element to the list.
|
bool | exists (const K &key) const |
| Checks the existance of a particular key in the list.
|
T & | get (const K &key) |
| Returns the value associated with the name provided.
|
const T & | get (const K &key) const |
| Const version returning the value associated with the given name.
|
int | index (const K &key) const |
| Returns the index of the first occuring element in the list.
|
T & | getNth (int nth) |
| Returns the nth value in the collection.
|
const T & | getNth (int nth) const |
| Returns the nth value in the collection.
|
const K & | key (int nth) const |
| Returns the nth key in the collection.
|
int | remove (const K &key) |
| Removes and entry from the list.
|
CollectorConstIter | begin () const |
| Const iterator into list.
|
CollectorConstIter | end () const |
| Const iterator to end of list.
|
CollectorIter | begin () |
| Returns the start of the list for iterating purposes.
|
CollectorIter | end () |
| Returns the end of the list.
|
template<typename K, typename T, template< class > class ComparePolicy = SimpleCompare, template< class > class RemovalPolicy = NoopRemoval, template< class > class CopyPolicy = DefaultCopy>
class Isis::CollectorMap< K, T, ComparePolicy, RemovalPolicy, CopyPolicy >
Collector/container for arbitrary items.
Used to contain types with iterators of const and non-const conditions. This is a multimap that contains arbitrary keys with arbitrary elements. It is intended to be used for pointers and copyable objects. They should be rather efficient in the copy out operation so large objects may not be suitable or classes that do not have a good copy operator. During testing it was noted that an object is copied up to four times and destroyed three times upon an add() operation.
This class is implemented using policies. The ComparePolicy is used to test key elements such as strings and double values. The NoCaseStringCompare policy is provided that expedites case insensitive string key comparisons. The RobustFloatCompare implements the comparison of double or float key types. Direct comparisons of floats can be problematic due to round off and storage manifestations of these values in conputers. The default policy, SimpleCompare, does a simple parameter to key equality test.
The RemovalPolicy is provided when a map value is removed from the list. This allows pointers and arrays to be stored in the map as well. To store pointers, use PointerRemoval and for arrays there is the ArrayRemoval policy. The default is the NoopRemoval policy which simply lets the destructor handle removals.
The CopyPolicy is necessary to properly handle the copying of elements. This is especially important for pointers and arrays. In order to minimize difficult passing strategies, map elements are passed by address and the return type is the element type. DefaultCopy simply copies the elements as is relying on the element T assigment operator to do the right thing. For pointers to objects, the PointerCopy allocates the object using the copy constructor. One could provide a similar operator assuming a clone() method existed for the type T element. The ArrayCopy policy is left to the user to provide their own as it cannot support arrays of varying length. (One should use std::vector instead!) Users can supply their own CopyPolicy that need only expose a copy(cont T *src) method.
Here are some examples that demonstrate how this policy-based template class can be used:
CollectorMap<QString, double, NoCaseStringCompare > dmap;
cout << "\nSize of double map = " << dmap.size() << endl;
dmap.add("one", 1.0);
dmap.add("two", 2.0);
cout << "Size of double map = " << dmap.size() << endl;
cout << "One = " << dmap.get("one") << endl;
cout << "Two = " << dmap.get("Two") << endl;
const double &one = dmap.get("one");
cout << "\nTest Const one = " << one << endl;
dmap.remove("one");
Using this class internal to classes is perhaps where it may be applied more frequently. The example below shows how to declare an integer key using pointers to classes:
class ClassTest {
public:
ClassTest(int n = 0) : _n(n) { }
~ClassTest() { }
int Ident() const { return (_n); }
private:
int _n;
};
PointerRemoval, PointerCopy> PointerMap;
PointerMap ctest2;
ctest2.add(4,new ClassTest(4));
ctest2.add(5,new ClassTest(5));
ctest2.add(6,new ClassTest(6));
ctest2.add(7,new ClassTest(7));
cout << "Remove ClassTest 6\n";
ctest2.remove(6);
PointerMap map2(ctest2);
cout << "Find element 7: " << map2.find(7)->Ident() << endl;
And, finally, an example of how to use duplicate keys:
typedef CollectorMap<int,QString> IntStr;
IntStr dupstr(IntStr::DuplicateKeys);
dupstr.add(1,"One");
dupstr.add(1, "One #2");
dupstr.add(1,"One #3");
dupstr.add(2,"Two");
dupstr.add(2,"Two #2");
dupstr.add(3,"Three");
cout << "Size of Dup object: " << dupstr.size() << endl;
cout << "Number Ones: " << dupstr.count(1) << endl;
cout << "Number Twos: " << dupstr.count(2) << endl;
cout << "Number Threes: " << dupstr.count(3) << endl;
cout << "Number Fours: " << dupstr.count(4) << endl;
IntStr::CollectorConstIter isIter;
int j = 0;
for (isIter = dupstr.begin() ; isIter != dupstr.end() ; ++isIter, j++) {
cout << "IntStr[" << j << "] = {" << isIter->first << ", "
<< isIter->second << "}, Index: " << dupstr.index(isIter->first)
<< endl;
cout << "Nth Test Ident = " << dupstr.getNth(j) << endl;
}
The output of the above example is:
Size of Dup object: 6
Number Ones: 3
Number Twos: 2
Number Threes: 1
Number Fours: 0
IntStr[0] = {1, One}, Index: 0
Nth Test Ident = One
IntStr[1] = {1, One #2}, Index: 0
Nth Test Ident = One #2
IntStr[2] = * {1, One #3}, Index: 0
Nth Test Ident = One #3
IntStr[3] * = {2, Two}, Index: 3
Nth Test Ident = Two
IntStr[4] = * {2, Two #2}, Index: 3
Nth Test Ident = Two #2
IntStr[5] = * {3, Three}, Index: 5
Nth Test Ident = Three
- Author
- 2006-06-21 Kris Becker