mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-07 17:56:01 +02:00
An attempt at performance optimization.
Change-Id: Ia4b6839626381935af859ae468de418fa004c867 Reviewed-on: https://git.eclipse.org/r/19934 Reviewed-by: Nathan Ridge <zeratul976@hotmail.com> Reviewed-by: Sergey Prigogin <eclipse.sprigogin@gmail.com> IP-Clean: Sergey Prigogin <eclipse.sprigogin@gmail.com> Tested-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
This commit is contained in:
parent
d9c4584257
commit
227b03e6b8
1 changed files with 20 additions and 7 deletions
|
@ -154,17 +154,30 @@ public final class CollectionUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter the elements of a collection down to just the ones
|
* Filter the elements of a collection down to just the ones that match the given predicate.
|
||||||
* that match the given predicate.
|
|
||||||
* @since 5.6
|
* @since 5.6
|
||||||
*/
|
*/
|
||||||
public static <T> Collection<T> filter(Collection<T> collection, IUnaryPredicate<T> predicate) {
|
public static <T> Collection<T> filter(Collection<T> collection, IUnaryPredicate<T> predicate) {
|
||||||
if (collection.isEmpty())
|
if (collection.isEmpty())
|
||||||
return collection;
|
return collection;
|
||||||
Collection<T> result = new ArrayList<T>();
|
Collection<T> result = null;
|
||||||
for (T t : collection)
|
int n = 0;
|
||||||
if (predicate.apply(t))
|
for (T t : collection) {
|
||||||
result.add(t);
|
if (predicate.apply(t)) {
|
||||||
return result;
|
if (result != null) {
|
||||||
|
result.add(t);
|
||||||
|
} else {
|
||||||
|
++n;
|
||||||
|
}
|
||||||
|
} else if (result == null) {
|
||||||
|
result = new ArrayList<T>(collection.size() - 1);
|
||||||
|
for (T u : collection) {
|
||||||
|
if (--n < 0)
|
||||||
|
break;
|
||||||
|
result.add(u);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result == null ? collection : result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue