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
29 struct RoundBuf(T) {
30    
31     private {
32         T[] _items = void;
33         size_t _put;
34         size_t _taken;
35     }
36 
37     /** initializes round buffer of size $(D n) */
38     this(size_t n) {
39         _items = new T[n];
40     }
41 
42     /// Input range primitives
43     bool empty() @property const {
44         return _put == _taken;
45     }
46 
47     /// ditto
48     auto ref front() @property {
49         enforce(!empty, "roundbuffer is empty");
50         return _items[_taken % $];
51     }
52 
53     /// ditto
54     void popFront() {
55         enforce(!empty, "roundbuffer is empty");
56         ++_taken;
57     }
58 
59     ///
60     auto ref back() @property {
61       enforce(!empty, "roundbuffer is empty");
62       return _items[(_put - 1) % $];
63     }
64 
65     /// Output range primitive
66     void put(T item) {
67         enforce(!full, "roundbuffer is full");
68         enforce(_put < _put.max, "ringbuffer overflow");
69         _items[_put % $] = item;
70         ++_put;
71     }
72 
73     /// Check if buffer is full
74     bool full() @property const {
75         return _put == _taken + _items.length;
76     }
77    
78     /// Current number of elements
79     size_t length() @property const {
80         return _put - _taken;
81     }
82 }
83 
84 unittest {
85     auto buf = RoundBuf!int(4);
86     assert(buf.empty);
87 
88     buf.put(1);
89     buf.put(2);
90     assert(buf.length == 2);
91     assert(buf.front == 1);
92     buf.popFront();
93     buf.put(1);
94     buf.put(0);
95     buf.put(3);
96     assert(buf.full);
97     buf.popFront();
98     buf.put(4);
99     buf.popFront();
100     buf.popFront();
101     assert(buf.front == 3);
102     buf.popFront();
103     assert(buf.front == 4);
104 }