//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// PLEASE DO NOT EDIT WITHOUT THE EXPLICIT CONSENT OF THE AUTHOR! \\
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
using System;
using System.Text;
using System.Collections;
using Ilog.Language.Util;
namespace Ilog.Language.Util
{
/**
* This class is a generic data type for sets of objects. Instances of
* this class denote subsets of a fixed reference set - an ArrayList
* of Objects - called the base structure. All sets operations
* are implemented efficiently using an underlying bitset representation.
* In effect, using this class alleviates the need to deal directly with the
* represention and always manipulate sets as implicit collections of
* objects.
*
* NB: There are some important caveats to be aware of when using
* SetOf:
*
* - this class will work correctly assuming that no element
* is ever removed from the base structure;
*
- for efficiency reasons, no check is made that the base
* structure is indeed a set (i.e., contains no duplicates);
*
*
*
* @version Last modified on Fri May 20 20:02:17 2005 by hak
* @author Hassan Aït-Kaci
* @copyright © 2000 ILOG, S.A.
*/
public class SetOf : IEnumerable
{
/**
* The base structure comprising the collection of all elements
* that may belong to this set.
*/
private ArrayList elements;
/**
* This set's underlying representation as a bit word.
*/
private BitSet carrier;
/**
* Constructs a new (empty) set with the specified array.
* NB: the concrete list structure used is an ArrayList.
*
* @param elements base structure as an array
*/
public SetOf (Object[] elements)
{
this.elements = new ArrayList(elements.Length);
for (int i=0; iSize.
*/
public int Count
{
get
{
return carrier.Cardinality;
}
}
/**
* This denotes the number of elements in this set.
* Identical to Count.
*/
public int Size
{
get
{
return carrier.Cardinality;
}
}
/**
* This is a set representing this set's reference collection; that is,
* a set containing all the base structure's elements.
*/
public SetOf Top
{
get
{
if (FULL_SET==null || FULL_SET.elements != elements)
{
FULL_SET = new SetOf(elements);
for (int i=0; inull if empty.
*/
public Object FirstElement ()
{
int i = FirstIndex();
return (i<0) ? null : elements[i];
}
/**
* NB: The following set operations assume that all
* operands refer to the same base structure.
*/
/**
* Modifies this set to the union of this set and the specified set.
*
* @param other a set
* @return this set
*/
public SetOf Union (SetOf other)
{
carrier.Or(other.carrier);
return this;
}
/**
* Returns a new set equal to the union of two sets without modifying
* either one.
*
* @param one a set
* @param two a set
* @return a new set which is the union of one and two
*/
public static SetOf Union (SetOf one, SetOf two)
{
SetOf newset = new SetOf(one);
newset.carrier.Or(two.carrier);
return newset;
}
/**
* Returns a new set equal to the union of two sets without modifying
* either one.
*
* @param one a set
* @param two a set
* @return a new set which is the union of one and two
*/
public static SetOf operator + (SetOf one, SetOf two)
{
SetOf newset = new SetOf(one);
newset.carrier.Or(two.carrier);
return newset;
}
/**
* Modifies this set to the intersection of this set and the specified set.
*
* @param other a set
* @return this set
*/
public SetOf Intersection (SetOf other)
{
carrier.And(other.carrier);
return this;
}
/**
* Returns a new set equal to the intersection of two sets without
* modifying either one.
*
* @param one a set
* @param two a set
* @return a new set which is the intersection of one and two
*/
public static SetOf Intersection (SetOf one, SetOf two)
{
SetOf newset = new SetOf(one);
newset.carrier.And(two.carrier);
return newset;
}
/**
* Returns a new set equal to the intersection of two sets without
* modifying either one.
*
* @param one a set
* @param two a set
* @return a new set which is the intersection of one and two
*/
public static SetOf operator & (SetOf one, SetOf two)
{
SetOf newset = new SetOf(one);
newset.carrier.And(two.carrier);
return newset;
}
/**
* Modifies this set to the symmetric difference of this set and the
* specified set.
*
* @param other a set
* @return this set
*/
public SetOf Exclusion (SetOf other)
{
carrier.Xor(other.carrier);
return this;
}
/**
* Returns a new set equal to the symmetric difference of two sets
* without modifying either one.
*
* @param one a set
* @param two a set
* @return a new set which is the symmetric difference of one and two
*/
public static SetOf Exclusion (SetOf one, SetOf two)
{
SetOf newset = new SetOf(one);
newset.carrier.Xor(two.carrier);
return newset;
}
/**
* Returns a new set equal to the symmetric difference of two sets
* without modifying either one.
*
* @param one a set
* @param two a set
* @return a new set which is the symmetric difference of one and two
*/
public static SetOf operator ^ (SetOf one, SetOf two)
{
SetOf newset = new SetOf(one);
newset.carrier.Xor(two.carrier);
return newset;
}
/**
* Modifies this set to the set difference of this set and the
* specified set.
*
* @param other a set
* @return this set
*/
public SetOf Minus (SetOf other)
{
carrier.AndNot(other.carrier);
return this;
}
/**
* Returns a new set equal to the set difference of two sets without
* modifying either one.
*
* @param one a set
* @param two a set
* @return a new set which is the difference of one and two
*/
public static SetOf Minus (SetOf one, SetOf two)
{
SetOf newset = new SetOf(one);
newset.carrier.AndNot(two.carrier);
return newset;
}
/**
* Returns a new set equal to the set difference of two sets without
* modifying either one.
*
* @param one a set
* @param two a set
* @return a new set which is the difference of one and two
*/
public static SetOf operator - (SetOf one, SetOf two)
{
SetOf newset = new SetOf(one);
newset.carrier.AndNot(two.carrier);
return newset;
}
/**
* Modifies this set to its complement (relative to the full set of
* elements).
*
* @return this set
*/
public SetOf Not ()
{
carrier.Xor(Top.carrier);
return this;
}
/**
* Returns a new set equal to the complement (relative to the full
* set of elements) of the specified set without modifying it.
*
* @param s a set
*/
public static SetOf Not (SetOf s)
{
SetOf newset = new SetOf(s);
newset.carrier.Xor(s.Top.carrier);
return newset;
}
/**
* Returns a new set equal to the complement (relative to the full
* set of elements) of the specified set without modifying it.
*
* @param s a set
*/
public static SetOf operator - (SetOf s)
{
SetOf newset = new SetOf(s);
newset.carrier.Xor(s.Top.carrier);
return newset;
}
/**
* Returns true iff the specified index is that of an element of this set.
*
* @param index the index
*/
public bool ContainsIndex (int index)
{
return carrier.Get(index);
}
/**
* Returns true iff the specified object is an element of this set.
*
* @param elt the object
*/
public bool Contains (Object elt)
{
return carrier.Get(IndexOf(elt));
}
/**
* Returns true iff this set is included in or equal to the
* specified set.
*
* @param other a set
*/
public bool IsSubsetOf (SetOf other)
{
return carrier.IsSubsetOf(other.carrier);
}
/**
* Returns true iff set one is included in or equal to set two.
*
* @param one a set
* @param two a set
*/
public static bool operator <= (SetOf one, SetOf two)
{
return one.carrier.IsSubsetOf(two.carrier);
}
/**
* Returns true iff set two is included in or equal to set one.
*
* @param one a set
* @param two a set
*/
public static bool operator >= (SetOf one, SetOf two)
{
return two.carrier.IsSubsetOf(one.carrier);
}
/**
* Returns true iff this set is strictly included in the
* specified set.
*
* @param other a set
*/
public bool isStrictSubsetOf (SetOf other)
{
return carrier.IsStrictSubsetOf(other.carrier);
}
/**
* Returns true iff set one is strictly included in set two.
*
* @param one a set
* @param two a set
*/
public static bool operator < (SetOf one, SetOf two)
{
return one.carrier.IsStrictSubsetOf(two.carrier);
}
/**
* Returns true iff set two is strictly included in set one.
*
* @param one a set
* @param two a set
*/
public static bool operator > (SetOf one, SetOf two)
{
return two.carrier.IsStrictSubsetOf(one.carrier);
}
/**
* Returns true iff this set is equal to the specified set.
*
* @param other a set
*/
public bool IsEqualTo (SetOf other)
{
return carrier.Equals(other.carrier);
}
/**
* Returns true iff the specified object is a set equal to this.
*
* @param other an object
*/
public override bool Equals (Object other)
{
if (other is SetOf)
return carrier.Equals(((SetOf)other).carrier);
return false;
}
/**
* Returns a hashcode for this set.
*/
public override int GetHashCode ()
{
return carrier.GetHashCode();
}
/**
* Returns a string representation of this set as a comma-separated
* elements of elements between curly braces.
*/
public override String ToString ()
{
StringBuilder buffer = new StringBuilder(3*Count+2).Append('{');
String separator = "";
foreach (int i in carrier)
{
buffer.Append(separator);
separator = ", ";
buffer.Append(elements[i]);
}
return buffer.Append('}').ToString();
}
/**
* Returns a string representation of this set as a bit string of 0's and 1's.
*/
public String ToBitString ()
{
return ToBitString('0','1');
}
/**
* Returns a string representation of this set as a bit string of two
* specified characters standing for "absent" and "present" respectively.
*
* @param out a character
* @param in a character
*/
public String ToBitString (char off, char on)
{
StringBuilder s = new StringBuilder(elements.Count);
for (int i=0; i
*/
public IEnumerator GetEnumerator ()
{
foreach (int i in carrier)
yield return elements[i];
}
/**
* Returns an enumeration-like object over the base structure's indices for
* the elements of this set.
*/
public IEnumerator GetIndexEnumerator ()
{
return carrier.GetEnumerator();
}
}
}