Skip to content

Commit

Permalink
feat: to_index converts a array to a set
Browse files Browse the repository at this point in the history
  • Loading branch information
wangl-cc committed Oct 22, 2021
1 parent 314c75a commit 9c41ff7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 7 additions & 4 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:

Expand Down
2 changes: 2 additions & 0 deletions src/FunctionIndices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 9c41ff7

Please sign in to comment.