Skip to content

Commit

Permalink
Migrated many for loops to their appropriate algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
jjatie committed Sep 17, 2018
1 parent 40df0e7 commit 30595e4
Show file tree
Hide file tree
Showing 35 changed files with 224 additions and 600 deletions.
8 changes: 3 additions & 5 deletions Source/Charts/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,9 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate

// calculate how many digits are needed
setupDefaultFormatter(min: _data.getYMin(), max: _data.getYMax())

for set in _data.dataSets
{
if set.needsFormatter || set.valueFormatter === _defaultValueFormatter
{

_data.dataSets.forEach { (set) in
if set.needsFormatter || set.valueFormatter === _defaultValueFormatter {
set.valueFormatter = _defaultValueFormatter
}
}
Expand Down
46 changes: 11 additions & 35 deletions Source/Charts/Charts/PieChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,22 +228,10 @@ open class PieChartView: PieRadarChartViewBase
/// Checks if the given index is set to be highlighted.
@objc open func needsHighlight(index: Int) -> Bool
{
// no highlight
if !valuesToHighlight()
{
return false
// check if the xvalue for the given dataset needs highlight
return _indicesToHighlight.contains {
Int($0.x) == index
}

for i in 0 ..< _indicesToHighlight.count
{
// check if the xvalue for the given dataset needs highlight
if Int(_indicesToHighlight[i].x) == index
{
return true
}
}

return false
}

/// calculates the needed angle for a given value
Expand All @@ -268,31 +256,19 @@ open class PieChartView: PieRadarChartViewBase
{
// take the current angle of the chart into consideration
let a = (angle - self.rotationAngle).normalizedAngle
for i in 0 ..< _absoluteAngles.count
{
if _absoluteAngles[i] > a
{
return i
}
}

return -1 // return -1 if no index found
return absoluteAngles.firstIndex {
$0 > a
} ?? -1 // return -1 if no index found
}

/// - returns: The index of the DataSet this x-index belongs to.
@objc open func dataSetIndexForIndex(_ xValue: Double) -> Int
{
var dataSets = _data?.dataSets ?? []

for i in 0 ..< dataSets.count
{
if (dataSets[i].entryForXValue(xValue, closestToY: Double.nan) !== nil)
{
return i
}
}

return -1
return _data?
.dataSets
.firstIndex {
$0.entryForXValue(xValue, closestToY: .nan) != nil
} ?? -1
}

/// - returns: An integer array of all the different angles the chart slices
Expand Down
33 changes: 12 additions & 21 deletions Source/Charts/Charts/PieRadarChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -698,27 +698,18 @@ open class PieRadarChartViewBase: ChartViewBase
return 0.0
}

var firstSample = _velocitySamples[0]
var lastSample = _velocitySamples[_velocitySamples.count - 1]

// Look for a sample that's closest to the latest sample, but not the same, so we can deduce the direction
var beforeLastSample = firstSample
for i in stride(from: (_velocitySamples.count - 1), through: 0, by: -1)
{
beforeLastSample = _velocitySamples[i]
if beforeLastSample.angle != lastSample.angle
{
break
}
}

var firstSample = _velocitySamples.first!
var lastSample = _velocitySamples.last!

// Look for a sample that's closest to the last sample, but not the same, so we can deduce the direction
let beforeLastSample = _velocitySamples.reversed()
.last {
$0.angle == lastSample.angle
} ?? firstSample

// Calculate the sampling time
var timeDelta = lastSample.time - firstSample.time
if timeDelta == 0.0
{
timeDelta = 0.1
}

let timeDelta = CGFloat(max(lastSample.time - firstSample.time, 0.1))

// Calculate clockwise/ccw by choosing two values that should be closest to each other,
// so if the angles are two far from each other we know they are inverted "for sure"
var clockwise = lastSample.angle >= beforeLastSample.angle
Expand All @@ -738,7 +729,7 @@ open class PieRadarChartViewBase: ChartViewBase
}

// The velocity
var velocity = abs((lastSample.angle - firstSample.angle) / CGFloat(timeDelta))
var velocity = abs((lastSample.angle - firstSample.angle) / timeDelta)

// Direction?
if !clockwise
Expand Down
20 changes: 6 additions & 14 deletions Source/Charts/Charts/RadarChartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,12 @@ open class RadarChartView: PieRadarChartViewBase
let sliceAngle = self.sliceAngle

let max = _data?.maxEntryCountSet?.entryCount ?? 0

var index = 0

for i in 0..<max
{
let referenceAngle = sliceAngle * CGFloat(i + 1) - sliceAngle / 2.0

if referenceAngle > a
{
index = i
break
}
}


let index = (0..<max).first {
let referenceAngle = sliceAngle * CGFloat($0 + 1) - sliceAngle / 2.0
return referenceAngle > a
} ?? 0

return index
}

