초기커밋
This commit is contained in:
72
ServerBase/Entity/Aggregation/EntityAggregator.cs
Normal file
72
ServerBase/Entity/Aggregation/EntityAggregator.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
using ServerCore; using ServerBase;
|
||||
|
||||
|
||||
namespace ServerBase;
|
||||
|
||||
|
||||
public class EntityAggregator<TKey, TCounterType>
|
||||
where TKey : ITuple
|
||||
where TCounterType : struct
|
||||
{
|
||||
private readonly ConcurrentDictionary<TKey, TCounterType> m_counters = new();
|
||||
|
||||
public EntityAggregator()
|
||||
{
|
||||
}
|
||||
|
||||
public void reset()
|
||||
{
|
||||
m_counters.Clear();
|
||||
}
|
||||
|
||||
public void incCounter(TKey key, TCounterType toIncValue, Func<TCounterType, TCounterType, TCounterType> fnUpdate)
|
||||
{
|
||||
var add_counter = delegate (TKey key)
|
||||
{
|
||||
return toIncValue;
|
||||
};
|
||||
|
||||
var update_recorder = delegate (TKey key, TCounterType currValue)
|
||||
{
|
||||
return fnUpdate(currValue, toIncValue);
|
||||
};
|
||||
|
||||
m_counters.AddOrUpdate(key, add_counter, update_recorder);
|
||||
}
|
||||
|
||||
public void decCounter(TKey key, TCounterType toDecValue, Func<TCounterType, TCounterType, TCounterType> fnUpdate)
|
||||
{
|
||||
var add_counter = delegate (TKey key)
|
||||
{
|
||||
return toDecValue;
|
||||
};
|
||||
|
||||
var update_recorder = delegate (TKey key, TCounterType currValue)
|
||||
{
|
||||
return fnUpdate(currValue, toDecValue);
|
||||
};
|
||||
|
||||
m_counters.AddOrUpdate(key, add_counter, update_recorder);
|
||||
}
|
||||
|
||||
public TCounterType getCounter(TKey key, TCounterType defaultValue)
|
||||
{
|
||||
if(false == m_counters.TryGetValue(key, out var found_counter))
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return found_counter;
|
||||
}
|
||||
|
||||
public ConcurrentDictionary<TKey, TCounterType> getCounters() => m_counters;
|
||||
}
|
||||
Reference in New Issue
Block a user