Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BiDictionary data type and its simple unity tests
* Modify the structure a bit so the project will be more maintainable * Add unit test support * Clean up the package dependencies
- Loading branch information
Showing
18 changed files
with
489 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...tructures/FibonacciHeap/Constants.cs.meta → ...uctures/BiDictionary/BiDictionary.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "DSUnity" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.