1 #ifndef __SDR_BUFFER_HH__
2 #define __SDR_BUFFER_HH__
14 #include "exception.hh"
67 inline char *
ptr()
const {
return (
char *)
_ptr; }
160 inline bool operator<(const Buffer<T> &other)
const {
162 return this->
_ptr < other._ptr;
172 if ((idx >=
_size) || (idx < 0)) {
174 err <<
"Index " << idx <<
" out of bounds [0," <<
_size <<
")";
184 for (
size_t i=0; i<
size(); i++) {
185 nrm2 += std::real(std::conj((*
this)[i])*(*
this)[i]);
187 return std::sqrt(nrm2);
193 for (
size_t i=0; i<
size(); i++) {
194 nrm += std::abs((*
this)[i]);
200 inline double norm(
double p)
const {
202 for (
size_t i=0; i<
size(); i++) {
203 nrm += std::pow(std::abs((*
this)[i]), p);
205 return std::pow(nrm, 1./p);
210 for (
size_t i=0; i<
size(); i++) {
218 for (
size_t i=0; i<
size(); i++) {
239 return this->
sub(0, n);
254 template <
class Scalar>
256 operator<< (std::ostream &stream, const sdr::Buffer<Scalar> &buffer)
259 if (10 < buffer.size()) {
260 stream << +buffer[0];
261 for (
size_t i=1; i<5; i++) {
262 stream <<
", " << +buffer[i];
264 stream <<
", ..., " << +buffer[buffer.size()-6];
265 for (
size_t i=1; i<5; i++) {
266 stream <<
", " << +buffer[buffer.size()+i-6];
269 if (0 < buffer.size()) {
270 stream << +buffer[0];
271 for (
size_t i=1; i<buffer.size(); i++) {
272 stream <<
", " << +buffer[i];
287 template <
class Scalar>
296 for (
size_t i=0; i<N; i++) {
306 typename std::map<void *, Buffer<Scalar> >::iterator item =
_buffers.begin();
307 for(; item !=
_buffers.end(); item++) {
308 item->second.unref();
333 if (
_buffers.size() == numBuffers) {
return; }
336 size_t N = (numBuffers -
_buffers.size());
337 for (
size_t i=0; i<N; i++) {
380 if ((idx < 0) || (idx>=(
int)
_b_stored)) {
382 err <<
"RawRingBuffer: Index " << idx <<
" out of bounds [0," <<
bytesLen() <<
").";
417 memcpy(
_ptr+put_idx, src.
data(), num_a);
428 if (N > dest.
bytesLen()) {
return false; }
430 if (N >
bytesLen()) {
return false; }
440 memcpy(dest.
data()+num_a,
_ptr, N-num_a);
472 template <
class Scalar>
545 #endif // __SDR_BUFFER_HH__
RawRingBuffer()
Empty constructor.
Definition: buffer.cc:84
Buffer(const RawBuffer &other)
Explicit type cast.
Definition: buffer.hh:143
size_t _b_offset
Holds the offset of the buffer in bytes.
Definition: buffer.hh:98
double norm(double p) const
Returns the norm of the buffer.
Definition: buffer.hh:200
std::vector< void * > _free_buffers
A vector of all unused buffers.
Definition: buffer.hh:351
double norm() const
Returns the norm of the buffer.
Definition: buffer.hh:191
Base class of all buffers, represents an untyped array of bytes.
Definition: buffer.hh:32
size_t _take_idx
The current read pointer.
Definition: buffer.hh:464
const RingBuffer< Scalar > & operator=(const RingBuffer< Scalar > &other)
Assigment operator, turns this buffer into a reference to the other ring buffer.
Definition: buffer.hh:490
size_t _stored
The number of stored elements.
Definition: buffer.hh:540
virtual ~RawRingBuffer()
Destructor.
Definition: buffer.cc:102
Buffer()
Empty constructor.
Definition: buffer.hh:115
virtual void bufferUnused(const RawBuffer &buffer)=0
Gets called once an owned buffer gets unused.
Buffer< T > & operator/=(const T &a)
In-place, element wise division of the buffer with the scalar a.
Definition: buffer.hh:217
Buffer< oT > as() const
Explicit type cast.
Definition: buffer.hh:226
size_t bytesFree() const
Returns the number of free bytes in the ring buffer.
Definition: buffer.hh:395
A simple typed ring-buffer.
Definition: buffer.hh:473
virtual ~RingBuffer()
Destructor.
Definition: buffer.hh:487
size_t bytesLen() const
Returns the number of bytes available for reading.
Definition: buffer.hh:392
void clear()
Clear the ring-buffer.
Definition: buffer.hh:453
void ref() const
Increment reference counter.
Definition: buffer.cc:57
size_t stored() const
Returns the number of stored elements.
Definition: buffer.hh:502
Definition: autocast.hh:8
bool take(const RawBuffer &dest, size_t N)
Take N bytes from the ring buffer and store it into the given buffer dest.
Definition: buffer.hh:426
bool isEmpty() const
Returns true if the buffer is invalid/empty.
Definition: buffer.hh:77
size_t storageSize() const
Returns the raw buffer size in bytes.
Definition: buffer.hh:75
BufferSet(size_t N, size_t size)
Preallocates N buffers of size size.
Definition: buffer.hh:292
T & operator[](int idx) const
Element access.
Definition: buffer.hh:170
int refCount() const
Returns the reference counter.
Definition: buffer.hh:84
BufferOwner * _owner
Holds a weak reference the buffer owner.
Definition: buffer.hh:104
size_t size() const
Returns the size of the ring buffer.
Definition: buffer.hh:506
virtual ~Buffer()
Destructor.
Definition: buffer.hh:138
size_t _b_length
Holds the length of the buffer (view) in bytes.
Definition: buffer.hh:100
The runtime error class.
Definition: exception.hh:36
void resize(size_t N)
Resizes the ring buffer to N elements.
Definition: buffer.hh:532
size_t _bufferSize
Size of each buffer.
Definition: buffer.hh:346
size_t size() const
Returns the number of elements of type T in this buffer.
Definition: buffer.hh:166
A set of buffers, that tracks their usage.
Definition: buffer.hh:288
char & operator[](int idx)
Element access.
Definition: buffer.hh:378
size_t bytesLen() const
Returns the size of the buffer by the view.
Definition: buffer.hh:73
Buffer(size_t N, BufferOwner *owner=0)
Creates a buffer with N samples.
Definition: buffer.hh:126
A simple ring buffer.
Definition: buffer.hh:356
char * data() const
Returns the pointer to the data of the buffer view.
Definition: buffer.hh:69
void drop(size_t N)
Drops at most N bytes from the buffer.
Definition: buffer.hh:446
RingBuffer(const RingBuffer< Scalar > &other)
Copy constructor, creates a reference to the other ring buffer.
Definition: buffer.hh:483
Buffer(const Buffer< T > &other)
Create a new reference to the buffer.
Definition: buffer.hh:132
RingBuffer(size_t N)
Constructs a ring buffer of size N.
Definition: buffer.hh:480
bool take(const Buffer< Scalar > &dest, size_t N)
Takes N elements from the buffer and stores them into dest.
Definition: buffer.hh:519
Buffer< T > tail(size_t n) const
Returns a new view on this buffer.
Definition: buffer.hh:243
double norm2() const
Returns the norm of the buffer.
Definition: buffer.hh:182
Buffer< T > head(size_t n) const
Returns a new view on this buffer.
Definition: buffer.hh:237
RawBuffer()
Constructs an empty buffer.
Definition: buffer.cc:10
virtual void bufferUnused(const RawBuffer &buffer)
Callback gets called once the buffer gets unused.
Definition: buffer.hh:324
size_t _storage_size
Holds the size of the buffer in bytes.
Definition: buffer.hh:96
bool hasBuffer()
Returns true if there is a free buffer.
Definition: buffer.hh:315
const RawRingBuffer & operator=(const RawRingBuffer &other)
Assignment operator, turns this ring buffer into a reference to the other one.
Definition: buffer.hh:370
RingBuffer()
Empty constructor.
Definition: buffer.hh:477
size_t free() const
Returns the number of free elements.
Definition: buffer.hh:504
size_t _size
The size of the ring buffer.
Definition: buffer.hh:538
char * _ptr
Holds the pointer to the data or 0, if buffer is empty.
Definition: buffer.hh:94
std::map< void *, Buffer< Scalar > > _buffers
Holds a reference to each buffer of the buffer set, referenced by the data pointer of the buffer...
Definition: buffer.hh:349
size_t bytesOffset() const
Returns the offset of the data by the view.
Definition: buffer.hh:71
const Buffer< T > & operator=(const Buffer< T > other)
Assignment operator, turns this buffer into a reference to the other buffer.
Definition: buffer.hh:152
void resize(size_t numBuffers)
Resize the buffer set.
Definition: buffer.hh:332
Buffer< T > & operator*=(const T &a)
In-place, element wise product of the buffer with the scalar a.
Definition: buffer.hh:209
bool put(const Buffer< Scalar > &data)
Puts the given elements into the ring buffer.
Definition: buffer.hh:510
Buffer< T > sub(size_t offset, size_t len) const
Returns a new view on this buffer.
Definition: buffer.hh:231
size_t _b_stored
Offset of the write pointer relative to the _take_idx ptr, such that the number of stored bytes is _b...
Definition: buffer.hh:467
char * ptr() const
Returns the pointer to the data (w/o view).
Definition: buffer.hh:67
const RawBuffer & operator=(const RawBuffer &other)
Assignment.
Definition: buffer.hh:54
Abstract class (interface) of a buffer owner.
Definition: buffer.hh:24
void unref()
Dereferences the buffer.
Definition: buffer.cc:63
bool isUnused() const
We assume here that buffers are owned by one object: A buffer is therefore "unused" if the owner hold...
Definition: buffer.hh:87
void drop(size_t N)
Drops N elements from the ring buffer.
Definition: buffer.hh:527
int * _refcount
The reference counter.
Definition: buffer.hh:102
virtual ~RawBuffer()
Destructor.
Definition: buffer.cc:51
Buffer< Scalar > getBuffer()
Obtains a free buffer.
Definition: buffer.hh:318
bool put(const RawBuffer &src)
Puts the given data into the ring-buffer.
Definition: buffer.hh:401
size_t _size
Holds the number of elements of type T in the buffer.
Definition: buffer.hh:250
void resize(size_t N)
Resizes the ring buffer.
Definition: buffer.hh:456
A typed buffer.
Definition: buffer.hh:111
virtual ~BufferSet()
Destructor, unreferences all buffers.
Definition: buffer.hh:305
Scalar & operator[](int idx)
Element access.
Definition: buffer.hh:497
Buffer(T *data, size_t size)
Constructor from raw data.
Definition: buffer.hh:120