wasSharp – Diff between revs 20 and 27
?pathlinks?
Rev 20 | Rev 27 | |||
---|---|---|---|---|
Line 26... | Line 26... | |||
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 | } |
|
Line 37... | Line 51... | |||
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(); |
|
Line 90... | Line 104... | |||
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; |
|
Line 138... | Line 157... | |||
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 | |