-
Notifications
You must be signed in to change notification settings - Fork 1
/
CircularBuffer.hs
127 lines (110 loc) · 3.08 KB
/
CircularBuffer.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
--
-- Based on https://github.com/owickstrom/hedgehog-inline-java-testing/blob/d0e51bd6712ce548b175262718d25649c5280933/src/main
--
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DoAndIfThenElse #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE StaticPointers #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module CircularBuffer where
-- | F# is supported as well, if you import
-- import Clr.Inline (Clr, ClrPtr(..), GCHandle(..), fsharp)
--
-- F# and C# can be mixed and matched as well in the same .hs file
-- import Clr.Inline (Clr, ClrPtr(..), GCHandle(..), csharp, fsharp)
--
import Clr.Inline (Clr, ClrPtr(..), GCHandle(..), csharp)
import System.IO.Unsafe (unsafePerformIO)
-- | A Generic Circular Buffer in C#
-- http://www.blackwasp.co.uk/CircularBuffer.aspx
--
[csharp|
public class CircularBuffer<T>
{
private readonly T[] buffer;
private readonly int bufferSize;
private readonly object syncLock = new object();
private int head;
private int tail;
private int length;
public CircularBuffer(int bufferSize)
{
this.buffer =
new T[bufferSize];
this.bufferSize =
bufferSize;
this.head =
bufferSize - 1;
}
private int NextIndex(int index)
{
return (index + 1) % this.bufferSize;
}
public T Get()
{
lock (this.syncLock)
{
T dequeued =
this.buffer[this.tail];
this.tail =
this.NextIndex(this.tail);
this.length--;
return dequeued;
}
}
private bool IsFull => this.length == this.bufferSize;
public void Put(T toAdd)
{
lock (this.syncLock)
{
this.head = this.NextIndex(this.head);
this.buffer[this.head] = toAdd;
if (this.IsFull)
this.tail = this.NextIndex(this.tail);
else
this.length++;
}
}
public T[] Buffer => this.buffer;
public int Length => this.length;
}
|]
-- | Haskell wrapper of CircularBuffer<T> class.
--
new :: Int -> IO (Clr "CircularBuffer<int>")
new capacity =
[csharp|
CircularBuffer<int> {
return new CircularBuffer<int>($capacity:int);
}
|]
put :: Clr "CircularBuffer<int>" -> Int -> IO ()
put x v =
[csharp|
($x:CircularBuffer<int>).Put($v:int);
|]
size :: Clr "CircularBuffer<int>" -> IO Int
size x =
[csharp|
int {
return ($x:CircularBuffer<int>).Length;
}
|]
get :: Clr "CircularBuffer<int>" -> IO (Maybe Int)
get x = do
Just <$> [csharp|
int {
return ($x:CircularBuffer<int>).Get();
}
|]
instance Eq (Clr "CircularBuffer<int>") where
x == y = unsafePerformIO $ do
[csharp|
bool {
var @this = $x:CircularBuffer<int>;
var other = $y:CircularBuffer<int>;
return System.Linq.Enumerable.SequenceEqual(
@this.Buffer, other.Buffer);
}
|]