File: roundbuf.d

package info (click to toggle)
sambamba 1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,528 kB
  • sloc: sh: 220; python: 166; ruby: 147; makefile: 103
file content (120 lines) | stat: -rw-r--r-- 3,518 bytes parent folder | download | duplicates (3)
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
/*
    This file is part of BioD.
    Copyright (C) 2012    Artem Tarasov <lomereiter@gmail.com>

    Permission is hereby granted, free of charge, to any person obtaining a
    copy of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
    DEALINGS IN THE SOFTWARE.

*/
module bio.core.utils.roundbuf;

import std.exception;

/// Cyclic buffer, see https://en.wikipedia.org/wiki/Circular_buffer
/// Bails out if you try to push beyond its size
///
/// Note the RoundBuf has copy semantics. So if you pass in a
/// Struct it will copy its contents.

struct RoundBuf(T) {

    private {
      T[] _items = void; // max_length is the size of round buf
      size_t _put;       // moves the counter forward
      size_t _start;     // current start of the buffer
    }

    /** initializes round buffer of size $(D n) */
    this(size_t n) {
        _items = new T[n];
    }

    /// Input range primitives
    bool empty() @property const {
        return _put == _start;
    }

    /// Get the item at the front (non-destructive)
    auto ref front() @property {
        enforce(!empty, "roundbuffer is empty");
        return _items[_start % $];
    }

    /// Move the front forward (destructive)
    void popFront() {
        enforce(!empty, "roundbuffer is empty");
        ++_start;
    }

    /// Returns the tail item (non-destructive)
    auto ref back() @property {
      enforce(!empty, "roundbuffer is empty");
      return _items[(_put - 1) % $];
    }

    /// Add and item at the tail and move pointer forward (destructive)
    void put(T item) {
        enforce(!full, "roundbuffer is full");
        enforce(_put < _put.max, "ringbuffer size_t overflow");
        _items[_put % $] = item;
        ++_put;
    }

    /// Check if buffer is full
    bool full() @property const {
      return _put == _start + max_length;
    }

    /// Current number of elements
    size_t length() @property const {
        return _put - _start;
    }

    size_t max_length() @property const {
      return _items.length;
    }
}

unittest {
  import std.stdio;
    auto buf = RoundBuf!int(4);
    assert(buf.empty);

    buf.put(1);
    buf.put(2);
    assert(buf.length == 2);
    assert(buf.front == 1);
    buf.popFront();
    buf.put(1);
    buf.put(0);
    buf.put(3);
    assert(buf.full);
    buf.popFront();
    buf.put(4);
    buf.popFront();
    buf.popFront();
    assert(buf.front == 3);
    buf.popFront();
    assert(buf.front == 4);
    buf.put(4);
    buf.put(5);
    buf.put(6);
    assert(buf.length == buf.max_length);
    // should bomb out
    assertThrown!Exception(buf.put(7));
}