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.










share|improve this question
























  • 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

















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.










share|improve this question
























  • 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















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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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














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) {
// ...
}





share|improve this answer























  • Here is a cute one: if Set(array.indices).isSuperset(of: closedRange)
    – Carpsen90
    Nov 8 at 14:07






  • 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












  • 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













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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) {
// ...
}





share|improve this answer























  • Here is a cute one: if Set(array.indices).isSuperset(of: closedRange)
    – Carpsen90
    Nov 8 at 14:07






  • 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












  • 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

















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) {
// ...
}





share|improve this answer























  • Here is a cute one: if Set(array.indices).isSuperset(of: closedRange)
    – Carpsen90
    Nov 8 at 14:07






  • 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












  • 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















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) {
// ...
}





share|improve this answer














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) {
// ...
}






share|improve this answer














share|improve this answer



share|improve this answer








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, 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




















  • Here is a cute one: if Set(array.indices).isSuperset(of: closedRange)
    – Carpsen90
    Nov 8 at 14:07






  • 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












  • 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


















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




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff