/*
* Process Hacker -
* delta manager
*
* Copyright (C) 2008 wj32
*
* This file is part of Process Hacker.
*
* Process Hacker is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Process Hacker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Process Hacker. If not, see .
*/
using System.Collections.Generic;
namespace ProcessHacker.Common
{
///
/// Defines subtraction for a numeric type.
///
/// The numeric type.
public interface ISubtractor
{
///
/// Subtracts v2 from v1, i.e., v1 - v2.
///
T Subtract(T v1, T v2);
}
public static class Subtractor
{
private static Int64Subtractor _int64Subtractor = new Int64Subtractor();
private static Int32Subtractor _int32Subtractor = new Int32Subtractor();
private static DoubleSubtractor _doubleSubtractor = new DoubleSubtractor();
private static FloatSubtractor _floatSubtractor = new FloatSubtractor();
public static Int64Subtractor Int64Subtractor
{
get { return _int64Subtractor; }
}
public static Int32Subtractor Int32Subtractor
{
get { return _int32Subtractor; }
}
public static DoubleSubtractor DoubleSubtractor
{
get { return _doubleSubtractor; }
}
public static FloatSubtractor FloatSubtractor
{
get { return _floatSubtractor; }
}
}
///
/// Provides subtraction for 64-bit integers.
///
public class Int64Subtractor : ISubtractor
{
public long Subtract(long v1, long v2)
{
return v1 - v2;
}
}
///
/// Provides subtraction for 32-bit integers.
///
public class Int32Subtractor : ISubtractor
{
public int Subtract(int v1, int v2)
{
return v1 - v2;
}
}
///
/// Provides subtraction for double-precision floating-point values.
///
public class DoubleSubtractor : ISubtractor
{
public double Subtract(double v1, double v2)
{
return v1 - v2;
}
}
///
/// Provides subtraction for single-precision floating-point values.
///
public class FloatSubtractor : ISubtractor
{
public float Subtract(float v1, float v2)
{
return v1 - v2;
}
}
///
/// Provides methods for managing deltas of discrete sets of data.
///
public sealed class DeltaManager
{
private Dictionary _values;
private Dictionary _deltas;
private ISubtractor _subtractor;
///
/// Creates a delta manager using the specified subtractor.
///
/// A subtractor for the appropriate type.
public DeltaManager(ISubtractor subtractor)
{
_subtractor = subtractor;
_values = new Dictionary();
_deltas = new Dictionary();
}
public DeltaManager(ISubtractor subtractor, IEqualityComparer comparer)
{
_subtractor = subtractor;
_values = new Dictionary(comparer);
_deltas = new Dictionary(comparer);
}
public TValue this[TKey key]
{
get { return _deltas[key]; }
set { _deltas[key] = value; }
}
public TValue GetDelta(TKey key)
{
return _deltas[key];
}
public void Add(TKey key, TValue initialValue)
{
_values.Add(key, initialValue);
_deltas.Add(key, initialValue);
}
public void SetDelta(TKey key, TValue value)
{
_deltas[key] = value;
}
public TValue Update(TKey key, TValue value)
{
_deltas[key] = _subtractor.Subtract(value, _values[key]);
_values[key] = value;
return _deltas[key];
}
}
}