Merge pull request #118 from kvr000/features/SPRNET-1581

SPRNET-1581: LinkedHashDictionary implementation
This commit is contained in:
Steve Bohlen
2015-11-22 13:17:39 -05:00
3 changed files with 318 additions and 0 deletions

View File

@@ -0,0 +1,110 @@
#region License
/*
* Copyright © 2015-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Spring.Collections.Generic
{
/// <summary>
/// Partial IDictionary implementation which serves as a common base for final implementations.
/// </summary>
/// <author>Zbynek Vyskovsky, kvr@centrum.cz</author>
public abstract class AbstractDictionary<TKey, TValue>: System.Collections.Generic.IDictionary<TKey, TValue>
{
public abstract void Add(TKey key, TValue value);
public abstract bool ContainsKey(TKey key);
public abstract bool Remove(TKey key);
public abstract bool TryGetValue(TKey key, out TValue value);
public abstract void Clear();
public bool Contains(KeyValuePair<TKey, TValue> item)
{
TValue value;
return TryGetValue(item.Key, out value) && (item.Value == null ? value == null : item.Value.Equals(value));
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
}
public abstract int Count { get; }
public bool IsReadOnly
{
get {
return false;
}
}
public abstract bool Remove(KeyValuePair<TKey, TValue> item);
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return EntriesSet().GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return EntriesSet().GetEnumerator();
}
public ICollection<TKey> Keys
{
get {
return EntriesSet().Select((KeyValuePair<TKey, TValue> entry) => entry.Key).ToList();
}
}
public ICollection<TValue> Values
{
get {
return EntriesSet().Select((KeyValuePair<TKey, TValue> entry) => entry.Value).ToList();
}
}
public TValue this[TKey key]
{
get {
TValue value;
if (!TryGetValue(key, out value))
throw new KeyNotFoundException("Key not found in map: "+key);
return value;
}
set {
Add(key, value);
}
}
public void Add(KeyValuePair<TKey, TValue> item)
{
Add(item.Key, item.Value);
}
protected abstract IEnumerable<KeyValuePair<TKey, TValue>> EntriesSet();
}
}

View File

@@ -0,0 +1,137 @@
#region License
/*
* Copyright © 2015-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Spring.Collections.Generic
{
/// <summary>
/// IDictionary implementation which preserves the order of inserted items.
/// </summary>
/// <author>Zbynek Vyskovsky, kvr@centrum.cz</author>
public class LinkedHashDictionary<TKey, TValue>: AbstractDictionary<TKey, TValue>
{
public override void Add(TKey key, TValue value)
{
Node node;
if (items.TryGetValue(key, out node)) {
node.value = value;
}
else {
node = new Node();
node.key = key;
node.value = value;
if ((node.previousLinked = linkedTail) != null)
node.previousLinked.nextLinked = node;
node.nextLinked = null;
linkedTail = node;
if (linkedHead == null)
linkedHead = node;
items.Add(key, node);
}
}
public override bool ContainsKey(TKey key)
{
return items.ContainsKey(key);
}
public override bool Remove(TKey key)
{
Node node;
if (!items.TryGetValue(key, out node))
return false;
if (node.previousLinked != null) {
node.previousLinked.nextLinked = node.nextLinked;
}
else {
linkedHead = node.nextLinked;
}
if (node.nextLinked != null) {
node.nextLinked.previousLinked = node.previousLinked;
}
else {
linkedTail = node.previousLinked;
}
return true;
}
public override bool TryGetValue(TKey key, out TValue value)
{
Node node;
if (!items.TryGetValue(key, out node)) {
value = default(TValue);
return false;
}
value = node.value;
return true;
}
public override void Clear()
{
items.Clear();
linkedHead = null;
linkedTail = null;
}
public override bool Remove(KeyValuePair<TKey, TValue> item)
{
Node node;
if (!items.TryGetValue(item.Key, out node))
return false;
if (!node.value.Equals(item.Value))
return false;
return Remove(item.Key);
}
public override int Count
{
get { return items.Count; }
}
protected class Node
{
public TKey key;
public TValue value;
public Node previousLinked;
public Node nextLinked;
}
protected override IEnumerable<KeyValuePair<TKey, TValue>> EntriesSet()
{
List<KeyValuePair<TKey, TValue>> entries = new List<KeyValuePair<TKey, TValue>>();
for (Node node = linkedHead; node != null; node = node.nextLinked)
entries.Add(new KeyValuePair<TKey, TValue>(node.key, node.value));
return entries;
}
private Node linkedHead = null;
private Node linkedTail = null;
private Dictionary<TKey, Node> items = new Dictionary<TKey, Node>();
}
}

View File

@@ -0,0 +1,71 @@
#region License
/*
* Copyright © 2015-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using Spring.Collections.Generic
using NUnit.Framework;
namespace Spring.Collections.Generic.Test
{
/// <summary>
/// This class contains tests for LinkedHashDictionary
/// </summary>
/// <author>Zbynek Vyskovsky, kvr@centrum.cz</author>
[TestFixture]
public class LinkedHashDictionaryTest
{
[Test]
public void TestSunnyDay()
{
IDictionary<int, string> td = new LinkedHashDictionary<int, string>();
td.Add(0, "a");
td.Add(1, "b");
td.Add(2, "c");
td.Add(1, "b2");
td.Add(3, "d");
td.Add(4, "e");
Assert.IsTrue(td.ContainsKey(0));
Assert.IsTrue(td.ContainsKey(1));
Assert.IsTrue(td.ContainsKey(2));
Assert.IsTrue(td.ContainsKey(3));
Assert.IsTrue(td.ContainsKey(4));
Assert.AreEqual("a", td[0]);
Assert.AreEqual("b2", td[1]);
int[] keys = td.Keys.ToArray();
Assert.AreEqual(5, keys.Length);
Assert.AreEqual(0, keys[0]);
Assert.AreEqual(1, keys[1]);
Assert.AreEqual(2, keys[2]);
Assert.AreEqual(3, keys[3]);
Assert.AreEqual(4, keys[4]);
td.Remove(3);
td.Remove(2);
Assert.AreEqual(3, td.Count);
td.Remove(1);
Assert.AreEqual(2, td.Count);
td.Remove(4);
td.Remove(0);
Assert.AreEqual(0, td.Count);
Assert.AreEqual(0, td.Keys.Count);
}
}
}