org.zedlib
Class HashBag

java.lang.Object
  extended byjava.util.AbstractCollection
      extended byjava.util.AbstractSet
          extended byjava.util.HashSet
              extended byorg.zedlib.HashSet
                  extended byorg.zedlib.HashRel
                      extended byorg.zedlib.HashFun
                          extended byorg.zedlib.HashBag
All Implemented Interfaces:
Bag, Cloneable, Collection, Function, Relation, Serializable, Set, Set

public class HashBag
extends HashFun
implements Bag

Bags are collections of elements of X in which the number of times an element occurs is significant.

Author:
Brad Long
See Also:
Serialized Form

Constructor Summary
HashBag()
           
HashBag(Bag b)
          Constructs a bag from another given bag.
HashBag(Collection c)
          Constructs a bag by iterating over a given collection.
HashBag(Map m)
          Constructs a bag by iterating over a given map.
 
Method Summary
 boolean add(Baglet b)
          Adds a maplet to this bag.
 boolean add(Maplet m)
          Adds a maplet to this bag.
 boolean add(Object o)
          Adds an object to this bag.
 boolean add(Object x, int y)
          Adds an object to this bag with the specified number of occurrences.
 boolean containsMember(Object x)
          Determines whether an object is in the bag.
 boolean containsValue(int count)
          A convenience function for the alternative range().contains(new Integer(count)).
 int getCount(Object o)
          Counts the number of times an element appears in this bag.
protected  Set getInstance()
           
protected  Set getInstance(Set s)
           
 boolean isSubBagOf(Bag c)
          Determines whether this bag is a sub-bag of a given bag.
 Object put(Object x, int y)
          Adds an object to this bag with the specified number of occurrences.
 boolean remove(Object o)
          Removes an occurrence of the given object from this function.
 
Methods inherited from class org.zedlib.HashFun
containsKey, containsValue, getMaplet, getValue, put, put
 
Methods inherited from class org.zedlib.HashRel
add, addAll, addAll, buildRelation, composition, domain, domainAntiRestriction, domainRestriction, image, inverse, isFunction, isInjection, isReflexive, override, range, rangeAntiRestriction, rangeRestriction, toString, transitiveClosure
 
Methods inherited from class org.zedlib.HashSet
cartesianProduct, difference, difference, identity, intersection, intersection, isProperSubsetOf, isSubsetOf, isSupersetOf, union, union
 
Methods inherited from class java.util.HashSet
clear, clone, contains, isEmpty, iterator, size
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
containsAll, retainAll, toArray, toArray
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.zedlib.Function
containsKey, containsValue, getMaplet, put
 
Methods inherited from interface org.zedlib.Relation
add, addAll, composition, domain, domainAntiRestriction, domainRestriction, image, inverse, isFunction, isInjection, isReflexive, override, range, rangeAntiRestriction, rangeRestriction, transitiveClosure
 
Methods inherited from interface org.zedlib.Set
cartesianProduct, difference, identity, intersection, isSubsetOf, isSupersetOf, union
 
Methods inherited from interface java.util.Set
addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, removeAll, retainAll, size, toArray, toArray
 

Constructor Detail

HashBag

public HashBag()

HashBag

public HashBag(Collection c)
Constructs a bag by iterating over a given collection.


HashBag

public HashBag(Map m)
Constructs a bag by iterating over a given map.

Throws:
ClassCastException - if any value in the map is not an Integer

HashBag

public HashBag(Bag b)
Constructs a bag from another given bag.

Method Detail

getInstance

protected Set getInstance()
Overrides:
getInstance in class HashFun

getInstance

protected Set getInstance(Set s)
Overrides:
getInstance in class HashFun

getCount

public int getCount(Object o)
Counts the number of times an element appears in this bag.

Specified by:
getCount in interface Bag
Parameters:
o - the object for which to count occurrences
Returns:
the number of times the given key appears in the bag

remove

public boolean remove(Object o)
Removes an occurrence of the given object from this function.

Specified by:
remove in interface Set
Parameters:
o - the object to remove
Returns:
true if an object was removed

containsValue

public boolean containsValue(int count)
A convenience function for the alternative range().contains(new Integer(count)).

Specified by:
containsValue in interface Bag
Parameters:
count - any integer being the number of occurrences to check for
Returns:
true if at least one object exists in the bag occurrence times.

add

public boolean add(Object o)
Adds an object to this bag.

Specified by:
add in interface Set
Overrides:
add in class HashRel
Parameters:
o - the object to add
Returns:
true if the object was added.

add

public boolean add(Baglet b)
Adds a maplet to this bag.

Specified by:
add in interface Bag
Parameters:
b - the baglet to add
Returns:
true if the maplet was added or false if the maplet already exists or number of occurrences given is less then one.
Throws:
ClassCastException - if y in the maplet (x,y) is not an Integer
See Also:
Set.add(Object o)

add

public boolean add(Maplet m)
Adds a maplet to this bag.

Specified by:
add in interface Relation
Overrides:
add in class HashFun
Parameters:
m - the maplet to add
Returns:
true if the maplet was added or false if the maplet already exists or number of occurrences given is less then one.
Throws:
ClassCastException - if y in the maplet (x,y) is not an Integer

add

public boolean add(Object x,
                   int y)
Adds an object to this bag with the specified number of occurrences.

Specified by:
add in interface Bag
Parameters:
x - the object to add
y - the number of times the object should appear in the bag
Returns:
true if the maplet was added

put

public Object put(Object x,
                  int y)
Adds an object to this bag with the specified number of occurrences.

Specified by:
put in interface Bag
Parameters:
x - the object to add
y - the number of times the object should appear in the bag
Returns:
the Integer object that was replaced or null if x was not previously in the bag or occurrences is less then one.

containsMember

public boolean containsMember(Object x)
Determines whether an object is in the bag. The relationship B.containsMember(x) holds exactly if x appears in B a non-zero number of times.

Specified by:
containsMember in interface Bag
Parameters:
x - the object to check for existence
Returns:
true if the object is in the bag

isSubBagOf

public boolean isSubBagOf(Bag c)
Determines whether this bag is a sub-bag of a given bag. A bag B is a sub-bag of another bag C if each element occurs in B no more often than it occurs in C.

Specified by:
isSubBagOf in interface Bag
Parameters:
c - any bag
Returns:
true if this bag is a sub-bag of c


Copyright © 2006 Brad Long. All Rights Reserved.