Debian Codegraph @

src/FIBO.cc

raw
1 /*
2 * This file is part of MUSIC.
3 * Copyright (C) 2008, 2009 INCF
4 *
5 * MUSIC is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * MUSIC is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <cstring>
19 #include <iostream>
20 #include "music/FIBO.hh"
21
22 namespace MUSIC {
23
24 FIBO::FIBO (int es)
25 {
26 configure (es);
27 }
28
29
30 void
31 FIBO::configure (int es)
32 {
33 elementSize = es;
34 size_ = elementSize * nInitial;
35 if (es > 0)
36 buffer.resize (size_);
37 current = 0;
38 }
39
40
41 bool
42 FIBO::isEmpty ()
43 {
44 return current == 0;
45 }
46
47
48 void*
49 FIBO::insert ()
50 {
51 if (current == size_)
52 grow (2 * size_);
53 // Here we use the assumption that vector memory is contiguous
54 // Josuttis says this is the intention of STL even though the
55 // first version of the report is not clear about this.
56 void* memory = static_cast<void*> (&buffer[current]);
57 current += elementSize;
58 return memory;
59 }
60
61
62 void
63 FIBO::insert (void* elements, int n_elements)
64 {
65 int blockSize = elementSize * n_elements;
66 if (current + blockSize > size_)
67 grow (3 * (current + blockSize) / 2);
68 // Here we use the assumption that vector memory is contiguous
69 // Josuttis says this is the intention of STL even though the
70 // first version of the report is not clear about this.
71 void* memory = static_cast<void*> (&buffer[current]);
72 memcpy (memory, elements, blockSize);
73 current += blockSize;
74 }
75
76
77 void
78 FIBO::clear ()
79 {
80 current = 0;
81 }
82
83
84 void
85 FIBO::nextBlockNoClear (void*& data, int& blockSize)
86 {
87
88 data = static_cast<void*> (&buffer[0]);
89 blockSize = current;
90 }
91
92
93 void
94 FIBO::nextBlock (void*& data, int& blockSize)
95 {
96 nextBlockNoClear (data, blockSize);
97 clear ();
98 }
99
100
101 void
102 FIBO::grow (int newSize)
103 {
104 size_ = newSize;
105 buffer.resize (size_);
106 }
107
108 }