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


1.8
date	92.08.28.10.03.56;	author jm;	state Exp;
branches;
next	1.7;

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

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

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

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

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

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

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


desc
@ValueSet implements the new type for value sets and also includes the
most common operations on value sets
@


1.8
log
@added RCS keywords "RCS" and "Log"
@
text
@/* $RCS$
   $Log$
*/


#include "ValueSet.h"


ValueSet::ValueSet(const ValueSet& vs) {
    this->size = vs.size;
    if (this->size == 0)
	this->value_array = NULL;
    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];
    }
}


ValueSet::ValueSet(ValueType v0) {
    this->value_array = new ValueType[this->size = 1];

    this->value_array[0] = v0;
}


ValueSet::ValueSet(ValueType v0, ValueType v1) {
    ValueType *ret_array = new ValueType[this->size = 2];

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


ValueSet::ValueSet(ValueType v0, ValueType v1, ValueType v2) {
    ValueType *ret_array = new ValueType[this->size = 3];

    ret_array[0] = v0;
    ret_array[1] = v1;
    ret_array[2] = v2;
    
    this->value_array = make_set(ret_array, this->size);
    delete [] ret_array;
}


ValueSet::ValueSet(ValueType v0, ValueType v1, ValueType v2, ValueType v3) {
    ValueType *ret_array = new ValueType[this->size = 4];

    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;
}


ValueSet::ValueSet(ValueType v0, ValueType v1, ValueType v2, ValueType v3, 
                   ValueType v4) {
    ValueType *ret_array = new ValueType[this->size = 5];

    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;
}


ValueSet::ValueSet(const ValueType* ar, unsigned int sz) {
    this->value_array = make_set(ar, sz);
    this->size = sz;
}


ValueType* ValueSet::make_set(const ValueType* a, unsigned int &sz){
    ValueType *ar1 = new ValueType[sz];
    
    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;
}

void ValueSet::remove_dup(ValueType* &a, unsigned int &sz) {

    ValueType *ret_array = new ValueType[sz-count_dup(a,sz)];
    
    unsigned int di = 0;
    
    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;
}

int ValueSet::count_dup(ValueType* a, unsigned int sz) {
    int count = 0;
    for (unsigned int i=0; i < sz-1; i++)
	if (a[i] == a[i+1])
	    count++;
    return count;
}

ostream& operator << (ostream& stream, ValueSet& 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;
}

ValueSet& ValueSet::operator=(const ValueSet& vs) {
    if (this == &vs) 
	return *this;

    // handle empty set possibility

    if (vs.size == 0) {

	delete[] this->value_array;  // OK even if value_array already NULL
	this->value_array = NULL;
	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;
}

ValueSet operator| (const ValueSet &left, const ValueSet &right) {
    ValueType* temp = new ValueType[left.size + right.size];

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

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

    if (li == left.size)
	for (;ri < right.size;ri++)
	    temp[ci++] = right.value_array[ri];
    else
	for (;li < left.size;li++)
	    temp[ci++] = left.value_array[li];

    return ValueSet(temp, ci);
}

ValueSet operator& (const ValueSet &left, const ValueSet &right) {
    ValueType* temp = new ValueType[left.card() < right.card() 
				    ? left.size : right.size];

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

    if ((left.empty()) || (right.empty()))
	return ValueSet();

    if ((left.max() < right.min()) || (left.min() > right.max()))
	return ValueSet();

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

    while ((li < left.size) && (ri < right.size))
	if (left.value_array[li] < right.value_array[ri])
	    li++;
	else {
	    if (left.value_array[li] == right.value_array[ri]) {
		temp[ci++] = right.value_array[ri];
		li++;
	    }
	    ri++;
	}

    return ValueSet(temp, ci);
}

ValueSet operator- (const ValueSet &left, const ValueSet &right) {
    ValueType* temp = new ValueType[left.size];

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

    if (left.empty())
	return ValueSet();
    if (right.empty())
	return ValueSet(left);

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

    while ((li < left.size) && (ri < right.size))
	if (left.value_array[li] < right.value_array[ri])
	    temp[ci++] = left.value_array[li++];
	else {
	    if (left.value_array[li] == right.value_array[ri]) {
		li++;
	    }
	    ri++;
	}

    if (ri == right.size)
	for (;li<left.size;li++)
	    temp[ci++] = left.value_array[li];

    return ValueSet(temp, ci);
}

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

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

    if (left.empty())
	return ValueSet(right);

    if (right.empty())
	return ValueSet(left);

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

    while ((li < left.size) && (ri < right.size)) 
	if (left.value_array[li] < right.value_array[ri])
	    temp[ci++] = left.value_array[li++];
	else
	    if (left.value_array[li] > right.value_array[ri])
		temp[ci++] = right.value_array[ri++];
	    else { 
		li++;
		ri++;
	    }

    if (ri == right.size)
	for (;li<left.size;li++)
	    temp[ci++] = left.value_array[li];

    if (li == left.size)
	for (;ri<right.size;ri++)
	    temp[ci++] = right.value_array[ri];

    return ValueSet(temp, ci);
}

char operator<= (const ValueSet &left, const ValueSet &right){

    unsigned int li = 0, ri = 0;

    if (left.empty())
	return 1;

    if (right.empty())
	return 0;

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

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

char operator>= (const ValueSet &left, const ValueSet &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;
}

char operator== (const ValueSet &left, const ValueSet &right){

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

    for (unsigned int i = 0; i < left.size; i++)
       if (left.value_array[i] != right.value_array[i])
	   return 0;

    return 1;
}

char operator!= (const ValueSet &left, const ValueSet &right){
    return !(left == right);
}

ValueSet value_range(ValueType v0, ValueType v1) {
    ValueSet vs;
    int sz = v1-v0;

    if (sz < 0)
	return ValueSet();
    if (sz == 0)
	return ValueSet(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.7
log
@ Back to untemplatized version. all operators working fine.
@
text
@d1 5
@


1.6
log
@ValueSet is templatized instead of having a typedef
@
text
@d4 10
a13 1
// takes an array and its size, and trims it
d16 121
a136 2
template <class ValueType>
ValueSet<ValueType>& ValueSet<ValueType>::operator=(const ValueSet& vs) {
d143 1
d164 1
a164 2
template <class ValueType>
ValueSet<ValueType> operator | (const ValueSet<ValueType> &left, const ValueSet<ValueType> &right) {
d189 1
a189 2
template <class ValueType>
ValueSet<ValueType> operator & (const ValueSet<ValueType> &left, const ValueSet<ValueType> &right) {
d217 127
a343 2
template <class ValueType>
ValueSet<ValueType> value_range(ValueType v0, ValueType v1) {
@


1.5
log
@changed typedef to float to see how it behaved with floats.
@
text
@a2 3
//int value_type_compare1(void *i, void *j) {
//    return(*(ValueType*)i - *(ValueType*)j);
//}
a3 9
int ValueSet::count_dup(ValueType* a, unsigned int sz) {
    int count = 0;
    for (unsigned int i=0; i < sz-1; i++)
	if (a[i] == a[i+1])
	    count++;
    return count;
}


a5 1
void ValueSet::remove_dup(ValueType* &a, unsigned int &sz) {
d7 2
a8 45
    ValueType *ret_array = new ValueType[sz-count_dup(a,sz)];
    
    unsigned int di = 0;
    
    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;
}

ValueType* ValueSet::make_set(const ValueType* a, unsigned int &sz){
    ValueType *ar1 = new ValueType[sz];
    
    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;
}

ValueSet::ValueSet(const ValueSet& vs) {
	this->size = vs.size;
	if (this->size == 0)
	    this->value_array = NULL;
	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];
	}
    }

ValueSet& ValueSet::operator=(const ValueSet& vs) {
d35 2
a36 59
ValueSet::ValueSet(ValueType v0) {
    this->value_array = new ValueType[this->size = 1];

    this->value_array[0] = v0;
}

ValueSet::ValueSet(ValueType v0, ValueType v1) {
    ValueType *ret_array = new ValueType[this->size = 2];

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

ValueSet::ValueSet(ValueType v0, ValueType v1, ValueType v2) {
    ValueType *ret_array = new ValueType[this->size = 3];

    ret_array[0] = v0;
    ret_array[1] = v1;
    ret_array[2] = v2;
    
    this->value_array = make_set(ret_array, this->size);
    delete [] ret_array;
}

ValueSet::ValueSet(ValueType v0, ValueType v1, ValueType v2, ValueType v3) {
    ValueType *ret_array = new ValueType[this->size = 4];

    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;
}

ValueSet::ValueSet(ValueType v0, ValueType v1, ValueType v2, ValueType v3, 
                   ValueType v4) {
    ValueType *ret_array = new ValueType[this->size = 5];

    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;
}

ValueSet::ValueSet(const ValueType* ar, unsigned int sz) {
    this->value_array = make_set(ar, sz);
    this->size = sz;
}

ValueSet operator | (const ValueSet &left, const ValueSet &right) {
d61 2
a62 1
ValueSet operator & (const ValueSet &left, const ValueSet &right) {
d90 2
a91 1
ValueSet value_range(ValueType v0, ValueType v1) {
a108 13
}

ostream& operator << (ostream& stream, ValueSet& 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;
@


1.4
log
@Added operator & and value_range funtion
@
text
@d3 3
a5 3
int value_type_compare1(void *i, void *j) {
    return(*(ValueType*)i - *(ValueType*)j);
}
d19 1
d47 1
a47 1
    qsort(ar1, sz, sizeof(ValueType), value_type_compare1);
@


1.3
log
@Corrected problems with make_set (contravariance)
@
text
@d3 17
a19 3
    int value_type_compare1(void *i, void *j) {
	return(*(ValueType*)i - *(ValueType*)j);
    }
d21 1
a21 7
    int ValueSet::count_dup(ValueType* a, unsigned int sz) {
	int count = 0;
	for (unsigned int i=0; i < sz-1; i++)
	    if (a[i] == a[i+1])
		count++;
	return count;
    }
d23 3
d27 1
a27 1
    // takes an array and its size, and trims it
d29 7
a35 15
    void ValueSet::remove_dup(ValueType* &a, unsigned int &sz) {
	ValueType *ret_array = new ValueType[sz-count_dup(a,sz)];
	
	unsigned int di = 0;
	
	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;
    }
d37 14
a50 11
    ValueType* ValueSet::make_set(const ValueType* a, unsigned int &sz){
	ValueType *ar1 = new ValueType[sz];
	
	for (unsigned int i=0; i<sz; i++)
	    ar1[i] = a[i];
	
	qsort(ar1, sz, sizeof(ValueType), value_type_compare1);
	remove_dup(ar1,sz);
	
	return ar1;
    }
d199 20
@


1.2
log
@Added sort and remove duplicate routines. We're trying to make
these routines private to ValueSet
@
text
@d3 2
a4 2
    int value_type_compare(ValueType *i, ValueType *j) {
	return(*i - *j);
d7 1
a7 1
    int count_dup(ValueType* a, unsigned int sz) {
d18 2
a19 3
    void remove_dup(ValueType* &a, unsigned int &sz) {
	unsigned int newsz = sz-count_dup(a,sz);
	ValueType *ret_array = new ValueType[newsz];
d27 1
a27 1
	ret_array[di] = a[sz-1];
d31 1
a31 1
	sz = newsz;
d34 1
a34 1
    ValueType* make_set(const ValueType* a, unsigned int &sz){
d40 1
a40 1
	qsort(ar1, sz, sizeof(ValueType), value_type_compare);
d143 1
a143 2
    unsigned int newsz = left.size + right.size;
    ValueType* temp = new ValueType[newsz];
d167 28
d198 6
a203 4
    for (int i=0; i < last_index; i++)
	stream << vs.value_array[i] << " ";
    if (vs.size > 0)
	stream << vs.value_array[last_index];
@


1.1
log
@Initial revision
@
text
@d3 44
d56 1
a56 1
}
d86 2
a87 2
    this->size = 1;
    this->value_array = new ValueType[1];
d92 7
a98 17
    if (v0==v1) {
	this->size = 1;
	this->value_array = new ValueType[1];
	this->value_array[0] = v0;
    }
    else {
	this->size = 2;
	this->value_array = new ValueType[2];
	if (v0<v1) {
	    this->value_array[0] = v0;
	    this->value_array[1] = v1;
	}
	else {
	    this->value_array[0] = v1;
	    this->value_array[1] = v0;
	}
    }
d102 1
a102 1
    ValueSet vs = ValueSet(v0,v1) | ValueSet(v2);
d104 6
a109 1
    init(vs.value_array, vs.size);
d113 1
a113 1
    ValueSet vs = ValueSet(v0,v1) | ValueSet(v2,v3);
d115 7
a121 2
    init(vs.value_array, vs.size);

d126 1
a126 1
    ValueSet vs = ValueSet(v0,v1) | ValueSet(v2,v3,v4);
d128 8
a135 2
    init(vs.value_array, vs.size);

d139 2
a140 1
    init(ar,sz);
d143 3
a146 4

ValueSet ValueSet::operator | (const ValueSet &vs) {
    ValueSet result;

d149 4
a152 7
    result.value_array = new ValueType[result.size = this->size+vs.size];
    

    if (this == &vs) return *this;
    while ((li < this->size) && (ri < vs.size))
	if (this->value_array[li] < vs.value_array[ri])
	    result.value_array[ci++] = this->value_array[li++];
d154 2
a155 2
	    result.value_array[ci++] = vs.value_array[ri];
	    if (this->value_array[li] == vs.value_array[ri++])
d159 3
a161 3
    if (li == this->size)
	for (;ri < vs.size;ri++)
	    result.value_array[ci++] = vs.value_array[ri];
d163 4
a166 4
	for (;li < this->size;li++)
	    result.value_array[ci++] = vs.value_array[li];
    result.size = ci;
    return result;
d169 10
d180 1
a180 10
    ostream& operator << (ostream& stream, ValueSet& vs) {
	int last_index = vs.size-1;
	stream << "<";
	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;
    }
@
