mirror of
https://github.com/qvacua/vimr.git
synced 2024-09-11 17:15:34 +03:00
Simplify closure a bit
This commit is contained in:
parent
ca7a270f52
commit
848017f450
@ -51,7 +51,7 @@ public extension RandomAccessCollection where Index == Int {
|
||||
return result.map { $0! }
|
||||
}
|
||||
|
||||
func groupedRanges<T: Equatable>(
|
||||
func groupedRangesOriginal<T: Equatable>(
|
||||
with marker: (Index, Element) -> T
|
||||
) -> [CountableClosedRange<Index>] {
|
||||
if self.isEmpty { return [] }
|
||||
@ -86,6 +86,42 @@ public extension RandomAccessCollection where Index == Int {
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func groupedRanges<T: Equatable>(
|
||||
with marker: (Element) -> T
|
||||
) -> [CountableClosedRange<Index>] {
|
||||
if self.isEmpty { return [] }
|
||||
if self.count == 1 { return [self.startIndex...self.startIndex] }
|
||||
|
||||
var result = [CountableClosedRange<Index>]()
|
||||
result.reserveCapacity(self.count / 2)
|
||||
|
||||
let inclusiveEndIndex = self.endIndex - 1
|
||||
var lastStartIndex = self.startIndex
|
||||
var lastEndIndex = self.startIndex
|
||||
var lastMarker = marker(self.first!) // self is not empty!
|
||||
for i in self.startIndex..<self.endIndex {
|
||||
let currentMarker = marker(self[i])
|
||||
|
||||
if lastMarker == currentMarker {
|
||||
if i == inclusiveEndIndex {
|
||||
result.append(lastStartIndex...i)
|
||||
}
|
||||
} else {
|
||||
result.append(lastStartIndex...lastEndIndex)
|
||||
lastMarker = currentMarker
|
||||
lastStartIndex = i
|
||||
|
||||
if i == inclusiveEndIndex {
|
||||
result.append(i...i)
|
||||
}
|
||||
}
|
||||
|
||||
lastEndIndex = i
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
public extension NSRange {
|
||||
|
@ -24,7 +24,7 @@ class ArraySliceTest: XCTestCase {
|
||||
Dummy(value: 2, marker: false),
|
||||
|
||||
Dummy(value: 3, marker: false),
|
||||
][1...3].groupedRanges { _, element in element.marker }
|
||||
][1...3].groupedRanges { element in element.marker }
|
||||
|
||||
expect(grouped).to(equal(
|
||||
[
|
||||
@ -43,7 +43,7 @@ class ArraySliceTest: XCTestCase {
|
||||
Dummy(value: 3, marker: true),
|
||||
|
||||
Dummy(value: 3, marker: true),
|
||||
][1...3].groupedRanges { _, element in element.marker }
|
||||
][1...3].groupedRanges { element in element.marker }
|
||||
|
||||
expect(grouped).to(equal(
|
||||
[
|
||||
@ -61,7 +61,7 @@ class ArraySliceTest: XCTestCase {
|
||||
Dummy(value: 2, marker: true),
|
||||
|
||||
Dummy(value: 3, marker: true),
|
||||
][1...2].groupedRanges { _, element in element.marker }
|
||||
][1...2].groupedRanges { element in element.marker }
|
||||
|
||||
expect(grouped).to(equal(
|
||||
[
|
||||
@ -81,7 +81,7 @@ class ArraySliceTest: XCTestCase {
|
||||
Dummy(value: 1, marker: true),
|
||||
|
||||
Dummy(value: 1, marker: true),
|
||||
][1...5].groupedRanges { _, element in element.marker }
|
||||
][1...5].groupedRanges { element in element.marker }
|
||||
|
||||
expect(grouped).to(equal(
|
||||
[
|
||||
@ -103,7 +103,7 @@ class ArraySliceTest: XCTestCase {
|
||||
Dummy(value: 1, marker: true),
|
||||
|
||||
Dummy(value: 1, marker: true),
|
||||
][1...5].groupedRanges { _, element in element.marker }
|
||||
][1...5].groupedRanges { element in element.marker }
|
||||
|
||||
expect(grouped).to(equal(
|
||||
[
|
||||
@ -121,7 +121,7 @@ class ArraySliceTest: XCTestCase {
|
||||
Dummy(value: 0, marker: true),
|
||||
|
||||
Dummy(value: 0, marker: true),
|
||||
][1...1].groupedRanges { _, element in element.marker }
|
||||
][1...1].groupedRanges { element in element.marker }
|
||||
|
||||
expect(grouped).to(equal(
|
||||
[
|
||||
@ -138,7 +138,7 @@ class SwiftCommonsTest: XCTestCase {
|
||||
Dummy(value: 1, marker: false),
|
||||
Dummy(value: 2, marker: false),
|
||||
Dummy(value: 3, marker: false),
|
||||
].groupedRanges { _, element in element.marker }
|
||||
].groupedRanges { element in element.marker }
|
||||
|
||||
expect(grouped).to(equal(
|
||||
[
|
||||
@ -154,7 +154,7 @@ class SwiftCommonsTest: XCTestCase {
|
||||
Dummy(value: 1, marker: false),
|
||||
Dummy(value: 2, marker: false),
|
||||
Dummy(value: 3, marker: true),
|
||||
].groupedRanges { _, element in element.marker }
|
||||
].groupedRanges { element in element.marker }
|
||||
|
||||
expect(grouped).to(equal(
|
||||
[
|
||||
@ -168,7 +168,7 @@ class SwiftCommonsTest: XCTestCase {
|
||||
let grouped = [
|
||||
Dummy(value: 0, marker: true),
|
||||
Dummy(value: 1, marker: true),
|
||||
].groupedRanges { _, element in element.marker }
|
||||
].groupedRanges { element in element.marker }
|
||||
|
||||
expect(grouped).to(equal(
|
||||
[
|
||||
@ -184,7 +184,7 @@ class SwiftCommonsTest: XCTestCase {
|
||||
Dummy(value: 1, marker: false),
|
||||
Dummy(value: 1, marker: true),
|
||||
Dummy(value: 1, marker: true),
|
||||
].groupedRanges { _, element in element.marker }
|
||||
].groupedRanges { element in element.marker }
|
||||
|
||||
expect(grouped).to(equal(
|
||||
[
|
||||
@ -202,7 +202,7 @@ class SwiftCommonsTest: XCTestCase {
|
||||
Dummy(value: 1, marker: true),
|
||||
Dummy(value: 1, marker: false),
|
||||
Dummy(value: 1, marker: true),
|
||||
].groupedRanges { _, element in element.marker }
|
||||
].groupedRanges { element in element.marker }
|
||||
|
||||
expect(grouped).to(equal(
|
||||
[
|
||||
@ -216,7 +216,7 @@ class SwiftCommonsTest: XCTestCase {
|
||||
func testArrayGroup6() {
|
||||
let grouped = [
|
||||
Dummy(value: 0, marker: true),
|
||||
].groupedRanges { _, element in element.marker }
|
||||
].groupedRanges { element in element.marker }
|
||||
|
||||
expect(grouped).to(equal(
|
||||
[
|
||||
|
@ -213,7 +213,7 @@ extension NvimView {
|
||||
) -> [AttributesRun] {
|
||||
rowRange.map { row in
|
||||
self.ugrid.cells[row][columnRange]
|
||||
.groupedRanges(with: { _, cell in cell.attrId })
|
||||
.groupedRanges(with: { cell in cell.attrId })
|
||||
.compactMap { range in
|
||||
let cells = self.ugrid.cells[row][range]
|
||||
|
||||
|
@ -102,7 +102,7 @@ final class Typesetter {
|
||||
return [FontGlyphRun(font: font, glyphs: glyphs, positions: positions)]
|
||||
}
|
||||
|
||||
let groupRanges = glyphs.groupedRanges { _, element in element == 0 }
|
||||
let groupRanges = glyphs.groupedRanges { element in element == 0 }
|
||||
let groupRuns: [[FontGlyphRun]] = groupRanges.map { range in
|
||||
if glyphs[range.lowerBound] == 0 {
|
||||
let nvimUtf16Cells = unichars[range].map { [$0] }
|
||||
|
@ -75,7 +75,7 @@ class PerfTester {
|
||||
let ugrid = self.ugrids[index]
|
||||
return rowRange.map { row in
|
||||
ugrid.cells[row][columnRange]
|
||||
.groupedRanges(with: { _, cell in cell.attrId })
|
||||
.groupedRanges(with: { cell in cell.attrId })
|
||||
.compactMap { range in
|
||||
let cells = ugrid.cells[row][range]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user