How to check if Array contains ClosedRange?
up vote
1
down vote
favorite
In my application written in Swift 4.2 I have the following code:
let arrayOfIntegers = [2, 1, 9, 5, 4, 6, 8, 7]
let unknownLowerBound = 4
let unknownUpperBound = 20
let closedRange = ClosedRange<Int>(uncheckedBounds: (lower: unknownLowerBound,
upper: unknownUpperBound))
let subRange = arrayOfIntegers[closedRange]
subRange.forEach { print($0) }
As you can guess when I am running this code I receive the following error: Fatal error: Array index is out of range
. I want to prevent it.
arrays swift range
add a comment |
up vote
1
down vote
favorite
In my application written in Swift 4.2 I have the following code:
let arrayOfIntegers = [2, 1, 9, 5, 4, 6, 8, 7]
let unknownLowerBound = 4
let unknownUpperBound = 20
let closedRange = ClosedRange<Int>(uncheckedBounds: (lower: unknownLowerBound,
upper: unknownUpperBound))
let subRange = arrayOfIntegers[closedRange]
subRange.forEach { print($0) }
As you can guess when I am running this code I receive the following error: Fatal error: Array index is out of range
. I want to prevent it.
arrays swift range
Is the array guaranteed to be sorted and contiguous? Do you want to know if all elements of the range are included in the array? Do you simply want to check if the array contains that range of elements or do you also want to retrieve them from the array?
– Dávid Pásztor
Nov 8 at 10:54
@DávidPásztor Array may not be sorted. No, it is enough if Array contains at least 1 item of ClosedRange. And I want to print the items that I retrieved from ClosedRange.
– Roman Podymov
Nov 8 at 11:06
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
In my application written in Swift 4.2 I have the following code:
let arrayOfIntegers = [2, 1, 9, 5, 4, 6, 8, 7]
let unknownLowerBound = 4
let unknownUpperBound = 20
let closedRange = ClosedRange<Int>(uncheckedBounds: (lower: unknownLowerBound,
upper: unknownUpperBound))
let subRange = arrayOfIntegers[closedRange]
subRange.forEach { print($0) }
As you can guess when I am running this code I receive the following error: Fatal error: Array index is out of range
. I want to prevent it.
arrays swift range
In my application written in Swift 4.2 I have the following code:
let arrayOfIntegers = [2, 1, 9, 5, 4, 6, 8, 7]
let unknownLowerBound = 4
let unknownUpperBound = 20
let closedRange = ClosedRange<Int>(uncheckedBounds: (lower: unknownLowerBound,
upper: unknownUpperBound))
let subRange = arrayOfIntegers[closedRange]
subRange.forEach { print($0) }
As you can guess when I am running this code I receive the following error: Fatal error: Array index is out of range
. I want to prevent it.
arrays swift range
arrays swift range
edited Nov 8 at 10:58
asked Nov 8 at 10:41
Roman Podymov
1,23611226
1,23611226
Is the array guaranteed to be sorted and contiguous? Do you want to know if all elements of the range are included in the array? Do you simply want to check if the array contains that range of elements or do you also want to retrieve them from the array?
– Dávid Pásztor
Nov 8 at 10:54
@DávidPásztor Array may not be sorted. No, it is enough if Array contains at least 1 item of ClosedRange. And I want to print the items that I retrieved from ClosedRange.
– Roman Podymov
Nov 8 at 11:06
add a comment |
Is the array guaranteed to be sorted and contiguous? Do you want to know if all elements of the range are included in the array? Do you simply want to check if the array contains that range of elements or do you also want to retrieve them from the array?
– Dávid Pásztor
Nov 8 at 10:54
@DávidPásztor Array may not be sorted. No, it is enough if Array contains at least 1 item of ClosedRange. And I want to print the items that I retrieved from ClosedRange.
– Roman Podymov
Nov 8 at 11:06
Is the array guaranteed to be sorted and contiguous? Do you want to know if all elements of the range are included in the array? Do you simply want to check if the array contains that range of elements or do you also want to retrieve them from the array?
– Dávid Pásztor
Nov 8 at 10:54
Is the array guaranteed to be sorted and contiguous? Do you want to know if all elements of the range are included in the array? Do you simply want to check if the array contains that range of elements or do you also want to retrieve them from the array?
– Dávid Pásztor
Nov 8 at 10:54
@DávidPásztor Array may not be sorted. No, it is enough if Array contains at least 1 item of ClosedRange. And I want to print the items that I retrieved from ClosedRange.
– Roman Podymov
Nov 8 at 11:06
@DávidPásztor Array may not be sorted. No, it is enough if Array contains at least 1 item of ClosedRange. And I want to print the items that I retrieved from ClosedRange.
– Roman Podymov
Nov 8 at 11:06
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
You can check if the range of valid array indices “clamped” to
the given closed range is equal to that range:
let array = [1, 2, 3, 4, 5, 6, 7, 8]
let closedRange = 4...20
if array.indices.clamped(to: Range(closedRange)) == Range(closedRange) {
let subArray = array[closedRange]
print(subArray)
} else {
print("closedRange contains invalid indices")
}
Or, equivalently:
if array.indices.contains(closedRange.lowerBound)
&& array.indices.contains(closedRange.upperBound) {
// ...
}
Here is a cute one:if Set(array.indices).isSuperset(of: closedRange)
– Carpsen90
Nov 8 at 14:07
1
@Carpsen90: Yes, butSet(array.indices)
creates a (potentially large) set of all indices. The above methods operate only on the lower and upper bound of ranges.
– Martin R
Nov 8 at 14:11
If we're talking efficiency, would this be slightly better:if (array.indices.lowerBound <= closedRange.lowerBound) && (array.indices.upperBound >= closedRange.upperBound)
? Looking at the bounds, I suspect, is faster thancontains
. (The first solution does create two ranges)
– Carpsen90
Nov 8 at 14:21
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You can check if the range of valid array indices “clamped” to
the given closed range is equal to that range:
let array = [1, 2, 3, 4, 5, 6, 7, 8]
let closedRange = 4...20
if array.indices.clamped(to: Range(closedRange)) == Range(closedRange) {
let subArray = array[closedRange]
print(subArray)
} else {
print("closedRange contains invalid indices")
}
Or, equivalently:
if array.indices.contains(closedRange.lowerBound)
&& array.indices.contains(closedRange.upperBound) {
// ...
}
Here is a cute one:if Set(array.indices).isSuperset(of: closedRange)
– Carpsen90
Nov 8 at 14:07
1
@Carpsen90: Yes, butSet(array.indices)
creates a (potentially large) set of all indices. The above methods operate only on the lower and upper bound of ranges.
– Martin R
Nov 8 at 14:11
If we're talking efficiency, would this be slightly better:if (array.indices.lowerBound <= closedRange.lowerBound) && (array.indices.upperBound >= closedRange.upperBound)
? Looking at the bounds, I suspect, is faster thancontains
. (The first solution does create two ranges)
– Carpsen90
Nov 8 at 14:21
add a comment |
up vote
4
down vote
accepted
You can check if the range of valid array indices “clamped” to
the given closed range is equal to that range:
let array = [1, 2, 3, 4, 5, 6, 7, 8]
let closedRange = 4...20
if array.indices.clamped(to: Range(closedRange)) == Range(closedRange) {
let subArray = array[closedRange]
print(subArray)
} else {
print("closedRange contains invalid indices")
}
Or, equivalently:
if array.indices.contains(closedRange.lowerBound)
&& array.indices.contains(closedRange.upperBound) {
// ...
}
Here is a cute one:if Set(array.indices).isSuperset(of: closedRange)
– Carpsen90
Nov 8 at 14:07
1
@Carpsen90: Yes, butSet(array.indices)
creates a (potentially large) set of all indices. The above methods operate only on the lower and upper bound of ranges.
– Martin R
Nov 8 at 14:11
If we're talking efficiency, would this be slightly better:if (array.indices.lowerBound <= closedRange.lowerBound) && (array.indices.upperBound >= closedRange.upperBound)
? Looking at the bounds, I suspect, is faster thancontains
. (The first solution does create two ranges)
– Carpsen90
Nov 8 at 14:21
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You can check if the range of valid array indices “clamped” to
the given closed range is equal to that range:
let array = [1, 2, 3, 4, 5, 6, 7, 8]
let closedRange = 4...20
if array.indices.clamped(to: Range(closedRange)) == Range(closedRange) {
let subArray = array[closedRange]
print(subArray)
} else {
print("closedRange contains invalid indices")
}
Or, equivalently:
if array.indices.contains(closedRange.lowerBound)
&& array.indices.contains(closedRange.upperBound) {
// ...
}
You can check if the range of valid array indices “clamped” to
the given closed range is equal to that range:
let array = [1, 2, 3, 4, 5, 6, 7, 8]
let closedRange = 4...20
if array.indices.clamped(to: Range(closedRange)) == Range(closedRange) {
let subArray = array[closedRange]
print(subArray)
} else {
print("closedRange contains invalid indices")
}
Or, equivalently:
if array.indices.contains(closedRange.lowerBound)
&& array.indices.contains(closedRange.upperBound) {
// ...
}
edited Nov 8 at 12:04
answered Nov 8 at 10:57
Martin R
384k53832940
384k53832940
Here is a cute one:if Set(array.indices).isSuperset(of: closedRange)
– Carpsen90
Nov 8 at 14:07
1
@Carpsen90: Yes, butSet(array.indices)
creates a (potentially large) set of all indices. The above methods operate only on the lower and upper bound of ranges.
– Martin R
Nov 8 at 14:11
If we're talking efficiency, would this be slightly better:if (array.indices.lowerBound <= closedRange.lowerBound) && (array.indices.upperBound >= closedRange.upperBound)
? Looking at the bounds, I suspect, is faster thancontains
. (The first solution does create two ranges)
– Carpsen90
Nov 8 at 14:21
add a comment |
Here is a cute one:if Set(array.indices).isSuperset(of: closedRange)
– Carpsen90
Nov 8 at 14:07
1
@Carpsen90: Yes, butSet(array.indices)
creates a (potentially large) set of all indices. The above methods operate only on the lower and upper bound of ranges.
– Martin R
Nov 8 at 14:11
If we're talking efficiency, would this be slightly better:if (array.indices.lowerBound <= closedRange.lowerBound) && (array.indices.upperBound >= closedRange.upperBound)
? Looking at the bounds, I suspect, is faster thancontains
. (The first solution does create two ranges)
– Carpsen90
Nov 8 at 14:21
Here is a cute one:
if Set(array.indices).isSuperset(of: closedRange)
– Carpsen90
Nov 8 at 14:07
Here is a cute one:
if Set(array.indices).isSuperset(of: closedRange)
– Carpsen90
Nov 8 at 14:07
1
1
@Carpsen90: Yes, but
Set(array.indices)
creates a (potentially large) set of all indices. The above methods operate only on the lower and upper bound of ranges.– Martin R
Nov 8 at 14:11
@Carpsen90: Yes, but
Set(array.indices)
creates a (potentially large) set of all indices. The above methods operate only on the lower and upper bound of ranges.– Martin R
Nov 8 at 14:11
If we're talking efficiency, would this be slightly better:
if (array.indices.lowerBound <= closedRange.lowerBound) && (array.indices.upperBound >= closedRange.upperBound)
? Looking at the bounds, I suspect, is faster than contains
. (The first solution does create two ranges)– Carpsen90
Nov 8 at 14:21
If we're talking efficiency, would this be slightly better:
if (array.indices.lowerBound <= closedRange.lowerBound) && (array.indices.upperBound >= closedRange.upperBound)
? Looking at the bounds, I suspect, is faster than contains
. (The first solution does create two ranges)– Carpsen90
Nov 8 at 14:21
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206040%2fhow-to-check-if-array-contains-closedrange%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Is the array guaranteed to be sorted and contiguous? Do you want to know if all elements of the range are included in the array? Do you simply want to check if the array contains that range of elements or do you also want to retrieve them from the array?
– Dávid Pásztor
Nov 8 at 10:54
@DávidPásztor Array may not be sorted. No, it is enough if Array contains at least 1 item of ClosedRange. And I want to print the items that I retrieved from ClosedRange.
– Roman Podymov
Nov 8 at 11:06