about summary refs log tree commit diff
path: root/LibMatrix/Extensions/EnumerableExtensions.cs
diff options
context:
space:
mode:
Diffstat (limited to 'LibMatrix/Extensions/EnumerableExtensions.cs')
-rw-r--r--LibMatrix/Extensions/EnumerableExtensions.cs90
1 files changed, 76 insertions, 14 deletions
diff --git a/LibMatrix/Extensions/EnumerableExtensions.cs b/LibMatrix/Extensions/EnumerableExtensions.cs
index 42d9491..ace2c0c 100644
--- a/LibMatrix/Extensions/EnumerableExtensions.cs
+++ b/LibMatrix/Extensions/EnumerableExtensions.cs
@@ -1,29 +1,91 @@
+using System.Collections.Frozen;
+using System.Collections.Immutable;
+
 namespace LibMatrix.Extensions;
 
 public static class EnumerableExtensions {
+    public static int insertions = 0;
+    public static int replacements = 0;
+
     public static void MergeStateEventLists(this IList<StateEvent> oldState, IList<StateEvent> newState) {
-        foreach (var stateEvent in newState) {
-            var old = oldState.FirstOrDefault(x => x.Type == stateEvent.Type && x.StateKey == stateEvent.StateKey);
-            if (old is null) {
-                oldState.Add(stateEvent);
-                continue;
+        // foreach (var stateEvent in newState) {
+        //     var old = oldState.FirstOrDefault(x => x.Type == stateEvent.Type && x.StateKey == stateEvent.StateKey);
+        //     if (old is null) {
+        //         oldState.Add(stateEvent);
+        //         continue;
+        //     }
+        //
+        //     oldState.Remove(old);
+        //     oldState.Add(stateEvent);
+        // }
+
+        foreach (var e in newState) {
+            switch (FindIndex(e)) {
+                case -1:
+                    oldState.Add(e);
+                    break;
+                case var index:
+                    oldState[index] = e;
+                    break;
             }
+        }
 
-            oldState.Remove(old);
-            oldState.Add(stateEvent);
+        int FindIndex(StateEvent needle) {
+            for (int i = 0; i < oldState.Count; i++) {
+                var old = oldState[i];
+                if (old.Type == needle.Type && old.StateKey == needle.StateKey)
+                    return i;
+            }
+
+            return -1;
         }
     }
 
     public static void MergeStateEventLists(this IList<StateEventResponse> oldState, IList<StateEventResponse> newState) {
-        foreach (var stateEvent in newState) {
-            var old = oldState.FirstOrDefault(x => x.Type == stateEvent.Type && x.StateKey == stateEvent.StateKey);
-            if (old is null) {
-                oldState.Add(stateEvent);
-                continue;
+        foreach (var e in newState) {
+            switch (FindIndex(e)) {
+                case -1:
+                    oldState.Add(e);
+                    break;
+                case var index:
+                    oldState[index] = e;
+                    break;
+            }
+        }
+
+        int FindIndex(StateEventResponse needle) {
+            for (int i = 0; i < oldState.Count; i++) {
+                var old = oldState[i];
+                if (old.Type == needle.Type && old.StateKey == needle.StateKey)
+                    return i;
+            }
+
+            return -1;
+        }
+    }
+
+    public static void MergeStateEventLists(this List<StateEventResponse> oldState, List<StateEventResponse> newState) {
+        foreach (var e in newState) {
+            switch (FindIndex(e)) {
+                case -1:
+                    oldState.Add(e);
+                    insertions++;
+                    break;
+                case var index:
+                    oldState[index] = e;
+                    replacements++;
+                    break;
+            }
+        }
+
+        int FindIndex(StateEventResponse needle) {
+            for (int i = 0; i < oldState.Count; i++) {
+                var old = oldState[i];
+                if (old.Type == needle.Type && old.StateKey == needle.StateKey)
+                    return i;
             }
 
-            oldState.Remove(old);
-            oldState.Add(stateEvent);
+            return -1;
         }
     }
 }
\ No newline at end of file