Skip to content

Commit 593ba02

Browse files
committed
Change some APIs.
1 parent 8487c4a commit 593ba02

File tree

5 files changed

+97
-52
lines changed

5 files changed

+97
-52
lines changed

src/Berrysoft.Data/Graph.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public interface IGraph<T>
7171
/// <param name="head">The specified vertex.</param>
7272
void ClearTails(T head);
7373
/// <summary>
74-
/// Get a <see cref="ILookup{TKey, TElement}"/> contains all heads and their arcs.
74+
/// Get a <see cref="IMutableLookup{TKey, TElement}"/> contains all heads and their arcs.
7575
/// </summary>
76-
/// <returns>An instance of <see cref="ILookup{TKey, TElement}"/>.</returns>
76+
/// <returns>An instance of <see cref="IMutableLookup{TKey, TElement}"/>.</returns>
7777
ILookup<T, T> GetHeads();
7878
/// <summary>
7979
/// Get an <see cref="IEnumerable{T}"/> of all heads of a specified tail.
@@ -82,9 +82,9 @@ public interface IGraph<T>
8282
/// <returns>An instance of <see cref="IEnumerable{T}"/>.</returns>
8383
ICollection<T> GetHeads(T tail);
8484
/// <summary>
85-
/// Get a <see cref="ILookup{TKey, TElement}"/> contains all tails and their arcs.
85+
/// Get a <see cref="IMutableLookup{TKey, TElement}"/> contains all tails and their arcs.
8686
/// </summary>
87-
/// <returns>An instance of <see cref="ILookup{TKey, TElement}"/>.</returns>
87+
/// <returns>An instance of <see cref="IMutableLookup{TKey, TElement}"/>.</returns>
8888
ILookup<T, T> GetTails();
8989
/// <summary>
9090
/// Get an <see cref="IEnumerable{T}"/> of all tails of a specified head.
@@ -789,9 +789,9 @@ public void ClearArc(T vertex)
789789
/// <param name="head">The specified vertex.</param>
790790
public void ClearTails(T head) => _arcs.RemoveKey2(head);
791791
/// <summary>
792-
/// Get an <see cref="ILookup{TKey, TElement}"/> contains all heads and their arcs.
792+
/// Get an <see cref="IMutableLookup{TKey, TElement}"/> contains all heads and their arcs.
793793
/// </summary>
794-
/// <returns>An instance of <see cref="ILookup{TKey, TElement}"/>.</returns>
794+
/// <returns>An instance of <see cref="IMutableLookup{TKey, TElement}"/>.</returns>
795795
public ILookup<T, T> GetHeads() => _arcs.ToLookupFromKey1();
796796
/// <summary>
797797
/// Get an <see cref="IEnumerable{T}"/> of all heads of a specified tail.
@@ -800,9 +800,9 @@ public void ClearArc(T vertex)
800800
/// <returns>An instance of <see cref="IEnumerable{T}"/>.</returns>
801801
public ICollection<T> GetHeads(T tail) => _arcs.GetValuesFromKey1(tail);
802802
/// <summary>
803-
/// Get an <see cref="ILookup{TKey, TElement}"/> contains all tails and their arcs.
803+
/// Get an <see cref="IMutableLookup{TKey, TElement}"/> contains all tails and their arcs.
804804
/// </summary>
805-
/// <returns>An instance of <see cref="ILookup{TKey, TElement}"/>.</returns>
805+
/// <returns>An instance of <see cref="IMutableLookup{TKey, TElement}"/>.</returns>
806806
public ILookup<T, T> GetTails() => _arcs.ToLookupFromKey2();
807807
/// <summary>
808808
/// Get an <see cref="IEnumerable{T}"/> of all tails of a specified head.

src/Berrysoft.Data/Lookup.cs

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Collections.ObjectModel;
5-
using SysLinq = System.Linq;
5+
using System.Linq;
66

