Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.6 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. decayacc.cpp
  5. Abstract:
  6. SIS Groveler decaying accumulator
  7. Authors:
  8. John Douceur, 1998
  9. Environment:
  10. User Mode
  11. Revision History:
  12. --*/
  13. #include "all.hxx"
  14. DecayingAccumulator::DecayingAccumulator(
  15. unsigned int time_constant)
  16. {
  17. ASSERT(this != 0);
  18. this->time_constant = double(time_constant);
  19. ASSERT(this->time_constant >= 0.0);
  20. decaying_accumulation = 0.0;
  21. update_time = GET_TICK_COUNT();
  22. }
  23. void
  24. DecayingAccumulator::increment(
  25. int increase)
  26. {
  27. ASSERT(this != 0);
  28. ASSERT(time_constant >= 0.0);
  29. ASSERT(decaying_accumulation >= 0.0);
  30. ASSERT(increase >= 0);
  31. unsigned int current_time = GET_TICK_COUNT();
  32. unsigned int elapsed_time = current_time - update_time;
  33. ASSERT(signed(elapsed_time) >= 0);
  34. double coefficient = 0.0;
  35. if (time_constant > 0.0)
  36. {
  37. coefficient = exp(-double(elapsed_time)/time_constant);
  38. }
  39. ASSERT(coefficient >= 0.0);
  40. ASSERT(coefficient <= 1.0);
  41. decaying_accumulation =
  42. coefficient * decaying_accumulation + double(increase);
  43. ASSERT(decaying_accumulation >= 0.0);
  44. update_time = current_time;
  45. }
  46. double
  47. DecayingAccumulator::retrieve_value() const
  48. {
  49. ASSERT(this != 0);
  50. ASSERT(time_constant >= 0.0);
  51. ASSERT(decaying_accumulation >= 0.0);
  52. unsigned int current_time = GET_TICK_COUNT();
  53. unsigned int elapsed_time = current_time - update_time;
  54. ASSERT(signed(elapsed_time) >= 0);
  55. double coefficient = 0.0;
  56. if (time_constant > 0.0)
  57. {
  58. coefficient = exp(-double(elapsed_time)/time_constant);
  59. }
  60. ASSERT(coefficient >= 0.0);
  61. ASSERT(coefficient <= 1.0);
  62. return coefficient * decaying_accumulation;
  63. }