From d96780c376110d5b14bf05c5c1d523810f5036e0 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Thu, 10 Sep 2020 14:58:45 +0100 Subject: [PATCH] Better comments on DeduplicateStateEntries --- roomserver/types/types.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/roomserver/types/types.go b/roomserver/types/types.go index f573ce3e0..f5b45763f 100644 --- a/roomserver/types/types.go +++ b/roomserver/types/types.go @@ -74,22 +74,23 @@ func (a StateEntry) LessThan(b StateEntry) bool { 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 { - result := a if len(a) < 2 { return a } sort.SliceStable(a, func(i, j int) bool { return a[i].LessThan(a[j]) }) - for i := 0; i < len(result)-1; i++ { - if result[i].StateKeyTuple == result[i+1].StateKeyTuple { - result = append(result[:i], result[i+1:]...) + for i := 0; i < len(a)-1; i++ { + if a[i].StateKeyTuple == a[i+1].StateKeyTuple { + a = append(a[:i], a[i+1:]...) i-- } } - return result + return a } // StateAtEvent is the state before and after a matrix event.