mirror of
https://github.com/SebastianWendel/nixpkgs.git
synced 2024-09-20 04:19:00 +02:00
lib/lists: mutuallyExclusive function optimization
The current implementation of `mutuallyExclusive` builds a new list with length subtracted by one on every recursive call which is expensive. When b is empty, the function still traverses a in its entirety before returning a result. The new implementation uses `any` to check if each element of list b is in list a using `elem`. This maintains short circuiting when list a or b is empty and has a worst case time complexity of O(nm).
This commit is contained in:
parent
509d236edf
commit
cd6397519f
|
@ -663,9 +663,6 @@ rec {
|
||||||
/* Test if two lists have no common element.
|
/* Test if two lists have no common element.
|
||||||
It should be slightly more efficient than (intersectLists a b == [])
|
It should be slightly more efficient than (intersectLists a b == [])
|
||||||
*/
|
*/
|
||||||
mutuallyExclusive = a: b:
|
mutuallyExclusive = a: b: length a == 0 || !(any (x: elem x a) b);
|
||||||
(builtins.length a) == 0 ||
|
|
||||||
(!(builtins.elem (builtins.head a) b) &&
|
|
||||||
mutuallyExclusive (builtins.tail a) b);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue