carbBolusArrays.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. func findNextCarbTime(timeWithin: Int, needle: TimeInterval, haystack: [carbGraphStruct], startingIndex: Int) -> Bool {
  29. if startingIndex > haystack.count - 2 { return false }
  30. if haystack[startingIndex + 1].date - needle < Double(timeWithin) {
  31. return true
  32. }
  33. return false
  34. }
  35. func findNextBolusTime(timeWithin: Int, needle: TimeInterval, haystack: [bolusGraphStruct], startingIndex: Int) -> Bool {
  36. var last = false
  37. var next = true
  38. if startingIndex > haystack.count - 2 { return false }
  39. if startingIndex == 0 { return false }
  40. // Nothing to right that requires shift
  41. if haystack[startingIndex + 1].date - needle > Double(timeWithin) {
  42. return false
  43. } else {
  44. // Nothing to left preventing shift
  45. if needle - haystack[startingIndex - 1].date > Double(timeWithin) {
  46. return true
  47. }
  48. }
  49. return false
  50. }
  51. }