|
using System.Collections.Generic;
using System.Threading;
namespace Hik.Collections
{
/// <summary>
/// This class is used to store key-value based items in a thread safe manner.
/// It uses System.Collections.Generic.SortedList internally.
/// </summary>
/// <typeparam name="TK">Key type</typeparam>
/// <typeparam name="TV">Value type</typeparam>
public class ThreadSafeSortedList<TK, TV>
{
/// <summary>
/// Gets/adds/replaces an item by key.
/// </summary>
/// <param name="key">Key to get/set value</param>
/// <returns>Item associated with this key</returns>
public TV this[TK key]
{
get
{
_lock.EnterReadLock();
try
{
return _items.ContainsKey(key) ? _items[key] : default(TV);
}
finally
{
_lock.ExitReadLock();
}
}
set
{
_lock.EnterWriteLock();
try
{
_items[key] = value;
}
finally
{
_lock.ExitWriteLock();
}
}
}
/// <summary>
/// Internal collection to store items.
/// </summary>
protected readonly SortedList<TK, TV> _items;
/// <summary>
/// Used to synchronize access to _items list.
/// </summary>
protected readonly ReaderWriterLockSlim _lock;
/// <summary>
/// Creates a new ThreadSafeSortedList object.
/// </summary>
public ThreadSafeSortedList()
{
_items = new SortedList<TK, TV>();
_lock = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
}
/// <summary>
/// Checks if collection contains spesified key.
/// </summary>
/// <param name="key">Key to check</param>
/// <returns>True; if collection contains given key</returns>
public bool ContainsKey(TK key)
{
_lock.EnterReadLock();
try
{
return _items.ContainsKey(key);
}
finally
{
_lock.ExitReadLock();
}
}
/// <summary>
/// Checks if collection contains spesified item.
/// </summary>
/// <param name="item">Item to check</param>
/// <returns>True; if collection contains given item</returns>
public bool ContainsValue(TV item)
{
_lock.EnterReadLock();
try
{
return _items.ContainsValue(item);
}
finally
{
_lock.ExitReadLock();
}
}
/// <summary>
/// Removes an item from collection.
/// </summary>
/// <param name="key">Key of item to remove</param>
public bool Remove(TK key)
{
_lock.EnterWriteLock();
try
{
if (!_items.ContainsKey(key))
{
return false;
}
_items.Remove(key);
return true;
}
finally
{
_lock.ExitWriteLock();
}
}
/// <summary>
/// Gets all items in collection.
/// </summary>
/// <returns>Item list</returns>
public List<TV> GetAllItems()
{
_lock.EnterReadLock();
try
{
return new List<TV>(_items.Values);
}
finally
{
_lock.ExitReadLock();
}
}
/// <summary>
/// Removes all items from list.
/// </summary>
public void ClearAll()
{
_lock.EnterWriteLock();
try
{
_items.Clear();
}
finally
{
_lock.ExitWriteLock();
}
}
/// <summary>
/// Gets then removes all items in collection.
/// </summary>
/// <returns>Item list</returns>
public List<TV> GetAndClearAllItems()
{
_lock.EnterWriteLock();
try
{
var list = new List<TV>(_items.Values);
_items.Clear();
return list;
}
finally
{
_lock.ExitWriteLock();
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I have started programming at 14 years old using Pascal as hobby. Then I interested in web development (HTML, JavaScript, ASP...) before university.
I graduated from Sakarya University Computer Engineering. At university, I learned C++, Visual Basic.NET, C#, ASP.NET and Java. I partly implemented ARP, IP and TCP protocols in Java as my final term project.
Now, I am working on Windows and web based software development mostly using Microsoft technologies in my own company.
My open source projects:
* ABP Framework:
https://abp.io
* jTable:
http://jtable.org
* Others:
https://github.com/hikalkan
My personal web site:
https://halilibrahimkalkan.com