-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNpzDictionary.cs
526 lines (462 loc) · 23.8 KB
/
NpzDictionary.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.IO.Compression;
using System.Linq;
namespace ChainerModelLoader
{
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Dictionary of npzs. </summary>
///
/// <seealso cref="T:ChainerModelLoader.NpzDictionary{System.Array}"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
public class NpzDictionary : NpzDictionary<Array>
{
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Initializes a new instance of the ChainerModelLoader.NpzDictionary class. </summary>
///
/// <param name="path"> Full pathname of the file. </param>
////////////////////////////////////////////////////////////////////////////////////////////////////
public NpzDictionary(string path): base(new FileStream(path, FileMode.Open))
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a new instance of the ChainerModelLoader.NpzDictionary class.
/// </summary>
///
/// <param name="stream"> The stream. </param>
////////////////////////////////////////////////////////////////////////////////////////////////////
public NpzDictionary(Stream stream) : base(stream)
{
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Loads the given s. </summary>
///
/// <param name="s"> The s to load. </param>
///
/// <returns> A T. </returns>
///
/// <seealso cref="M:ChainerModelLoader.NpzDictionary{System.Array}.Load(Stream)"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
protected override Array Load(Stream s)
{
return NpyFormat.LoadMatrix(s);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Dictionary of npzs. </summary>
///
/// <typeparam name="T"> Generic type parameter. </typeparam>
///
/// <seealso cref="T:System.IDisposable"/>
/// <seealso cref="T:System.Collections.Generic.IReadOnlyDictionary{System.String, T}"/>
/// <seealso cref="T:System.Collections.Generic.ICollection{T}"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
public class NpzDictionary<T> : IDisposable, IReadOnlyDictionary<string, T>, ICollection<T> where T : class, ICloneable, IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
{
/// <summary> The stream. </summary>
Stream stream;
/// <summary> The archive. </summary>
ZipArchive archive;
/// <summary> True to disposed value. </summary>
bool disposedValue = false;
/// <summary> The entries. </summary>
Dictionary<string, ZipArchiveEntry> entries;
/// <summary> The arrays. </summary>
Dictionary<string, T> arrays;
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Initializes a new instance of the ChainerModelLoader.NpzDictionary<T> class.
/// </summary>
///
/// <param name="stream"> The stream. </param>
////////////////////////////////////////////////////////////////////////////////////////////////////
public NpzDictionary(Stream stream)
{
this.stream = stream;
archive = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen: true);
entries = new Dictionary<string, ZipArchiveEntry>();
foreach (var entry in archive.Entries)
{
entries[entry.FullName] = entry;
}
arrays = new Dictionary<string, T>();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Gets an enumerable collection that contains the keys in the read-only dictionary.
/// </summary>
///
/// <value> An enumerable collection that contains the keys in the read-only dictionary. </value>
///
/// <seealso cref="P:System.Collections.Generic.IReadOnlyDictionary{System.String, T}.Keys"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
public IEnumerable<string> Keys
{
get { return entries.Keys; }
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Gets an enumerable collection that contains the values in the read-only dictionary.
/// </summary>
///
/// <value> An enumerable collection that contains the values in the read-only dictionary. </value>
///
/// <seealso cref="P:System.Collections.Generic.IReadOnlyDictionary{System.String, T}.Values"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
public IEnumerable<T> Values
{
get { return entries.Values.Select(OpenEntry); }
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Gets the number of elements contained in the
/// <see cref="T:System.Collections.Generic.ICollection`1" />.
/// </summary>
///
/// <value>
/// The number of elements contained in the
/// <see cref="T:System.Collections.Generic.ICollection`1" />.
/// </value>
///
/// <seealso cref="P:System.Collections.Generic.ICollection{T}.Count"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
public int Count
{
get { return entries.Count; }
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Gets the synchronise root. </summary>
///
/// <value> The synchronise root. </value>
////////////////////////////////////////////////////////////////////////////////////////////////////
public object SyncRoot
{
get { return ((ICollection)entries).SyncRoot; }
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Gets a value indicating whether this object is synchronized. </summary>
///
/// <value> True if this object is synchronized, false if not. </value>
////////////////////////////////////////////////////////////////////////////////////////////////////
public bool IsSynchronized
{
get { return ((ICollection)entries).IsSynchronized; }
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1" />
/// is read-only.
/// </summary>
///
/// <value>
/// true if the <see cref="T:System.Collections.Generic.ICollection`1" /> is read-only; otherwise,
/// false.
/// </value>
///
/// <seealso cref="P:System.Collections.Generic.ICollection{T}.IsReadOnly"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
public bool IsReadOnly
{
get { return true; }
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Gets the element that has the specified key in the read-only dictionary. </summary>
///
/// <exception cref="T:System.ArgumentNullException"> . </exception>
/// <exception cref="T:System.Collections.Generic.KeyNotFoundException"> The property is
/// retrieved and
/// <paramref name="key" />
/// is not found. </exception>
///
/// <param name="key"> The key to locate. </param>
///
/// <returns> The element that has the specified key in the read-only dictionary. </returns>
///
/// <seealso cref="M:System.Collections.Generic.IReadOnlyDictionary{System.String, T}.this(string)"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
public T this[string key]
{
get { return OpenEntry(entries[key]); }
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Opens an entry. </summary>
///
/// <param name="entry"> The entry. </param>
///
/// <returns> A T. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
private T OpenEntry(ZipArchiveEntry entry)
{
T array;
if (arrays.TryGetValue(entry.FullName, out array))
return array;
Stream s = entry.Open();
array = Load(s);
arrays[entry.FullName] = array;
return array;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Loads the given s. </summary>
///
/// <param name="s"> The s to load. </param>
///
/// <returns> A T. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
protected virtual T Load(Stream s)
{
return NpyFormat.Load<T>(s);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Determines whether the read-only dictionary contains an element that has the specified key.
/// </summary>
///
/// <exception cref="T:System.ArgumentNullException"> . </exception>
///
/// <param name="key"> The key to locate. </param>
///
/// <returns>
/// true if the read-only dictionary contains an element that has the specified key; otherwise,
/// false.
/// </returns>
///
/// <seealso cref="M:System.Collections.Generic.IReadOnlyDictionary{System.String, T}.ContainsKey(string)"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
public bool ContainsKey(string key)
{
return entries.ContainsKey(key);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Gets the value that is associated with the specified key. </summary>
///
/// <exception cref="T:System.ArgumentNullException"> . </exception>
///
/// <param name="key"> The key to locate. </param>
/// <param name="value"> [out] When this method returns, the value associated with the
/// specified key, if the key is found; otherwise, the default value for the
/// type of the <paramref name="value" /> parameter. This parameter is passed
/// uninitialized. </param>
///
/// <returns>
/// true if the object that implements the
/// <see cref="T:System.Collections.Generic.IReadOnlyDictionary`2" /> interface contains an
/// element that has the specified key; otherwise, false.
/// </returns>
///
/// <seealso cref="M:System.Collections.Generic.IReadOnlyDictionary{System.String, T}.TryGetValue(string,out T)"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
public bool TryGetValue(string key, out T value)
{
value = default(T);
ZipArchiveEntry entry;
if (!entries.TryGetValue(key, out entry))
return false;
value = OpenEntry(entry);
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Gets the enumerator. </summary>
///
/// <returns> The enumerator. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
public IEnumerator<KeyValuePair<string, T>> GetEnumerator()
{
foreach (var entry in archive.Entries)
yield return new KeyValuePair<string, T>(entry.FullName, OpenEntry(entry));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Gets the enumerator. </summary>
///
/// <returns> The enumerator. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
IEnumerator IEnumerable.GetEnumerator()
{
foreach (var entry in archive.Entries)
yield return new KeyValuePair<string, T>(entry.FullName, OpenEntry(entry));
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Gets the enumerator. </summary>
///
/// <typeparam name="T"> Generic type parameter. </typeparam>
///
/// <returns> The enumerator. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
foreach (var entry in archive.Entries)
yield return OpenEntry(entry);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1" /> to an
/// <see cref="T:System.Array" />, starting at a particular <see cref="T:System.Array" /> index.
/// </summary>
///
/// <param name="array"> The array. </param>
/// <param name="arrayIndex"> Zero-based index of the array. </param>
////////////////////////////////////////////////////////////////////////////////////////////////////
public void CopyTo(Array array, int arrayIndex)
{
foreach (var v in this)
array.SetValue(v, arrayIndex++);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Copies the elements of the <see cref="T:System.Collections.Generic.ICollection`1" /> to an
/// <see cref="T:System.Array" />, starting at a particular <see cref="T:System.Array" /> index.
/// </summary>
///
/// <exception cref="T:System.ArgumentNullException"> . </exception>
/// <exception cref="T:System.ArgumentOutOfRangeException"> . </exception>
/// <exception cref="T:System.ArgumentException"> The number of elements in the source
/// <see cref="T:System.Collections.Generic.ICollection`1" />
/// is greater than the available space
/// from <paramref name="arrayIndex" />
/// to the end of the destination
/// <paramref name="array" />. </exception>
///
/// <param name="array"> The one-dimensional <see cref="T:System.Array" /> that is the
/// destination of the elements copied from
/// <see cref="T:System.Collections.Generic.ICollection`1" />. The
/// <see cref="T:System.Array" /> must have zero-based indexing. </param>
/// <param name="arrayIndex"> The zero-based index in <paramref name="array" /> at which
/// copying begins. </param>
///
/// <seealso cref="M:System.Collections.Generic.ICollection{T}.CopyTo(T[],int)"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
public void CopyTo(T[] array, int arrayIndex)
{
foreach (var v in this)
array.SetValue(v, arrayIndex++);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1" />.
/// </summary>
///
/// <exception cref="ReadOnlyException"> Thrown when a Read Only error condition
/// occurs. </exception>
/// <exception cref="T:System.NotSupportedException"> The
/// <see cref="T:System.Collections.Generic.ICollection`1" />
/// is read-only. </exception>
///
/// <param name="item"> The object to add to the
/// <see cref="T:System.Collections.Generic.ICollection`1" />. </param>
///
/// <seealso cref="M:System.Collections.Generic.ICollection{T}.Add(T)"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
public void Add(T item)
{
throw new ReadOnlyException();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1" />.
/// </summary>
///
/// <exception cref="ReadOnlyException"> Thrown when a Read Only error condition
/// occurs. </exception>
/// <exception cref="T:System.NotSupportedException"> The
/// <see cref="T:System.Collections.Generic.ICollection`1" />
/// is read-only. </exception>
///
/// <seealso cref="M:System.Collections.Generic.ICollection{T}.Clear()"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
public void Clear()
{
throw new ReadOnlyException();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1" /> contains a
/// specific value.
/// </summary>
///
/// <param name="item"> The object to locate in the
/// <see cref="T:System.Collections.Generic.ICollection`1" />. </param>
///
/// <returns>
/// true if <paramref name="item" /> is found in the
/// <see cref="T:System.Collections.Generic.ICollection`1" />; otherwise, false.
/// </returns>
///
/// <seealso cref="M:System.Collections.Generic.ICollection{T}.Contains(T)"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
public bool Contains(T item)
{
foreach (var v in this)
if (Object.Equals(v.Value, item))
return true;
return false;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Removes the first occurrence of a specific object from the
/// <see cref="T:System.Collections.Generic.ICollection`1" />.
/// </summary>
///
/// <exception cref="ReadOnlyException"> Thrown when a Read Only error condition
/// occurs. </exception>
/// <exception cref="T:System.NotSupportedException"> The
/// <see cref="T:System.Collections.Generic.ICollection`1" />
/// is read-only. </exception>
///
/// <param name="item"> The object to remove from the
/// <see cref="T:System.Collections.Generic.ICollection`1" />. </param>
///
/// <returns>
/// true if <paramref name="item" /> was successfully removed from the
/// <see cref="T:System.Collections.Generic.ICollection`1" />; otherwise, false. This method also
/// returns false if <paramref name="item" /> is not found in the original
/// <see cref="T:System.Collections.Generic.ICollection`1" />.
/// </returns>
///
/// <seealso cref="M:System.Collections.Generic.ICollection{T}.Remove(T)"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
public bool Remove(T item)
{
throw new ReadOnlyException();
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged
/// resources.
/// </summary>
///
/// <param name="disposing"> True to release both managed and unmanaged resources; false to
/// release only unmanaged resources. </param>
////////////////////////////////////////////////////////////////////////////////////////////////////
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
archive.Dispose();
stream.Dispose();
}
archive = null;
stream = null;
entries = null;
arrays = null;
disposedValue = true;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged
/// resources.
/// </summary>
///
/// <seealso cref="M:System.IDisposable.Dispose()"/>
////////////////////////////////////////////////////////////////////////////////////////////////////
public void Dispose()
{
Dispose(true);
}
}
}