head	1.7;
access;
symbols
	r1_0:1.7
	r0_11:1.7
	r0_10:1.7
	r0_9:1.7
	Eglin_alpha_1:1.7;
locks; strict;


1.7
date	92.09.16.17.18.24;	author jm;	state Exp;
branches;
next	1.6;

1.6
date	92.09.09.14.36.35;	author jm;	state Exp;
branches;
next	1.5;

1.5
date	92.09.08.14.37.44;	author jm;	state Exp;
branches;
next	1.4;

1.4
date	92.09.01.13.59.36;	author jm;	state Exp;
branches;
next	1.3;

1.3
date	92.08.28.10.01.36;	author jm;	state Exp;
branches;
next	1.2;

1.2
date	92.08.27.16.02.08;	author jm;	state Exp;
branches;
next	1.1;

1.1
date	92.08.26.17.04.21;	author jm;	state Exp;
branches;
next	;


desc
@New version of ValueSet.cc, now in C-Front style
@


1.7
log
@last version of .C -> moved to .c
@
text
@/*  $RCSfile: ValueSet.c,v $
    $Log:	ValueSet.c,v $
 * Revision 1.3  92/09/16  16:41:30  jm
 * added comments to remove_dup and count_dup functions.
 * changed NULL to 0.
 * 
 * Revision 1.2  92/09/16  14:47:23  jm
 * Included value_range function as an independent function
 * function contains() now returns 1 or 0
 * added keyword 'const' to function count_dup
 * added keyword 'const' to all member operators
 * added definition for operators '>' and '<'
 * added definition for function compare
 * 
 * Revision 1.1  92/09/16  14:46:28  jm
 * Initial revision
 * 
Revision 1.6  92/09/09  14:36:35  jm
cleaned up use of dynamic memory in operator.

Revision 1.5  92/09/08  14:37:44  jm
 the operators have been redefined to be class members in the case of
 valueset op element, and valueset op valueset. The combination
 element op valueset remains as a friend.
 the function make_set does not take parameters anymore. It operates on
 the internal value_array and size.
 function value_range has been changed from class member to independent. It
 will only work with integer arguments.
 predicate operators now return an int instead of a char, in order to be
 able to print the result.

Revision 1.4  92/09/01  13:59:36  jm
added friend functions for operators between elements and ValueSets.
Added a guard for the "bsearch" function
templatized ValueSet
changed the "-" operator to the "/" operator

Revision 1.3  92/08/28  10:01:36  jm
added RCS keywords "RCSfile" and "Log"

*/

#include <stream.h>
#include "ValueSet.h"

ValueSet<int> value_range(int v0, int v1) {
    ValueSet<int> vs;
    int sz = v1 - v0;
    
    if (sz < 0)
	return ValueSet<int>();
    if (sz == 0)
	return ValueSet<int>(v0);
    int *ret_array = new int[++sz];
    for (int i = 0; i < sz; i++)
	ret_array[i] = v0+i;
    return ValueSet<int>(ret_array, sz);
}

template <class ValueType>
int ValueSet<ValueType>::contains(ValueType e) const {
    
    if (this->size == 0) return 0;
	
    return ((int) bsearch((ValueType *)(&e), 
			 (ValueType *)this->value_array,
			 this->size, sizeof(ValueType),
			 this->value_type_compare) == 0 ? 0 : 1);
}    
    
template <class ValueType>
int ValueSetComparator<ValueType>::value_type_compare(
    const void *t1, const void *t2) {
    if (*(ValueType *)t1 > *(ValueType *)t2)
	return 1;
    
    if (*(ValueType *)t1 < *(ValueType *)t2)
	return -1;
    return 0;
}

template <class ValueType>
ValueSet<ValueType>::ValueSet(const ValueSet<ValueType>& vs) {
    this->size = vs.size;
    if (this->size == 0)
	this->value_array = 0;
    else {
	this->value_array = new ValueType[this->size];
	for (unsigned int i = 0; i < this->size; i++)
	    this->value_array[i] = vs.value_array[i];
    }
}

template <class ValueType>
ValueSet<ValueType>::ValueSet(ValueType v0) {
    this->value_array = new ValueType[this->size = 1];
    
    this->value_array[0] = v0;
}

template <class ValueType>
ValueSet<ValueType>::ValueSet(ValueType v0, ValueType v1) {
    
    this->value_array = new ValueType[this->size = 2];

    this->value_array[0] = v0;
    this->value_array[1] = v1;
    make_set();
}

template <class ValueType>
ValueSet<ValueType>::ValueSet(ValueType v0, ValueType v1, ValueType v2) {

    this->value_array = new ValueType[this->size = 3];
    
    this->value_array[0] = v0;
    this->value_array[1] = v1;
    this->value_array[2] = v2;
    make_set();
}

