RWTPtrMap(3C++) RWTPtrMap(3C++)
Name
RWTPtrMap<K,T,C> - Rogue Wave library class
Synopsis
#include <rw/tpmap.h>
RWTPtrMap<K,T,C> m;
Standard C++ Library Dependent!RWTPtrMap requires the Standard C++ Library.
Description
This class maintains a pointer-based collection of associations of type
pair<K* const, T*>. The first part of the association is a key of type
K*, the second is its associated item of type T*. Order is determined by
the key according to a comparison object of type C. C must induce a
total ordering on elements of type K via a public member
bool operator()(const K& x, const K& y)
which returns true if x and its partner should precede y and its partner
within the collection. The structure less<T> from the C++-standard
header file <functional> is an example. Note that keys will be
dereferenced before being compared. RWTPtrMap<K,T,C> will not accept a
key that compares equal to any key already in the collection.
(RWTPtrMultiMap<K,T,C> may contain multiple keys that compare equal to
each other.) Equality is based on the comparison object and not on the
== operator. Given a comparison object comp, keys a and b are equal if
!comp(a,b) && !comp(b,a).
Persistence
Isomorphic.
Examples
In this example, a map of RWCStrings and RWDates is exercised.
//
// tpmap.cpp
//
#include <rw/tpmap.h>
Page 1
RWTPtrMap(3C++) RWTPtrMap(3C++)
#include <rw/cstring.h>
#include <rw/rwdate.h>
#include <iostream.h>
#include <function.h>
main(){
RWTPtrMap<RWCString, RWDate, less<RWCString> > birthdays;
birthdays.insert
(
new RWCString("John"),
new RWDate(12, "April", 1975)
);
birthdays.insert
(
new RWCString("Ivan"),
new RWDate(2, "Nov", 1980)
);
// Alternative syntax:
birthdays[new RWCString("Susan")] =
new RWDate(30, "June", 1955);
birthdays[new RWCString("Gene")] =
new RWDate(5, "Jan", 1981);
// Print a birthday:
RWCString key("John");
cout << *birthdays[&key] << endl;
return 0;
}
Program Output:
04/12/75
Related Classes
Class RWTPtrMultiMap<K,T,C> offers the same interface to a pointer-based
collection that accepts multiple keys that compare equal to each other.
RWTPtrSet<T,C> maintains a pointer-based collection of keys without the
associated items. Class map<K*,T*,deref_compare<C,K, allocator> > is the
C++-standard collection that serves as the underlying implementation for
this collection.
Public Typedefs
typedef rw_deref_compare<C,K> container_comp;
typedef map<K*,T*,container_comp, allocator> container_type;
typedef container_type::size_type size_type;
typedef container_type::difference_type difference_type;
typedef container_type::iterator iterator;
typedef container_type::const_iterator const_iterator;
typedef pair <K* const, T*> value_type;
typedef pair <K* const, T*>& reference;
typedef const pair <K* const, T*>& const_reference;
typedef K* value_type_key;
typedef T* value_type_data;
typedef K*& reference_key;
typedef T*& reference_data;
typedef const K*const& const_reference_key;
Page 2
RWTPtrMap(3C++) RWTPtrMap(3C++)
typedef const T*const& const_reference_data;
Public Constructors
RWTPtrMap<K,T,C>
(const container_comp& comp = container_comp());
Constructs an empty map with comparator comp.
RWTPtrMap<K,T,C>(const RWTPtrMap<K,T,C>& rwm);
Copy constructor.
RWTPtrMap<K,T,C>(const container_type& m);
Constructs a map by copying all elements from m.
RWTPtrMap<K,T,C>
(value_type* first,value_type* last,
const container_comp& comp = container_comp());
Constructs a map by copying elements from the array of pairs pointed to
by first, up to, but not including, the pair pointed to by last.
Public Member Operators
RWTPtrMap<K,T,C>&
operator=(const RWTPtrMap<K,T,C>& m);
RWTPtrMap<K,T,C>&
operator=(const container_type& m);
Destroys all associations in self and replaces them by copying all
associations from m.
bool
operator<(const RWTPtrMap<K,T,C>& m) const;
Returns true if self compares lexicographically less than m, otherwise
returns false. Keys in each collection are dereferenced before being
compared. Assumes that type K has well-defined less-than semantics.
bool
operator==(const RWTPtrMap<K,T,C>& m) const;
Returns true if self compares equal to m, otherwise returns false. Two
Page 3
RWTPtrMap(3C++) RWTPtrMap(3C++)
collections are equal if both have the same number of entries, and
iterating through both collections produces, in turn, individual keys
that compare equal to each other. Keys are dereferenced before being
compared.
T*&
operator[](const K* key);
Looks up key and returns a reference to its associated item. If the key
is not in the dictionary, then it will be added with an associated
uninitialized pointer of type T*. Because of this, if there is a
possibility that a key will not be in the dictionary, then this operator
should only be used as an lvalue.
Public Member Functions
void
apply(void (*fn)(const K*,T*&,void*),void* d);
void
apply(void (*fn)(const K*,const T*,void*),void* d) const;
Applies the user-defined function pointed to by fn to every association
in the collection. This function must have one of the prototypes:
void yourfun(const K* key, T*& a, void* d);
void yourfun(const K* key, const T* a, void* d);
Client data may be passed through parameter d.
void
applyToKeyAndValue(void (*fn)(const K*,T*&,void*),void* d);
void
applyToKeyAndValue
(void (*fn)(const K*,const T*,void*),void* d) const;
This is a deprecated version of the apply member above. It behaves
exactly the same as apply.
iterator
begin();
const_iterator
begin() const;
Page 4
RWTPtrMap(3C++) RWTPtrMap(3C++)
Returns an iterator positioned at the first pair in self.
void
clear();
Clears the collection by removing all items from self.
void
clearAndDestroy();
Removes all associations from the collection and uses operator delete to
destroy the objects pointed to by the keys and their associated items.
Do not use this method if multiple pointers to the same object are
stored. (This could happen even if keys all compare different, since
items are not considered during comparison.)
bool
contains(const K* key) const;
Returns true if there exists a key j in self that compares equal to *key,
otherwise returns false.
bool
contains(bool (*fn)(value_type,void*), void* d) const;
Returns true if there exists an association a in self such that the
expression ((*fn)(a,d)) is true, otherwise returns false. fn points to a
user-defined tester function which must have prototype:
bool yourTester(value_type a, void* d);
Client data may be passed through parameter d.
iterator
end();
const_iterator
end() const;
Returns an iterator positioned "just past" the last association in self.
size_type
entries() const;
Page 5
RWTPtrMap(3C++) RWTPtrMap(3C++)
Returns the number of associations in self.
const K*
find(const K* key) const;
If there exists a key j in self that compares equal to *key, then j is
returned. Otherwise, returns rwnil.
value_type
find(bool (*fn)(value_type,void*), void* d) const;
If there exists an association a in self such that the expression
((*fn)(a,d)) is true, then returns a. Otherwise, returns
pair<rwnil,rwnil>. fn points to a user-defined tester function which
must have prototype:
bool yourTester(value_type a, void* d);
Client data may be passed through parameter d.
T*
findValue(const K* key);
const T*
findValue(const K* key) const;
If there exists a key j in self that compares equal to *key, returns the
item associated with j. Otherwise, returns rwnil.
const K*
findKeyAndValue(const K* key, T*& tr);
const K*
findKeyAndValue(const K* key, const T*& tr) const;
If there exists a key j in self that compares equal to *key, assigns the
item associated with j to tr, and returns j. Otherwise, returns rwnil
and leaves the value of tr unchanged.
bool
insert(K* key, T* a);
Page 6
RWTPtrMap(3C++) RWTPtrMap(3C++)
Adds key with associated item a to the collection. Returns true if the
insertion is successful, otherwise returns false. The function will
return true unless the collection already holds an association with the
equivalent key.
bool
insertKeyAndValue(K* key, T* a);
This is a deprecated version of the insert member above. It behaves
exactly the same as insert.
bool
isEmpty() const;
Returns true if there are no items in the collection, false otherwise.
size_type
occurrencesOf(const K* key) const;
Returns the number of keys j in self that compare equal to *key.
size_type
occurrencesOf
(bool (*fn)(value_type,void*), void* d) const;
Returns the number of associations a in self such that the
expression((*fn)(a,d)) is true. fn points to a user-defined tester
function which must have prototype:
bool yourTester(value_type a, void* d);
Client data may be passed through parameter d.
K*
remove(const K* key);
Removes the first association with key j in self that compare euqal to
*key and returns j. Returns rwnil if there is no such association.
K*
remove(bool (*fn)(value_type,void*), void* d);
Removes the first association a in self such that the expression
((*fn)(a,d)) is true and returns its key. Returns rwnil if there is no
such association. fn points to a user-defined tester function which must
Page 7
RWTPtrMap(3C++) RWTPtrMap(3C++)
have prototype:
bool yourTester(value_type a, void* d);
Client data may be passed through parameter d.
size_type
removeAll(const K* key);
Removes all associations with key j in self that compare equal to *key.
Returns the number of associations removed.
size_type
removeAll(bool (*fn)(value_type,void*), void* d);
Removes all associations a in self such that the expression
((*fn)(a,d))is true. Returns the number removed. fn points to a user-
defined tester function which must have prototype:
bool yourTester(value_type a, void* d);
Client data may be passed through parameter d.
container_type
std();
const container_type
std() const;
Returns a reference to the underlying C++-standard collection that serves
as the implementation for self.
Related Global Operations
RWvostream&
operator<<(RWvostream& strm, const RWTPtrMap<K,T,C>& coll);
RWFile&
operator<<(RWFile& strm, const RWTPtrMap<K,T,C>& coll);
Saves the collection coll onto the output stream strm, or a reference to
it if it has already been saved.
RWvistream&
operator>>(RWvistream& strm, RWTPtrMap<K,T,C>& coll);
RWFile&
operator>>(RWFile& strm, RWTPtrMap<K,T,C>& coll);
Page 8
RWTPtrMap(3C++) RWTPtrMap(3C++)
Restores the contents of the collection coll from the input stream strm.
RWvistream&
operator>>(RWvistream& strm, RWTPtrMap<K,T,C>*& p);
RWFile&
operator>>(RWFile& strm, RWTPtrMap<K,T,C>*& p);
Looks at the next object on the input stream strm and either creates a
new collection off the heap and sets p to point to it, or sets p to point
to a previously read instance. If a collection is created off the heap,
then you are responsible for deleting it.
Page 9