1 /*
2     This file is part of BioD.
3     Copyright (C) 2012    Artem Tarasov <lomereiter@gmail.com>
4 
5     Permission is hereby granted, free of charge, to any person obtaining a
6     copy of this software and associated documentation files (the "Software"),
7     to deal in the Software without restriction, including without limitation
8     the rights to use, copy, modify, merge, publish, distribute, sublicense,
9     and/or sell copies of the Software, and to permit persons to whom the
10     Software is furnished to do so, subject to the following conditions:
11 
12     The above copyright notice and this permission notice shall be included in
13     all copies or substantial portions of the Software.
14 
15     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21     DEALINGS IN THE SOFTWARE.
22 
23 */
24 module bio.core.utils.roundbuf;
25 
26 import std.exception;
27 
28 /// Cyclic buffer, see https://en.wikipedia.org/wiki/Circular_buffer
29 /// Bails out if you try to push beyond its size
30 ///
31 /// Note the RoundBuf has copy semantics. So if you pass in a
32 /// Struct it will copy its contents.
33 
34 struct RoundBuf(T) {
35 
36     private {
37       T[] _items = void; // max_length is the size of round buf
38       size_t _put;       // moves the counter forward
39       size_t _start;     // current start of the buffer
40     }
41 
42     /** initializes round buffer of size $(D n) */
43     this(size_t n) {
44         _items = new T[n];
45     }
46 
47     /// Input range primitives
48     bool empty() @property const {
49         return _put == _start;
50     }
51 
52     /// Get the item at the front (non-destructive)
53     auto ref front() @property {
54         enforce(!empty, "roundbuffer is empty");
55         return _items[_start % $];
56     }
57 
58     /// Move the front forward (destructive)
59     void popFront() {
60         enforce(!empty, "roundbuffer is empty");
61         ++_start;
62     }
63 
64     /// Returns the tail item (non-destructive)
65     auto ref back() @property {
66       enforce(!empty, "roundbuffer is empty");
67       return _items[(_put - 1) % $];
68     }
69 
70     /// Add and item at the tail and move pointer forward (destructive)
71     void put(T item) {
72         enforce(!full, "roundbuffer is full");
73         enforce(_put < _put.max, "ringbuffer size_t overflow");
74         _items[_put % $] = item;
75         ++_put;
76     }
77 
78     /// Check if buffer is full
79     bool full() @property const {
80       return _put == _start + max_length;
81     }
82 
83     /// Current number of elements
84     size_t length() @property const {
85         return _put - _start;
86     }
87 
88     size_t max_length() @property const {
89       return _items.length;
90     }
91 }
92 
93 unittest {
94   import std.stdio;
95     auto buf = RoundBuf!int(4);
96     assert(buf.empty);
97 
98     buf.put(1);
99     buf.put(2);
100     assert(buf.length == 2);
101     assert(buf.front == 1);
102     buf.popFront();
103     buf.put(1);
104     buf.put(0);
105     buf.put(3);
106     assert(buf.full);
107     buf.popFront();
108     buf.put(4);
109     buf.popFront();
110     buf.popFront();
111     assert(buf.front == 3);
112     buf.popFront();
113     assert(buf.front == 4);
114     buf.put(4);
115     buf.put(5);
116     buf.put(6);
117     assert(buf.length == buf.max_length);
118     // should bomb out
119     assertThrown!Exception(buf.put(7));
120 }