Contents

yockow/swiftranges

`SwiftRanges` provides some kinds of range that are not implemented in Swift Standard Library.

Ranges

| Name | Lower Bound | Upper Bound | Implemented in | |-------------------------|-------------|-------------|------------------------| | ClosedRange | Included | Included | Swift Standard Library | | LeftOpenRange | Excluded | Included | This Library | | OpenRange | Excluded | Excluded | This Library | | Range | Included | Excluded | Swift Standard Library | | PartialRangeFrom | Included | (Pos. Inf.) | Swift Standard Library | | PartialRangeGreaterThan | Excluded | (Pos. Inf.) | This Library | | PartialRangeThrough | (Neg. Inf.) | Included | Swift Standard Library | | PartialRangeUpTo | (Neg. Inf.) | Excluded | Swift Standard Library | | UnboundedRange | (Neg. Inf.) | (Pos. Inf.) | Swift Standard Library |

Other structs implemented in this library

  • AnyRange: A type-erased range. (Deprecated)
  • RangeDictionary: A collection like Dictionary whose key is a range.
  • GeneralizedRangeSet: A set that can contain multiple types of ranges. (Renamed from MultipleRanges)

Requirements

  • Swift >=6.2
  • macOS or Linux

Usage

Left-Open Ranges

import Ranges

let leftOpenRange: LeftOpenRange<Int> = 10<..20
print(leftOpenRange.contains(10)) // -> false
print(leftOpenRange.contains(15)) // -> true
print(leftOpenRange.contains(20)) // -> true

let openRange: OpenRange<Int> = 10<..<20
print(openRange.contains(10)) // -> false
print(openRange.contains(15)) // -> true
print(openRange.contains(20)) // -> false

let greaterThan: PartialRangeGreaterThan<Int> = 10<..
print(greaterThan.contains(10)) // -> false
print(greaterThan.contains(Int.max)) // -> true

print(greaterThan.overlaps(...11)) // -> true
print(greaterThan.overlaps(..<11)) // -> false
                                   // Because there is no integer in "10<..<11"

`RangeDictionary`

var dic: RangeDictionary<Int, String> = [
  1...2: "Index",
  3...10: "Chapter 01",
  11...40: "Chapter 02"
]

print(dic[1]) // Prints "Index"
print(dic[5]) // Prints "Chapter 01"
print(dic[15]) // Prints "Chapter 02"
print(dic[100]) // Prints "nil"

dic.insert("Prologue", forRange: 2...5)
print(dic[5]) // Prints "Prologue"

`GeneralizedRangeSet`

import Ranges

var set = GeneralizedRangeSet<Int>()
set.insert(10...20) 
set.insert(30...40)
print(set.contains(15)) // -> true
print(set.contains(25)) // -> false
print(set.contains(35)) // -> true

set.subtract(15...35)
print(set.ranges) // -> [10..<15, 35<..40]

Other methods like Set are also implemented in GeneralizedRangeSet:

  • intersection(_:)
  • union(_:)
  • symmetricDifference(_:)

License

MIT License. See "LICENSE.txt" for more information.

Package Metadata

Repository: yockow/swiftranges

Default branch: main

README: README.md