#include <OTC/collctn/set.hh> template<class T> class OTC_Set {
public:
static os_typespec* get_os_typespec();
~OTC_Set();
OTC_Set(int (*theRankFn)(T const&,T const&)=0);
OTC_Set(OTC_Set<T> const& theSet);
void removeAll();
OTC_Set<T>& operator=(OTC_Set<T> const& theSet);
void add(T const& theItem);
void remove(T const& theItem);
OTC_Boolean contains(T const& theItem) const;
T const& pickItem() const;
inline u_int population() const;
inline OTC_Boolean isEmpty() const;
OTC_Iterator<T> items( OTC_Direction theDirection=OTCLIB_FORWARD, OTC_Protection theProtection=OTCLIB_SAFE ) const;
};
OTC_Set
implements a collection for maintaining a set of objects
and for determining membership of that set. Once an item is placed
into the set, it cannot be retrieved. If this behaviour is
required, one of the map classes should be used.
Commensurate with the properties of a set, no duplicate items are
allowed. In addition, the items placed into the collection must be
able to have a hash value generated for them through the use of
the OTC_HashActions
class.
OTC_Set(int (*theRankFn)(T const&,T const&)=0);
theRankFn
is an
optional comparison function to be used in
placed of OTC_RankActions
.
OTC_Set(OTC_Set<T> const& theSet);
theSet
.
void removeAll();
OTC_Set<T>& operator=(OTC_Set<T> const& theSet);
theSet
.
Returns a reference to this set.
void add(T const& theItem);
theItem
is not in this set already,
it will be added to this set. If theItem
is in the set already, an exception is
raised.
void remove(T const& theItem);
theItem
is in this set, it will be
removed and deleted. If theItem
is not
in this set, an exception will be raised.
OTC_Boolean contains(T const& theItem) const;
OTCLIB_TRUE
if theItem
is
contained in this set.
T const& pickItem() const;
inline u_int population() const;
inline OTC_Boolean isEmpty() const;
OTCLIB_TRUE
if the collection is
empty.
OTCLIB_SAFE
or OTCLIB_UNSAFE
.
To get an unsafe iterator, the OTCLIB_UNSAFE
argument should
be used.
The first argument to the following functions indicates the
direction of traversal of the iterator. Traversal in the
direction of first to last item is indicated by a value of
OTCLIB_FORWARD
. Traversal in the reverse direction is
indicated by a value of OTCLIB_BACKWARD
. The default value
is OTCLIB_FORWARD
. In the OTC_Set
class, traversal in
the forward direction will result in the first item being
accessible being the oldest item in the set. Moving the
iterator will result in successively newer items in the set
being accessible.
When iterating over items, in the order in which they were
inserted, be very careful about inserting new items into the set.
This is because any new items inserted, may also be seen by the
iterator. If you were inserting a new item for every item seen,
you would end up with an infinite loop.
OTC_Iterator<T> items(
OTC_Direction theDirection=OTCLIB_FORWARD,
OTC_Protection theProtection=OTCLIB_SAFE
) const;
OTC_Set<EX_Foo const*>
and not just OTC_Set<EX_Foo*>
. If
this is not done, it would be possible for a user to modify parts
of the item from which the hash value is generated. If the hash
value for an object changes, the object will no longer be
accessible.
The OTC_Bucket
class is used internally to hold the items. Thus,
the OTC_BaseActions
class may be used to provide actions to be
performed when items are inserted or removed from the set.
Since an attempt to remove an item from the set which is not
there, will generate an exception, it is important to first check
that an item is actually in the set, by calling the contains()
function.
OTC_Iterator
, OTC_Bucket
, OTC_HashActions
, OTC_RankActions