From 6d1bec632a55cfb84e2e7074cb86bfa7e3f0ef8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sun, 9 May 2021 10:00:17 +0200 Subject: [PATCH 1/3] inbounds for is_strictly_increasing --- src/Utilities/functions.jl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index 30161a6579..98b337424e 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -660,12 +660,12 @@ function is_canonical(f::Union{SQF,VQF}) end """ - is_strictly_sorted(x::AbstractVector, by, filter) + is_strictly_sorted(x::Vector, by, filter) Returns `true` if `by(x[i]) < by(x[i + 1])` and `filter(x[i]) == true` for all indices i. """ -function is_strictly_sorted(x::AbstractVector, by, filter) +function is_strictly_sorted(x::Vector, by, filter) if isempty(x) return true end @@ -673,10 +673,12 @@ function is_strictly_sorted(x::AbstractVector, by, filter) return false end for i in eachindex(x)[2:end] - if by(x[i]) <= by(x[i-1]) + prev = @inbounds(x[i-1]) + cur = @inbounds(x[i]) + if by(cur) <= by(prev) return false end - if !filter(x[i]) + if !filter(cur) return false end end From 1891b28d55473ede7a0c2d61fa7d27c3a8924658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sun, 9 May 2021 17:03:24 -0400 Subject: [PATCH 2/3] Suggested improved implementation --- src/Utilities/functions.jl | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index 98b337424e..cdfef64000 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -669,18 +669,21 @@ function is_strictly_sorted(x::Vector, by, filter) if isempty(x) return true end - if !filter(first(x)) + current_x = first(x) + if !filter(current_x) return false end - for i in eachindex(x)[2:end] - prev = @inbounds(x[i-1]) - cur = @inbounds(x[i]) - if by(cur) <= by(prev) + current_fx = by(current_x) + @inbounds for i in eachindex(x)[2:end] + next_x = x[i] + if !filter(next_x) return false end - if !filter(cur) + next_fx = by(next_x) + if next_fx <= current_fx return false end + current_x, current_fx = next_x, next_fx end return true end From a6cb62dff64e99c7a04471e75a34ba8921a8a672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sun, 9 May 2021 17:09:27 -0400 Subject: [PATCH 3/3] Add @inbounds --- src/Utilities/functions.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index cdfef64000..983e5ed5ff 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -669,13 +669,13 @@ function is_strictly_sorted(x::Vector, by, filter) if isempty(x) return true end - current_x = first(x) + @inbounds current_x = first(x) if !filter(current_x) return false end current_fx = by(current_x) - @inbounds for i in eachindex(x)[2:end] - next_x = x[i] + for i in eachindex(x)[2:end] + @inbounds next_x = x[i] if !filter(next_x) return false end