Re: Comparing a Vector with Another Vector or a Single Value
On Dec 27, 4:53 am, "Charles" <electrochuckREM...@XXXatt.net> wrote:
Consider a function that compares a vector of doubles, for instance, with
either another vector of equal length or just a single double. The type of
comparison is not known until runtime. The output will be a vector of
bools.
Case 1:
For each element in vectorA, if vectorA[x] is greater than vectorB[x],
output[x] = true. Otherwise output[x] = false.
Case 2:
For each element in vectorA, if vectorA[x] is greater than doubleB,
output[x] = true. Otherwise output[x] = false.
No problem. If at runtime this must be a changeable operation, that
pretty much forces you to use a virtual function and implement it
differently for your comparisons. One implementation could hold the
"other" vector and do the element-wise comparison. Another
implementation holds a single value and compares it to each element.
Oh, beware of vectors of bools. It "works" but has different behavior
from other vectors.
My plan is to use a Boost:Variant to hold either a pointer to vectorB or
doubleB itself. The comparison function will step through vectorA and
either
compare each element with the corresponding element in vectorB or just the
single doubleB value. (I could also just fill a vector with multiple
copies
of doubleB, eliminating Case 2 above.)
The variant idea sounds somewhat interesting, but I don't see it
really helping. It will hold data but does not abstract the operation
that must be done. Also, to make use of the variant you must know the
held type, and thus store that information elsewhere (still in a
concrete object implementing a virtual function?) Or possibly type-
querying the variant until it doesn't fail. That's why I don't like
this approach.
Here's an example:
#include <iostream>
#include <vector>
#include <algorithm>
template <class T>
class abstract_vector_comparitor
{
public:
virtual ~abstract_vector_comparitor() {}
virtual void compare(
std::vector<T> const & elts,
std::vector<bool> & output) const = 0;
};
template <class T>
class vector_vector_comparitor : public abstract_vector_comparitor<T>
{
public:
vector_vector_comparitor(std::vector<T> const & values)
: values_(values)
{
}
virtual void compare(
std::vector<T> const & elts,
std::vector<bool> & output) const
{
std::vector<bool> result(std::max(values_.size(), elts.size()));
for (int i = 0, end = std::min(values_.size(), elts.size());
i != end;
++i)
{
result[i] = elts[i] == values_[i];
}
output.swap(result);
}
private:
std::vector<T> values_;
};
template <class T>
class value_vector_comparitor : public abstract_vector_comparitor<T>
{
public:
value_vector_comparitor(T value) : value_(value) { }
virtual void compare(
std::vector<T> const & elts,
std::vector<bool> & output) const
{
std::vector<bool> result(elts.size());
for (int i = 0, end = elts.size(); i != end; ++i)
{
result[i] = elts[i] == value_;
}
output.swap(result);
}
private:
T value_;
};
void display(std::vector<bool> const & vb)
{
for (int i = 0; i < vb.size(); ++i)
{
std::cout << "result[" << i << "]="
<< std::boolalpha << vb[i] << std::endl;
}
}
int main()
{
typedef std::vector<int> int_vec;
int_vec iv;
iv.push_back(1);
iv.push_back(2);
iv.push_back(3);
int_vec iv2(iv);
iv2[1] = 0;
vector_vector_comparitor<int> vec_vc(iv2);
value_vector_comparitor<int> val_vc(2);
std::vector<bool> result;
abstract_vector_comparitor<int> * avc;
std::cout << "comparing iv. comparitor currently holds another vec
\n";
avc = &vec_vc;
avc->compare(iv, result);
display(result);
std::cout << "comparing iv. comparitor currently holds a single
value\n";
avc = &val_vc;
avc->compare(iv, result);
display(result);
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]