-
Notifications
You must be signed in to change notification settings - Fork 846
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f60b903
commit 91465a2
Showing
9 changed files
with
160 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
| ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NBitcoin | ||
{ | ||
public class MedianFilterInt32 | ||
{ | ||
Queue<Int32> vValues; | ||
Queue<Int32> vSorted; | ||
uint nSize; | ||
|
||
public MedianFilterInt32(uint size, Int32 initialValue) | ||
{ | ||
nSize = size; | ||
vValues = new Queue<Int32>((int)size); | ||
vValues.Enqueue(initialValue); | ||
vSorted = new Queue<Int32>(vValues); | ||
} | ||
|
||
public Int32 Median | ||
{ | ||
get | ||
{ | ||
int size = vSorted.Count; | ||
if(size <= 0) | ||
throw new InvalidOperationException("size <= 0"); | ||
|
||
var sortedList = vSorted.ToList(); | ||
if(size % 2 == 1) | ||
{ | ||
return sortedList[size / 2]; | ||
} | ||
else // Even number of elements | ||
{ | ||
return (sortedList[size / 2 - 1] + sortedList[size / 2]) / 2; | ||
} | ||
} | ||
} | ||
|
||
public void Input(Int32 value) | ||
{ | ||
if(vValues.Count == nSize) | ||
{ | ||
vValues.Dequeue(); | ||
} | ||
vValues.Enqueue(value); | ||
vSorted = new Queue<Int32>(vValues.OrderBy(o => o)); | ||
} | ||
} | ||
public class MedianFilterInt64 | ||
{ | ||
Queue<Int64> vValues; | ||
Queue<Int64> vSorted; | ||
uint nSize; | ||
|
||
public MedianFilterInt64(uint size, Int64 initialValue) | ||
{ | ||
nSize = size; | ||
vValues = new Queue<Int64>((int)size); | ||
vValues.Enqueue(initialValue); | ||
vSorted = new Queue<Int64>(vValues); | ||
} | ||
|
||
public Int64 Median | ||
{ | ||
get | ||
{ | ||
int size = vSorted.Count; | ||
if(size <= 0) | ||
throw new InvalidOperationException("size <= 0"); | ||
|
||
var sortedList = vSorted.ToList(); | ||
if(size % 2 == 1) | ||
{ | ||
return sortedList[size / 2]; | ||
} | ||
else // Even number of elements | ||
{ | ||
return (sortedList[size / 2 - 1] + sortedList[size / 2]) / 2; | ||
} | ||
} | ||
} | ||
|
||
public void Input(Int64 value) | ||
{ | ||
if(vValues.Count == nSize) | ||
{ | ||
vValues.Dequeue(); | ||
} | ||
vValues.Enqueue(value); | ||
vSorted = new Queue<Int64>(vValues.OrderBy(o => o)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters