Skip to content

Commit 53f7c3a

Browse files
authored
Migrating to built-in algorithms (#3892)
1 parent a969c46 commit 53f7c3a

19 files changed

+103
-448
lines changed

Source/Charts/Charts/ChartViewBase.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
402402
/// - Returns: `true` if there are values to highlight, `false` ifthere are no values to highlight.
403403
@objc open func valuesToHighlight() -> Bool
404404
{
405-
return _indicesToHighlight.count > 0
405+
return !_indicesToHighlight.isEmpty
406406
}
407407

408408
/// Highlights the values at the given indices in the given DataSets. Provide

Source/Charts/Charts/PieChartView.swift

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,12 @@ open class PieChartView: PieRadarChartViewBase
191191
_absoluteAngles.reserveCapacity(entryCount)
192192

193193
let yValueSum = (_data as! PieChartData).yValueSum
194-
195-
var dataSets = data.dataSets
196194

197195
var cnt = 0
198196

199-
for i in 0 ..< data.dataSetCount
197+
for set in data.dataSets
200198
{
201-
let set = dataSets[i]
202-
let entryCount = set.entryCount
203-
204-
for j in 0 ..< entryCount
199+
for j in 0 ..< set.entryCount
205200
{
206201
guard let e = set.entryForIndex(j) else { continue }
207202

@@ -224,22 +219,7 @@ open class PieChartView: PieRadarChartViewBase
224219
/// Checks if the given index is set to be highlighted.
225220
@objc open func needsHighlight(index: Int) -> Bool
226221
{
227-
// no highlight
228-
if !valuesToHighlight()
229-
{
230-
return false
231-
}
232-
233-
for i in 0 ..< _indicesToHighlight.count
234-
{
235-
// check if the xvalue for the given dataset needs highlight
236-
if Int(_indicesToHighlight[i].x) == index
237-
{
238-
return true
239-
}
240-
}
241-
242-
return false
222+
return _indicesToHighlight.contains { Int($0.x) == index }
243223
}
244224

245225
/// calculates the needed angle for a given value
@@ -259,36 +239,22 @@ open class PieChartView: PieRadarChartViewBase
259239
{
260240
fatalError("PieChart has no XAxis")
261241
}
262-
242+
263243
open override func indexForAngle(_ angle: CGFloat) -> Int
264244
{
245+
// TODO: Return nil instead of -1
265246
// take the current angle of the chart into consideration
266247
let a = (angle - self.rotationAngle).normalizedAngle
267-
for i in 0 ..< _absoluteAngles.count
268-
{
269-
if _absoluteAngles[i] > a
270-
{
271-
return i
272-
}
273-
}
274-
275-
return -1 // return -1 if no index found
248+
return _absoluteAngles.firstIndex { $0 > a } ?? -1
276249
}
277250

278251
/// - Returns: The index of the DataSet this x-index belongs to.
279252
@objc open func dataSetIndexForIndex(_ xValue: Double) -> Int
280253
{
281-
var dataSets = _data?.dataSets ?? []
282-
283-
for i in 0 ..< dataSets.count
284-
{
285-
if (dataSets[i].entryForXValue(xValue, closestToY: Double.nan) !== nil)
286-
{
287-
return i
288-
}
289-
}
290-
291-
return -1
254+
// TODO: Return nil instead of -1
255+
return _data?.dataSets.firstIndex {
256+
$0.entryForXValue(xValue, closestToY: .nan) != nil
257+
} ?? -1
292258
}
293259

294260
/// - Returns: An integer array of all the different angles the chart slices

Source/Charts/Charts/RadarChartView.swift

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,9 @@ open class RadarChartView: PieRadarChartViewBase
168168
let sliceAngle = self.sliceAngle
169169

170170
let max = _data?.maxEntryCountSet?.entryCount ?? 0
171-
172-
var index = 0
173-
174-
for i in 0..<max
175-
{
176-
let referenceAngle = sliceAngle * CGFloat(i + 1) - sliceAngle / 2.0
177-
178-
if referenceAngle > a
179-
{
180-
index = i
181-
break
182-
}
183-
}
184-
185-
return index
171+
return (0..<max).firstIndex {
172+
sliceAngle * CGFloat($0 + 1) - sliceAngle / 2.0 > a
173+
} ?? max
186174
}
187175

188176
/// The object that represents all y-labels of the RadarChart.

Source/Charts/Components/AxisBase.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,8 @@ open class AxisBase: ComponentBase
261261
/// Removes the specified ChartLimitLine from the axis.
262262
@objc open func removeLimitLine(_ line: ChartLimitLine)
263263
{
264-
for i in 0 ..< _limitLines.count
265-
{
266-
if _limitLines[i] === line
267-
{
268-
_limitLines.remove(at: i)
269-
return
270-
}
271-
}
264+
guard let i = _limitLines.firstIndex(of: line) else { return }
265+
_limitLines.remove(at: i)
272266
}
273267

274268
/// Removes all LimitLines from the axis.

Source/Charts/Data/Implementations/ChartBaseDataSet.swift

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,7 @@ open class ChartBaseDataSet: NSObject, IChartDataSet, NSCopying
253253
/// - alpha: alpha to apply to the set `colors`
254254
@objc open func setColors(_ colors: [NSUIColor], alpha: CGFloat)
255255
{
256-
var colorsWithAlpha = colors
257-
258-
for i in 0 ..< colorsWithAlpha.count
259-
{
260-
colorsWithAlpha[i] = colorsWithAlpha[i] .withAlphaComponent(alpha)
261-
}
262-
263-
self.colors = colorsWithAlpha
256+
self.colors = colors.map { $0.withAlphaComponent(alpha) }
264257
}
265258

266259
/// Sets colors with a specific alpha value.
@@ -409,14 +402,9 @@ open class ChartBaseDataSet: NSObject, IChartDataSet, NSCopying
409402

410403
open override var debugDescription: String
411404
{
412-
var desc = description + ":"
413-
414-
for i in 0 ..< self.entryCount
415-
{
416-
desc += "\n" + (self.entryForIndex(i)?.description ?? "")
405+
return (0..<entryCount).reduce(description + ":") {
406+
$0 + "\n" + (self.entryForIndex($1)?.description ?? "")
417407
}
418-
419-
return desc
420408
}
421409

422410
// MARK: - NSCopying

0 commit comments

Comments
 (0)