calculateUnreadInRanges calculates how many unread messages are within the given ranges. unreadStart is the first unread message SeqId (readID + 1), unreadEnd is the last possible message SeqId. Assumes ranges are sorted by Low ascending.
(readID, lastID int, ranges []types.Range)
| 4008 | // unreadStart is the first unread message SeqId (readID + 1), unreadEnd is the last possible message SeqId. |
| 4009 | // Assumes ranges are sorted by Low ascending. |
| 4010 | func calculateUnreadInRanges(readID, lastID int, ranges []types.Range) int { |
| 4011 | if readID >= lastID { |
| 4012 | // No unread messages |
| 4013 | return 0 |
| 4014 | } |
| 4015 | |
| 4016 | unreadStart := readID + 1 |
| 4017 | unreadEnd := lastID |
| 4018 | |
| 4019 | // Sum up unread messages. |
| 4020 | count := 0 |
| 4021 | |
| 4022 | for i := 0; i < len(ranges); i++ { |
| 4023 | rangeStart := ranges[i].Low |
| 4024 | rangeEnd := ranges[i].Hi |
| 4025 | if rangeEnd == 0 { |
| 4026 | rangeEnd = rangeStart + 1 |
| 4027 | } |
| 4028 | // Find the first range where rangeEnd > readID |
| 4029 | if rangeEnd <= readID { |
| 4030 | continue |
| 4031 | } |
| 4032 | |
| 4033 | // Find intersection of [unreadStart, unreadEnd] and [rangeStart, rangeEnd) |
| 4034 | intersectionStart := max(unreadStart, rangeStart) |
| 4035 | intersectionEnd := min(unreadEnd+1, rangeEnd) // +1 because unreadEnd is inclusive |
| 4036 | |
| 4037 | if intersectionStart < intersectionEnd { |
| 4038 | count += intersectionEnd - intersectionStart |
| 4039 | } |
| 4040 | } |
| 4041 | |
| 4042 | return count |
| 4043 | } |
searching dependent graphs…