claudemartin / enum-bit-set

EnumSet and BitSet in one!
http://claude-martin.ch/enumbitset/
7 stars 3 forks source link

ElementOf.(EnumBitSet) #4

Closed Mandemus closed 6 years ago

Mandemus commented 6 years ago

When declaring my bit vector as an EnumBitSet according to the examples, elementOf() appears to internally use the collection.contains() method not bitwise math or get(ordinal) as a comparison. Sort of defeats one of the reasons for using a bitvector.

private static enum Permission implements EnumBitSetHelper<Permission> {
        READ, WRITE, EXECUTE, DELETE;
}

final EnumBitSet<Permission> permissions = bob.getPermissions();
// Add element to EnumBitSet:
permissions.add(Permission.READ);

// Check EnumBitSet:
if (Permission.WRITE.elementOf(permissions))
        Files.write(path, bytes);
claudemartin commented 6 years ago

elementOf is a default method in an interface. It always has one parameter but there are many overloads: BigInteger, BitSet, Collection, Enum, long When you pass an EnumBitSet it will pass the call to the actual EnumSet. Both RegularEnumSet and JumboEnumSet will use bit operations. So what exactly would be the issue here?

Mandemus commented 6 years ago

IntelliJ is saying something different. When following it through it dropped right to collection.contains() which iterated over the collection elements.

    default boolean elementOf(Collection<E> collection) {
        return ((Collection)Objects.requireNonNull(collection)).contains(this);
    }

That's the override it's using, at line 42.

claudemartin commented 6 years ago

To be more precise: You call WRITE.elementOf(permissions) and permissions is some collection. So it's elementOf(Collection<E>). That will just call permissions.contains(WRITE). Then that is permissions.bitset.contains(WRITE). Which might be this: java.util.JumboEnumSet.contains(Object) and that's basically return (elements[eOrdinal >>> 6] & (1L << eOrdinal)) != 0;. In the example it's obviously just a regular enum set. I've just tested this in Eclipse: image

Mandemus commented 6 years ago

Ok thank you for the clarification. It was deceiving seeing it drop through all the overrides surrounding it.

Great library. It's cleaned up the bitvector code immensely in our 120k line project.

claudemartin commented 6 years ago

I'm amazed that anyone is using my code. I wrote this when I was a student. And I'm glad it works outside of Eclipse. There's a Java 10 branch, if you ever want to switch to a newer Version of Java.