Skip to content

Commit

Permalink
Add BiDictionary data type and its simple unity tests
Browse files Browse the repository at this point in the history
* Modify the structure a bit so the project will be more maintainable
* Add unit test support
* Clean up the package dependencies
  • Loading branch information
erdiizgi committed Oct 16, 2019
1 parent 7b06229 commit 3c6e43f
Show file tree
Hide file tree
Showing 18 changed files with 489 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -187,7 +187,7 @@ PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# **/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
Expand Down
8 changes: 8 additions & 0 deletions Assets/DataStructures/BiDictionary.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions Assets/DataStructures/BiDictionary/BiDictionary.cs
@@ -0,0 +1,76 @@
using System.Collections;
using System.Collections.Generic;

namespace DataStructures.BiDictionary
{
public class BiDictionary<TKey, TValue> : IBiDictionary<TKey, TValue>
{
private readonly Dictionary<TKey, TValue> forward = new Dictionary<TKey, TValue>();
private readonly Dictionary<TValue, TKey> reverse = new Dictionary<TValue, TKey>();

public BiDictionary()
{
KeyMap = forward;
ValueMap = reverse;
}

public Dictionary<TKey, TValue> KeyMap { get; private set; }
public Dictionary<TValue, TKey> ValueMap { get; private set; }

public int Count() => forward.Count;

public void Add(TKey key, TValue value)
{
if (key != null && value != null)
{
forward.Add(key, value);
reverse.Add(value, key);
}
}

public bool ContainsKey(TKey key)
{
return forward.ContainsKey(key);
}

public bool ContainsValue(TValue value)
{
return reverse.ContainsKey(value);
}

public bool RemoveKey(TKey key)
{
var value = forward[key];
return value != null && reverse.ContainsKey(value)
&& reverse.Remove(value) && forward.Remove(key);
}

public bool RemoveValue(TValue value)
{
var key = reverse[value];
return key != null && forward.ContainsKey(key)
&& forward.Remove(key) && reverse.Remove(value);
}

public IEnumerator<KeyValuePair<TValue, TKey>> GetValueEnumerator()
{
return reverse.GetEnumerator();
}

public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return forward.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return forward.GetEnumerator();
}

public void Clear()
{
forward.Clear();
reverse.Clear();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Assets/DataStructures/BiDictionary/IBiDictionary.cs
@@ -0,0 +1,20 @@
using System.Collections.Generic;

namespace DataStructures.BiDictionary
{
public interface IBiDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
{
Dictionary<TKey, TValue> KeyMap { get; }
Dictionary<TValue, TKey> ValueMap { get; }

void Add(TKey key, TValue value);
void Clear();
bool ContainsKey(TKey key);
bool ContainsValue(TValue value);
int Count();
new IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator();
IEnumerator<KeyValuePair<TValue, TKey>> GetValueEnumerator();
bool RemoveKey(TKey key);
bool RemoveValue(TValue value);
}
}
11 changes: 11 additions & 0 deletions Assets/DataStructures/BiDictionary/IBiDictionary.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/DataStructures/DSUnity.asmdef
@@ -0,0 +1,3 @@
{
"name": "DSUnity"
}
7 changes: 7 additions & 0 deletions Assets/DataStructures/DSUnity.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions Assets/DataStructures/FibonacciHeap/Constants.cs

This file was deleted.

0 comments on commit 3c6e43f

Please sign in to comment.