template <class ValueType>
ValueSet<ValueType>::ValueSet(ValueType v0, ValueType v1,ValueType v2, 
			      ValueType v3) {
    this->value_array = new ValueType[this->size = 4];
    
    this->value_array[0] = v0;
    this->value_array[1] = v1;
    this->value_array[2] = v2;
    this->value_array[3] = v3;
    make_set();

}

template <class ValueType>
ValueSet<ValueType>::ValueSet(ValueType v0, ValueType v1, ValueType v2, 
			      ValueType v3, ValueType v4) {
    this->value_array = new ValueType[this->size = 5];
    
    this->value_array[0] = v0;
    this->value_array[1] = v1;
    this->value_array[2] = v2;
    this->value_array[3] = v3;
    this->value_array[4] = v4;
    make_set();
}

template <class ValueType>
ValueSet<ValueType>::ValueSet(ValueType* const ar, unsigned int sz) {
    if (sz == 0) {
	this->size = 0;
	this->value_array = 0;
    }
    else {
	this->value_array = new ValueType[this->size = sz];
	for (unsigned int i=0; i < sz; i++)
	    this->value_array[i] = ar[i];
	make_set();
    }
}

// make_set: sorts the value_array and removes duplicates.

template <class ValueType>
void ValueSet<ValueType>::make_set() {
    if (this->size == 0)
	this->value_array = 0;
    else {
	qsort(this->value_array, this->size, sizeof(ValueType),
	      this->value_type_compare);
	remove_dup();
    }
}

// remove_dup is for the private use of make_set
// this->size must not be 0.

template <class ValueType>
void ValueSet<ValueType>::remove_dup() {
    
    ValueType *clean_array = new ValueType[this->size - count_dup()];
    
    unsigned int di = 0;
    
    for (unsigned int i=0; i < this->size - 1; i++)
	if (this->value_array[i] != this->value_array[i+1])
	    clean_array[di++] = this->value_array[i];
    clean_array[di++] = this->value_array[this->size - 1];
    delete [] this->value_array;
    this->value_array = clean_array;
    this->size = di;
}

// count_dup is for the private use of make_set

template <class ValueType>
int ValueSet<ValueType>::count_dup() const {
    int count = 0;
    for (unsigned int i=0; i < this->size - 1; i++)
	if (this->value_array[i] == this->value_array[i+1])
	    count++;
    return count;
}

template <class ValueType>
ValueSet<ValueType>& 
ValueSet<ValueType>::operator=  (const ValueSet<ValueType>& vs) {
    if (this == &vs) 
	return *this;

    // handle empty set possibility
    
    if (vs.size == 0) {
	delete[] this->value_array;  // OK even if value_array already 0
	this->value_array = 0; 
	this->size = 0;
	return *this;
    }
    
    // handle the typical case
    
    if (vs.size != this->size) {
	delete[] this->value_array;
	this->value_array = new ValueType[vs.size];
	this->size = vs.size;
    }
    
    for (unsigned int i = 0; i < this->size; i++)
	this->value_array[i] = vs.value_array[i];
    
    return *this;
}

template <class ValueType>
ValueSet<ValueType> 
ValueSet<ValueType>::operator| (const ValueSet<ValueType> &right) const {
    unsigned int li = 0, ri = 0, ci = 0;

    if (this == &right) 
	return ValueSet<ValueType>(*this);

    ValueType* temp = new ValueType[this->size + right.size];
    while ((li < this->size) && (ri < right.size))
	if (this->value_array[li] < right.value_array[ri])
	    temp[ci++] = this->value_array[li++];
	else {
	    temp[ci++] = right.value_array[ri];
	    if (this->value_array[li] == right.value_array[ri++])
		li++;
	}
    if (li == this->size)
	for (;ri < right.size;ri++)
	    temp[ci++] = right.value_array[ri];
    else
	for (;li < this->size;li++)
	    temp[ci++] = this->value_array[li];
    return ValueSet<ValueType>(temp, ci);
}

template <class ValueType>
ValueSet<ValueType> 
ValueSet<ValueType>::operator| (const ValueType &right) const {
    return *this | ValueSet<ValueType>(right);
}

template <class ValueType>
ValueSet<ValueType> 
operator| (const ValueType &left, const ValueSet<ValueType> &right) {
    return ValueSet<ValueType>(left) | right;
}

template <class ValueType>
ValueSet<ValueType> 
ValueSet<ValueType>::operator& (const ValueSet<ValueType> &right)  const {
    unsigned int li = 0, ri = 0, ci = 0;

    if ((this->empty()) || (right.empty()))
	return ValueSet<ValueType>();
    if ((this->max() < right.min()) || (this->min() > right.max()))
	return ValueSet<ValueType>();
    if (this == &right) 
	return ValueSet<ValueType>(*this);

    ValueType* temp = new ValueType[this->card() < right.card() 
				    ? this->size : right.size];

    while ((li < this->size) && (ri < right.size))
	if (this->value_array[li] < right.value_array[ri])
	    li++;
	else {
	    if (this->value_array[li] == right.value_array[ri]) {
		temp[ci++] = right.value_array[ri];
		li++;
	    }
	    ri++;
	}
    return ValueSet<ValueType>(temp, ci);
}

