Go to the previous, next section.
#include <nihcl/Bag.h>
A
Bag
is a
Set
of
AssocInt
objects. Each
AssocInt
object associates an object in a
Bag
with a count of the number of occurrences of that object. Each time an equal object, as determined by applying
isEqual()
, is added to a
Bag
, the object's associated occurrence count is incremented. Thus, unlike a
Set
, a
Bag
may contain multiple occurrences of equal objects.
Bag(unsigned
capacity
=DEFAULT_CAPACITY)
Bag
that can hold up to
capacity
objects. An
NIHCL_ALLOCSIZE
error is raised if
capacity
is not greater than 0. If an attempt is made to add more than
capacity
objects to a
Bag
,
reSize()
is called to increase its capacity. Since this is an expensive operation, it is wise to supply a reasonable estimate for
capacity.
Bag(const Bag&
b)
Bag
that is the same size as
Bag
b
and initializes it to point to the same objects contained in
b.
virtual Object* add(Object&
ob)
Bag
to see if it already contains an object that is equal to
ob, as determined by applying
isEqual()
. If not,
add()
adds
ob
to this
Bag
with an occurrence count of 1 and returns a pointer to
ob. If an object that is equal to
ob
is already in this
Bag
,
add()
increments the occurrence count for this object and returns a pointer to this object instead of a pointer to
ob.
Object* addWithOccurrences(Object&
ob, unsigned
n)
Bag::add()
, except that
ob's occurrence count is initialized to or increased by
n
instead of 1.
virtual Object* remove(const Object&
ob)
Bag
, as determined by applying
isEqual()
. If no equal object is found,
remove()
raises an
NIHCL_REMOVEERR
; otherwise,
remove()
decrements this object's occurrence count. If the occurrence count is non-zero,
remove()
returns 0; when an object's occurrence count reaches zero, the object is removed from this
Bag
and
remove()
returns a pointer to it.
virtual void removeAll()
Bag
.
virtual unsigned hash() const
Set
can use as a hash table probe. Class
Bag
implements
hash()
by exclusive ORing the results of applying
hash()
to all objects in a
Bag
.
virtual unsigned occurrencesOf(const Object&
ob) const
Bag
that is equal to
ob, as determined by applying
isEqual()
. Returns 0 if no equal object is contained in this
Bag
.
bool operator==(const Bag&) const
bool operator!=(const Bag&) const
YES
if the right
Bag
operand is equal to (or not equal to) the left
Bag
operand. Two
Bag
s
a
and
b
are equal if they contain the same number of objects and if, for each object in
a, there is an
isEqual()
object in
b
with the same occurrence count.
virtual bool isEqual(const Object&
b) const
YES
if this
Bag
and the argument object are comparable and they have equal values. The argument object
a
is comparable if it returns the address of class
Bag
's class descriptor for its
species()
. Class
Bag
implements
isEqual()
by applying
isEqual()
to all objects in a
Bag
.
void operator=(const Bag&)
Bag
operand with a copy of the right
Bag
operand. Only the pointers to the objects contained in the right operand are copied, not the objects they point to.
virtual void deepenShallowCopy()
deepCopy()
to all the object pointers, replacing each pointer with a pointer to its copy.
virtual Object*& at(int
i)
virtual const Object *const& at(int
i) const
Set
object used by this
Bag
. This will be a pointer to either the
nil
object (if the
ith hash table position is unoccupied), or a pointer to an
AssocInt
that associates an object in this
Bag
with its occurrence count. Because this function relies on the underlying implementation of class
Bag
as a
Set
of
AssocInt
s, it is not recommended for general use.
virtual unsigned capacity() const
Bag
can hold before it will need to be expanded.
virtual unsigned size() const
Bag
. Since
size()
includes multiple occurrences of duplicate (i.e.
isEqual()
) objects, a
Bag
's
size()
may exceed its
capacity()
.
virtual void reSize(unsigned
newSize)
Bag
to
newSize. If
newSize
is less than or equal to
capacity()
, then the capacity is not changed.
virtual void dumpOn(ostream&
strm
=cerr) const
dumpOn()
to all
AssocInt
objects in this
Bag
to print each object and its occurrence count, then prints a right square bracket and a newline.
virtual Object* doNext(Iterator&
pos) const
Bag
, or 0 if no more objects remain. The
Iterator
argument
pos
maintains the current position in the
Bag
.
virtual const Class* species() const
Bag
.
virtual void _reader(OIOifd&
fd)
virtual void _reader(OIOin&
strm)
addWithOccurrences()
to add them to this
Bag
. Does nothing unless called from the
readFrom()
constructor of a most-derived class. This assures that
_reader()
calls the most-derived implementation of the virtual function
addWithOccurrences()
.
virtual void storer(OIOofd&
fd) const
virtual void storer(OIOout&
strm) const
storeOn()
to each of the objects contained in this
Bag
to store them on
fd
or
strm
along with each object's occurrence count.
virtual int compare(const Object&) const
shouldNotImplement()
.
NIHCL_REMOVEERR
Go to the previous, next section.