change mapandarray array update time

0.9.1.0-post-fixes
UbitUmarov 2019-01-06 00:47:32 +00:00
parent be94a0bffa
commit f2b447a08f
1 changed files with 25 additions and 21 deletions

View File

@ -41,14 +41,16 @@ namespace OpenSim.Framework
{
private Dictionary<TKey, TValue> m_dict;
private TValue[] m_array;
private object m_syncRoot = new object();
private int m_lastArrayVersion;
private int m_arrayVersion;
/// <summary>Number of values currently stored in the collection</summary>
public int Count { get { return m_array.Length; } }
public int Count { get { return m_dict.Count; } }
/// <summary>NOTE: This collection is thread safe. You do not need to
/// acquire a lock to add, remove, or enumerate entries. This
/// synchronization object should only be locked for larger
/// transactions</summary>
private object m_syncRoot = new object();
public object SyncRoot { get { return m_syncRoot; } }
/// <summary>
@ -58,6 +60,8 @@ namespace OpenSim.Framework
{
m_dict = new Dictionary<TKey, TValue>();
m_array = new TValue[0];
m_lastArrayVersion = 0;
m_arrayVersion = 0;
}
/// <summary>
@ -68,6 +72,8 @@ namespace OpenSim.Framework
{
m_dict = new Dictionary<TKey, TValue>(capacity);
m_array = new TValue[0];
m_lastArrayVersion = 0;
m_arrayVersion = 0;
}
/// <summary>
@ -85,7 +91,7 @@ namespace OpenSim.Framework
bool containedKey = m_dict.ContainsKey(key);
m_dict[key] = value;
CreateArray();
++m_arrayVersion;
return !containedKey;
}
@ -103,8 +109,8 @@ namespace OpenSim.Framework
lock (m_syncRoot)
{
m_dict.Add(key, value);
CreateArray();
return m_array.Length;
++m_arrayVersion;
return m_dict.Count;
}
}
@ -118,8 +124,7 @@ namespace OpenSim.Framework
lock (m_syncRoot)
{
bool removed = m_dict.Remove(key);
CreateArray();
++m_arrayVersion;
return removed;
}
}
@ -159,6 +164,8 @@ namespace OpenSim.Framework
{
m_dict = new Dictionary<TKey, TValue>();
m_array = new TValue[0];
m_lastArrayVersion = 0;
m_arrayVersion = 0;
}
}
@ -170,20 +177,17 @@ namespace OpenSim.Framework
/// values</returns>
public TValue[] GetArray()
{
lock (m_syncRoot)
{
if(m_lastArrayVersion != m_arrayVersion)
{
TValue[] array = new TValue[m_dict.Count];
m_dict.Values.CopyTo(array, 0);
m_array = array;
m_lastArrayVersion = m_arrayVersion;
}
return m_array;
}
private void CreateArray()
{
// Rebuild the array from the dictionary. This method must be
// called from inside a lock
TValue[] array = new TValue[m_dict.Count];
int i = 0;
foreach (TValue value in m_dict.Values)
array[i++] = value;
m_array = array;
}
}
}