template <class ValueType>
ValueSet<ValueType> 
ValueSet<ValueType>::operator& (const ValueType &right) const {
    if (this->contains(right))
	return ValueSet<ValueType>(right);
    else
	return ValueSet<ValueType>();
}

template <class ValueType>
ValueSet<ValueType> 
operator& (const ValueType &left, const ValueSet<ValueType> &right) {
    if (right.contains(left))
	return ValueSet<ValueType>(left);
    else
	return ValueSet<ValueType>();
}

template <class ValueType>
ValueSet<ValueType> 
ValueSet<ValueType>::operator/ (const ValueSet<ValueType> &right) const {
    unsigned int li = 0, ri = 0, ci = 0;

    if (this->empty())
	return ValueSet<ValueType>();
    if (right.empty())
	return ValueSet<ValueType>(*this);
    if (this == &right) 
	return ValueSet<ValueType>();

    ValueType* temp = new ValueType[this->size];
    while ((li < this->size) && (ri < right.size))
	if (this->value_array[li] < right.value_array[ri])
	    temp[ci++] = this->value_array[li++];
	else {
	    if (this->value_array[li] == right.value_array[ri]) {
		li++;
	    }
	    ri++;
	}
    if (ri == right.size)
	for (;li<this->size;li++)
	    temp[ci++] = this->value_array[li];
    return ValueSet<ValueType>(temp, ci);
}

template <class ValueType>
ValueSet<ValueType> 
ValueSet<ValueType>::operator/ (const ValueType &right) const {
    return *this / ValueSet<ValueType>(right);
}

template <class ValueType>
ValueSet<ValueType> 
operator/ (const ValueType &left, const ValueSet<ValueType> &right) {
    if (right.contains(left))
	return ValueSet<ValueType>();
    else
	return ValueSet<ValueType>(left);
}

template <class ValueType>
ValueSet<ValueType> 
ValueSet<ValueType>::operator^ (const ValueSet<ValueType> &right) const {
    unsigned int li = 0, ri = 0, ci = 0;

    if (this->empty())
	return ValueSet<ValueType>(right);
    if (right.empty())
	return ValueSet<ValueType>(*this);
    if (this == &right) 
	return ValueSet<ValueType>();

    ValueType* temp = new ValueType[this->size + right.size];
    while ((li < this->size) && (ri < right.size)) 
	if (this->value_array[li] < right.value_array[ri])
	    temp[ci++] = this->value_array[li++];
	else
	    if (this->value_array[li] > right.value_array[ri])
		temp[ci++] = right.value_array[ri++];
	    else { 
		li++;
		ri++;
	    }
    if (ri == right.size)
	for (;li<this->size;li++)
	    temp[ci++] = this->value_array[li];
    if (li == this->size)
	for (;ri<right.size;ri++)
	    temp[ci++] = right.value_array[ri];
    return ValueSet<ValueType>(temp, ci);
}

template <class ValueType>
ValueSet<ValueType> 
ValueSet<ValueType>::operator^ (const ValueType &right) const {

    if (this->contains(right))
	return ValueSet<ValueType>(*this / right);
    else
	return ValueSet<ValueType>(*this | right);
}

template <class ValueType>
ValueSet<ValueType> 
operator^ (const ValueType &left, const ValueSet<ValueType> &right) {

    if (right.contains(left))
	return ValueSet<ValueType>(right / left);
    else
	return ValueSet<ValueType>(right | left);
}

template <class ValueType> 
int 
ValueSet<ValueType>::operator<= (const ValueSet<ValueType> &right) const {

    unsigned int li = 0, ri = 0;

    if (this->empty())
	return 1;
    if (right.empty())
	return 0;
    if (this == &right) return 1;
    while ((li < this->size) && (ri < right.size)) {
	if (this->value_array[li] < right.value_array[ri])
	    return 0;
	if (this->value_array[li] == right.value_array[ri++])
	    li++;
    }
    return 1;
}

template <class ValueType> 
int 
ValueSet<ValueType>::operator<= (const ValueType &right) const {

    if (this->size == 0) 
	return 1;
    if (this->size == 1)
	return this->max() == right;
    return 0;
}

template <class ValueType> 
int 
operator<= (const ValueType &left, const ValueSet<ValueType> &right){

    if (right.contains(left)) 
	return 1;
    return 0;
}

template <class ValueType>
int 
ValueSet<ValueType>::operator>= (const ValueSet<ValueType> &right) const {
    return right <= *this;
}

