![]() |
The Simons
|
#include <Bag.h>
Inheritance diagram for Bag:
The Bag class is one of the Unordered collections. In a Bag, elements may occur multiple times. The inserted order of elements is not significant. Adding new elements always includes them, increasing the size of the Bag. Adding, removing and retaining collections of elements is equivalent to mutating bag union, bag difference and bag intersection, respectively. Comparing two Bags may determine that one is included by (LESS), equal to (EQUAL) or includes (MORE) the other, or that they are disjoint (NONE) or intersect (SOME). Bags are the most efficient Collection in which to count the quantity of each element.
A Bag is implemented as a chained hash-table. It locates chains of EQUAL elements by hashing on an element with an average seek time of 1.3 lookups. In the rare event of a collision, element chains are relocated. When the last element in a chain is removed, the table is compacted in an average time of 2.3 lookups. The hash table is never more than 2/3 full for optimum performance.
Public Member Functions | |
Bag () | |
Construct an empty Bag. | |
virtual | ~Bag () |
Release an unused Bag. | |
Bag (CollectionID) | |
Construct a Bag from another Collection. | |
virtual ObjectID | clone () const |
Clone a shallow copy of this Bag. | |
virtual Natural | size () const |
The cardinality of this Bag. | |
virtual Boolean | has (ObjectID) const |
Test if this Bag contains a particular Object. | |
virtual Natural | count (ObjectID) const |
Count the occurrences of an Object in this Bag. | |
virtual ObjectID | find (ObjectID) const |
Find the Bag of Objects equal to another in this Bag. | |
virtual ObjectID | item (Integer) const |
Select an Object contained in this Bag. | |
virtual Void | add (ObjectID) |
Add an Object to this Bag. | |
virtual Void | remove (ObjectID) |
Remove an Object from this Bag. | |
Void | clear (ObjectID) |
Remove all occurrences of an Object from this Bag. | |
Protected Member Functions | |
Bag (const Bag &) | |
Copy another Bag. | |
Void | deepAdd (EntryID) |
Add an Entry to this Bag, which is a Relation. | |
Void | deepRemove (EntryID) |
Remove an Entry from this Bag, which is a Relation. |
|
Copy another Bag. Required by C++ to ensure that copying a Bag faithfully copies an Unordered collection. Takes a shallow copy of the other Bag by allocating a hash table with the same capacity, then copying all the chains of elements to the same hashed locations. Used by clone(), this method is more efficient than the constructor to build a Bag from a Collection.
|
|
Construct an empty Bag. The size() of the Bag is zero, but a hash table of the default capacity is constructed and every entry is initialised to empty.
|
|
Release an unused Bag. Deletes the hash table used to store the Bag's elements. This is done by deleting the lists of elements stored at each hashed location, then deleting the table itself. |
|
Construct a Bag from another Collection. This is the main method for converting an arbitrary Collection into a Bag, especially if it is desired to count the quantities of particular elements. The other Collection is unchanged, but this Bag will share all of the Collection's elements. Allocates a hash table with enough capacity to hold all the elements of the other Collection. Inserts them into this Bag using addAll(), which preserves duplicate elements but disregards their order.
|
|
Add an Object to this Bag. Inserts the Object into this Bag, even if the Object is already present, and increments the size of the Bag. The order of inserted objects is essentially arbitrary, though objects judged EQUAL under compare() will tend to cluster together, since they will hash to the same location and will be chained together in the table. Rarely, two chains of non-EQUAL objects may hash to the same location (a collision) and one of these chains will be displaced. The size of the underlying hash table will expand when it reaches 2/3 full.
Reimplemented from Collection. Reimplemented in Relation. |
|
Remove all occurrences of an Object from this Bag. Removes all Objects, judged EQUAL under compare(), to the requested Object, if such Objects are found. Has no effect if no such Object is present in the Bag. The underlying hash table is compacted if the removed Objects were in a collision-set.
|
|
Clone a shallow copy of this Bag.
Reimplemented from Object. Reimplemented in Relation. |
|
Count the occurrences of an Object in this Bag.
Reimplemented from Unordered. |
|
Add an Entry to this Bag, which is a Relation. Inserts the Entry uniquely into this Bag, replacing any existing Entry that is judged deep EQUAL, under deepCompare(). This Bag is a Relation, containing multiple Entries with the same domain key, which are judged EQUAL under compare(). This secret method is needed to ensure that each Entry is added uniquely. Rarely, two chains of non-EQUAL entries may hash to the same location (a collision) and one of these chains will be displaced. The size of the underlying hash table will expand when it reaches 2/3 full.
|
|
Remove an Entry from this Bag, which is a Relation. Removes an Entry from this Bag that is judged deep EQUAL, under deepCompare(), to the requested Entry. Has no effect if no such Entry is present in this Bag. This Bag is a Relation, containing multiple Entries with the same domain key, which are all judged EQUAL under compare(). This secret method is needed to remove only the Entry that is deep EQUAL to the requested Entry. The underlying hash table may be compacted if the last Entry in a collision-set is removed.
|
|
Find the Bag of Objects equal to another in this Bag. Returns a Bag of Objects, judged EQUAL under compare(), to the argument Object, or null if no such Objects are found.
Reimplemented from Unordered. |
|
Test if this Bag contains a particular Object.
Reimplemented from Collection. |
|
Select an Object contained in this Bag. Selects an arbitrary element from this Bag; however, each distinct index is guaranteed to access an element at a distinct location in the Bag. Iterating over the Bag visits every uniquely-added element once, and every multiply-added element as many times as it was added. Repeated access to the same element at the same index is in constant time. Sequential access to all elements using an incrementing index is in near-constant time. Random access is in linear time.
Reimplemented from Collection. |
|
Remove an Object from this Bag. Removes a single Object, judged EQUAL under compare(), to the requested Object, if such an Object is found. Has no effect if no such Object is present in this Bag. The underlying hash table may be compacted if the last Object in a collision-set is removed.
Reimplemented from Unordered. Reimplemented in Relation. |
|
The cardinality of this Bag.
Reimplemented from Collection. |