Skip to content

Commit 6c7671a

Browse files
committed
Big fix.
1 parent 891d95a commit 6c7671a

File tree

7 files changed

+151
-139
lines changed

7 files changed

+151
-139
lines changed

src/Berrysoft.Data/DebugView.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Diagnostics;
43
using System.Linq;
54

65
namespace Berrysoft.Data
76
{
8-
internal sealed class MapDebugView<K, V>
7+
internal sealed class IMapDebugView<K, V>
98
{
10-
private readonly Map<K, V> map;
11-
public MapDebugView(Map<K, V> map)
9+
private readonly IMap<K, V> map;
10+
public IMapDebugView(IMap<K, V> map)
1211
{
1312
this.map = map ?? throw ExceptionHelper.ArgumentNull(nameof(map));
1413
}
@@ -23,20 +22,31 @@ public KeyPair<K, V>[] Items
2322
}
2423
}
2524
}
26-
internal sealed class LookupDebugView<K, V>
25+
internal sealed class IMutableLookupDebugView<K, V>
2726
{
28-
private readonly Lookup<K, V> lookup;
29-
public LookupDebugView(Lookup<K, V> lookup)
27+
private readonly IMutableLookup<K, V> lookup;
28+
public IMutableLookupDebugView(IMutableLookup<K, V> lookup)
3029
{
3130
this.lookup = lookup ?? throw ExceptionHelper.ArgumentNull(nameof(lookup));
3231
}
3332
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
34-
public Lookup<K, V>.Grouping[] Items => ((IEnumerable<Lookup<K, V>.Grouping>)lookup).ToArray();
33+
public IGrouping<K, V>[] Items => lookup.ToArray();
3534
}
36-
internal sealed class MultiMapDebugView<K, V>
35+
internal sealed class ICountableGroupingDebugView<K,V>
3736
{
38-
private readonly MultiMap<K, V> multiMap;
39-
public MultiMapDebugView(MultiMap<K, V> multiMap)
37+
private readonly ICountableGrouping<K, V> grouping;
38+
public ICountableGroupingDebugView(ICountableGrouping<K,V> grouping)
39+
{
40+
this.grouping = grouping ?? throw ExceptionHelper.ArgumentNull(nameof(grouping));
41+
}
42+
public K Key => grouping.Key;
43+
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
44+
public V[] Items => grouping.ToArray();
45+
}
46+
internal sealed class IMultiMapDebugView<K, V>
47+
{
48+
private readonly IMultiMap<K, V> multiMap;
49+
public IMultiMapDebugView(IMultiMap<K, V> multiMap)
4050
{
4151
this.multiMap = multiMap ?? throw ExceptionHelper.ArgumentNull(nameof(multiMap));
4252
}

src/Berrysoft.Data/Lookup.cs

Lines changed: 108 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ internal interface ICountableGrouping<TKey, TElement> : IGrouping<TKey, TElement
6969
/// <typeparam name="TKey">The type of the keys in the <see cref="Lookup{TKey, TElement}"/>.</typeparam>
7070
/// <typeparam name="TElement">The type of the elements of each value in the <see cref="Lookup{TKey, TElement}"/>.</typeparam>
7171
[Serializable]
72-
[DebuggerTypeProxy(typeof(LookupDebugView<,>))]
72+
[DebuggerTypeProxy(typeof(IMutableLookupDebugView<,>))]
7373
[DebuggerDisplay("Count = {Count}")]
74-
public class Lookup<TKey, TElement> : IMutableLookup<TKey, TElement>, IEnumerable<KeyValuePair<TKey, TElement>>, IEnumerable<Lookup<TKey, TElement>.Grouping>
74+
public class Lookup<TKey, TElement> : IMutableLookup<TKey, TElement>, IEnumerable<KeyValuePair<TKey, TElement>>, IEnumerable<Grouping<TKey, TElement>>
7575
{
76-
private readonly Dictionary<TKey, Grouping> dic;
76+
private readonly Dictionary<TKey, Grouping<TKey, TElement>> dic;
7777
/// <summary>
7878
/// Initialize a new instance of <see cref="Lookup{TKey, TElement}"/> class.
7979
/// </summary>
@@ -101,7 +101,7 @@ public Lookup(IEqualityComparer<TKey> comparer)
101101
/// <param name="comparer">The comparer of the keys.</param>
102102
public Lookup(int capacity, IEqualityComparer<TKey> comparer)
103103
{
104-
dic = new Dictionary<TKey, Grouping>(capacity, comparer);
104+
dic = new Dictionary<TKey, Grouping<TKey, TElement>>(capacity, comparer);
105105
}
106106
/// <summary>
107107
/// Initialize a new instance of <see cref="Lookup{TKey, TElement}"/> class.
@@ -120,10 +120,10 @@ public Lookup(ILookup<TKey, TElement> lookup, IEqualityComparer<TKey> comparer)
120120
switch (lookup ?? throw ExceptionHelper.ArgumentNull(nameof(lookup)))
121121
{
122122
case Lookup<TKey, TElement> lkp:
123-
dic = new Dictionary<TKey, Grouping>(lkp.dic, comparer);
123+
dic = new Dictionary<TKey, Grouping<TKey, TElement>>(lkp.dic, comparer);
124124
break;
125125
default:
126-
dic = new Dictionary<TKey, Grouping>(lookup.Count, comparer);
126+
dic = new Dictionary<TKey, Grouping<TKey, TElement>>(lookup.Count, comparer);
127127
foreach (var grouping in lookup)
128128
{
129129
foreach (var item in grouping)
@@ -145,7 +145,7 @@ public ICollection<TElement> this[TKey key]
145145
{
146146
if (!dic.ContainsKey(key))
147147
{
148-
dic[key] = new Grouping(key);
148+
dic[key] = new Grouping<TKey, TElement>(key);
149149
}
150150
return dic[key];
151151
}
@@ -167,11 +167,11 @@ public ICollection<TElement> this[TKey key]
167167
/// </summary>
168168
/// <param name="key">The specified key.</param>
169169
/// <param name="element">The specified element.</param>
170-
public void Add(TKey key,TElement element)
170+
public void Add(TKey key, TElement element)
171171
{
172-
if(!dic.ContainsKey(key))
172+
if (!dic.ContainsKey(key))
173173
{
174-
dic[key] = new Grouping(key);
174+
dic[key] = new Grouping<TKey, TElement>(key);
175175
}
176176
dic[key].AddElement(element);
177177
}
@@ -241,9 +241,9 @@ public bool TryGetElements(TKey key, out IEnumerable<TElement> elements)
241241
/// <returns>An enumerator for the <see cref="Lookup{TKey, TElement}"/>.</returns>
242242
public IEnumerator<KeyValuePair<TKey, TElement>> GetEnumerator()
243243
{
244-
foreach(var grouping in dic)
244+
foreach (var grouping in dic)
245245
{
246-
foreach(var item in grouping.Value)
246+
foreach (var item in grouping.Value)
247247
{
248248
yield return new KeyValuePair<TKey, TElement>(grouping.Key, item);
249249
}
@@ -258,7 +258,7 @@ public IEnumerator<KeyValuePair<TKey, TElement>> GetEnumerator()
258258
/// Returns a generic enumerator that iterates through the <see cref="Lookup{TKey, TElement}"/>.
259259
/// </summary>
260260
/// <returns>An enumerator for the <see cref="Lookup{TKey, TElement}"/>.</returns>
261-
IEnumerator<Grouping> IEnumerable<Grouping>.GetEnumerator() => GetEnumeratorInternal();
261+
IEnumerator<Grouping<TKey,TElement>> IEnumerable<Grouping<TKey, TElement>>.GetEnumerator() => GetEnumeratorInternal();
262262
/// <summary>
263263
/// Returns an enumerator that iterates through the <see cref="Lookup{TKey, TElement}"/>. This class cannot be inherited.
264264
/// </summary>
@@ -268,107 +268,110 @@ public IEnumerator<KeyValuePair<TKey, TElement>> GetEnumerator()
268268
/// Returns a generic enumerator that iterates through the <see cref="Lookup{TKey, TElement}"/>.
269269
/// </summary>
270270
/// <returns>An enumerator for the <see cref="Lookup{TKey, TElement}"/>.</returns>
271-
internal IEnumerator<Grouping> GetEnumeratorInternal()
271+
internal IEnumerator<Grouping<TKey, TElement>> GetEnumeratorInternal()
272272
{
273273
foreach (var item in dic)
274274
{
275275
yield return item.Value;
276276
}
277277
}
278+
}
279+
/// <summary>
280+
/// Represents a key and a sequence of elements.
281+
/// </summary>
282+
[Serializable]
283+
[DebuggerTypeProxy(typeof(ICountableGroupingDebugView<,>))]
284+
[DebuggerDisplay("Count = {Count}")]
285+
internal class Grouping<TKey, TElement> : ICountableGrouping<TKey, TElement>, ICollection<TElement>
286+
{
287+
private readonly TKey key;
288+
private readonly Collection<TElement> collection;
278289
/// <summary>
279-
/// Represents a key and a sequence of elements.
290+
/// Initialize a new instance of <see cref="Grouping{TKey, TElement}"/> class.
280291
/// </summary>
281-
internal class Grouping : ICountableGrouping<TKey, TElement>, ICollection<TElement>
292+
/// <param name="key"></param>
293+
public Grouping(TKey key)
282294
{
283-
private readonly TKey key;
284-
private readonly Collection<TElement> collection;
285-
/// <summary>
286-
/// Initialize a new instance of <see cref="Grouping"/> class.
287-
/// </summary>
288-
/// <param name="key"></param>
289-
public Grouping(TKey key)
290-
{
291-
this.key = key;
292-
this.collection = new Collection<TElement>();
293-
}
294-
/// <summary>
295-
/// The key of the <see cref="Grouping"/>.
296-
/// </summary>
297-
public TKey Key => key;
298-
/// <summary>
299-
/// Count of the elements.
300-
/// </summary>
301-
public int Count => collection.Count;
302-
/// <summary>
303-
/// The <see cref="Grouping"/> is not read-only.
304-
/// </summary>
305-
public bool IsReadOnly => true;
306-
/// <summary>
307-
/// Adds element to the grouping.
308-
/// </summary>
309-
/// <param name="item">The specified element.</param>
310-
internal void AddElement(TElement item) => collection.Add(item);
311-
/// <summary>
312-
/// This function is not supported.
313-
/// </summary>
314-
/// <param name="item">The specified element.</param>
315-
/// <exception cref="NotSupportedException"/>
316-
public void Add(TElement item) => throw ExceptionHelper.NotSupported();
317-
/// <summary>
318-
/// Clears the elements.
319-
/// </summary>
320-
internal void ClearElements() => collection.Clear();
321-
/// <summary>
322-
/// This function is not supported.
323-
/// </summary>
324-
/// <exception cref="NotSupportedException"/>
325-
public void Clear() => throw ExceptionHelper.NotSupported();
326-
/// <summary>
327-
/// Removes the specified element.
328-
/// </summary>
329-
/// <param name="item">The specified element.</param>
330-
/// <returns><see langword="true"/> if removes successfully; otherwise, <see langword="false"/>.</returns>
331-
internal bool RemoveElement(TElement item) => collection.Remove(item);
332-
/// <summary>
333-
/// This function is not supported.
334-
/// </summary>
335-
/// <param name="item">The specified element.</param>
336-
/// <returns><see langword="true"/> if removes successfully; otherwise, <see langword="false"/>.</returns>
337-
/// <exception cref="NotSupportedException"/>
338-
public bool Remove(TElement item) => throw ExceptionHelper.NotSupported();
339-
/// <summary>
340-
/// Determines whether a specified key is in the <see cref="Grouping"/>.
341-
/// </summary>
342-
/// <param name="item">The element to find in the <see cref="Grouping"/>.</param>
343-
/// <returns><see langword="true"/> if <paramref name="item"/> is in the <see cref="Grouping"/>; otherwise, <see langword="false"/>.</returns>
344-
public bool Contains(TElement item) => collection.Contains(item);
345-
/// <summary>
346-
/// Copies the entire <see cref="Collection{T}"/> to a compatible one-dimensional <see cref="Array"/>, starting at the specified index of the target array.
347-
/// </summary>
348-
/// <param name="array">The one-dimensional <see cref="Array"/> that is the destination of the elements copied from <see cref="Collection{T}"/>. The <see cref="Array"/> must have zero-based indexing.</param>
349-
/// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
350-
public void CopyTo(TElement[] array, int arrayIndex)
351-
{
352-
if (array == null)
353-
throw ExceptionHelper.ArgumentNull(nameof(array));
354-
if (arrayIndex < 0)
355-
throw ExceptionHelper.ArgumentOutOfRange(nameof(arrayIndex));
356-
if (arrayIndex > array.Length)
357-
throw ExceptionHelper.ArgumentOutOfRange(nameof(arrayIndex));
358-
if (array.Length - arrayIndex < collection.Count)
359-
throw ExceptionHelper.ArrayTooSmall();
360-
collection.CopyTo(array, arrayIndex);
361-
}
362-
/// <summary>
363-
/// Returns a generic enumerator that iterates through the <see cref="Grouping"/>.
364-
/// </summary>
365-
/// <returns>An enumerator for the <see cref="Grouping"/>.</returns>
366-
public IEnumerator<TElement> GetEnumerator() => collection.GetEnumerator();
367-
/// <summary>
368-
/// Returns a generic enumerator that iterates through the <see cref="Grouping"/>.
369-
/// </summary>
370-
/// <returns>An enumerator for the <see cref="Grouping"/>.</returns>
371-
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
295+
this.key = key;
296+
this.collection = new Collection<TElement>();
372297
}
298+
/// <summary>
299+
/// The key of the <see cref="Grouping{TKey, TElement}"/>.
300+
/// </summary>
301+
public TKey Key => key;
302+
/// <summary>
303+
/// Count of the elements.
304+
/// </summary>
305+
public int Count => collection.Count;
306+
/// <summary>
307+
/// The <see cref="Grouping{TKey, TElement}"/> is not read-only.
308+
/// </summary>
309+
public bool IsReadOnly => true;
310+
/// <summary>
311+
/// Adds element to the grouping.
312+
/// </summary>
313+
/// <param name="item">The specified element.</param>
314+
internal void AddElement(TElement item) => collection.Add(item);
315+
/// <summary>
316+
/// This function is not supported.
317+
/// </summary>
318+
/// <param name="item">The specified element.</param>
319+
/// <exception cref="NotSupportedException"/>
320+
public void Add(TElement item) => throw ExceptionHelper.NotSupported();
321+
/// <summary>
322+
/// Clears the elements.
323+
/// </summary>
324+
internal void ClearElements() => collection.Clear();
325+
/// <summary>
326+
/// This function is not supported.
327+
/// </summary>
328+
/// <exception cref="NotSupportedException"/>
329+
public void Clear() => throw ExceptionHelper.NotSupported();
330+
/// <summary>
331+
/// Removes the specified element.
332+
/// </summary>
333+
/// <param name="item">The specified element.</param>
334+
/// <returns><see langword="true"/> if removes successfully; otherwise, <see langword="false"/>.</returns>
335+
internal bool RemoveElement(TElement item) => collection.Remove(item);
336+
/// <summary>
337+
/// This function is not supported.
338+
/// </summary>
339+
/// <param name="item">The specified element.</param>
340+
/// <returns><see langword="true"/> if removes successfully; otherwise, <see langword="false"/>.</returns>
341+
/// <exception cref="NotSupportedException"/>
342+
public bool Remove(TElement item) => throw ExceptionHelper.NotSupported();
343+
/// <summary>
344+
/// Determines whether a specified key is in the <see cref="Grouping{TKey, TElement}"/>.
345+
/// </summary>
346+
/// <param name="item">The element to find in the <see cref="Grouping{TKey, TElement}"/>.</param>
347+
/// <returns><see langword="true"/> if <paramref name="item"/> is in the <see cref="Grouping{TKey, TElement}"/>; otherwise, <see langword="false"/>.</returns>
348+
public bool Contains(TElement item) => collection.Contains(item);
349+
/// <summary>
350+
/// Copies the entire <see cref="Collection{T}"/> to a compatible one-dimensional <see cref="Array"/>, starting at the specified index of the target array.
351+
/// </summary>
352+
/// <param name="array">The one-dimensional <see cref="Array"/> that is the destination of the elements copied from <see cref="Collection{T}"/>. The <see cref="Array"/> must have zero-based indexing.</param>
353+
/// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
354+
public void CopyTo(TElement[] array, int arrayIndex)
355+
{
356+
if (array == null)
357+
throw ExceptionHelper.ArgumentNull(nameof(array));
358+
if (arrayIndex < 0)
359+
throw ExceptionHelper.ArgumentOutOfRange(nameof(arrayIndex));
360+
if (arrayIndex > array.Length)
361+
throw ExceptionHelper.ArgumentOutOfRange(nameof(arrayIndex));
362+
if (array.Length - arrayIndex < collection.Count)
363+
throw ExceptionHelper.ArrayTooSmall();
364+
collection.CopyTo(array, arrayIndex);
365+
}
366+
/// <summary>
367+
/// Returns a generic enumerator that iterates through the <see cref="Grouping{TKey, TElement}"/>.
368+
/// </summary>
369+
/// <returns>An enumerator for the <see cref="Grouping{TKey, TElement}"/>.</returns>
370+
public IEnumerator<TElement> GetEnumerator() => collection.GetEnumerator();
371+
/// <summary>
372+
/// Returns a generic enumerator that iterates through the <see cref="Grouping{TKey, TElement}"/>.
373+
/// </summary>
374+
/// <returns>An enumerator for the <see cref="Grouping{TKey, TElement}"/>.</returns>
375+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
373376
}
374377
}

src/Berrysoft.Data/Map.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public override string ToString()
204204
/// <typeparam name="TKey1">Type of key1.</typeparam>
205205
/// <typeparam name="TKey2">Type of key2.</typeparam>
206206
[Serializable]
207-
[DebuggerTypeProxy(typeof(MapDebugView<,>))]
207+
[DebuggerTypeProxy(typeof(IMapDebugView<,>))]
208208
[DebuggerDisplay("Count = {Count}")]
209209
public class Map<TKey1, TKey2> : IMap<TKey1, TKey2>
210210
{

src/Berrysoft.Data/MultiMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public static MultiMap<TKey1, TKey2> ToMultiMap<TSource, TKey1, TKey2>(this IEnu
156156
/// <typeparam name="TKey1">Type of key1.</typeparam>
157157
/// <typeparam name="TKey2">Type of key2.</typeparam>
158158
[Serializable]
159-
[DebuggerTypeProxy(typeof(MultiMapDebugView<,>))]
159+
[DebuggerTypeProxy(typeof(IMultiMapDebugView<,>))]
160160
[DebuggerDisplay("Count = {Count}")]
161161
public class MultiMap<TKey1, TKey2> : IMultiMap<TKey1, TKey2>
162162
{

src/Berrysoft.Unsafe/ByReference.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,4 @@ public ref T Value
3232
get => ref AsRef<T>(_ptr);
3333
}
3434
}
35-
[DebuggerDisplay("{Value}")]
36-
[DebuggerTypeProxy(typeof(PointerDebugView<>))]
37-
public readonly unsafe struct ReadOnlyRefrence<T>
38-
{
39-
private readonly void* _ptr;
40-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
41-
public ReadOnlyRefrence(in T value)
42-
{
43-
_ptr = AsPointer(ref AsRef(in value));
44-
}
45-
public ref readonly T Value
46-
{
47-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
48-
get => ref AsRef<T>(_ptr);
49-
}
50-
}
5135
}

src/Berrysoft.Unsafe/PointerDebugView.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ public PointerDebugView(ByReference<T> ptr)
3333
{
3434
target = ptr.Value;
3535
}
36-
public PointerDebugView(ReadOnlyRefrence<T> ptr)
37-
{
38-
target = ptr.Value;
39-
}
4036
/// <summary>
4137
/// Target of the pointer.
4238
/// </summary>

0 commit comments

Comments
 (0)