template <class ValueType>
int 
ValueSet<ValueType>::operator>= (const ValueType &right) const {
    return right <= *this;
}

template <class ValueType>
int 
operator>= (const ValueType &left, const ValueSet<ValueType> &right){
    return right <= left;
}

template <class ValueType> 
int 
ValueSet<ValueType>::operator< (const ValueSet<ValueType> &right) const {

    unsigned int li = 0, ri = 0;

    if (this->empty())
	return 1;
    if (right.empty())
	return 0;
    if (this == &right) return 0;
    while ((li < this->size) && (ri < right.size)) {
	if (this->value_array[li] < right.value_array[ri])
	    return 0;
	if (this->value_array[li] == right.value_array[ri++])
	    li++;
    }

    if (li == this->size)
	if (ri == right.size)
	    return 0;
	else
	    return 1;
    return 0;
}

template <class ValueType> 
int 
ValueSet<ValueType>::operator< (const ValueType &right) const {

    return (this->size == 0);
}

template <class ValueType> 
int 
operator< (const ValueType &left, const ValueSet<ValueType> &right){

    return (right.contains(left) && (right.size > 1));

}

template <class ValueType>
int 
ValueSet<ValueType>::operator> (const ValueSet<ValueType> &right) const {

    return (right < *this);

}

template <class ValueType>
int 
ValueSet<ValueType>::operator> (const ValueType &right) const {
    return (right < *this);
}

template <class ValueType>
int 
operator> (const ValueType &left, const ValueSet<ValueType> &right){
    return (right < left);
}

template <class ValueType> 
int 
ValueSet<ValueType>::operator== (const ValueSet<ValueType> &right) const {

    if (this->size != right.size)
	return 0;
    for (unsigned int i = 0; i < this->size; i++)
       if (this->value_array[i] != right.value_array[i])
	   return 0;
    return 1;
}

template <class ValueType> 
int 
ValueSet<ValueType>::operator== (const ValueType &right) const {

    if (this->size != 1)
	return 0;
    return this->max() == right;
}

template <class ValueType> 
int 
operator== (const ValueType &left, const ValueSet<ValueType> &right){

    if (right.size != 1)
	return 0;
    return right.max() == left;
}

template <class ValueType> 
int 
ValueSet<ValueType>::operator!= (const ValueSet<ValueType> &right) const {
    return !(*this == right);
}

template <class ValueType> 
int 
ValueSet<ValueType>::operator!= (const ValueType &right) const {
    return !(*this == right);
}

template <class ValueType> 
int 
operator!= (const ValueType &left, const ValueSet<ValueType> &right){
    return !(left == right);
}

template <class ValueType>
ValueSet<ValueType>::LatticeRelation
compare(const ValueSet<ValueType> &lhs, const ValueSet<ValueType> &rhs) {

   if (lhs == rhs)
       return ValueSet<ValueType>::EQUAL;
   if (lhs < rhs)
       return ValueSet<ValueType>::P_SUBSET;
   if (lhs > rhs)
       return ValueSet<ValueType>::P_SUPERSET;
   return ValueSet<ValueType>::NO_REL;

}
@


