Skip to content

Commit

Permalink
Refactor distinct to use select.
Browse files Browse the repository at this point in the history
Signed-off-by: Donald Raab <Donald.Raab@bnymellon.com>
  • Loading branch information
donraab committed Jul 30, 2022
1 parent 7195830 commit 21a08f8
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -721,17 +721,8 @@ final class Immutable<name>ArrayList
@Override
public Immutable<name>List distinct()
{
<name>ArrayList target = new <name>ArrayList();
Mutable<name>Set seenSoFar = new <name>HashSet(this.size());

for (<type> each : this.items)
{
if (seenSoFar.add(each))
{
target.add(each);
}
}
return target.toImmutable();
return this.select(seenSoFar::add);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1022,18 +1022,8 @@ public class <name>ArrayList extends Abstract<name>Iterable
@Override
public Mutable<name>List distinct()
{
<name>ArrayList target = new <name>ArrayList();
Mutable<name>Set seenSoFar = new <name>HashSet(this.size());

for (int i = 0; i \< this.size; i++)
{
<type> each = this.items[i];
if (seenSoFar.add(each))
{
target.add(each);
}
}
return target;
return this.select(seenSoFar::add);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,8 @@ public void each(CharProcedure procedure)
@Override
public CharAdapter distinct()
{
StringBuilder builder = new StringBuilder();
MutableCharSet seenSoFar = CharSets.mutable.empty();

int size = this.size();
for (int i = 0; i < size; i++)
{
char each = this.get(i);
if (seenSoFar.add(each))
{
builder.append(each);
}
}
return new CharAdapter(builder.toString());
return this.select(seenSoFar::add);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,20 +174,8 @@ public void each(IntProcedure procedure)
@Override
public CodePointAdapter distinct()
{
StringBuilder builder = new StringBuilder();
MutableIntSet seenSoFar = IntSets.mutable.empty();

int length = this.adapted.length();
for (int i = 0; i < length; )
{
int codePoint = this.adapted.codePointAt(i);
if (seenSoFar.add(codePoint))
{
builder.appendCodePoint(codePoint);
}
i += Character.charCount(codePoint);
}
return new CodePointAdapter(builder.toString());
return this.select(seenSoFar::add);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -924,16 +924,7 @@ public static <T> void forEachWithIndex(T[] objectArray, int size, ObjectIntProc
public static <T, R extends List<T>> R distinct(T[] objectArray, int size, R targetList)
{
MutableSet<T> seenSoFar = Sets.mutable.empty();

for (int i = 0; i < size; i++)
{
T each = objectArray[i];
if (seenSoFar.add(each))
{
targetList.add(each);
}
}
return targetList;
return InternalArrayIterate.select(objectArray, size, seenSoFar::add, targetList);
}

/**
Expand All @@ -950,17 +941,7 @@ public static <T> FastList<T> distinct(T[] objectArray, int size)
public static <T> FastList<T> distinct(T[] objectArray, int size, HashingStrategy<? super T> hashingStrategy)
{
MutableSet<T> seenSoFar = UnifiedSetWithHashingStrategy.newSet(hashingStrategy);

FastList<T> result = FastList.newList();
for (int i = 0; i < size; i++)
{
T each = objectArray[i];
if (seenSoFar.add(each))
{
result.add(each);
}
}
return result;
return InternalArrayIterate.select(objectArray, size, seenSoFar::add, FastList.newList());
}

public static <T> long sumOfInt(T[] array, int size, IntFunction<? super T> function)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1097,20 +1097,10 @@ public static <K, T, R extends MutableMapIterable<K, T>> R groupByUniqueKey(
* @deprecated in 7.0.
*/
@Deprecated
public static <T, R extends List<T>> R distinct(
Iterator<T> iterator,
R targetCollection)
public static <T, R extends List<T>> R distinct(Iterator<T> iterator, R targetCollection)
{
Set<T> seenSoFar = Sets.mutable.empty();
while (iterator.hasNext())
{
T item = iterator.next();
if (seenSoFar.add(item))
{
targetCollection.add(item);
}
}
return targetCollection;
return IteratorIterate.select(iterator, seenSoFar::add, targetCollection);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1265,16 +1265,7 @@ public static <T, P, A, R extends Collection<A>> R collectWith(
public static <T, R extends List<T>> R distinct(List<T> list, R targetList)
{
MutableSet<T> seenSoFar = Sets.mutable.empty();
int size = list.size();
for (int i = 0; i < size; i++)
{
T item = list.get(i);
if (seenSoFar.add(item))
{
targetList.add(item);
}
}
return targetList;
return RandomAccessListIterate.select(list, seenSoFar::add, targetList);
}

/**
Expand Down

0 comments on commit 21a08f8

Please sign in to comment.