1 /*
2     This file is part of BioD.
3     Copyright (C) 2013-2017    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.outbuffer;
25 
26 import std.array;
27 import std.exception;
28 
29 ///
30 class OutBuffer {
31     private {
32         ubyte[] _heap;
33 
34         ubyte* _heap_ptr() @property { return _heap.ptr; }
35         size_t _heap_used;
36         size_t _heap_capacity;
37     }
38 
39     ///
40     this(size_t initial_capacity) {
41         _heap = uninitializedArray!(ubyte[])(initial_capacity);
42         _heap_capacity = initial_capacity;
43     }
44 
45     ///
46     ubyte[] data() @property {
47         return _heap_ptr[0 .. _heap_used];
48     }
49 
50     ///
51     size_t length() @property const {
52         return _heap_used;
53     }
54 
55     /// Remove last elements such that new size is equal to $(D size).
56     void shrink(size_t size) {
57         enforce(size <= length);
58         _heap_used = size;
59     }
60 
61     ///
62     size_t capacity() @property const {
63         return _heap_capacity;
64     }
65     
66     /// ditto
67     void capacity(size_t new_capacity) @property {
68         if (new_capacity <= _heap_capacity)
69             return;
70 
71         _heap.length = new_capacity;
72         _heap_capacity = new_capacity;
73     }
74         
75     ///
76     void put(T)(T bytes) if (is(T == ubyte[])) {
77         size_t needed = bytes.length + _heap_used;
78         if (needed > _heap_capacity) {
79             do {
80                 _heap_capacity = _heap_capacity * 3 / 2;
81             } while (_heap_capacity < needed);
82             _heap.length = _heap_capacity;
83         }
84         _heap_ptr[_heap_used .. _heap_used + bytes.length] = bytes[];
85         _heap_used = needed;
86     }
87 
88     /// Dumps raw bytes into the buffer. No endianness conversion or whatever.
89     void put(T)(auto ref T value) if (!is(T == ubyte[])) {
90         put((cast(ubyte*)(&value))[0 .. T.sizeof]);
91     }
92 
93     /// Responsibility that there's enough capacity is on the user
94     void putUnsafe(T)(T bytes) if (is(T == ubyte[])) {
95         _heap_ptr[_heap_used .. _heap_used + bytes.length] = bytes[];
96         _heap_used += bytes.length;
97     }
98 
99     /// ditto
100     void putUnsafe(T)(auto ref T value) if (!is(T == ubyte[])) {
101         putUnsafe((cast(ubyte*)(&value))[0 .. T.sizeof]);
102     }
103 
104     ///
105     void clear() {
106         _heap_used = 0;
107     }
108 }