carbBolusArrays.swift 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // LoopFollow
  2. // carbBolusArrays.swift
  3. import Foundation
  4. extension MainViewController {
  5. func findNearestBGbyTime(needle: TimeInterval, haystack: [ShareGlucoseData], startingIndex: Int) -> (sgv: Double, foundIndex: Int) {
  6. // If we can't find a match or things fail, put it at 100 BG
  7. if startingIndex > haystack.count { return (100.00, 0) }
  8. for i in startingIndex ..< haystack.count {
  9. // i has reached the end without a result. Put the dot at 100
  10. if i == haystack.count - 1 { return (100.00, 0) }
  11. if needle >= haystack[i].date, needle < haystack[i + 1].date {
  12. return (Double(haystack[i].sgv), i)
  13. }
  14. }
  15. return (100.00, 0)
  16. }
  17. func findNearestBolusbyTime(timeWithin: Int, needle: TimeInterval, haystack: [bolusGraphStruct], startingIndex: Int) -> (offset: Bool, foundIndex: Int) {
  18. // If we can't find a match or things fail, put it at 100 BG
  19. for i in startingIndex ..< haystack.count {
  20. // i has reached the end without a result. return 0
  21. let timeDiff = needle - haystack[i].date
  22. if timeDiff <= Double(timeWithin), timeDiff >= Double(-timeWithin) { return (true, i) }
  23. if i == haystack.count - 1 { return (false, 0) }
  24. if timeDiff < Double(-timeWithin) { return (false, 0) }
  25. }
  26. return (false, 0)
  27. }
  28. }