Skip to content

Commit

Permalink
IndexSet range array initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Jan 18, 2024
1 parent 1e69522 commit 9f5f8da
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 9 additions & 1 deletion Sources/Rearrange/IndexSet+NSRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ public extension IndexSet {
self.init(integersIn: Range<IndexSet.Element>(range)!)
}

init(ranges: [NSRange]) {
self.init()

insert(ranges: ranges)
}

mutating func insert(range: NSRange) {
insert(integersIn: Range<IndexSet.Element>(range)!)
}

mutating func insert(ranges: [NSRange]) {
ranges.forEach { insert(range: $0) }
for range in ranges {
insert(range: range)
}
}

mutating func remove(integersIn range: NSRange) {
Expand Down
9 changes: 8 additions & 1 deletion Tests/RearrangeTests/IndexSetExtenstionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import XCTest

class IndexSetExtenstionTests: XCTestCase {
final class IndexSetExtenstionTests: XCTestCase {
func testSpanningRange() {
var set = IndexSet()

Expand Down Expand Up @@ -43,6 +43,13 @@ class IndexSetExtenstionTests: XCTestCase {
XCTAssertEqual(set, IndexSet(integersIn: NSRange(location: 0, length: 5)))
}

func testRangesInitializer() {
let ranges = [NSRange(0..<5), NSRange(5..<10)]
let set = IndexSet(ranges: ranges)

XCTAssertEqual(set, IndexSet(integersIn: 0..<10))
}

func testIntersets() {
var set = IndexSet()

Expand Down

0 comments on commit 9f5f8da

Please sign in to comment.