Bag Class

A bag is one of the most general collection mechanisms. It is also called a multiset in mathematics because it is like a set except that multiple inclusion is possible. In other words, more than one pointer to the same object can be added into a bag. In HCCL, class bag is inherited from class container (see Figure 1). Since most of the basic operations of a bag, such as add( entity * ent ) and size() can be derived from class container, only a few functions are needed to construct class bag. The main added capability is to remove(...) a pointer to an entity and to remove_all() the occurrences of an entity.The declaration of class bag is:

#ifndef _BAG_H_
#define _BAG_H_
/* - - - - - - - - - - - - - - - - - - - - - - - - - - */

class bag: public container {
private:
protected:
Bool is_head(entity * ENT); // check if ENT is the head element
element * previous(entity * ENT); // get to the element preceeding an occurrent of ENT
element * previous(char * NAME); //get to the element preceding one with this NAME
void remove_head(); // remove the head
void remove_middle(entity * ENT); // remove from the middle
void remove_middle(char * NAME);

public:
bag();
~bag();
int number_of(entity * ENT); // how many occurrences of ENT are in container?
int number_of(char * NAME);
void remove(entity * ENT); // remove an occurrence of ENT from container, if any
void remove(char * NAME);
void remove_all(entity * ENT); // remove all occurrences of ENT
void remove_all(char * NAME);
bag * container_to_bag(container * c); //convert a container to an equivalent bag
};
#endif

Since a bag can hold multiple occurrences of the same object, there are functions, number_of(entity * ENT) and number_of(char * NAME), to return this number.