about summary refs log tree commit diff
path: root/LibMatrix/Extensions/EnumerableExtensions.cs
blob: ace2c0c2abeb18b2d92a7b63fdecb1f53259e0bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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;
        //     }
        //
        //     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;
            }
        }

        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 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;
            }

            return -1;
        }
    }
}