G
Guest
How does one get a Java enum class to support hamming distance?
Regards,
Ken Asbury
Regards,
Ken Asbury
How does one get a Java enum class to support hamming distance?
By implementing the appropriate methods.
I suppose that your question means the following: "Given two sets of
flags, I want to compute the sum n1+n2, where n1 is the number of flags
which are in the first set and not in the second, and n2 is the number
of flags which are in the second set and not in the first. How can one
do that if flags are enumeration constants ?"
Then I may give this answer:
Sets of flags, when flags are constants from an 'enum' construction,
are represented by a java.util.EnumSet instance. This method shall
then yield the Hamming distance between sets e1 and e2:
static <E extends Enum<E>> int hammingDistance(
EnumSet<E> e1, EnumSet<E> e2)
{
EnumSet<E> f1 = e1.clone();
f1.removeAll(e2);
EnumSet<E> f2 = e2.clone();
f2.removeAll(e1);
return f1.size() + f2.size();
}
(This is untested.)
--Thomas Pornin
Thanks!
Ken- Hide quoted text -
- Show quoted text -
An ideal insider's answer... Terse, true and absolutely meaningless to
me, an obvious novice.
An ideal insider's answer... Terse, true and absolutely meaningless to
me, an obvious novice.
How does one get a Java enum class to support hamming distance?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.