wasSharp – Diff between revs 20 and 27

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 20 Rev 27
1 /////////////////////////////////////////////////////////////////////////// 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // 2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // 3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. // 4 // rights of fair usage, the disclaimer and warranty conditions. //
5 /////////////////////////////////////////////////////////////////////////// 5 ///////////////////////////////////////////////////////////////////////////
6   6  
7 using System; 7 using System;
8 using System.Collections.Generic; 8 using System.Collections.Generic;
9 using System.Diagnostics; 9 using System.Diagnostics;
10 using System.Linq; 10 using System.Linq;
11 using System.Threading; 11 using System.Threading;
12 using System.Xml.Serialization; 12 using System.Xml.Serialization;
13   13  
14 namespace wasSharp.Timers 14 namespace wasSharp.Timers
15 { 15 {
16 /////////////////////////////////////////////////////////////////////////// 16 ///////////////////////////////////////////////////////////////////////////
17 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // 17 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
18 /////////////////////////////////////////////////////////////////////////// 18 ///////////////////////////////////////////////////////////////////////////
19 /// <summary> 19 /// <summary>
20 /// An alarm class similar to the UNIX alarm with the added benefit 20 /// An alarm class similar to the UNIX alarm with the added benefit
21 /// of a decaying timer that tracks the time between rescheduling. 21 /// of a decaying timer that tracks the time between rescheduling.
22 /// </summary> 22 /// </summary>
23 /// <remarks> 23 /// <remarks>
24 /// (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 24 /// (C) Wizardry and Steamworks 2013 - License: GNU GPLv3
25 /// </remarks> 25 /// </remarks>
26 public class DecayingAlarm : IDisposable 26 public class DecayingAlarm : IDisposable
27 { 27 {
28 [Flags] 28 [Flags]
29 public enum DECAY_TYPE 29 public enum DECAY_TYPE
30 { 30 {
31 [Reflection.NameAttribute("none")] [XmlEnum(Name = "none")] NONE = 0, 31 [Reflection.NameAttribute("none")]
-   32 [XmlEnum(Name = "none")]
-   33 NONE = 0,
-   34  
32 [Reflection.NameAttribute("arithmetic")] [XmlEnum(Name = "arithmetic")] ARITHMETIC = 1, 35 [Reflection.NameAttribute("arithmetic")]
-   36 [XmlEnum(Name = "arithmetic")]
-   37 ARITHMETIC = 1,
-   38  
33 [Reflection.NameAttribute("geometric")] [XmlEnum(Name = "geometric")] GEOMETRIC = 2, 39 [Reflection.NameAttribute("geometric")]
-   40 [XmlEnum(Name = "geometric")]
-   41 GEOMETRIC = 2,
-   42  
34 [Reflection.NameAttribute("harmonic")] [XmlEnum(Name = "harmonic")] HARMONIC = 4, 43 [Reflection.NameAttribute("harmonic")]
-   44 [XmlEnum(Name = "harmonic")]
-   45 HARMONIC = 4,
-   46  
35 [Reflection.NameAttribute("weighted")] [XmlEnum(Name = "weighted")] WEIGHTED = 5 47 [Reflection.NameAttribute("weighted")]
-   48 [XmlEnum(Name = "weighted")]
-   49 WEIGHTED = 5
36 } 50 }
37   51  
38 private readonly DECAY_TYPE decay = DECAY_TYPE.NONE; 52 private readonly DECAY_TYPE decay = DECAY_TYPE.NONE;
39 private readonly Stopwatch elapsed = new Stopwatch(); 53 private readonly Stopwatch elapsed = new Stopwatch();
40 private readonly object LockObject = new object(); 54 private readonly object LockObject = new object();
41 private readonly HashSet<double> times = new HashSet<double>(); 55 private readonly HashSet<double> times = new HashSet<double>();
42 private Timer alarm; 56 private Timer alarm;
43   57  
44 /// <summary> 58 /// <summary>
45 /// The default constructor using no decay. 59 /// The default constructor using no decay.
46 /// </summary> 60 /// </summary>
47 public DecayingAlarm() 61 public DecayingAlarm()
48 { 62 {
49 Signal = new ManualResetEvent(false); 63 Signal = new ManualResetEvent(false);
50 } 64 }
51   65  
52 /// <summary> 66 /// <summary>
53 /// The constructor for the DecayingAlarm class taking as parameter a decay type. 67 /// The constructor for the DecayingAlarm class taking as parameter a decay type.
54 /// </summary> 68 /// </summary>
55 /// <param name="decay">the type of decay: arithmetic, geometric, harmonic, heronian or quadratic</param> 69 /// <param name="decay">the type of decay: arithmetic, geometric, harmonic, heronian or quadratic</param>
56 public DecayingAlarm(DECAY_TYPE decay) 70 public DecayingAlarm(DECAY_TYPE decay)
57 { 71 {
58 Signal = new ManualResetEvent(false); 72 Signal = new ManualResetEvent(false);
59 this.decay = decay; 73 this.decay = decay;
60 } 74 }
61   75  
62 public ManualResetEvent Signal { get; set; } 76 public ManualResetEvent Signal { get; set; }
63   77  
64 public void Dispose() 78 public void Dispose()
65 { 79 {
66 Dispose(true); 80 Dispose(true);
67 GC.SuppressFinalize(this); 81 GC.SuppressFinalize(this);
68 } 82 }
69   83  
70 ~DecayingAlarm() 84 ~DecayingAlarm()
71 { 85 {
72 Dispose(false); 86 Dispose(false);
73 } 87 }
74   88  
75 public void Alarm(double deadline) 89 public void Alarm(double deadline)
76 { 90 {
77 lock (LockObject) 91 lock (LockObject)
78 { 92 {
79 switch (alarm == null) 93 switch (alarm == null)
80 { 94 {
81 case true: 95 case true:
82 elapsed.Start(); 96 elapsed.Start();
83 alarm = new Timer(() => 97 alarm = new Timer(() =>
84 { 98 {
85 lock (LockObject) 99 lock (LockObject)
86 { 100 {
87 Signal.Set(); 101 Signal.Set();
88 elapsed.Stop(); 102 elapsed.Stop();
89 times.Clear(); 103 times.Clear();
90 alarm.Dispose(); 104 alarm.Dispose();
91 alarm = null; 105 alarm = null;
92 } 106 }
93 }, deadline, 0); 107 }, deadline, 0);
94 return; 108 return;
-   109  
95 case false: 110 case false:
96 elapsed.Stop(); 111 elapsed.Stop();
97 times.Add(elapsed.ElapsedMilliseconds); 112 times.Add(elapsed.ElapsedMilliseconds);
98 switch (decay) 113 switch (decay)
99 { 114 {
100 case DECAY_TYPE.ARITHMETIC: 115 case DECAY_TYPE.ARITHMETIC:
101 alarm?.Change( 116 alarm?.Change(
102 (int) ((deadline + times.Aggregate((a, b) => b + a))/(1f + times.Count)), 0); 117 (int)((deadline + times.Aggregate((a, b) => b + a)) / (1f + times.Count)), 0);
103 break; 118 break;
-   119  
104 case DECAY_TYPE.GEOMETRIC: 120 case DECAY_TYPE.GEOMETRIC:
105 alarm?.Change((int) Math.Pow(deadline*times.Aggregate((a, b) => b*a), 121 alarm?.Change((int)Math.Pow(deadline * times.Aggregate((a, b) => b * a),
106 1f/(1f + times.Count)), 0); 122 1f / (1f + times.Count)), 0);
107 break; 123 break;
-   124  
108 case DECAY_TYPE.HARMONIC: 125 case DECAY_TYPE.HARMONIC:
109 alarm?.Change((int) ((1f + times.Count)/ 126 alarm?.Change((int)((1f + times.Count) /
110 (1f/deadline + times.Aggregate((a, b) => 1f/b + 1f/a))), 0); 127 (1f / deadline + times.Aggregate((a, b) => 1f / b + 1f / a))), 0);
111 break; 128 break;
-   129  
112 case DECAY_TYPE.WEIGHTED: 130 case DECAY_TYPE.WEIGHTED:
113 var d = new HashSet<double>(times) {deadline}; 131 var d = new HashSet<double>(times) { deadline };
114 var total = d.Aggregate((a, b) => b + a); 132 var total = d.Aggregate((a, b) => b + a);
115 alarm?.Change( 133 alarm?.Change(
116 (int) d.Aggregate((a, b) => Math.Pow(a, 2)/total + Math.Pow(b, 2)/total), 0); 134 (int)d.Aggregate((a, b) => Math.Pow(a, 2) / total + Math.Pow(b, 2) / total), 0);
117 break; 135 break;
-   136  
118 default: 137 default:
119 alarm?.Change((int) deadline, 0); 138 alarm?.Change((int)deadline, 0);
120 break; 139 break;
121 } 140 }
122 elapsed.Reset(); 141 elapsed.Reset();
123 elapsed.Start(); 142 elapsed.Start();
124 break; 143 break;
125 } 144 }
126 } 145 }
127 } 146 }
128   147  
129 private void Dispose(bool dispose) 148 private void Dispose(bool dispose)
130 { 149 {
131 if (alarm != null) 150 if (alarm != null)
132 { 151 {
133 alarm.Dispose(); 152 alarm.Dispose();
134 alarm = null; 153 alarm = null;
135 } 154 }
136 } 155 }
137   156  
138 public DecayingAlarm Clone() 157 public DecayingAlarm Clone()
139 { 158 {
140 return new DecayingAlarm(decay); 159 return new DecayingAlarm(decay);
141 } 160 }
142 } 161 }
143 } -  
144   162 }
-   163  
145
Generated by GNU Enscript 1.6.5.90.
-  
146   -  
147   -  
148   -