1.6
log
@cleaned up use of dynamic memory in operator.
@
text
@d1 20
a20 2
/*  $RCSfile: ValueSet.C,v $
    $Log:	ValueSet.C,v $
d46 14
d65 1
a65 1
    return (int) bsearch((ValueType *)(&e), 
d68 1
a68 1
			 this->value_type_compare);
d86 1
a86 1
	this->value_array = NULL;
d152 1
a152 1
	this->value_array = NULL;
d175 3
d194 2
d197 1
a197 1
int ValueSet<ValueType>::count_dup() {
a205 14
ostream& operator<< (ostream& stream, ValueSet<ValueType>& vs) {
    int last_index = vs.size-1;
    stream << "<";
    if (vs.size > 0) {
	for (int i=0; i < last_index; i++)
	    stream << vs.value_array[i] << " ";
	if (vs.size > 0)
	    stream << vs.value_array[last_index];
    }
    stream << ">" << '\n';
    return stream;
}

template <class ValueType>
d214 2
a215 2
	delete[] this->value_array;  // OK even if value_array already NULL
	this->value_array = NULL; 
d236 1
a236 1
ValueSet<ValueType>::operator| (const ValueSet<ValueType> &right) {
d262 1
a262 1
ValueSet<ValueType>::operator| (const ValueType &right) {
d274 1
a274 1
ValueSet<ValueType>::operator& (const ValueSet<ValueType> &right) {
d302 1
a302 1
ValueSet<ValueType>::operator& (const ValueType &right) {
d320 1
a320 1
ValueSet<ValueType>::operator/ (const ValueSet<ValueType> &right) {
d348 1
a348 1
ValueSet<ValueType>::operator/ (const ValueType &right) {
d363 1
a363 1
ValueSet<ValueType>::operator^ (const ValueSet<ValueType> &right) {
d395 1
a395 1
ValueSet<ValueType>::operator^ (const ValueType &right) {
d415 1
a415 1
ValueSet<ValueType>::operator<= (const ValueSet<ValueType> &right){
d435 1
a435 1
ValueSet<ValueType>::operator<= (const ValueType &right){
d455 1
a455 1
ValueSet<ValueType>::operator>= (const ValueSet<ValueType> &right){
d461 1
a461 1
ValueSet<ValueType>::operator>= (const ValueType &right){
d473 1
a473 1
ValueSet<ValueType>::operator== (const ValueSet<ValueType> &right){
d475 61
d546 1
a546 1
ValueSet<ValueType>::operator== (const ValueType &right){
d564 1
a564 1
ValueSet<ValueType>::operator!= (const ValueSet<ValueType> &right){
d570 1
a570 1
ValueSet<ValueType>::operator!= (const ValueType &right){
d580 3
d584 7
d592 1
a592 2


@


1.5
log
@ the operators have been redefined to be class members in the case of
 valueset op element, and valueset op valueset. The combination
 element op valueset remains as a friend.
 the function make_set does not take parameters anymore. It operates on
 the internal value_array and size.
 function value_range has been changed from class member to independent. It
 will only work with integer arguments.
 predicate operators now return an int instead of a char, in order to be
 able to print the result.
@
text
@d3 11
a133 1
    
a184 1

a213 2
    ValueType* temp = new ValueType[this->size + right.size];

d216 4
a219 1
    if (this == &right) return ValueSet<ValueType>(*this);
a251 3
    ValueType* temp = new ValueType[this->card() < right.card() 
				    ? this->size : right.size];

d258 6
a263 1
    if (this == &right) return ValueSet<ValueType>(*this);
a279 1

a288 1

a297 2
    ValueType* temp = new ValueType[this->size];

d304 4
a307 1
    if (this == &right) return ValueSet<ValueType>();
a331 1

a340 2
    ValueType* temp = new ValueType[this->size + right.size];

d347 4
a350 1
    if (this == &right) return ValueSet<ValueType>();
a401 2
	while (this->value_array[li] > right.value_array[ri])
	    ri++;
d404 2
a405 2
	li++;
	ri++;
@


1.4
log
@added friend functions for operators between elements and ValueSets.
Added a guard for the "bsearch" function
templatized ValueSet
changed the "-" operator to the "/" operator
@
text
@d1 1
a1 1
/*  $RCSfile$
d3 6
d15 1
a15 1
#include "ValueSet.H"
d18 3
a20 5
char ValueSet<ValueType>::contains(ValueType e) const {
	ValueType *ptr;

	if (this->size == 0)
	    return 0;
d22 5
a26 7
	ptr = (ValueType *)bsearch((ValueType *)(&e), 
				   (ValueType *)this->value_array,
				   this->size, sizeof(ValueType),
				   this->value_type_compare);
	
	return (ptr == NULL ? 0 : 1);
    }    
a59 1
    ValueType *ret_array = new ValueType[this->size = 2];
d61 5
a65 5
    ret_array[0] = v0;
    ret_array[1] = v1;
    
    this->value_array = make_set(ret_array, this->size);
    delete [] ret_array;
d70 2
a71 1
    ValueType *ret_array = new ValueType[this->size = 3];
d73 4
a76 6
    ret_array[0] = v0;
    ret_array[1] = v1;
    ret_array[2] = v2;
    
    this->value_array = make_set(ret_array, this->size);
    delete [] ret_array;
d82 1
a82 1
    ValueType *ret_array = new ValueType[this->size = 4];
d84 6
a89 7
    ret_array[0] = v0;
    ret_array[1] = v1;
    ret_array[2] = v2;
    ret_array[3] = v3;
    
    this->value_array = make_set(ret_array, this->size);
    delete [] ret_array;
d95 1
a95 1
    ValueType *ret_array = new ValueType[this->size = 5];
d97 6
a102 8
    ret_array[0] = v0;
    ret_array[1] = v1;
    ret_array[2] = v2;
    ret_array[3] = v3;
    ret_array[4] = v4;
    
    this->value_array = make_set(ret_array, this->size);
    delete [] ret_array;
d106 11
a116 3
ValueSet<ValueType>::ValueSet(const ValueType* ar, unsigned int sz) {
    this->value_array = make_set(ar, sz);
    this->size = sz;
d119 2
d122 1
a122 2
ValueType* ValueSet<ValueType>::make_set(const ValueType* a, unsigned int &sz){
    ValueType *ar1 = new ValueType[sz];
d124 7
a130 8
    if (sz == 0)
	return NULL;
    ar1 = new ValueType[sz]; 
    for (unsigned int i=0; i<sz; i++)
	ar1[i] = a[i];
    qsort(ar1, sz, sizeof(ValueType), value_type_compare);
    remove_dup(ar1,sz);
    return ar1;
d134 1
a134 1
void ValueSet<ValueType>::remove_dup(ValueType* &a, unsigned int &sz) {
d136 1
a136 1
    ValueType *ret_array = new ValueType[sz-count_dup(a,sz)];
d140 7
a146 7
    for (unsigned int i=0; i < sz-1; i++)
	if (a[i] != a[i+1])
	    ret_array[di++] = a[i];
    ret_array[di++] = a[sz-1];
    delete [] a;
    a = ret_array;
    sz = di;
d150 1
a150 1
int ValueSet<ValueType>::count_dup(ValueType* a, unsigned int sz) {
d152 2
a153 2
    for (unsigned int i=0; i < sz-1; i++)
	if (a[i] == a[i+1])
d175 1
d204 2
a205 2
operator| (const ValueSet<ValueType> &left, const ValueSet<ValueType> &right) {
    ValueType* temp = new ValueType[left.size + right.size];
d209 4
a212 4
    if (&left == &right) return ValueSet<ValueType>(left);
    while ((li < left.size) && (ri < right.size))
	if (left.value_array[li] < right.value_array[ri])
	    temp[ci++] = left.value_array[li++];
d215 1
a215 1
	    if (left.value_array[li] == right.value_array[ri++])
d218 1
a218 1
    if (li == left.size)
d222 2
a223 2
	for (;li < left.size;li++)
	    temp[ci++] = left.value_array[li];
d229 2
a230 21
operator| (const ValueType &left, const ValueSet<ValueType> &right) {
    ValueType* temp = new ValueType[1 + right.size];

    unsigned int li = 0, ri = 0, ci = 0;

    while ((li < 1) && (ri < right.size))
	if (left < right.value_array[ri]) {
	    temp[ci++] = left;
	    li++;
	}
	else {
	    temp[ci++] = right.value_array[ri];
	    if (left == right.value_array[ri++])
		li++;
	}
    if (li == 1)
	for (;ri < right.size;ri++)
	    temp[ci++] = right.value_array[ri];
    else
	temp[ci++] = left;
    return ValueSet<ValueType>(temp, ci);
d235 2
a236 20
operator| (const ValueSet<ValueType> &left, const ValueType &right) {
    ValueType* temp = new ValueType[left.size + 1];
    
    unsigned int li = 0, ri = 0, ci = 0;

    while ((li < left.size) && (ri < 1))
	if (left.value_array[li] < right)
	    temp[ci++] = left.value_array[li++];
	else {
	    temp[ci++] = right;
	    ri++;
	    if (left.value_array[li] == right)
		li++;
	}
    if (ri != 1)
	    temp[ci++] = right;
    else
	for (;li < left.size;li++)
	    temp[ci++] = left.value_array[li];
    return ValueSet<ValueType>(temp, ci);
d241 3
a243 3
operator& (const ValueSet<ValueType> &left, const ValueSet<ValueType> &right) {
    ValueType* temp = new ValueType[left.card() < right.card() 
				    ? left.size : right.size];
d247 1
a247 1
    if ((left.empty()) || (right.empty()))
d249 1
a249 1
    if ((left.max() < right.min()) || (left.min() > right.max()))
d251 3
a253 3
    if (&left == &right) return ValueSet<ValueType>(left);
    while ((li < left.size) && (ri < right.size))
	if (left.value_array[li] < right.value_array[ri])
d256 1
a256 1
	    if (left.value_array[li] == right.value_array[ri]) {
d267 1
a267 1
operator& (const ValueType &left, const ValueSet<ValueType> &right) {
d269 2
a270 2
    if (right.contains(left))
	return ValueSet<ValueType>(left);
d277 1
a277 1
operator& (const ValueSet<ValueType> &left, const ValueType &right) {
d279 2
a280 2
    if (left.contains(right))
	return ValueSet<ValueType>(right);
d287 2
a288 2
operator/ (const ValueSet<ValueType> &left, const ValueSet<ValueType> &right) {
    ValueType* temp = new ValueType[left.size];
d292 1
a292 1
    if (left.empty())
d295 5
a299 5
	return ValueSet<ValueType>(left);
    if (&left == &right) return ValueSet<ValueType>();
    while ((li < left.size) && (ri < right.size))
	if (left.value_array[li] < right.value_array[ri])
	    temp[ci++] = left.value_array[li++];
d301 1
a301 1
	    if (left.value_array[li] == right.value_array[ri]) {
d307 2
a308 2
	for (;li<left.size;li++)
	    temp[ci++] = left.value_array[li];
d314 6
d330 2
a331 1
operator/ (const ValueSet<ValueType> &left, const ValueType &right) {
a332 23
    if (!left.contains(right))
	return ValueSet<ValueType>(left);

    ValueType* temp = new ValueType[left.size-1];

    unsigned int li = 0, ci = 0;

    if (left.empty())
	return ValueSet<ValueType>();
    while (li < left.size)
	if ((left.value_array[li] < right) || (left.value_array[li] > right))
	    temp[ci++] = left.value_array[li++];
	else
	    li++;
    
    return ValueSet<ValueType>(temp, ci);
}

template <class ValueType>
ValueSet<ValueType> 
operator^ (const ValueSet<ValueType> &left, const ValueSet<ValueType> &right) {
    ValueType* temp = new ValueType[left.size + right.size];

d335 1
a335 1
    if (left.empty())
d338 5
a342 5
	return ValueSet<ValueType>(left);
    if (&left == &right) return ValueSet<ValueType>();
    while ((li < left.size) && (ri < right.size)) 
	if (left.value_array[li] < right.value_array[ri])
	    temp[ci++] = left.value_array[li++];
d344 1
a344 1
	    if (left.value_array[li] > right.value_array[ri])
d351 3
a353 3
	for (;li<left.size;li++)
	    temp[ci++] = left.value_array[li];
    if (li == left.size)
d361 1
a361 1
operator^ (const ValueType &left, const ValueSet<ValueType> &right) {
d363 2
a364 2
    if (right.contains(left))
	return ValueSet<ValueType>(right/left);
d366 1
a366 1
	return ValueSet<ValueType>(right|left);
d371 1
a371 1
operator^ (const ValueSet<ValueType> &left, const ValueType &right) {
d373 2
a374 2
    if (left.contains(right))
	return ValueSet<ValueType>(left/right);
d376 1
a376 1
	return ValueSet<ValueType>(left|right);
d380 2
a381 2
char 
operator<= (const ValueSet<ValueType> &left, const ValueSet<ValueType> &right){
d385 1
a385 1
    if (left.empty())
d389 3
a391 3
    if (&left == &right) return 1;
    while ((li < left.size) && (ri < right.size)) {
	while (left.value_array[li] > right.value_array[ri])
d393 1
a393 1
	if (left.value_array[li] < right.value_array[ri])
d402 2
a403 2
char 
operator<= (const ValueType &left, const ValueSet<ValueType> &right){
d405 1
a405 1
    if (right.contains(left)) 
d407 2
d413 2
a414 2
char 
operator<= (const ValueSet<ValueType> &left, const ValueType &right){
d416 1
a416 1
    if (left.size == 0) 
a417 2
    if (left.size == 1)
	return (left.max() == right ? 1 : 0);
d422 3
a424 19
char 
operator>= (const ValueSet<ValueType> &left, const ValueSet<ValueType> &right){

    unsigned int li = 0, ri = 0;

    if (right.empty())
	return 1;
    if (left.empty())
	return 0;
    if (&left == &right) return 1;
    while ((ri < right.size) && (li < left.size)) {
	while (right.value_array[ri] > left.value_array[li])
	    li++;
	if (right.value_array[ri] < left.value_array[li])
	    return 0;
	li++;
	ri++;
    }
    return 1;
d428 3
a430 8
char 
operator>= (const ValueType &left, const ValueSet<ValueType> &right){

    if (right.size == 0) 
	return 1;
    if (right.size == 1)
	return (right.max() == left ? 1 : 0);
    return 0;
d434 3
a436 6
char 
operator>= (const ValueSet<ValueType> &left, const ValueType &right){

    if (left.contains(right)) 
	return 1;
    return 0;
d440 2
a441 2
char 
operator== (const ValueSet<ValueType> &left, const ValueSet<ValueType> &right){
d443 1
a443 1
    if (left.size != right.size)
d445 2
a446 2
    for (unsigned int i = 0; i < left.size; i++)
       if (left.value_array[i] != right.value_array[i])
d452 2
a453 2
char 
operator== (const ValueType &left, const ValueSet<ValueType> &right){
d455 1
a455 1
    if (right.size != 1)
d457 1
a457 1
    return (right.max() == left ? 1 : 0);
d461 2
a462 2
char 
operator== (const ValueSet<ValueType> &left, const ValueType &right){
d464 1
a464 1
    if (left.size != 1)
d466 1
a466 1
    return (left.max() == right ? 1 : 0);
d470 3
a472 3
char 
operator!= (const ValueSet<ValueType> &left, const ValueSet<ValueType> &right){
    return !(left == right);
d476 3
a478 3
char 
operator!= (const ValueType &left, const ValueSet<ValueType> &right){
    return !(left == right);
d482 2
a483 2
char 
operator!= (const ValueSet<ValueType> &left, const ValueType &right){
a486 16
template <class ValueType>
ValueSet<ValueType> value_range(ValueType v0, ValueType v1) {
    ValueSet<ValueType> vs;
    int sz = (int)v1 - (int)v0;
    
    if (sz < 0)
	return ValueSet<ValueType>();
    if (sz == 0)
	return ValueSet<ValueType>(v0);
    ValueType *ret_array = new ValueType[++sz];
    for (int i = 0; i < sz; i++)
	ret_array[i] = v0+i;
    vs.value_array = ret_array;
    vs.size = sz;
    return vs;
}
@


1.3
log
@added RCS keywords "RCS" and "Log"
@
text
@d1 5
a5 2
/*  $RCS$
    $Log$
a7 1

d9 1
a9 1
#include "ValueSet.h"
d11 17
a27 1
int ValueSetComparator::value_type_compare(
d31 1
d37 2
a38 1
ValueSet::ValueSet(const ValueSet& vs) {
d49 2
a50 2

ValueSet::ValueSet(ValueType v0) {
d52 1
a52 1

d56 2
a57 2

ValueSet::ValueSet(ValueType v0, ValueType v1) {
d59 1
a59 1

d67 2
a68 2

ValueSet::ValueSet(ValueType v0, ValueType v1, ValueType v2) {
d70 1
a70 1

d79 3
a81 2

ValueSet::ValueSet(ValueType v0, ValueType v1, ValueType v2, ValueType v3) {
d83 1
a83 1

d93 3
a95 3

ValueSet::ValueSet(ValueType v0, ValueType v1, ValueType v2, ValueType v3, 
                   ValueType v4) {
d97 1
a97 1

d108 2
a109 2

ValueSet::ValueSet(const ValueType* ar, unsigned int sz) {
d114 2
a115 2

ValueType* ValueSet::make_set(const ValueType* a, unsigned int &sz){
a119 1
    
a120 2
    
    
a122 1
    
a124 1
    
d128 3
a130 2
void ValueSet::remove_dup(ValueType* &a, unsigned int &sz) {

a137 1
    
a138 1
    
d144 2
a145 1
int ValueSet::count_dup(ValueType* a, unsigned int sz) {
d153 2
a154 1
ostream& operator << (ostream& stream, ValueSet& vs) {
d167 3
a169 1
ValueSet& ValueSet::operator=(const ValueSet& vs) {
d174 1
a174 1

a175 1

d177 1
a177 1
	this->value_array = NULL;
d181 1
a181 1

d183 1
a183 1

d189 1
a189 1

d196 3
a198 1
ValueSet operator| (const ValueSet &left, const ValueSet &right) {
d203 1
a203 1
    if (&left == &right) return ValueSet(left);
a211 1

d218 2
d221 23
a243 1
    return ValueSet(temp, ci);
d246 27
a272 1
ValueSet operator& (const ValueSet &left, const ValueSet &right) {
d279 1
a279 2
	return ValueSet();

d281 2
a282 4
	return ValueSet();

    if (&left == &right) return ValueSet(left);

d293 2
d296 8
a303 1
    return ValueSet(temp, ci);
d306 13
a318 1
ValueSet operator/ (const ValueSet &left, const ValueSet &right) {
d324 1
a324 1
	return ValueSet();
d326 2
a327 4
	return ValueSet(left);

    if (&left == &right) return ValueSet();

a336 1

d340 2
d343 8
a350 1
    return ValueSet(temp, ci);
d353 25
a377 1
ValueSet operator^ (const ValueSet &left, const ValueSet &right) {
d383 1
a383 2
	return ValueSet(right);

d385 2
a386 4
	return ValueSet(left);

    if (&left == &right) return ValueSet();

a396 1

a399 1

d403 2
d406 8
a413 1
    return ValueSet(temp, ci);
d416 3
a418 1
char operator<= (const ValueSet &left, const ValueSet &right){
d420 10
a433 1

a435 1

a436 1

d448 3
a450 1
char operator>= (const ValueSet &left, const ValueSet &right){
d452 20
a475 1

a477 1

a478 1

d490 3
a492 1
char operator== (const ValueSet &left, const ValueSet &right){
d494 20
a515 1

a518 1

d522 21
a542 1
char operator!= (const ValueSet &left, const ValueSet &right){
d546 15
a560 2
ValueSet value_range(ValueType v0, ValueType v1) {
    ValueSet vs;
d562 1
a562 1

d564 1
a564 1
	return ValueSet();
d566 1
a566 2
	return ValueSet(v0);
	    
a567 1

a569 1

a571 1

@


1.2
log
@ changed <iostream.h> to <stream.h>. It is working with typedef, and
>> is ready to be templatized.
@
text
@d1 5
d231 1
a231 1
ValueSet operator- (const ValueSet &left, const ValueSet &right) {
@


1.1
log
@Initial revision
@
text
@d1 1
d4 8
d354 1
a354 1
    int sz = v1-v0;
@
