Skip to content

Commit

Permalink
Copy constructor, allOf(), noneOf().
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyettinger committed Mar 27, 2024
1 parent 29ed784 commit 898b4bc
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/test/java/com/github/tommyettinger/ds/e/ESet.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public ESet(Enum<?>[] contents) {
addAll(contents);
}


/**
* Initializes this set so that it holds the given Enum values, with the universe of possible Enum constants this can hold
* determined by the type of the first Enum in {@code contents}.
Expand All @@ -91,6 +90,17 @@ public ESet(Collection<Enum<?>> contents) {
addAll(contents);
}

/**
* Copy constructor; uses a direct reference to the enum values that may be cached in {@code other}, but copies other fields.
* @param other another ESet that will have most of its data copied, but its cached {@code values()} results will be used directly
*/
public ESet (ESet other) {
this.size = other.size;
if(other.table != null)
this.table = Arrays.copyOf(other.table, other.table.length);
this.enumValues = other.enumValues;
}

/**
* Returns the number of elements in this set (its cardinality). If this
* set contains more than {@code Integer.MAX_VALUE} elements, returns
Expand Down Expand Up @@ -447,4 +457,33 @@ public static ESet with (Enum<?>... array) {
return new ESet(array);
}

/**
* Creates a new ESet using the given result of calling {@code values()} on an Enum type, but with no items initially
* stored in the set.
* <br>
* This is the same as calling {@link #ESet(Enum[], boolean)}.
*
* @param valuesResult almost always, the result of calling {@code values()} on an Enum type; used directly, not copied
* @return a new ESet with the specified universe of possible items, but none present in the set
*/
public static ESet noneOf(Enum<?>[] valuesResult) {
return new ESet(valuesResult, true);
}

/**
* Creates a new ESet using the given result of calling {@code values()} on an Enum type, and with all possible items initially
* stored in the set.
*
* @param valuesResult almost always, the result of calling {@code values()} on an Enum type; used directly, not copied
* @return a new ESet with the specified universe of possible items, and all of them present in the set
*/
public static ESet allOf(Enum<?>[] valuesResult) {
ESet coll = new ESet(valuesResult, true);

for (int i = 0; i < coll.table.length - 1; i++) {
coll.table[i] = -1;
}
coll.table[coll.table.length - 1] = -1 >>> -valuesResult.length;
return coll;
}
}

0 comments on commit 898b4bc

Please sign in to comment.