Changeset - b2f0d61c3507
[Not reviewed]
stream
0 2 2
Hasan Yavuz ÖZDERYA - 8 years ago 2017-12-12 03:29:00
hy@ozderya.net
added read only buffer for use with snapshots
4 files changed with 177 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/readonlybuffer.cpp
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2017 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include <QtGlobal>
 

	
 
#include "readonlybuffer.h"
 

	
 
ReadOnlyBuffer::ReadOnlyBuffer(const FrameBuffer* source) :
 
    ReadOnlyBuffer(source, 0, source->size())
 
{
 
    // empty
 
}
 

	
 
ReadOnlyBuffer::ReadOnlyBuffer(const FrameBuffer* source, unsigned start, unsigned n)
 
{
 
    Q_ASSERT(source->size() > 0);
 
    Q_ASSERT(start + n <= source->size());
 

	
 
    _size = n;
 
    data = new double[_size];
 

	
 
    for (unsigned i = 0; i < n; i++)
 
    {
 
        data[i] = source->sample(start + i);
 
    }
 

	
 
    /// if not exact copy of source re-calculate limits
 
    if (start == 0 && n == source->size())
 
    {
 
        _limits = source->limits();
 
    }
 
    else
 
    {
 
        // TODO: code duplication with RingBuffer::updateLimits, consider reuse
 
        _limits.start = data[0];
 
        _limits.end = data[0];
 

	
 
        for (unsigned i = 0; i < _size; i++)
 
        {
 
            if (data[i] > _limits.end)
 
            {
 
                _limits.end = data[i];
 
            }
 
            else if (data[i] < _limits.start)
 
            {
 
                _limits.start = data[i];
 
            }
 
        }
 
    }
 
}
 

	
 
ReadOnlyBuffer::~ReadOnlyBuffer()
 
{
 
    delete[] data;
 
}
 

	
 
unsigned ReadOnlyBuffer::size() const
 
{
 
    return _size;
 
}
 

	
 
double ReadOnlyBuffer::sample(unsigned i) const
 
{
 
    return data[i];
 
}
 

	
 
Range ReadOnlyBuffer::limits() const
 
{
 
    return _limits;
 
}
src/readonlybuffer.h
Show inline comments
 
new file 100644
 
/*
 
  Copyright © 2017 Hasan Yavuz Özderya
 

	
 
  This file is part of serialplot.
 

	
 
  serialplot is free software: you can redistribute it and/or modify
 
  it under the terms of the GNU General Public License as published by
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  serialplot is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU General Public License for more details.
 

	
 
  You should have received a copy of the GNU General Public License
 
  along with serialplot.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#ifndef READONLYBUFFER_H
 
#define READONLYBUFFER_H
 

	
 
// IMPORTANT TODO: rename to "framebuffer.h" when stream work is done.
 
#include "framebuffer2.h"
 

	
 
/// A read only frame buffer used for storing snapshot data. Main advantage of
 
/// this compared to `RingBuffer` is that reading data should be somewhat
 
/// faster.
 
class ReadOnlyBuffer : public FrameBuffer
 
{
 
public:
 
    /// Creates a buffer with data copied from `source`. Source buffer cannot be
 
    /// empty.
 
    ReadOnlyBuffer(const FrameBuffer* source);
 

	
 
    /// Creates a buffer from a slice of the `source`.
 
    ///
 
    /// @param start start of the slice
 
    /// @param n number of samples
 
    ///
 
    /// @important (start + n) should be smaller or equal than `source->size()`,
 
    /// otherwise it's an error.
 
    ReadOnlyBuffer(const FrameBuffer* source, unsigned start, unsigned n);
 

	
 
    ~ReadOnlyBuffer();
 

	
 
    virtual unsigned size() const;
 
    virtual double sample(unsigned i) const;
 
    virtual Range limits() const;
 

	
 
private:
 
    double* data;    ///< data storage
 
    unsigned _size;  ///< data size
 
    Range _limits;   ///< limits cache
 
};
 

	
 
#endif // READONLYBUFFER_H
tests/CMakeLists.txt
Show inline comments
 
@@ -30,6 +30,7 @@ add_executable(Test EXCLUDE_FROM_ALL
 
  ../src/indexbuffer.cpp
 
  ../src/linindexbuffer.cpp
 
  ../src/ringbuffer.cpp
 
  ../src/readonlybuffer.cpp
 
  )
 
add_test(NAME test1 COMMAND Test)
 
qt5_use_modules(Test Widgets)
tests/test.cpp
Show inline comments
 
@@ -25,6 +25,7 @@
 
#include "indexbuffer.h"
 
#include "linindexbuffer.h"
 
#include "ringbuffer.h"
 
#include "readonlybuffer.h"
 

	
 
TEST_CASE("samplepack with no X", "[memory]")
 
{
 
@@ -364,3 +365,35 @@ TEST_CASE("RingBuffer clear", "[memory, 
 
    REQUIRE(lim.start == 0.);
 
    REQUIRE(lim.end == 0.);
 
}
 

	
 
TEST_CASE("ReadOnlyBuffer", "[memory, buffer]")
 
{
 
    IndexBuffer source(10);
 

	
 
    ReadOnlyBuffer buf(&source);
 

	
 
    REQUIRE(buf.size() == 10);
 
    auto lim = buf.limits();
 
    REQUIRE(lim.start == 0.);
 
    REQUIRE(lim.end == 9.);
 
    for (unsigned i = 0; i < 10; i++)
 
    {
 
        REQUIRE(buf.sample(i) == i);
 
    }
 
}
 

	
 
TEST_CASE("ReadOnlyBuffer sliced constructor", "[memory, buffer]")
 
{
 
    IndexBuffer source(10);
 

	
 
    ReadOnlyBuffer buf(&source, 5, 4);
 

	
 
    REQUIRE(buf.size() == 4);
 
    auto lim = buf.limits();
 
    REQUIRE(lim.start == 5.);
 
    REQUIRE(lim.end == 8.);
 
    for (unsigned i = 0; i < 4; i++)
 
    {
 
        REQUIRE(buf.sample(i) == (i + 5));
 
    }
 
}
0 comments (0 inline, 0 general)