Expand Down
25 changes: 7 additions & 18 deletions Source/Charts/Components/AxisBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,12 @@ open class AxisBase: ComponentBase

@objc open func getLongestLabel() -> String
{
var longest = ""

for i in 0 ..< entries.count
{
let text = getFormattedLabel(i)

if longest.count < text.count
{
longest = text
}
let longest = entries.indices
.reduce("") {
let text = getFormattedLabel($1)
return $0.count < text.count ? text : $0
}

return longest
}

Expand Down Expand Up @@ -261,13 +255,8 @@ open class AxisBase: ComponentBase
/// Removes the specified ChartLimitLine from the axis.
@objc open func removeLimitLine(_ line: ChartLimitLine)
{
for i in 0 ..< _limitLines.count
{
if _limitLines[i] === line
{
_limitLines.remove(at: i)
return
}
if let index = _limitLines.firstIndex(where: { $0 === line }) {
_limitLines.remove(at: index)
}
}

Expand Down
20 changes: 5 additions & 15 deletions Source/Charts/Data/Implementations/ChartBaseDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,9 @@ open class ChartBaseDataSet: NSObject, IChartDataSet
/// - parameter alpha: alpha to apply to the set `colors`
@objc open func setColors(_ colors: [NSUIColor], alpha: CGFloat)
{
var colorsWithAlpha = colors

for i in 0 ..< colorsWithAlpha.count
{
colorsWithAlpha[i] = colorsWithAlpha[i] .withAlphaComponent(alpha)
self.colors = colors.map {
$0.withAlphaComponent(alpha)
}

self.colors = colorsWithAlpha
}

/// Sets colors with a specific alpha value.
Expand Down Expand Up @@ -398,15 +393,10 @@ open class ChartBaseDataSet: NSObject, IChartDataSet
}

open override var debugDescription: String
{
var desc = description + ":"

for i in 0 ..< self.entryCount
{
desc += "\n" + (self.entryForIndex(i)?.description ?? "")
{
return (0..<entryCount).reduce(into: description + ":") {
$0 += "\n" + (self.entryForIndex($1)?.description ?? "")
}

return desc
}

// MARK: - NSCopying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ open class BarChartData: BarLineScatterCandleBubbleChartData
{
fromX += diff
}

}

notifyDataChanged()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,8 @@ open class BarChartDataEntry: ChartDataEntry
/// - returns:
private static func calcSum(values: [Double]?) -> Double
{
guard let values = values
else { return 0.0 }

var sum = 0.0

for f in values
{
sum += f
}

return sum
return values?.reduce(into: 0.0) {
$0 += $1
} ?? 0
}
}
25 changes: 5 additions & 20 deletions Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,17 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, IBarChartDat
/// stacks. All values belonging to a stack are calculated separately.
private func calcEntryCountIncludingStacks(entries: [BarChartDataEntry])
{
_entryCountStacks = 0

for i in 0 ..< entries.count
{
if let vals = entries[i].yValues
{
_entryCountStacks += vals.count
}
else
{
_entryCountStacks += 1
}
_entryCountStacks = entries.reduce(into: 0) {
$0 += $1.yValues?.count ?? 1
}
}

/// calculates the maximum stacksize that occurs in the Entries array of this DataSet
private func calcStackSize(entries: [BarChartDataEntry])
{
for i in 0 ..< entries.count
{
if let vals = entries[i].yValues
{
if vals.count > _stackSize
{
_stackSize = vals.count
}
_stackSize = entries.reduce(into: 1) {
if let count = $1.yValues?.count {
$0 = count
}
}
}
Expand Down
Loading

0 comments on commit 30595e4

Please sign in to comment.