-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathCollectionMap.cs
155 lines (132 loc) · 4.08 KB
/
CollectionMap.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using Bencodex.Types;
using Nekoyume.Model.State;
namespace Nekoyume.Model
{
[Serializable]
public class CollectionMap : IState, IDictionary<int, int>, ISerializable
{
private Dictionary<int, int> _dictionary;
private Dictionary _serialized;
public CollectionMap()
{
_dictionary = new Dictionary<int, int>();
}
public CollectionMap(Bencodex.Types.Dictionary serialized)
{
_serialized = serialized;
}
private CollectionMap(SerializationInfo info, StreamingContext context)
{
_dictionary = (Dictionary<int, int>)info.GetValue(
nameof(Dictionary),
typeof(Dictionary<int, int>)
);
}
private Dictionary<int, int> Dictionary
{
get
{
if (_serialized is Dictionary d)
{
_dictionary = d.ToDictionary(
kv => kv.Key.ToInteger(),
kv => kv.Value.ToInteger()
);
_serialized = null;
return _dictionary;
}
return _dictionary;
}
set
{
_dictionary = value;
_serialized = null;
}
}
[SuppressMessage(
"Libplanet.Analyzers.ActionAnalyzer",
"LAA1002",
Justification = "It's serialized into a dictionary in the end.")]
public IValue Serialize() => _serialized ?? new Dictionary(
Dictionary.Select(kv =>
new KeyValuePair<IKey, IValue>(
(Text) kv.Key.ToString(CultureInfo.InvariantCulture),
(Text) kv.Value.ToString(CultureInfo.InvariantCulture)
)
)
);
public void Add(KeyValuePair<int, int> item)
{
if (Dictionary.ContainsKey(item.Key))
{
Dictionary[item.Key] += item.Value;
}
else
{
Dictionary[item.Key] = item.Value;
}
}
public void Clear()
{
Dictionary.Clear();
}
public bool Contains(KeyValuePair<int, int> item)
{
#pragma warning disable LAA1002
return Dictionary.Contains(item);
#pragma warning restore LAA1002
}
public void CopyTo(KeyValuePair<int, int>[] array, int arrayIndex)
{
throw new System.NotImplementedException();
}
public bool Remove(KeyValuePair<int, int> item)
{
return Dictionary.Remove(item.Key);
}
public int Count => Dictionary.Count;
public bool IsReadOnly => false;
public IEnumerator<KeyValuePair<int, int>> GetEnumerator()
{
return Dictionary.OrderBy(kv => kv.Key).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(int key, int value)
{
Add(new KeyValuePair<int, int>(key, value));
}
public bool ContainsKey(int key)
{
return Dictionary.ContainsKey(key);
}
public bool Remove(int key)
{
return Dictionary.Remove(key);
}
public bool TryGetValue(int key, out int value)
{
return Dictionary.TryGetValue(key, out value);
}
public int this[int key]
{
get => Dictionary[key];
set => Dictionary[key] = value;
}
public ICollection<int> Keys => Dictionary.Keys;
public ICollection<int> Values => Dictionary.Values;
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue(nameof(Dictionary), Dictionary);
}
}
}