77
namespace Berrysoft.Data
88
{
@@ -12,7 +12,7 @@ namespace Berrysoft.Data
1212
/// </summary>
1313
/// <typeparam name="TKey">Type of the keys.</typeparam>
1414
/// <typeparam name="TElement">Type of the elements.</typeparam>
15-
public interface ILookup<TKey, TElement> : SysLinq.ILookup<TKey, TElement>
15+
public interface IMutableLookup<TKey, TElement> : ILookup<TKey, TElement>
1616
{
1717
/// <summary>
1818
/// A collection of keys.
@@ -38,23 +38,23 @@ public interface ILookup<TKey, TElement> : SysLinq.ILookup<TKey, TElement>
3838
/// <returns><see langword="true"/> if removes successfully; otherwise, <see langword="false"/>.</returns>
3939
bool Remove(TKey key, TElement element);
4040
/// <summary>
41-
/// Clear the <see cref="ILookup{TKey, TElement}"/>.
41+
/// Clear the <see cref="IMutableLookup{TKey, TElement}"/>.
4242
/// </summary>
4343
void Clear();
4444
/// <summary>
4545
/// Gets the <see cref="IEnumerable{T}"/> sequence of values by a specified key.
4646
/// </summary>
4747
/// <param name="key">The specified key.</param>
4848
/// <param name="elements">When this method returns, contains the elements associated with the specified key, if the key is found; otherwise, <see langword="null"/>. This parameter is passed uninitialized.</param>
49-
/// <returns><see langword="true"/> if the <see cref="ILookup{TKey, TElement}"/> contains elements with the specified key; otherwise, <see langword="false"/>.</returns>
49+
/// <returns><see langword="true"/> if the <see cref="IMutableLookup{TKey, TElement}"/> contains elements with the specified key; otherwise, <see langword="false"/>.</returns>
5050
bool TryGetElements(TKey key, out IEnumerable<TElement> elements);
5151
}
5252
/// <summary>
5353
/// Represents a collection of objects that have a common key.
5454
/// </summary>
55-
/// <typeparam name="TKey">The type of the key of the <see cref="IGrouping{TKey, TElement}"/>.</typeparam>
56-
/// <typeparam name="TElement">The type of the values in the <see cref="IGrouping{TKey, TElement}"/>.</typeparam>
57-
internal interface IGrouping<TKey, TElement> : SysLinq.IGrouping<TKey, TElement>
55+
/// <typeparam name="TKey">The type of the key of the <see cref="ICountableGrouping{TKey, TElement}"/>.</typeparam>
56+
/// <typeparam name="TElement">The type of the values in the <see cref="ICountableGrouping{TKey, TElement}"/>.</typeparam>
57+
internal interface ICountableGrouping<TKey, TElement> : IGrouping<TKey, TElement>
5858
{
5959
/// <summary>
6060
/// Count of the elements.
@@ -68,7 +68,7 @@ internal interface IGrouping<TKey, TElement> : SysLinq.IGrouping<TKey, TElement>
6868
/// <typeparam name="TKey">The type of the keys in the <see cref="Lookup{TKey, TElement}"/>.</typeparam>
6969
/// <typeparam name="TElement">The type of the elements of each value in the <see cref="Lookup{TKey, TElement}"/>.</typeparam>
7070
[Serializable]
71-
public class Lookup<TKey, TElement> : ILookup<TKey, TElement>, IEnumerable<IGrouping<TKey, TElement>>
71+
public class Lookup<TKey, TElement> : IMutableLookup<TKey, TElement>, IEnumerable<KeyValuePair<TKey, TElement>>, IEnumerable<ICountableGrouping<TKey, TElement>>
7272
{
7373
private Dictionary<TKey, Grouping> dic;
7474
/// <summary>
@@ -80,7 +80,7 @@ public Lookup()
8080
/// <summary>
8181
/// Initialize a new instance of <see cref="Lookup{TKey, TElement}"/> class.
8282
/// </summary>
83-
/// <param name="capacity">The initial number of keys that the <see cref="ILookup{TKey, TElement}"/> can contain.</param>
83+
/// <param name="capacity">The initial number of keys that the <see cref="IMutableLookup{TKey, TElement}"/> can contain.</param>
8484
public Lookup(int capacity)
8585
: this(capacity, null)
8686
{ }
@@ -94,7 +94,7 @@ public Lookup(IEqualityComparer<TKey> comparer)
9494
/// <summary>
9595
/// Initialize a new instance of <see cref="Lookup{TKey, TElement}"/> class.
9696
/// </summary>
97-
/// <param name="capacity">The initial number of keys that the <see cref="ILookup{TKey, TElement}"/> can contain.</param>
97+
/// <param name="capacity">The initial number of keys that the <see cref="IMutableLookup{TKey, TElement}"/> can contain.</param>
9898
/// <param name="comparer">The comparer of the keys.</param>
9999
public Lookup(int capacity, IEqualityComparer<TKey> comparer)
100100
{
@@ -148,12 +148,6 @@ public ICollection<TElement> this[TKey key]
148148
}
149149
}
150150
/// <summary>
151-
/// Gets the collection of values indexed by the specified key.
152-
/// </summary>
153-
/// <param name="key">The key of the desired collection of values.</param>
154-
/// <returns>The collection of values indexed by the specified key.</returns>
155-
IEnumerable<TElement> SysLinq.ILookup<TKey, TElement>.this[TKey key] => this[key];
156-
/// <summary>
157151
/// Gets the number of key/value collection pairs in the <see cref="Lookup{TKey, TElement}"/>.
158152
/// </summary>
159153
public int Count => dic.Count;
@@ -162,6 +156,10 @@ public ICollection<TElement> this[TKey key]
162156
/// </summary>
163157
public ICollection<TKey> Keys => dic.Keys;
164158
/// <summary>
159+
/// A collection of keys.
160+
/// </summary>
161+
IEnumerable<TElement> ILookup<TKey, TElement>.this[TKey key] => this[key];
162+
/// <summary>
165163
/// Adds the specified key and element.
166164
/// </summary>
167165
/// <param name="key">The specified key.</param>
@@ -172,7 +170,7 @@ public void Add(TKey key,TElement element)
172170
{
173171
dic[key] = new Grouping(key);
174172
}
175-
dic[key].Add(element);
173+
dic[key].AddElement(element);
176174
}
177175
/// <summary>
178176
/// Removes the specified key and its elements.
@@ -193,7 +191,7 @@ public bool Remove(TKey key, TElement element)
193191
{
194192
if (dic.ContainsKey(key))
195193
{
196-
return dic[key].Remove(element);
194+
return dic[key].RemoveElement(element);
197195
}
198196
else
199197
{
@@ -207,15 +205,15 @@ public bool Remove(TKey key, TElement element)
207205
/// <returns><see langword="true"/> if <paramref name="key"/> is in the <see cref="Lookup{TKey, TElement}"/>; otherwise, <see langword="false"/>.</returns>
208206
public bool Contains(TKey key) => dic.ContainsKey(key);
209207
/// <summary>
210-
/// Clear the <see cref="ILookup{TKey, TElement}"/>.
208+
/// Clear the <see cref="IMutableLookup{TKey, TElement}"/>.
211209
/// </summary>
212210
public void Clear() => dic.Clear();
213211
/// <summary>
214-
/// Gets the <see cref="ICollection{T}"/> sequence of values by a specified key.
212+
/// Gets the <see cref="IEnumerable{T}"/> sequence of values by a specified key.
215213
/// </summary>
216214
/// <param name="key">The specified key.</param>
217215
/// <param name="elements">When this method returns, contains the elements associated with the specified key, if the key is found; otherwise, <see langword="null"/>. This parameter is passed uninitialized.</param>
218-
/// <returns><see langword="true"/> if the <see cref="ILookup{TKey, TElement}"/> contains elements with the specified key; otherwise, <see langword="false"/>.</returns>
216+
/// <returns><see langword="true"/> if the <see cref="IMutableLookup{TKey, TElement}"/> contains elements with the specified key; otherwise, <see langword="false"/>.</returns>
219217
public bool TryGetElements(TKey key, out ICollection<TElement> elements)
220218
{
221219
bool result = dic.TryGetValue(key, out var grouping);
@@ -227,8 +225,8 @@ public bool TryGetElements(TKey key, out ICollection<TElement> elements)
227225
/// </summary>
228226
/// <param name="key">The specified key.</param>
229227
/// <param name="elements">When this method returns, contains the elements associated with the specified key, if the key is found; otherwise, <see langword="null"/>. This parameter is passed uninitialized.</param>
230-
/// <returns><see langword="true"/> if the <see cref="ILookup{TKey, TElement}"/> contains elements with the specified key; otherwise, <see langword="false"/>.</returns>
231-
bool ILookup<TKey,TElement>.TryGetElements(TKey key, out IEnumerable<TElement> elements)
228+
/// <returns><see langword="true"/> if the <see cref="IMutableLookup{TKey, TElement}"/> contains elements with the specified key; otherwise, <see langword="false"/>.</returns>
229+
public bool TryGetElements(TKey key, out IEnumerable<TElement> elements)
232230
{
233231
bool result = dic.TryGetValue(key, out var grouping);
234232
elements = grouping;
@@ -238,13 +236,27 @@ bool ILookup<TKey,TElement>.TryGetElements(TKey key, out IEnumerable<TElement> e
238236
/// Returns a generic enumerator that iterates through the <see cref="Lookup{TKey, TElement}"/>.
239237
/// </summary>
240238
/// <returns>An enumerator for the <see cref="Lookup{TKey, TElement}"/>.</returns>
241-
public IEnumerator<SysLinq.IGrouping<TKey, TElement>> GetEnumerator() => GetEnumeratorInternal();
239+
public IEnumerator<KeyValuePair<TKey, TElement>> GetEnumerator()
240+
{
241+
foreach(var grouping in dic)
242+
{
243+
foreach(var item in grouping.Value)
244+
{
245+
yield return new KeyValuePair<TKey, TElement>(grouping.Key, item);
246+
}
247+
}
248+
}
242249
/// <summary>
243250
/// Returns a generic enumerator that iterates through the <see cref="Lookup{TKey, TElement}"/>.
244251
/// </summary>
245252
/// <returns>An enumerator for the <see cref="Lookup{TKey, TElement}"/>.</returns>
246253
IEnumerator<IGrouping<TKey, TElement>> IEnumerable<IGrouping<TKey, TElement>>.GetEnumerator() => GetEnumeratorInternal();
247254
/// <summary>
255+
/// Returns a generic enumerator that iterates through the <see cref="Lookup{TKey, TElement}"/>.
256+
/// </summary>
257+
/// <returns>An enumerator for the <see cref="Lookup{TKey, TElement}"/>.</returns>
258+
IEnumerator<ICountableGrouping<TKey, TElement>> IEnumerable<ICountableGrouping<TKey, TElement>>.GetEnumerator() => GetEnumeratorInternal();
259+
/// <summary>
248260
/// Returns an enumerator that iterates through the <see cref="Lookup{TKey, TElement}"/>. This class cannot be inherited.
249261
/// </summary>
250262
/// <returns>An enumerator for the <see cref="Lookup{TKey, TElement}"/>.</returns>
@@ -263,10 +275,10 @@ private IEnumerator<Grouping> GetEnumeratorInternal()
263275
/// <summary>
264276
/// Represents a key and a sequence of elements.
265277
/// </summary>
266-
private class Grouping : IGrouping<TKey, TElement>, ICollection<TElement>
278+
private class Grouping : ICountableGrouping<TKey, TElement>, ICollection<TElement>
267279
{
268-
private TKey key;
269-
private Collection<TElement> collection;
280+
private readonly TKey key;
281+
private readonly Collection<TElement> collection;
270282
/// <summary>
271283
/// Initialize a new instance of <see cref="Grouping"/> class.
272284
/// </summary>
@@ -287,22 +299,40 @@ public Grouping(TKey key)
287299
/// <summary>
288300
/// The <see cref="Grouping"/> is not read-only.
289301
/// </summary>
290-
public bool IsReadOnly => false;
302+
public bool IsReadOnly => true;
291303
/// <summary>
292304
/// Adds element to the grouping.
293305
/// </summary>
294306
/// <param name="item">The specified element.</param>
295-
public void Add(TElement item) => collection.Add(item);
307+
internal void AddElement(TElement item) => collection.Add(item);
308+
/// <summary>
309+
/// This function is not supported.
310+
/// </summary>
311+
/// <param name="item">The specified element.</param>
312+
/// <exception cref="NotSupportedException"/>
313+
public void Add(TElement item) => throw ExceptionHelper.NotSupported();
296314
/// <summary>
297315
/// Clears the elements.
298316
/// </summary>
299-
public void Clear() => collection.Clear();
317+
internal void ClearElements() => collection.Clear();
318+
/// <summary>
319+
/// This function is not supported.
320+
/// </summary>
321+
/// <exception cref="NotSupportedException"/>
322+
public void Clear() => throw ExceptionHelper.NotSupported();
300323
/// <summary>
301324
/// Removes the specified element.
302325
/// </summary>
303326
/// <param name="item">The specified element.</param>
304327
/// <returns><see langword="true"/> if removes successfully; otherwise, <see langword="false"/>.</returns>
305-
public bool Remove(TElement item) => collection.Remove(item);
328+
internal bool RemoveElement(TElement item) => collection.Remove(item);
329+
/// <summary>
330+
/// This function is not supported.
331+
/// </summary>
332+
/// <param name="item">The specified element.</param>
333+
/// <returns><see langword="true"/> if removes successfully; otherwise, <see langword="false"/>.</returns>
334+
/// <exception cref="NotSupportedException"/>
335+
public bool Remove(TElement item) => throw ExceptionHelper.NotSupported();
306336
/// <summary>
307337
/// Determines whether a specified key is in the <see cref="Grouping"/>.
308338
/// </summary>

src/Berrysoft.Data/Map.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ public static Map<TKey1, TKey2> ToMap<TSource, TKey1, TKey2>(this IEnumerable<TS
154154
/// <typeparam name="TKey1">Type of key1.</typeparam>
155155
/// <typeparam name="TKey2">Type of key2.</typeparam>
156156
[Serializable]
157-
public struct KeyPair<TKey1, TKey2>
157+
public readonly struct KeyPair<TKey1, TKey2>
158158
{
159-
private TKey1 key1;
160-
private TKey2 key2;
159+
private readonly TKey1 key1;
160+
private readonly TKey2 key2;
161161
/// <summary>
162162
/// First key of the pair.
163163
/// </summary>

0 commit comments

Comments
 (0)