Better comments on DeduplicateStateEntries

This commit is contained in:
Neil Alexander 2020-09-10 14:58:45 +01:00
parent b22731cdcd
commit d96780c376
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944

View file

@ -74,22 +74,23 @@ func (a StateEntry) LessThan(b StateEntry) bool {
return a.EventNID < b.EventNID return a.EventNID < b.EventNID
} }
// Deduplicate ensures that the latest NIDs are always presented in the case of duplicates. // Deduplicate takes a set of state entries and ensures that there are no
// duplicate (event type, state key) tuples. If there are then we dedupe
// them, making sure that the latest/highest NIDs are always chosen.
func DeduplicateStateEntries(a []StateEntry) []StateEntry { func DeduplicateStateEntries(a []StateEntry) []StateEntry {
result := a
if len(a) < 2 { if len(a) < 2 {
return a return a
} }
sort.SliceStable(a, func(i, j int) bool { sort.SliceStable(a, func(i, j int) bool {
return a[i].LessThan(a[j]) return a[i].LessThan(a[j])
}) })
for i := 0; i < len(result)-1; i++ { for i := 0; i < len(a)-1; i++ {
if result[i].StateKeyTuple == result[i+1].StateKeyTuple { if a[i].StateKeyTuple == a[i+1].StateKeyTuple {
result = append(result[:i], result[i+1:]...) a = append(a[:i], a[i+1:]...)
i-- i--
} }
} }
return result return a
} }
// StateAtEvent is the state before and after a matrix event. // StateAtEvent is the state before and after a matrix event.