-
Notifications
You must be signed in to change notification settings - Fork 190
Implement trueloc/falseloc #603
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ae608ee
Implement trueloc/falseloc
awvwgk 7b5ffac
Add tests for empty and complete index list
awvwgk 4a4ac22
Use subroutine to implement logicalloc
awvwgk 7ddcbcc
Add equivalent pack to testsuite
awvwgk b015d9f
Update specification
awvwgk 1346066
Add relation of trueloc/falseloc with which/merge/pack
awvwgk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
--- | ||
title: array | ||
--- | ||
|
||
# The `stdlib_array` module | ||
|
||
[TOC] | ||
|
||
## Introduction | ||
|
||
Module for index manipulation and array handling tasks. | ||
|
||
## Procedures and methods provided | ||
|
||
|
||
### `trueloc` | ||
|
||
#### Status | ||
|
||
Experimental | ||
|
||
#### Description | ||
|
||
Turn a logical mask into an index array by selecting all true values. | ||
Provides similar functionality like the built-in `where` or the intrinsic procedures `merge` and `pack` when working with logical mask. | ||
The built-in / intrinsics are usually preferable to `trueloc`, unless the access to the index array is required. | ||
|
||
#### Syntax | ||
|
||
`loc = [[trueloc(function)]] (array[, lbound])` | ||
|
||
#### Class | ||
|
||
Pure function. | ||
|
||
#### Arguments | ||
|
||
`array`: List of default logical arrays. This argument is `intent(in)`. | ||
|
||
`lbound`: Lower bound of the array to index. This argument is `optional` and `intent(in)`. | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#### Return value | ||
|
||
Returns an array of default integer size, with a maximum length of `size(array)` elements. | ||
|
||
#### Examples | ||
|
||
```fortran | ||
program demo_trueloc | ||
use stdlib_array, only : trueloc | ||
implicit none | ||
real, allocatable :: array(:) | ||
allocate(array(500)) | ||
call random_number(array) | ||
array(trueloc(array > 0.5)) = 0.0 | ||
end program demo_trueloc | ||
``` | ||
|
||
|
||
### `falseloc` | ||
|
||
#### Status | ||
|
||
Experimental | ||
|
||
#### Description | ||
|
||
Turn a logical mask into an index array by selecting all false values. | ||
Provides similar functionality like the built-in `where` or the intrinsic procedures `merge` and `pack` when working with logical mask. | ||
The built-in / intrinsics are usually preferable to `falseloc`, unless the access to the index array is required. | ||
|
||
#### Syntax | ||
|
||
`loc = [[falseloc(function)]] (array[, lbound])` | ||
|
||
#### Class | ||
|
||
Pure function. | ||
|
||
#### Arguments | ||
|
||
`array`: List of default logical arrays. This argument is `intent(in)`. | ||
|
||
`lbound`: Lower bound of the array to index. This argument is `optional` and `intent(in)`. | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#### Return value | ||
|
||
Returns an array of default integer size, with a maximum length of `size(array)` elements. | ||
|
||
#### Examples | ||
|
||
```fortran | ||
program demo_falseloc | ||
use stdlib_array, only : falseloc | ||
implicit none | ||
real, allocatable :: array(:) | ||
allocate(array(-200:200)) | ||
call random_number(array) | ||
array(falseloc(array < 0.5), lbound(array)) = 0.0 | ||
end program demo_falseloc | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
! SPDX-Identifier: MIT | ||
|
||
!> Module for index manipulation and general array handling | ||
!> | ||
!> The specification of this module is available [here](../page/specs/stdlib_array.html). | ||
module stdlib_array | ||
implicit none | ||
private | ||
|
||
public :: trueloc, falseloc | ||
|
||
contains | ||
|
||
!> Version: experimental | ||
!> | ||
!> Return the positions of the true elements in array. | ||
!> [Specification](../page/specs/stdlib_array.html#trueloc) | ||
pure function trueloc(array, lbound) result(loc) | ||
!> Mask of logicals | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logical, intent(in) :: array(:) | ||
!> Lower bound of array to index | ||
integer, intent(in), optional :: lbound | ||
!> Locations of true elements | ||
integer :: loc(count(array)) | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
call logicalloc(loc, array, .true., lbound) | ||
end function trueloc | ||
|
||
!> Version: experimental | ||
!> | ||
!> Return the positions of the false elements in array. | ||
!> [Specification](../page/specs/stdlib_array.html#falseloc) | ||
pure function falseloc(array, lbound) result(loc) | ||
!> Mask of logicals | ||
logical, intent(in) :: array(:) | ||
!> Lower bound of array to index | ||
integer, intent(in), optional :: lbound | ||
!> Locations of false elements | ||
integer :: loc(count(.not.array)) | ||
|
||
call logicalloc(loc, array, .false., lbound) | ||
end function falseloc | ||
|
||
!> Return the positions of the truthy elements in array | ||
pure subroutine logicalloc(loc, array, truth, lbound) | ||
!> Locations of truthy elements | ||
integer, intent(out) :: loc(:) | ||
!> Mask of logicals | ||
logical, intent(in) :: array(:) | ||
!> Truthy value | ||
logical, intent(in) :: truth | ||
!> Lower bound of array to index | ||
integer, intent(in), optional :: lbound | ||
integer :: i, pos, offset | ||
|
||
offset = 0 | ||
if (present(lbound)) offset = lbound - 1 | ||
|
||
i = 0 | ||
do pos = 1, size(array) | ||
if (array(pos).eqv.truth) then | ||
i = i + 1 | ||
loc(i) = pos + offset | ||
end if | ||
end do | ||
end subroutine logicalloc | ||
|
||
end module stdlib_array |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ADDTEST(logicalloc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
PROGS_SRC = test_logicalloc.f90 | ||
|
||
|
||
include ../Makefile.manual.test.mk |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.