// LoopFollow // carbBolusArrays.swift import Foundation extension MainViewController { func findNearestBGbyTime(needle: TimeInterval, haystack: [ShareGlucoseData], startingIndex: Int) -> (sgv: Double, foundIndex: Int) { // If we can't find a match or things fail, put it at 100 BG if startingIndex > haystack.count { return (100.00, 0) } for i in startingIndex ..< haystack.count { // i has reached the end without a result. Put the dot at 100 if i == haystack.count - 1 { return (100.00, 0) } if needle >= haystack[i].date, needle < haystack[i + 1].date { return (Double(haystack[i].sgv), i) } } return (100.00, 0) } func findNearestBolusbyTime(timeWithin: Int, needle: TimeInterval, haystack: [bolusGraphStruct], startingIndex: Int) -> (offset: Bool, foundIndex: Int) { // If we can't find a match or things fail, put it at 100 BG for i in startingIndex ..< haystack.count { // i has reached the end without a result. return 0 let timeDiff = needle - haystack[i].date if timeDiff <= Double(timeWithin), timeDiff >= Double(-timeWithin) { return (true, i) } if i == haystack.count - 1 { return (false, 0) } if timeDiff < Double(-timeWithin) { return (false, 0) } } return (false, 0) } }