Exercise: MultiValueDictionary

You are supposed to program a new collection class called MultiValueDicationary.

You are assumed to be familiar with

Group size: 2 student working together using the XP (eXtreme Programming) disciplines

Introduction

An ordinary Dictionary<TKey, TValue> maps keys to values: Each key maps to (at most) one value.

A MultiValueDictionary<TKey, TValue> maps keys to values, but each key maps to a set of values (set means no duplicates).

The MultiValueDictionary<TKey, TValue> is usefull in the dictionary part of a distributed file sharing system where a single file can be accessible on many IP adresses

File name IP adresses
SomeMovie 192.12.33.4, 188.3.2.4, 178.4.5.3
AnotherMovie 192.12.33.4, 185.3.2.3
IllegalMovie 188.3.2.4
YetAnotherMovie 194.22.4.5

A MultiValueDictionary<TKey, TValue> is similar to a Map<TKey, ISet<TValue>>, but it is not exactly the same ...

Googling for MultiValueDictionary you can find various classes - despite the name these classe does not do what I want. Do not spend time on them!

Getting started

Create a new Solution in Visual Studio. Name the Solution "MultiValueDictionary"

Create a class new

MultiValueDictionary<TKey, TValue> {
  private readonly IDictionary<TKey, ISet<TValue>> _data = new Dictionary<TKey, ISet<TValue>>();
  ...
}

The work

You must make the following methods for MultiValueDictionar<TKey, TValue>. The methods must be make in exactly this sequence.

You must make the test before you make the metod!!!

Refer to the documentation for details on each method.

  1. String ToString(), hard to test: you are allowed to skip testing for this method
  2. int Count, read-only property
  3. bool Add(key, value)
  4. ISet<TValue> Get(key)
  5. bool ContainsKey(key)
  6. bool Remove(key, value)
  7. ICollection<TKey> Keys. Property
  8. Extra: ICollection<ISet<TValue>> Values. Property.
    Must return a copy of the original data, or a read-only version of the original data
  9. Extra: bool TryGetValue(TKey key, out ISet<TValue> values)
    You may have to read about the semantics of the out keyword
  10. Extra: int AddAll(ISet<TKey> keys, value)
  11. Extra: int RemoveAll(value)
  12. Extra: bool Equals(Object obj)
    Implement the interface IEquatable<MultiValueDictionary<TKey, TValue>>
  13. Extra: int GetHashCode()
  14. Extra: Implement the interface IEnumerable<KeyValuePair<TKey, ISet<TValue>>>

Refer to the documentation for details on each method.

Extra: Documentation

Your class MultiValueDictionary<TKey, TValue> must be documented using Doxygen.

Extra: Copy constructor

Many collection classes have a so-called copy constructor.

A copy constructor is a constructor which is another collection.

  public MultiValueDictionary(MultiValueDictionary<TKey, TValue> other)

All the elemens of other are copied to this (the newly create object)