ex-col-05-building-directory/BuildingDirectory/MyDictionary.cs
github-classroom[bot] eab553c714
Initial commit
2025-01-02 16:52:58 +00:00

114 lines
No EOL
3.2 KiB
C#

using System.Diagnostics.CodeAnalysis;
namespace BuildingDirectory;
/// <summary>
/// A generic dictionary (hash map) implementation managing keys and associated values
/// </summary>
/// <typeparam name="TKey">Type of the keys; cannot be null</typeparam>
/// <typeparam name="TValue">Type of the values</typeparam>
public sealed class MyDictionary<TKey, TValue> where TKey : notnull
{
private const int InitialBuckets = 4;
private const int MaxDepth = 2;
// TODO
//private List<KeyValue>[] _buckets;
//private int _currentMaxDepth;
/// <summary>
/// Gets the count of items stored in this dictionary
/// </summary>
public int Count { get; private set; }
// TODO
private int NoOfBuckets => -1;
/// <summary>
/// Creates a new instance of the dictionary with default capacity
/// </summary>
public MyDictionary() : this(InitialBuckets)
{
}
private MyDictionary(int capacity)
{
// TODO
}
/// <summary>
/// Attempts to get the value associated with the given key.
/// Returns the default value for <see cref="TValue"/> if not found.
/// </summary>
/// <param name="key">Key of the required value</param>
// TODO
public TValue? this[TKey key] => default;
/// <summary>
/// Returns a list of all managed keys.
/// </summary>
/// <returns>A list of all keys</returns>
public List<TKey> GetKeys()
{
// TODO
return new();
}
/// <summary>
/// Returns a list of all managed values.
/// May contain duplicates.
/// </summary>
/// <returns>A list of all values</returns>
public List<TValue> GetValues()
{
// TODO
return new();
}
/// <summary>
/// Adds a value to the dictionary by its key.
/// If the key is already present the previous values is replaced.
/// </summary>
/// <param name="key">Key of the value</param>
/// <param name="value">Value to store</param>
public void Add(TKey key, TValue value)
{
// TODO
}
/// <summary>
/// Attempts to remove the given key and associated value from the dictionary.
/// Returns false if key is not found.
/// </summary>
/// <param name="key">Key to remove</param>
/// <returns>True if the key & value could be removed; false otherwise</returns>
public bool Remove(TKey key)
{
// TODO
return false;
}
/// <summary>
/// Attempts to get a value by the given key from the dictionary.
/// If not found value is set to the default of <see cref="TValue"/>.
/// </summary>
/// <param name="key">Key to look for</param>
/// <param name="value">Out param for storing value if found</param>
/// <returns>True if key was found; false otherwise</returns>
public bool TryGetValue(TKey key, out TValue? value)
{
// TODO
value = default;
return false;
}
/// <summary>
/// Checks if the dictionary contains the given key
/// </summary>
/// <param name="key">Key to search for</param>
/// <returns>True if key is found; false otherwise</returns>
public bool ContainsKey(TKey key)
{
// TODO
return false;
}
}