-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelta.cs
54 lines (49 loc) · 1.13 KB
/
Delta.cs
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
using System;
using System.Linq;
using System.Collections.Generic;
namespace patchiron
{
enum DeltaType { Addition, AddAfter, Removal };
struct Delta
{
public DeltaType Type { get; private set; }
public string Data { get; private set; }
public static Delta CreateAddition (string data)
{
return new Delta () {
Type = DeltaType.Addition,
Data = data
};
}
public static Delta CreateAddAfter (string data)
{
return new Delta ()
{
Type = DeltaType.AddAfter,
Data = data
};
}
public static Delta Removal = new Delta () { Type = DeltaType.Removal };
public static void ApplyListToChunk (PatchChunk chunk, Dictionary<int, Delta> deltaList)
{
foreach (var kv in deltaList.OrderBy (x => x.Key).Reverse ())
{
var action = kv.Value.Type;
switch (action)
{
case DeltaType.Addition:
chunk.InsertAt (kv.Key, kv.Value.Data);
break;
case DeltaType.AddAfter:
chunk.InsertAt (kv.Key + 1, kv.Value.Data);
break;
case DeltaType.Removal:
chunk.RemoveAt (kv.Key);
break;
default:
throw new NotImplementedException ();
}
}
}
}
}