-
-
Save laevandus/b9cfee8c3ed4f4675ba8aa8fbb811612 to your computer and use it in GitHub Desktop.
This file contains 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
func set(_ data: DispatchData, byteRange: CountableRange<Int>) { | |
if let insertionIndex = sortedItems.firstIndex(where: { $0.byteRange.startIndex >= byteRange.startIndex }) { | |
let canDiscard: Bool = { | |
guard !sortedItems[insertionIndex].byteRange.contains(byteRange) else { return true } | |
guard insertionIndex > sortedItems.startIndex else { return false } | |
return sortedItems[insertionIndex.advanced(by: -1)].byteRange.contains(byteRange) | |
}() | |
if !canDiscard { | |
sortedItems.insert(StorageItem(byteRange: byteRange, data: data), at: insertionIndex) | |
} | |
} | |
else { | |
sortedItems.append(StorageItem(byteRange: byteRange, data: data)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment