From 9c41ff7860446a354a1e25085c1e0b2b974c8098 Mon Sep 17 00:00:00 2001 From: Long Wang Date: Fri, 22 Oct 2021 17:56:07 +0800 Subject: [PATCH] feat: `to_index` converts a array to a set --- docs/src/index.md | 11 +++++++---- src/FunctionIndices.jl | 2 ++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/src/index.md b/docs/src/index.md index 77b0752..f9f867e 100644 --- a/docs/src/index.md +++ b/docs/src/index.md @@ -39,10 +39,13 @@ Besides, for out of bounds index like `A[4, 5]`, `A[not(4), not(5)]` is equivale ## Performant tips for `not` -* For a big amount of indices `I` should be exclude, convert it to a `Set` by `not(Set(I))` might faster `not(I)`. -* For small array, the optimized `not(x)` might be slower than normal `FI(!in(x))`. +For small array, the optimized `not(x)` might be slower in some case, +see [performance comparing](@ref performance) for details. -See [performance comparing](@ref performance) for details. +There are some tips for better performance: +* Use `FI(!in(x))` instead of `not(x)`. +* Create your own "Not" type, see [below example](@ref intro-define) for details. +* For a small array of indices like `not([1, 2, 3])`, `not(1, 2, 3)` will faster. ## Mechanism @@ -54,7 +57,7 @@ There are three methods determining how to convert `AFI` to array index: * [`FunctionIndices.to_function`](@ref): this function is called in default method `to_index` and convert the given `AFI` to a function. * [`FunctionIndices.indextype`](@ref): this function is called in `to_indices` and returns a type as the `Type` argument of `to_index`. The `indextype` accepts two arguments, the type of array and type of a `AFI`. -## Example to defined +## [Example to define "Not"](@ref intro-define) If you don't like the default behavior of `not`, creating a new "Not" index type is easy: diff --git a/src/FunctionIndices.jl b/src/FunctionIndices.jl index c2ea459..76f1659 100644 --- a/src/FunctionIndices.jl +++ b/src/FunctionIndices.jl @@ -94,6 +94,8 @@ struct NotIndex{T} <: AbstractNotIndex{T} parent::T end Base.parent(I::NotIndex) = I.parent +# ranges will not call this method, cause which override default `to_index` +to_function(I::NotIndex{<:AbstractArray}) = !in(Set(parent(I))) # convert to Vector{Int} by default for NotIndex indextype(::Type{<:AbstractArray}, ::Type{<:NotIndex}) = Vector{Int}