-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathCollectionMap.cs
More file actions
304 lines (269 loc) · 9.7 KB
/
Copy pathCollectionMap.cs
File metadata and controls
304 lines (269 loc) · 9.7 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
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
{
/// <summary>
/// Represents a collection map with key-value pairs.
/// Supports both Dictionary and List serialization formats for backward compatibility.
///
/// <para>
/// Serialization Format:
/// - Dictionary (Legacy): Uses key-value pairs for backward compatibility
/// - List (New): Uses ordered list for better performance and smaller size
/// </para>
///
/// <para>
/// Field Order (List Format):
/// 1. version - Serialization version number
/// 2. entries - List of key-value pairs ordered by key
/// </para>
/// </summary>
/// <remarks>
/// This class implements dual serialization support to ensure smooth migration
/// from the legacy Dictionary format to the new List format. The List format
/// provides better performance and smaller serialized data size.
///
/// <para>
/// Example usage:
/// <code>
/// // Create collection map
/// var map = new CollectionMap();
/// map.Add(1, 100);
/// map.Add(2, 200);
///
/// // Serialize to List format (new)
/// var serialized = map.Serialize(); // Returns List
///
/// // Deserialize from any format
/// var deserialized = new CollectionMap(serialized); // Supports both Dictionary and List
/// </code>
/// </para>
/// </remarks>
[Serializable]
public class CollectionMap : IState, IDictionary<int, int>, ISerializable
{
// Serialization version for backward compatibility
private const int SerializationVersion = 1;
// Field count constants for serialization
private const int CollectionMapFieldCount = 2; // version + entries
private Dictionary<int, int> _dictionary;
private IValue _serialized;
public CollectionMap()
{
_dictionary = new Dictionary<int, int>();
}
/// <summary>
/// Constructor for deserialization that supports both Dictionary and List formats.
/// </summary>
/// <param name="serialized">Serialized data in either Dictionary or List format</param>
/// <exception cref="ArgumentNullException">Thrown when serialized is null</exception>
/// <exception cref="ArgumentException">Thrown when serialized format is not supported</exception>
public CollectionMap(IValue serialized)
{
if (serialized == null)
{
throw new ArgumentNullException(nameof(serialized), "Serialized data cannot be null");
}
_serialized = serialized;
switch (serialized)
{
case Dictionary dict:
DeserializeFromDictionary(dict);
break;
case List list:
DeserializeFromList(list);
break;
default:
throw new ArgumentException(
$"Unsupported serialization format: {serialized.GetType().Name}. " +
$"Expected Dictionary or List, got {serialized.GetType().Name}. " +
$"This may indicate corrupted data or an unsupported serialization format.");
}
}
/// <summary>
/// Constructor for backward compatibility with Dictionary format.
/// </summary>
/// <param name="serialized">Dictionary containing serialized data</param>
public CollectionMap(Bencodex.Types.Dictionary serialized)
{
DeserializeFromDictionary(serialized);
}
/// <summary>
/// Deserializes data from Dictionary format (legacy support).
/// </summary>
/// <param name="dict">Dictionary containing serialized data</param>
private void DeserializeFromDictionary(Dictionary dict)
{
_dictionary = dict.ToDictionary(
kv => kv.Key.ToInteger(),
kv => kv.Value.ToInteger()
);
}
/// <summary>
/// Deserializes data from List format (new format).
/// Order: [version, entries]
/// </summary>
/// <param name="list">List containing serialized data</param>
private void DeserializeFromList(List list)
{
// Check if we have enough fields for CollectionMap
if (list.Count < CollectionMapFieldCount)
{
var fieldNames = string.Join(", ", GetFieldNames());
throw new ArgumentException(
$"Invalid list length for {GetType().Name}: expected at least {CollectionMapFieldCount}, got {list.Count}. " +
$"Required fields: {fieldNames}. " +
$"This may indicate corrupted data or an unsupported serialization format.");
}
// version (index 0)
var version = ((Integer)list[0]).Value;
if (version != SerializationVersion)
{
throw new ArgumentException(
$"Unsupported serialization version: {version}. " +
$"Expected {SerializationVersion}. " +
$"This may indicate corrupted data or an unsupported serialization format.");
}
// entries (index 1)
var entriesList = (List)list[1];
_dictionary = new Dictionary<int, int>();
foreach (var entry in entriesList)
{
var entryList = (List)entry;
var key = entryList[0].ToInteger();
var value = entryList[1].ToInteger();
_dictionary[key] = value;
}
}
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;
}
if (_serialized is List l)
{
DeserializeFromList(l);
_serialized = null;
}
return _dictionary;
}
set
{
_dictionary = value;
_serialized = null;
}
}
/// <summary>
/// Serializes the CollectionMap to List format (new format).
/// Order: [version, entries]
/// </summary>
/// <returns>List containing serialized data</returns>
public IValue Serialize()
{
var entries = Dictionary
.OrderBy(kv => kv.Key)
.Select(kv => List.Empty
.Add(kv.Key.Serialize())
.Add(kv.Value.Serialize()));
return List.Empty
.Add(SerializationVersion)
.Add(new List(entries));
}
/// <summary>
/// Gets the field names for serialization in order.
/// </summary>
/// <returns>Array of field names</returns>
private static string[] GetFieldNames()
{
return new[] { "version", "entries" };
}
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);
}
}
}