#include <rw/tvset.h> RWTValSet<T,C> s;
RWTValSet requires the Standard C++ Library.
This class maintains a collection of values, which are ordered according to a comparison object of type C. C must induce a total ordering on elements of type T via a public member
bool operator()(const T& x, const T& y) const
which returns true if x should precede y within the collection. The structure less<T> from the C++-standard header file <functional> is an example.
RWTValSet<T,C> will not accept an item that compares equal to an item already in the collection. (RWTValMultiSet<T,C> may contain multiple items that compare equal to each other.) Equality is based on the comparison object and not on the == operator. Given a comparison object comp, items a and b are equal if
!comp(a,b) && !comp(b,a).
The value type must have operator== and operator< defined. This requirement is imposed by the Standard C++ Library.
Isomorphic.
In this example, a set of RWCStrings is exercised.
// // tvsstr.cpp // #include <rw/tvset.h> #include <rw/cstring.h> #include <iostream.h> #include <function.h> main(){ RWTValSet<RWCString,less<RWCString> > set; set.insert("one"); set.insert("two"); set.insert("three"); set.insert("one"); // Rejected: already in collection cout << set.entries() << endl; // Prints "3" return 0; }
Class RWTValMultiSet<T,C> offers the same interface to a collection that accepts multiple items that compare equal to each other. RWTValMap<K,T,C> maintains a collection of key-value pairs.
Class set<T,C,allocator> is the C++-standard collection that serves as the underlying implementation for RWTValSet<T,C>.
typedef set<T,C,allocator> container_type; typedef container_type::iterator iterator; typedef container_type::const_iterator const_iterator; typedef container_type::size_type size_type; typedef:T value_type; typedef const T& const_reference;
RWTValSet<T,C>(const C& comp = C());
Constructs an empty set.
RWTValSet<T,C>(const container_type& s);
Constructs a set by copying all elements of s.
RWTValSet<T,C>(const RWTValSet<T,C>& rws);
Copy constructor.
RWTValSet<T,C> (const T* first,const T* last,const C& comp = C());
Constructs a set by copying elements from the array of Ts pointed to by first, up to, but not including, the element pointed to by last.
RWTValSet<T,C>& operator=(const RWTValSet<T,C>& s); RWTValSet<T,C>& operator=(const container_type& s);
Destroys all elements of self and replaces them by copying all elements of s.
bool operator<(const RWTValSet<T,C>& s) const; bool operator<(const container_type& s) const;
Returns true if self compares lexicographically less than s, otherwise returns false. Assumes that type T has well-defined less-than semantics (T::operator<(const T&) or equivalent).
bool operator==(const RWTValSet<T,C>& s) const; bool operator==(const set<T,C>& s) const;
Returns true if self compares equal to s, otherwise returns false. Two collections are equal if both have the same number of entries, and iterating through both collections produces, in turn, individual elements that compare equal to each other.
void apply(void (*fn)(const_reference,void*), void* d) const;
Applies the user-defined function pointed to by fn to every item in the collection. This function must have prototype:
void yourfun(const_reference a, void* d);
Client data may be passed through parameter d.
iterator begin(); const_iterator begin() const;
Returns an iterator positioned at the first element of self.
void clear();
Clears the collection by removing all items from self. Each item will have its destructor called.
bool contains(const_reference a) const;
Returns true if there exists an element t in self that compares equal to a, otherwise returns false.
bool contains(bool (*fn)(const_reference,void*), void* d) const;
Returns true if there exists an element t in self such that the expression ((*fn)(t,d)) is true, otherwise returns false. fn points to a user-defined tester function which must have prototype:
bool yourTester(const_reference a, void* d);
Client data may be passed through parameter d.
void difference(const RWTValSet<T,C>& s); void difference(const container_type& s);
Sets self to the set-theoretic difference given by (self - s).
iterator end(); const_iterator end() const;
Returns an iterator positioned "just past" the last element in self.
size_type entries() const;
Returns the number of items in self.
bool find(const_reference a, T& k) const;
If there exists an element t in self that compares equal to a, assigns t to k and returns true. Otherwise, returns false and leaves the value of k unchanged.
bool find(bool (*fn)(const_reference,void*), void* d, T& k) const;
If there exists an element t in self such that the expression ((*fn)(t,d)) is true, assigns t to k and returns true. Otherwise, returns false and leaves the value of k unchanged. fn points to a user-defined tester function which must have prototype:
bool yourTester(const_reference a, void* d);
Client data may be passed through parameter d.
bool insert(const_reference a);
Adds the 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 element with the equivalent key.
void intersection(const RWTValSet<T,C>& s); void intersection(const container_type& s);
Sets self to the intersection of self and s.
bool isEmpty() const;
Returns true if there are no items in the collection, false otherwise.
bool isEquivalent(const RWTValSet<T,C>& s) const;
Returns true if there is set equivalence between self and s, and returns false otherwise.
bool isProperSubsetOf(const RWTValSet<T,C>& s) const;
Returns true if self is a proper subset of s, and returns false otherwise.
bool isSubsetOf(const RWTValSet<T,C>& s) const;
Returns true if self is a subset of s; false otherwise.
size_type occurrencesOf(const_reference a) const;
Returns the number of elements t in self that compare equal to a.
size_type occurrencesOf(bool (*fn)(const T&,void*),void* d) const;
Returns the number of elements t in self such that the expression((*fn)(t,d)) is true. fn points to a user-defined tester function which must have prototype:
bool yourTester(const_reference a, void* d);
Client data may be passed through parameter d.
bool remove(const_reference a);
Removes the first element t in self that compares equal to a and returns true. Returns false if there is no such element.
bool remove(bool (*fn)(const_reference,void*), void* d);
Removes the first element t in self such that the expression ((*fn)(t,d)) is true and returns true. Returns false if there is no such element. fn points to a user-defined tester function which must have prototype:
bool yourTester(const_reference a, void* d);
Client data may be passed through parameter d.
size_type removeAll(const_reference a);
Removes all elements t in self that compare equal to a. Returns the number of items removed.
size_type removeAll(bool (*fn)(const_reference,void*), void* d);
Removes all elements t in self such that the expression ((*fn)(t,d))is true. Returns the number of items removed. fn points to a user-defined tester function which must have prototype:
bool yourTester(const_reference a, void* d);
Client data may be passed through parameter d.
set<T,C,allocator>& std(); const set<T,C,allocator>& std() const;
Returns a reference to the underlying C++-standard collection that serves as the implementation for self. This reference may be used freely, providing access to the C++-standard interface as well as interoperability with other software components that make use of the C++-standard collections.
void symmetricDifference(const RWTValSet<T,C>& s); void symmetricDifference(const container_type& s);
Sets self to the symmetric difference of self and s.
void Union(const RWTValSet<T,C>& s); void Union(const container_type& s);
Sets self to the union of self and s. Note the use of the uppercase "U"in Union to avoid conflict with the C++ reserved word.
RWvostream& operator<<(RWvostream& strm, const RWTValSet<T,C>& coll); RWFile& operator<<(RWFile& strm, const RWTValSet<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, RWTValSet<T,C>& coll); RWFile& operator>>(RWFile& strm, RWTValSet<T,C>& coll);
Restores the contents of the collection coll from the input stream strm.
RWvistream& operator>>(RWvistream& strm, RWTValSet<T,C>*& p); RWFile& operator>>(RWFile& strm, RWTValSet<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.